Skip to content

Commit

Permalink
feat: added method to duplicate predicate (#3432)
Browse files Browse the repository at this point in the history
* chore: added static method to duplicate predicate

* chore: refactored toNewInstance method

* chore: fixed some lint errors

* chore: added changeset

* Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts

Co-authored-by: Sérgio Torres <[email protected]>

* Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts

Co-authored-by: Sérgio Torres <[email protected]>

* Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts

Co-authored-by: Sérgio Torres <[email protected]>

* Update packages/fuel-gauge/src/predicate/predicate-duplication.test.ts

Co-authored-by: Sérgio Torres <[email protected]>

* Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicate.ts

Co-authored-by: Sérgio Torres <[email protected]>

* Update packages/fuel-gauge/src/predicate/utils/predicate/fundPredicate.ts

Co-authored-by: Sérgio Torres <[email protected]>

* chore: removed helpers

* chore: typing in fund function

* chore: refactored fundPredicate function to fundAccount

* feat: added method to duplicate predicate

---------

Co-authored-by: Dhaiwat <[email protected]>
Co-authored-by: Sérgio Torres <[email protected]>
Co-authored-by: Anderson Arboleya <[email protected]>
  • Loading branch information
4 people authored Jan 2, 2025
1 parent 4ccc628 commit d5a696c
Show file tree
Hide file tree
Showing 17 changed files with 455 additions and 82 deletions.
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
---

feat: added method to duplicate predicate
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

0 comments on commit d5a696c

Please sign in to comment.