Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added method to duplicate predicate #3432

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1e148cb
chore: added static method to duplicate predicate
YaTut1901 Nov 29, 2024
95cc305
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
Dhaiwat10 Dec 5, 2024
bac1e98
chore: refactored toNewInstance method
YaTut1901 Dec 5, 2024
db6106a
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 9, 2024
8c2e49c
chore: fixed some lint errors
YaTut1901 Dec 9, 2024
486d942
chore: added changeset
YaTut1901 Dec 9, 2024
cf7cf7f
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
Torres-ssf Dec 9, 2024
cede5c2
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 10, 2024
d8979d6
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
da551ba
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
33d79a4
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
1c55a2e
Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts
YaTut1901 Dec 10, 2024
d3b488a
Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicat…
YaTut1901 Dec 10, 2024
dd87424
Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicat…
YaTut1901 Dec 10, 2024
3a324b5
chore: removed helpers
YaTut1901 Dec 10, 2024
47b01ad
chore: typing in fund function
YaTut1901 Dec 10, 2024
bf2c404
chore: refactored fundPredicate function to fundAccount
YaTut1901 Dec 10, 2024
639c9c4
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 11, 2024
3fb9750
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 13, 2024
81c8cdf
feat: added method to duplicate predicate
YaTut1901 Dec 13, 2024
5f2e7a2
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
YaTut1901 Dec 17, 2024
f73ea72
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
arboleya Dec 31, 2024
c58f023
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
arboleya Jan 2, 2025
204862b
Merge branch 'master' into YaTut1901/chore/predicate-duplication-static
nedsalk Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-toes-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

chore: added static method to duplicate predicate
YaTut1901 marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 21 additions & 1 deletion packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export class Predicate<
bytes: Uint8Array;
predicateData: TData = [] as unknown as TData;
interface: Interface;

initialBytecode: Uint8Array;
configurableConstants: TConfigurables | undefined;
/**
* Creates an instance of the Predicate class.
*
Expand All @@ -73,8 +74,10 @@ export class Predicate<
const address = Address.fromB256(getPredicateRoot(predicateBytes));
super(address, provider);

this.initialBytecode = arrayify(bytecode);
this.bytes = predicateBytes;
this.interface = predicateInterface;
this.configurableConstants = configurableConstants;
if (data !== undefined && data.length > 0) {
this.predicateData = data;
}
Expand Down Expand Up @@ -147,6 +150,23 @@ export class Predicate<
return mainFn?.encodeArguments(this.predicateData) || new Uint8Array();
}

/**
* Creates a new Predicate instance from an existing Predicate instance.
* @param overrides - The data and configurable constants to override.
* @returns A new Predicate instance with the same bytecode, ABI and provider but with the ability to set the data and configurable constants.
*/
toNewInstance(
overrides: Pick<PredicateParams<TData, TConfigurables>, 'data' | 'configurableConstants'> = {}
) {
return new Predicate<TData, TConfigurables>({
bytecode: this.initialBytecode,
abi: this.interface.jsonAbi,
provider: this.provider,
data: overrides.data ?? this.predicateData,
configurableConstants: overrides.configurableConstants ?? this.configurableConstants,
});
}

/**
* Processes the predicate data and returns the altered bytecode and interface.
*
Expand Down
20 changes: 10 additions & 10 deletions packages/fuel-gauge/src/predicate/predicate-arguments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PredicateU32,
} from '../../test/typegen';

import { fundPredicate, assertBalances } from './utils/predicate';
import { fundAccount, assertBalances } from './utils/predicate';

/**
* @group node
Expand All @@ -34,7 +34,7 @@ describe('Predicate', () => {
});

// transfer funds to predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate, 3);
await fundAccount(fundingWallet, predicate, amountToPredicate, 3);

const receiver = Wallet.generate({ provider });
const initialReceiverBalance = await receiver.getBalance();
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Predicate', () => {
});

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const receiver = Wallet.generate({ provider });

Expand All @@ -91,7 +91,7 @@ describe('Predicate', () => {
const initialReceiverBalance = await receiver.getBalance();

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
receiver.address,
Expand All @@ -118,7 +118,7 @@ describe('Predicate', () => {
const predicate = new PredicateU32({ provider, data: [100] });

// fund predicate
await fundPredicate(fundingWallet, predicate, 90_000_00, 3);
await fundAccount(fundingWallet, predicate, 90_000_00, 3);

const receiver = Wallet.generate({ provider });
const initialReceiverBalance = await receiver.getBalance();
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('Predicate', () => {
const receiver = Wallet.generate({ provider });

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

await expect(
predicate.transfer(receiver.address, 50, provider.getBaseAssetId(), { gasLimit: 1000 })
Expand All @@ -212,7 +212,7 @@ describe('Predicate', () => {
const initialReceiverBalance = await receiver.getBalance();

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
receiver.address,
Expand Down Expand Up @@ -242,7 +242,7 @@ describe('Predicate', () => {
const predicate = new PredicateMultiArgs({ provider, data: [20, 30] });

// fund the predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
receiver.address,
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('Predicate', () => {
const predicate = new PredicateMultiArgs({ provider, data: [20, 30] });

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
receiver.address,
Expand All @@ -299,7 +299,7 @@ describe('Predicate', () => {
const predicate = new PredicateMultiArgs({ provider, data: [20, 20] });

// fund predicate
await fundPredicate(fundingWallet, predicate, amountToPredicate);
await fundAccount(fundingWallet, predicate, amountToPredicate);

const receiver = Wallet.generate({ provider });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils';

import { PredicateTrue, PredicateWithConfigurable } from '../../test/typegen';

import { fundPredicate, assertBalance } from './utils/predicate';
import { fundAccount, assertBalance } from './utils/predicate';

/**
* @group node
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('Predicate', () => {

const amountToTransfer = 200;

await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

// create destination wallet
const destination = WalletUnlocked.generate({
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('Predicate', () => {
await assertBalance(destination, 0, provider.getBaseAssetId());

// transfer funds to predicate
await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

// executing predicate transfer
const tx = await predicate.transfer(
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('Predicate', () => {
await assertBalance(destination, 0, provider.getBaseAssetId());

// transfer funds to predicate
await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

// executing predicate transfer
const tx = await predicate.transfer(
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('Predicate', () => {

await assertBalance(destination, 0, provider.getBaseAssetId());

await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
destination.address,
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('Predicate', () => {

await assertBalance(destination, 0, provider.getBaseAssetId());

await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
destination.address,
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('Predicate', () => {
provider: wallet.provider,
});

await fundPredicate(wallet, predicate, amountToPredicate);
await fundAccount(wallet, predicate, amountToPredicate);

await expect(
predicate.transfer(destination.address, 300, provider.getBaseAssetId(), { gasLimit: 1000 })
Expand Down
Loading
Loading