diff --git a/.changeset/gentle-toes-play.md b/.changeset/gentle-toes-play.md new file mode 100644 index 00000000000..98b9a4d469b --- /dev/null +++ b/.changeset/gentle-toes-play.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/account": patch +--- + +feat: added method to duplicate predicate diff --git a/packages/account/src/predicate/predicate.ts b/packages/account/src/predicate/predicate.ts index 1bc90f4a05e..2e9dacdb4c0 100644 --- a/packages/account/src/predicate/predicate.ts +++ b/packages/account/src/predicate/predicate.ts @@ -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. * @@ -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; } @@ -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, 'data' | 'configurableConstants'> = {} + ) { + return new Predicate({ + 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. * diff --git a/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts b/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts index d9c6d2f8ee9..84e0ddd80c9 100644 --- a/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts @@ -9,7 +9,7 @@ import { PredicateU32, } from '../../test/typegen'; -import { fundPredicate, assertBalances } from './utils/predicate'; +import { fundAccount, assertBalances } from './utils/predicate'; /** * @group node @@ -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(); @@ -67,7 +67,7 @@ describe('Predicate', () => { }); // fund predicate - await fundPredicate(fundingWallet, predicate, amountToPredicate); + await fundAccount(fundingWallet, predicate, amountToPredicate); const receiver = Wallet.generate({ provider }); @@ -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, @@ -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(); @@ -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 }) @@ -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, @@ -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, @@ -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, @@ -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 }); diff --git a/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts b/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts index 582f757e1a2..2a409c3d651 100644 --- a/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts @@ -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 @@ -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({ @@ -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( @@ -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( @@ -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, @@ -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, @@ -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 }) diff --git a/packages/fuel-gauge/src/predicate/predicate-duplication.test.ts b/packages/fuel-gauge/src/predicate/predicate-duplication.test.ts new file mode 100644 index 00000000000..54cec443091 --- /dev/null +++ b/packages/fuel-gauge/src/predicate/predicate-duplication.test.ts @@ -0,0 +1,352 @@ +import { WalletUnlocked } from '@fuel-ts/account'; +import { launchTestNode } from 'fuels/test-utils'; + +import { PredicateWithConfigurable } from '../../test/typegen'; +import type { + PredicateWithConfigurableConfigurables, + PredicateWithConfigurableInputs, +} from '../../test/typegen/predicates/PredicateWithConfigurable'; + +import { fundAccount, assertBalance } from './utils/predicate'; + +/** + * @group node + * @group browser + */ +describe('Predicate.fromInstance', () => { + const amountToPredicate = 300_000; + + const defaultValues = { + FEE: 10, + ADDRESS: '0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }; + + it('creates new predicate instance from existing one with default values', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const newPredicate: PredicateWithConfigurable = basePredicate.toNewInstance(); + + expect(newPredicate.predicateData).toEqual(basePredicate.predicateData); + expect(newPredicate.interface).toEqual(basePredicate.interface); + expect(newPredicate.provider).toEqual(basePredicate.provider); + expect(newPredicate.bytes).toEqual(basePredicate.bytes); + }); + + it('creates new predicate instance with custom data', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const data: PredicateWithConfigurableInputs = [ + 13, + '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + ]; + const newPredicate: PredicateWithConfigurable = basePredicate.toNewInstance({ data }); + + expect(newPredicate.predicateData).toEqual(data); + expect(newPredicate.interface).toEqual(basePredicate.interface); + expect(newPredicate.provider).toEqual(basePredicate.provider); + expect(newPredicate.bytes).toEqual(basePredicate.bytes); + expect(newPredicate.predicateData).not.toEqual(basePredicate.predicateData); + }); + + it('creates new predicate instance with configurable constants', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const configurableConstants: PredicateWithConfigurableConfigurables = { + FEE: 13, + ADDRESS: '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }; + const newPredicate: PredicateWithConfigurable = basePredicate.toNewInstance({ + configurableConstants, + }); + + expect(newPredicate.predicateData).toEqual(basePredicate.predicateData); + expect(newPredicate.interface).toEqual(basePredicate.interface); + expect(newPredicate.provider).toEqual(basePredicate.provider); + expect(newPredicate.bytes).toEqual( + new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants, + }).bytes + ); + }); + + it('supports chaining withData and withConfigurableConstants', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const data: PredicateWithConfigurableInputs = [ + 13, + '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + ]; + const configurableConstants: PredicateWithConfigurableConfigurables = { + FEE: 13, + ADDRESS: '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }; + const newPredicate: PredicateWithConfigurable = basePredicate.toNewInstance({ + data, + configurableConstants, + }); + + expect(newPredicate.predicateData).toEqual(data); + expect(newPredicate.interface).toEqual(basePredicate.interface); + expect(newPredicate.provider).toEqual(basePredicate.provider); + expect(newPredicate.bytes).toEqual( + new PredicateWithConfigurable({ + provider, + data, + configurableConstants, + }).bytes + ); + }); + + it('can create multiple different instances from same source, same bytecode', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const newPredicate1 = basePredicate.toNewInstance(); + const newPredicate2 = basePredicate.toNewInstance(); + + expect(newPredicate1.bytes).toEqual(newPredicate2.bytes); + }); + + it('can create multiple different instances from same source, different bytecode', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const configurableConstants: PredicateWithConfigurableConfigurables = { + FEE: 13, + ADDRESS: '0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }; + const newPredicate1 = basePredicate.toNewInstance(); + const newPredicate2 = basePredicate.toNewInstance({ configurableConstants }); + + expect(newPredicate1.bytes).not.toEqual(newPredicate2.bytes); + }); + + it('does not modify original instance', async () => { + using launched = await launchTestNode(); + + const provider = launched.provider; + const basePredicate = new PredicateWithConfigurable({ + provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const bytes = basePredicate.bytes; + const newPredicate = basePredicate.toNewInstance(); + + expect(newPredicate.bytes).toEqual(bytes); + }); + + it('can transfer funds using duplicated predicate with same data and configurable constants', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [wallet], + } = launched; + + const basePredicate = new PredicateWithConfigurable({ + provider: wallet.provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const predicate = basePredicate.toNewInstance(); + + const amountToTransfer = 200; + + await fundAccount(wallet, predicate, amountToPredicate); + + const destination = WalletUnlocked.generate({ + provider: wallet.provider, + }); + + await assertBalance(destination, 0, provider.getBaseAssetId()); + + const tx = await predicate.transfer( + destination.address, + amountToTransfer, + provider.getBaseAssetId(), + { + gasLimit: 1000, + } + ); + + await tx.waitForResult(); + + await assertBalance(destination, amountToTransfer, provider.getBaseAssetId()); + }); + + it('can transfer funds using duplicated predicate with different data and same configurable constants', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [wallet], + } = launched; + + const basePredicate = new PredicateWithConfigurable({ + provider: wallet.provider, + data: [13, '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96'], + configurableConstants: defaultValues, + }); + + const predicate = basePredicate.toNewInstance({ + data: [defaultValues.FEE, defaultValues.ADDRESS], + }); + + const amountToTransfer = 200; + + await fundAccount(wallet, predicate, amountToPredicate); + + const destination = WalletUnlocked.generate({ + provider: wallet.provider, + }); + + await assertBalance(destination, 0, provider.getBaseAssetId()); + + const tx = await predicate.transfer( + destination.address, + amountToTransfer, + provider.getBaseAssetId(), + { + gasLimit: 1000, + } + ); + + await tx.waitForResult(); + + await assertBalance(destination, amountToTransfer, provider.getBaseAssetId()); + }); + + it('can transfer funds using duplicated predicate with same data and different configurable constants', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [wallet], + } = launched; + + const basePredicate = new PredicateWithConfigurable({ + provider: wallet.provider, + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: { + FEE: 13, + ADDRESS: '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }, + }); + + const predicate = basePredicate.toNewInstance({ configurableConstants: defaultValues }); + + const amountToTransfer = 200; + + await fundAccount(wallet, predicate, amountToPredicate); + + const destination = WalletUnlocked.generate({ + provider: wallet.provider, + }); + + await assertBalance(destination, 0, provider.getBaseAssetId()); + + const tx = await predicate.transfer( + destination.address, + amountToTransfer, + provider.getBaseAssetId(), + { + gasLimit: 1000, + } + ); + + await tx.waitForResult(); + + await assertBalance(destination, amountToTransfer, provider.getBaseAssetId()); + }); + + it('can transfer funds using duplicated predicate with different data and configurable constants', async () => { + using launched = await launchTestNode(); + + const { + provider, + wallets: [wallet], + } = launched; + + const basePredicate = new PredicateWithConfigurable({ + provider: wallet.provider, + data: [13, '0x48966232edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96'], + configurableConstants: { + FEE: 15, + ADDRESS: '0x38966262edb5997574be45f94c665aedb41a1663f5b0528e765f355086eebf96', + }, + }); + + const predicate = basePredicate.toNewInstance({ + data: [defaultValues.FEE, defaultValues.ADDRESS], + configurableConstants: defaultValues, + }); + + const amountToTransfer = 200; + + await fundAccount(wallet, predicate, amountToPredicate); + + const destination = WalletUnlocked.generate({ + provider: wallet.provider, + }); + + await assertBalance(destination, 0, provider.getBaseAssetId()); + + const tx = await predicate.transfer( + destination.address, + amountToTransfer, + provider.getBaseAssetId(), + { + gasLimit: 1000, + } + ); + + await tx.waitForResult(); + + await assertBalance(destination, amountToTransfer, provider.getBaseAssetId()); + }); +}); diff --git a/packages/fuel-gauge/src/predicate/predicate-estimations.test.ts b/packages/fuel-gauge/src/predicate/predicate-estimations.test.ts index f37a0d06943..3919c0c9b9f 100644 --- a/packages/fuel-gauge/src/predicate/predicate-estimations.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-estimations.test.ts @@ -22,7 +22,7 @@ import { } from '../../test/typegen'; import type { Validation } from '../types/predicate'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -48,7 +48,7 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicateStruct, fundingAmount); + await fundAccount(wallet, predicateStruct, fundingAmount); const tx = new ScriptTransactionRequest(); @@ -138,7 +138,7 @@ describe('Predicate', () => { const predicateTrue = new PredicateTrue({ provider }); - await fundPredicate(wallet, predicateTrue, fundingAmount); + await fundAccount(wallet, predicateTrue, fundingAmount); const resources = await predicateTrue.getResourcesToSpend([[1, provider.getBaseAssetId()]]); tx.addResources(resources); @@ -167,8 +167,8 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicateTrue, fundingAmount); - await fundPredicate(wallet, predicateStruct, fundingAmount); + await fundAccount(wallet, predicateTrue, fundingAmount); + await fundAccount(wallet, predicateStruct, fundingAmount); const tx = new ScriptTransactionRequest(); const trueResources = await predicateTrue.getResourcesToSpend([ @@ -210,7 +210,7 @@ describe('Predicate', () => { data: [bn(amountToPredicate)], }); - await fundPredicate(wallet, predicateValidateTransfer, amountToPredicate); + await fundAccount(wallet, predicateValidateTransfer, amountToPredicate); const receiverWallet = WalletUnlocked.generate({ provider, @@ -252,7 +252,7 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicateStruct, fundingAmount); + await fundAccount(wallet, predicateStruct, fundingAmount); const transactionRequest = new ScriptTransactionRequest(); @@ -286,7 +286,7 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicateStruct, fundingAmount); + await fundAccount(wallet, predicateStruct, fundingAmount); const transactionRequest = new ScriptTransactionRequest(); diff --git a/packages/fuel-gauge/src/predicate/predicate-evaluations.test.ts b/packages/fuel-gauge/src/predicate/predicate-evaluations.test.ts index eab569adbc2..14bbbc60997 100644 --- a/packages/fuel-gauge/src/predicate/predicate-evaluations.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-evaluations.test.ts @@ -3,7 +3,7 @@ import { launchTestNode } from 'fuels/test-utils'; import { PredicateTrue, PredicateFalse } from '../../test/typegen/predicates'; -import { assertBalances, fundPredicate } from './utils/predicate'; +import { assertBalances, fundAccount } from './utils/predicate'; /** * @group node @@ -24,7 +24,7 @@ describe('Predicate', () => { const predicate = new PredicateTrue({ provider }); - await fundPredicate(wallet, predicate, 200_000); + await fundAccount(wallet, predicate, 200_000); const amountToReceiver = 50; @@ -55,7 +55,7 @@ describe('Predicate', () => { const predicate = new PredicateFalse({ provider }); - await fundPredicate(wallet, predicate, 200_000); + await fundAccount(wallet, predicate, 200_000); await expect( predicate.transfer(receiver.address, 50, provider.getBaseAssetId(), { diff --git a/packages/fuel-gauge/src/predicate/predicate-input-data.test.ts b/packages/fuel-gauge/src/predicate/predicate-input-data.test.ts index 6e80bc22a35..a804b0af70c 100644 --- a/packages/fuel-gauge/src/predicate/predicate-input-data.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-input-data.test.ts @@ -3,7 +3,7 @@ import { launchTestNode } from 'fuels/test-utils'; import { PredicateInputData } from '../../test/typegen'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -24,7 +24,7 @@ describe('Predicate', () => { const predicate = new PredicateInputData({ provider }); - await fundPredicate(wallet, predicate, amountToPredicate); + await fundAccount(wallet, predicate, amountToPredicate); const receiver = Wallet.generate({ provider }); diff --git a/packages/fuel-gauge/src/predicate/predicate-invalidations.test.ts b/packages/fuel-gauge/src/predicate/predicate-invalidations.test.ts index a1fb59388e0..ca812be9fee 100644 --- a/packages/fuel-gauge/src/predicate/predicate-invalidations.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-invalidations.test.ts @@ -4,7 +4,7 @@ import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils'; import { PredicateMainArgsStruct } from '../../test/typegen'; import type { Validation } from '../types/predicate'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -25,7 +25,7 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicate, 1000); + await fundAccount(wallet, predicate, 1000); const receiver = Wallet.generate({ provider }); @@ -59,7 +59,7 @@ describe('Predicate', () => { provider, }); - await fundPredicate(wallet, predicate, 1000); + await fundAccount(wallet, predicate, 1000); const receiver = Wallet.generate({ provider }); diff --git a/packages/fuel-gauge/src/predicate/predicate-populate-witness.test.ts b/packages/fuel-gauge/src/predicate/predicate-populate-witness.test.ts index 827ab20aa8d..162d5c4f67d 100644 --- a/packages/fuel-gauge/src/predicate/predicate-populate-witness.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-populate-witness.test.ts @@ -4,7 +4,7 @@ import { launchTestNode } from 'fuels/test-utils'; import { PredicateAssertNumber, PredicateAssertValue } from '../../test/typegen'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -46,7 +46,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(wallet, predicateAssertNumber, 500_000); + await fundAccount(wallet, predicateAssertNumber, 500_000); let transactionRequest = new ScriptTransactionRequest(); const receiver = Wallet.generate({ provider }); @@ -101,7 +101,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(fundingWallet, predicateAssertNumber, 500_000); + await fundAccount(fundingWallet, predicateAssertNumber, 500_000); const resources1 = await wallet1.getResourcesToSpend(quantity); const predicateAssertNumberWrongResources = await provider.getResourcesToSpend( @@ -156,7 +156,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); const receiver = Wallet.generate({ provider }); @@ -234,7 +234,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); const predicateAssertValue = new Predicate<[boolean]>({ abi: PredicateAssertValue.abi, @@ -243,7 +243,7 @@ describe('Predicate', () => { data: [true], }); - await fundPredicate(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); // predicate resources fetched as non predicate resources const predicateAssertNumberWrongResources = await provider.getResourcesToSpend( @@ -325,7 +325,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); const predicateAssertValue = new Predicate<[boolean]>({ abi: PredicateAssertValue.abi, @@ -334,7 +334,7 @@ describe('Predicate', () => { data: [true], }); - await fundPredicate(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); const resources1 = await wallet1.getResourcesToSpend(quantity); const resources2 = await wallet2.getResourcesToSpend(quantity); @@ -411,7 +411,7 @@ describe('Predicate', () => { data: [11], }); - await fundPredicate(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertNumber, 500_000, UTXOS_AMOUNT); const predicateAssertNumberWrongResources = await provider.getResourcesToSpend( predicateAssertNumber.address, @@ -429,7 +429,7 @@ describe('Predicate', () => { data: [true], }); - await fundPredicate(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); + await fundAccount(fundingWallet, predicateAssertValue, 500_000, UTXOS_AMOUNT); const predicateAssertValueWrongResources = await provider.getResourcesToSpend( predicateAssertValue.address, diff --git a/packages/fuel-gauge/src/predicate/predicate-with-contract.test.ts b/packages/fuel-gauge/src/predicate/predicate-with-contract.test.ts index 89cc08ed1d9..ff9976d3477 100644 --- a/packages/fuel-gauge/src/predicate/predicate-with-contract.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-with-contract.test.ts @@ -4,7 +4,7 @@ import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils'; import { CallTestContractFactory, TokenContractFactory } from '../../test/typegen/contracts'; import { PredicateMainArgsStruct, PredicateTrue } from '../../test/typegen/predicates'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -28,7 +28,7 @@ describe('Predicate', () => { // Create a instance of the contract with the predicate as the caller Account const contractPredicate = new Contract(contract.id, contract.interface, predicate); - await fundPredicate(wallet, predicate, amountToPredicate); + await fundAccount(wallet, predicate, amountToPredicate); const { waitForResult } = await contractPredicate.functions .return_context_amount() @@ -81,7 +81,7 @@ describe('Predicate', () => { ], }); - await fundPredicate(wallet, predicate, amountToPredicate); + await fundAccount(wallet, predicate, amountToPredicate); // executing predicate to transfer resources to receiver const tx = await predicate.transfer( diff --git a/packages/fuel-gauge/src/predicate/predicate-with-script.test.ts b/packages/fuel-gauge/src/predicate/predicate-with-script.test.ts index 10b9e5d72a1..cd002879999 100644 --- a/packages/fuel-gauge/src/predicate/predicate-with-script.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-with-script.test.ts @@ -5,7 +5,7 @@ import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils'; import { PredicateMainArgsStruct, ScriptMainArgs } from '../../test/typegen'; import type { Validation } from '../types/predicate'; -import { fundPredicate } from './utils/predicate'; +import { fundAccount } from './utils/predicate'; /** * @group node @@ -56,7 +56,7 @@ describe('Predicate', () => { ], }); - await fundPredicate(wallet, predicate, amountToPredicate); + await fundAccount(wallet, predicate, amountToPredicate); // executing predicate to transfer resources to receiver const tx = await predicate.transfer( diff --git a/packages/fuel-gauge/src/predicate/utils/predicate/fundAccount.ts b/packages/fuel-gauge/src/predicate/utils/predicate/fundAccount.ts new file mode 100644 index 00000000000..26587ab0de7 --- /dev/null +++ b/packages/fuel-gauge/src/predicate/utils/predicate/fundAccount.ts @@ -0,0 +1,26 @@ +import type { BigNumberish, Account } from 'fuels'; +import { ScriptTransactionRequest, BN } from 'fuels'; + +export const fundAccount = async ( + fundedAccount: Account, + accountToBeFunded: Account, + amount: BigNumberish, + utxosAmount: number = 1 +): Promise => { + const baseAssetId = fundedAccount.provider.getBaseAssetId(); + const request = new ScriptTransactionRequest(); + + for (let i = 0; i < utxosAmount; i++) { + request.addCoinOutput(accountToBeFunded.address, new BN(amount).div(utxosAmount), baseAssetId); + } + + const txCost = await fundedAccount.getTransactionCost(request); + request.gasLimit = txCost.gasUsed; + request.maxFee = txCost.maxFee; + await fundedAccount.fund(request, txCost); + + const submit = await fundedAccount.sendTransaction(request); + await submit.waitForResult(); + + return accountToBeFunded.getBalance(); +}; diff --git a/packages/fuel-gauge/src/predicate/utils/predicate/fundPredicate.ts b/packages/fuel-gauge/src/predicate/utils/predicate/fundPredicate.ts deleted file mode 100644 index 8d092e444a0..00000000000 --- a/packages/fuel-gauge/src/predicate/utils/predicate/fundPredicate.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { InputValue, BigNumberish, WalletUnlocked, Predicate } from 'fuels'; -import { ScriptTransactionRequest, BN } from 'fuels'; - -export const fundPredicate = async ( - wallet: WalletUnlocked, - predicate: Predicate, - amountToPredicate: BigNumberish, - utxosAmount: number = 1 -): Promise => { - const baseAssetId = wallet.provider.getBaseAssetId(); - const request = new ScriptTransactionRequest(); - - for (let i = 0; i < utxosAmount; i++) { - request.addCoinOutput( - predicate.address, - new BN(amountToPredicate).div(utxosAmount), - baseAssetId - ); - } - - const txCost = await wallet.getTransactionCost(request); - request.gasLimit = txCost.gasUsed; - request.maxFee = txCost.maxFee; - await wallet.fund(request, txCost); - - const submit = await wallet.sendTransaction(request); - await submit.waitForResult(); - - return predicate.getBalance(); -}; diff --git a/packages/fuel-gauge/src/predicate/utils/predicate/index.ts b/packages/fuel-gauge/src/predicate/utils/predicate/index.ts index c538f8abb17..0c6b6db67e3 100644 --- a/packages/fuel-gauge/src/predicate/utils/predicate/index.ts +++ b/packages/fuel-gauge/src/predicate/utils/predicate/index.ts @@ -1,3 +1,3 @@ export * from './assertBalance'; export * from './assertBalances'; -export * from './fundPredicate'; +export * from './fundAccount'; diff --git a/templates/nextjs/src/components/Predicate.tsx b/templates/nextjs/src/components/Predicate.tsx index bbc5ca23435..4fa1223b7cd 100644 --- a/templates/nextjs/src/components/Predicate.tsx +++ b/templates/nextjs/src/components/Predicate.tsx @@ -16,7 +16,7 @@ export default function Predicate() { successNotification, } = useNotification(); useNotification(); - const [predicate, setPredicate] = useState>(); + const [predicate, setPredicate] = useState(); const [predicatePin, setPredicatePin] = useState(); const [isLoading, setIsLoading] = useState(false); diff --git a/templates/vite/src/components/Predicate.tsx b/templates/vite/src/components/Predicate.tsx index 0bc92492dad..a4f74f770e2 100644 --- a/templates/vite/src/components/Predicate.tsx +++ b/templates/vite/src/components/Predicate.tsx @@ -15,7 +15,7 @@ export default function Predicate() { transactionSuccessNotification, successNotification, } = useNotification(); - const [predicate, setPredicate] = useState>(); + const [predicate, setPredicate] = useState(); const [predicatePin, setPredicatePin] = useState(); const [isLoading, setIsLoading] = useState(false);