From 35d2e65cd5cd24865fa20075b998d002e9b28f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Thu, 18 Jul 2024 17:40:02 +0200 Subject: [PATCH] chore: improve `fuel-gauge` tests (#2789) Co-authored-by: Chad Nehemiah --- .../fuel-gauge/src/await-execution.test.ts | 41 -------- .../fuel-gauge/src/call-test-contract.test.ts | 36 +++---- packages/fuel-gauge/src/doc-examples.test.ts | 63 +----------- packages/fuel-gauge/src/fee.test.ts | 28 +++--- .../src/funding-transaction.test.ts | 3 - .../src/is-function-readonly.test.ts | 27 +++--- packages/fuel-gauge/src/min-gas.test.ts | 15 +-- packages/fuel-gauge/src/options.test.ts | 64 ++++++------- .../fuel-gauge/src/payable-annotation.test.ts | 10 +- .../src/predicate-conditional-inputs.test.ts | 16 +--- .../src/predicate/predicate-arguments.test.ts | 96 +++++-------------- 11 files changed, 109 insertions(+), 290 deletions(-) diff --git a/packages/fuel-gauge/src/await-execution.test.ts b/packages/fuel-gauge/src/await-execution.test.ts index 8ff4f8bb083..9050a922903 100644 --- a/packages/fuel-gauge/src/await-execution.test.ts +++ b/packages/fuel-gauge/src/await-execution.test.ts @@ -38,45 +38,4 @@ describe('await-execution', () => { expect(response.gqlTransaction?.status?.type).toBe('SuccessStatus'); }); - - test.skip('transferring funds with awaitExecution works', async () => { - using launched = await launchTestNode(); - - const { provider } = launched; - - const { - wallets: [genesisWallet], - } = launched; - - const sendTransactionSpy = vi.spyOn(provider, 'sendTransaction'); - - const destination = Wallet.generate({ provider }); - - await genesisWallet.transfer(destination.address, 100, provider.getBaseAssetId(), { - gasLimit: 10_000, - }); - - expect(sendTransactionSpy).toHaveBeenCalledTimes(1); - const awaitExecutionArg = sendTransactionSpy.mock.calls[0][1]; - expect(awaitExecutionArg).toMatchObject({ awaitExecution: true }); - }); - - test('withdrawToBaseLayer works with awaitExecution', async () => { - using launched = await launchTestNode(); - - const { - provider, - wallets: [genesisWallet], - } = launched; - - const sendTransactionSpy = vi.spyOn(provider, 'sendTransaction'); - - const destination = Wallet.generate({ provider }); - - await genesisWallet.withdrawToBaseLayer(destination.address, 100, { - gasLimit: 44442, - }); - - expect(sendTransactionSpy).toHaveBeenCalledTimes(1); - }); }); diff --git a/packages/fuel-gauge/src/call-test-contract.test.ts b/packages/fuel-gauge/src/call-test-contract.test.ts index 0dc4f880bcc..87fa0f729b0 100644 --- a/packages/fuel-gauge/src/call-test-contract.test.ts +++ b/packages/fuel-gauge/src/call-test-contract.test.ts @@ -7,6 +7,9 @@ import bytecode from '../test/typegen/contracts/CallTestContractAbi.hex'; import { launchTestContract } from './utils'; +function setupContract() { + return launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); +} const U64_MAX = bn(2).pow(64).sub(1); /** @@ -15,8 +18,7 @@ const U64_MAX = bn(2).pow(64).sub(1); */ describe('CallTestContract', () => { it.each([0, 1337, U64_MAX.sub(1)])('can call a contract with u64 (%p)', async (num) => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); - + using contract = await setupContract(); const { waitForResult } = await contract.functions.foo(num).call(); const { value } = await waitForResult(); expect(value.toHex()).toEqual(bn(num).add(1).toHex()); @@ -30,8 +32,7 @@ describe('CallTestContract', () => { [{ a: false, b: U64_MAX.sub(1) }], [{ a: true, b: U64_MAX.sub(1) }], ])('can call a contract with structs (%p)', async (struct) => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); - + using contract = await setupContract(); const { waitForResult } = await contract.functions.boo(struct).call(); const { value } = await waitForResult(); expect(value.a).toEqual(!struct.a); @@ -39,7 +40,7 @@ describe('CallTestContract', () => { }); it('can call a function with empty arguments', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); const call1 = await contract.functions.empty().call(); const { value: empty } = await call1.waitForResult(); @@ -60,7 +61,7 @@ describe('CallTestContract', () => { }); it('function with empty return should resolve undefined', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); // Call method with no params but with no result and no value on config const { waitForResult } = await contract.functions.return_void().call(); @@ -138,10 +139,7 @@ describe('CallTestContract', () => { async (method, { values, expected }) => { // Type cast to Contract because of the dynamic nature of the test // But the function names are type-constrained to correct Contract's type - using contract = await launchTestContract({ - deployer: CallTestContractAbi__factory, - bytecode, - }); + using contract = await setupContract(); const { waitForResult } = await (contract as Contract).functions[method](...values).call(); const { value } = await waitForResult(); @@ -155,7 +153,7 @@ describe('CallTestContract', () => { ); it('Forward amount value on contract call', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); const baseAssetId = contract.provider.getBaseAssetId(); const { waitForResult } = await contract.functions .return_context_amount() @@ -170,7 +168,7 @@ describe('CallTestContract', () => { }); it('Forward asset_id on contract call', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); const assetId = ASSET_A; const { waitForResult } = await contract.functions @@ -186,7 +184,7 @@ describe('CallTestContract', () => { }); it('Forward asset_id on contract simulate call', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); const assetId = ASSET_A; const { waitForResult } = await contract.functions @@ -202,7 +200,7 @@ describe('CallTestContract', () => { }); it('can make multiple calls', async () => { - using contract = await launchTestContract({ deployer: CallTestContractAbi__factory, bytecode }); + using contract = await setupContract(); const num = 1337; const numC = 10; @@ -240,20 +238,14 @@ describe('CallTestContract', () => { }); it('Calling a simple contract function does only one dry run', async () => { - using contract = await launchTestContract({ - deployer: CallTestContractAbi__factory, - bytecode, - }); + using contract = await setupContract(); const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun'); await contract.functions.no_params().call(); expect(dryRunSpy).toHaveBeenCalledOnce(); }); it('Simulating a simple contract function does two dry runs', async () => { - using contract = await launchTestContract({ - deployer: CallTestContractAbi__factory, - bytecode, - }); + using contract = await setupContract(); const dryRunSpy = vi.spyOn(contract.provider.operations, 'dryRun'); await contract.functions.no_params().simulate(); diff --git a/packages/fuel-gauge/src/doc-examples.test.ts b/packages/fuel-gauge/src/doc-examples.test.ts index 05762f93263..9860c1893cd 100644 --- a/packages/fuel-gauge/src/doc-examples.test.ts +++ b/packages/fuel-gauge/src/doc-examples.test.ts @@ -1,4 +1,4 @@ -import type { Bech32Address, BigNumberish, Bytes, JsonAbi, WalletLocked } from 'fuels'; +import type { Bech32Address, BigNumberish, Bytes, WalletLocked } from 'fuels'; import { Predicate, bn, @@ -277,67 +277,14 @@ describe('Doc Examples', () => { const receiver = Wallet.generate({ provider }); - const AbiInputs: JsonAbi = { - types: [ - { - typeId: 0, - type: 'bool', - components: null, - typeParameters: null, - }, - { - typeId: 1, - type: 'struct B512', - components: null, - typeParameters: null, - }, - { - typeId: 2, - type: '[_; 3]', - components: [ - { - name: '__array_element', - type: 1, - typeArguments: null, - }, - ], - - typeParameters: null, - }, - ], - functions: [ - { - inputs: [ - { - name: 'data', - type: 2, - typeArguments: null, - }, - ], - name: 'main', - output: { - name: '', - type: 0, - typeArguments: null, - }, - attributes: null, - }, - ], - loggedTypes: [], - configurables: [], - messagesTypes: [], - }; const dataToSign = '0x0000000000000000000000000000000000000000000000000000000000000000'; const signature1 = await wallet1.signMessage(dataToSign); const signature2 = await wallet2.signMessage(dataToSign); const signature3 = await wallet3.signMessage(dataToSign); - const signatures = [signature1, signature2, signature3]; - const predicate = new Predicate({ - bytecode: PredicateTripleSigAbi__factory.bin, - provider, - abi: AbiInputs, - inputData: [signatures], - }); + const predicate = PredicateTripleSigAbi__factory.createInstance(provider, [ + [signature1, signature2, signature3], + ]); + const amountToPredicate = 600_000; const amountToReceiver = 100; diff --git a/packages/fuel-gauge/src/fee.test.ts b/packages/fuel-gauge/src/fee.test.ts index a59ef2226e5..d597321e6ca 100644 --- a/packages/fuel-gauge/src/fee.test.ts +++ b/packages/fuel-gauge/src/fee.test.ts @@ -1,4 +1,4 @@ -import { ContractFactory, Predicate, ScriptTransactionRequest, Wallet, getRandomB256 } from 'fuels'; +import { ContractFactory, ScriptTransactionRequest, Wallet, getRandomB256 } from 'fuels'; import type { BN } from 'fuels'; import { launchTestNode, ASSET_A, ASSET_B, expectToBeInRange } from 'fuels/test-utils'; @@ -193,21 +193,20 @@ describe('Fee', () => { }); it('should ensure fee is properly calculated on a contract call', async () => { - using launched = await launchTestNode(); + using launched = await launchTestNode({ + contractsConfigs: [ + { + deployer: CallTestContractAbi__factory, + bytecode: CallTestContractAbiHex, + }, + ], + }); const { + contracts: [contract], wallets: [wallet], } = launched; - const factory = new ContractFactory( - CallTestContractAbiHex, - CallTestContractAbi__factory.abi, - wallet - ); - - const deploy = await factory.deployContract(); - const { contract } = await deploy.waitForResult(); - const balanceBefore = await wallet.getBalance(); const { waitForResult } = await contract.functions.sum_multparams(1, 2, 3, 4, 5).call(); @@ -319,12 +318,7 @@ describe('Fee', () => { wallets: [wallet], } = launched; - const predicate = new Predicate({ - bytecode: PredicateU32Abi__factory.bin, - abi: PredicateU32Abi__factory.abi, - provider, - inputData: [1078], - }); + const predicate = PredicateU32Abi__factory.createInstance(provider, [1078]); const tx1 = await wallet.transfer(predicate.address, 1_000_000, provider.getBaseAssetId()); await tx1.wait(); diff --git a/packages/fuel-gauge/src/funding-transaction.test.ts b/packages/fuel-gauge/src/funding-transaction.test.ts index 32f66bed68b..322bf5ea276 100644 --- a/packages/fuel-gauge/src/funding-transaction.test.ts +++ b/packages/fuel-gauge/src/funding-transaction.test.ts @@ -49,7 +49,6 @@ describe('Funding Transactions', () => { const initialAmount = 500_000; using launched = await launchTestNode({ walletsConfig: { - count: 2, amountPerCoin: initialAmount, }, }); @@ -163,7 +162,6 @@ describe('Funding Transactions', () => { const initialAmount = 500_000; using launched = await launchTestNode({ walletsConfig: { - count: 2, amountPerCoin: initialAmount, }, }); @@ -216,7 +214,6 @@ describe('Funding Transactions', () => { const initialAmount = 100_000; using launched = await launchTestNode({ walletsConfig: { - count: 2, amountPerCoin: initialAmount, }, }); diff --git a/packages/fuel-gauge/src/is-function-readonly.test.ts b/packages/fuel-gauge/src/is-function-readonly.test.ts index 58c21273d16..7452826857f 100644 --- a/packages/fuel-gauge/src/is-function-readonly.test.ts +++ b/packages/fuel-gauge/src/is-function-readonly.test.ts @@ -3,40 +3,37 @@ import StorageTestContractAbiHex from '../test/typegen/contracts/StorageTestCont import { launchTestContract } from './utils'; +function setupContract() { + return launchTestContract({ + deployer: StorageTestContractAbi__factory, + bytecode: StorageTestContractAbiHex, + }); +} /** * @group node * @group browser */ describe('isReadOnly', () => { test('isReadOnly returns true for a read-only function', async () => { - using contractInstance = await launchTestContract({ - deployer: StorageTestContractAbi__factory, - bytecode: StorageTestContractAbiHex, - }); + using contract = await setupContract(); - const isReadOnly = contractInstance.functions.counter.isReadOnly(); + const isReadOnly = contract.functions.counter.isReadOnly(); expect(isReadOnly).toBe(true); }); test('isReadOnly returns false for a function containing write operations', async () => { - using contractInstance = await launchTestContract({ - deployer: StorageTestContractAbi__factory, - bytecode: StorageTestContractAbiHex, - }); + using contract = await setupContract(); - const isReadOnly = contractInstance.functions.increment_counter.isReadOnly(); + const isReadOnly = contract.functions.increment_counter.isReadOnly(); expect(isReadOnly).toBe(false); }); test('isReadOnly does not throw a runtime error for a function that does not use storage', async () => { - using contractInstance = await launchTestContract({ - deployer: StorageTestContractAbi__factory, - bytecode: StorageTestContractAbiHex, - }); + using contract = await setupContract(); - const isReadOnly = contractInstance.functions.return_true.isReadOnly(); + const isReadOnly = contract.functions.return_true.isReadOnly(); expect(isReadOnly).toBe(true); }); diff --git a/packages/fuel-gauge/src/min-gas.test.ts b/packages/fuel-gauge/src/min-gas.test.ts index 8a36da46632..39a56b19d48 100644 --- a/packages/fuel-gauge/src/min-gas.test.ts +++ b/packages/fuel-gauge/src/min-gas.test.ts @@ -3,7 +3,6 @@ import { TransactionStatus, ScriptTransactionRequest, Address, - Predicate, hexlify, getGasUsedFromReceipts, BigNumberCoder, @@ -118,12 +117,7 @@ describe('Minimum gas tests', () => { /** * Setup predicate */ - const predicate = new Predicate({ - bytecode: ComplexPredicateAbi__factory.bin, - abi: ComplexPredicateAbi__factory.abi, - provider, - inputData: [bn(1000)], - }); + const predicate = ComplexPredicateAbi__factory.createInstance(provider, [bn(1000)]); /** * Fund the predicate @@ -171,12 +165,7 @@ describe('Minimum gas tests', () => { /** * Setup predicate */ - const predicate = new Predicate({ - bytecode: ComplexPredicateAbi__factory.bin, - abi: ComplexPredicateAbi__factory.abi, - provider, - inputData: [bn(1000)], - }); + const predicate = ComplexPredicateAbi__factory.createInstance(provider, [bn(1000)]); /** * Fund the predicate diff --git a/packages/fuel-gauge/src/options.test.ts b/packages/fuel-gauge/src/options.test.ts index 394c9cdbd05..c1662a28e3b 100644 --- a/packages/fuel-gauge/src/options.test.ts +++ b/packages/fuel-gauge/src/options.test.ts @@ -22,9 +22,9 @@ function launchOptionsContract() { */ describe('Options Tests', () => { it('calls', async () => { - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const { waitForResult } = await contractInstance.functions.print_enum_option_array().call(); + const { waitForResult } = await contract.functions.print_enum_option_array().call(); const { value } = await waitForResult(); expect(value).toStrictEqual({ @@ -47,14 +47,14 @@ describe('Options Tests', () => { const someInput = U8_MAX; const noneInput = undefined; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_option(someInput).call(); + const call1 = await contract.functions.echo_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); expect(someValue).toBe(someInput); - const call2 = await contractInstance.functions.echo_option(noneInput).call(); + const call2 = await contract.functions.echo_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); expect(noneValue).toBe(noneInput); @@ -68,9 +68,9 @@ describe('Options Tests', () => { two: U32_MAX, }; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_struct_enum_option(someInput).call(); + const call1 = await contract.functions.echo_struct_enum_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); expect(someValue).toStrictEqual(someInput); @@ -82,7 +82,7 @@ describe('Options Tests', () => { two: undefined, }; - const call2 = await contractInstance.functions.echo_struct_enum_option(noneInput).call(); + const call2 = await contract.functions.echo_struct_enum_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); expect(noneValue).toStrictEqual(noneInput); @@ -91,23 +91,23 @@ describe('Options Tests', () => { it('echos vec option', async () => { const someInput = [U8_MAX, U16_MAX, U32_MAX]; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_vec_option(someInput).call(); + const call1 = await contract.functions.echo_vec_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); expect(someValue).toStrictEqual(someInput); const noneInput = [undefined, undefined, undefined]; - const call2 = await contractInstance.functions.echo_vec_option(noneInput).call(); + const call2 = await contract.functions.echo_vec_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); expect(noneValue).toStrictEqual(noneInput); const mixedInput = [U8_MAX, undefined, U32_MAX]; - const call3 = await contractInstance.functions.echo_vec_option(mixedInput).call(); + const call3 = await contract.functions.echo_vec_option(mixedInput).call(); const { value: mixedValue } = await call3.waitForResult(); expect(mixedValue).toStrictEqual(mixedInput); @@ -116,9 +116,9 @@ describe('Options Tests', () => { it('echos tuple option', async () => { const someInput = [U8_MAX, U16_MAX]; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_tuple_option(someInput).call(); + const call1 = await contract.functions.echo_tuple_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); @@ -126,7 +126,7 @@ describe('Options Tests', () => { const noneInput = [undefined, undefined]; - const call2 = await contractInstance.functions.echo_tuple_option(noneInput).call(); + const call2 = await contract.functions.echo_tuple_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); @@ -134,7 +134,7 @@ describe('Options Tests', () => { const mixedInput = [U8_MAX, undefined]; - const call3 = await contractInstance.functions.echo_tuple_option(mixedInput).call(); + const call3 = await contract.functions.echo_tuple_option(mixedInput).call(); const { value: mixedValue } = await call3.waitForResult(); @@ -144,9 +144,9 @@ describe('Options Tests', () => { it('echoes enum option', async () => { const someInput = { a: U8_MAX }; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_enum_option(someInput).call(); + const call1 = await contract.functions.echo_enum_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); @@ -154,7 +154,7 @@ describe('Options Tests', () => { const noneInput = { b: undefined }; - const call2 = await contractInstance.functions.echo_enum_option(noneInput).call(); + const call2 = await contract.functions.echo_enum_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); @@ -164,23 +164,23 @@ describe('Options Tests', () => { it('echos array option', async () => { const someInput = [U8_MAX, U16_MAX, 123]; - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_array_option(someInput).call(); + const call1 = await contract.functions.echo_array_option(someInput).call(); const { value: someValue } = await call1.waitForResult(); expect(someValue).toStrictEqual(someInput); const noneInput = [undefined, undefined, undefined]; - const call2 = await contractInstance.functions.echo_array_option(noneInput).call(); + const call2 = await contract.functions.echo_array_option(noneInput).call(); const { value: noneValue } = await call2.waitForResult(); expect(noneValue).toStrictEqual(noneInput); const mixedInput = [U8_MAX, undefined, 123]; - const call3 = await contractInstance.functions.echo_array_option(mixedInput).call(); + const call3 = await contract.functions.echo_array_option(mixedInput).call(); const { value: mixedValue } = await call3.waitForResult(); expect(mixedValue).toStrictEqual(mixedInput); @@ -193,10 +193,8 @@ describe('Options Tests', () => { }, }; - using contractInstance = await launchOptionsContract(); - const { waitForResult } = await contractInstance.functions - .echo_deeply_nested_option(input) - .call(); + using contract = await launchOptionsContract(); + const { waitForResult } = await contract.functions.echo_deeply_nested_option(input).call(); const { value } = await waitForResult(); @@ -214,11 +212,11 @@ describe('Options Tests', () => { }); const { - contracts: [contractInstance], + contracts: [contract], wallets: [wallet], } = launched; - const { waitForResult } = await contractInstance.functions + const { waitForResult } = await contract.functions .get_some_struct({ Address: { bits: wallet.address.toB256() } }) .call(); @@ -228,19 +226,19 @@ describe('Options Tests', () => { }); it('echoes option enum diff sizes', async () => { - using contractInstance = await launchOptionsContract(); + using contract = await launchOptionsContract(); - const call1 = await contractInstance.functions.echo_enum_diff_sizes(undefined).call(); + const call1 = await contract.functions.echo_enum_diff_sizes(undefined).call(); const { value } = await call1.waitForResult(); expect(value).toStrictEqual(undefined); - const call2 = await contractInstance.functions.echo_enum_diff_sizes({ a: U8_MAX }).call(); + const call2 = await contract.functions.echo_enum_diff_sizes({ a: U8_MAX }).call(); const { value: value2 } = await call2.waitForResult(); expect(value2).toStrictEqual({ a: U8_MAX }); - const call3 = await contractInstance.functions + const call3 = await contract.functions .echo_enum_diff_sizes({ b: '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c', }) diff --git a/packages/fuel-gauge/src/payable-annotation.test.ts b/packages/fuel-gauge/src/payable-annotation.test.ts index 54a6fe5083f..12ca9b6e9a3 100644 --- a/packages/fuel-gauge/src/payable-annotation.test.ts +++ b/packages/fuel-gauge/src/payable-annotation.test.ts @@ -39,8 +39,8 @@ test("don't allow sending coins to non-payable functions", async () => { const baseAssetId = contract.provider.getBaseAssetId(); // This should fail because the function is not payable - await expect(async () => - contract.functions + expect(async () => { + const tx = await contract.functions .non_payable() .callParams({ forward: { @@ -48,8 +48,10 @@ test("don't allow sending coins to non-payable functions", async () => { assetId: baseAssetId, }, }) - .call() - ).rejects.toThrowError( + .call(); + + await tx.waitForResult(); + }).rejects.toThrowError( `The target function non_payable cannot accept forwarded funds as it's not marked as 'payable'.` ); }); diff --git a/packages/fuel-gauge/src/predicate-conditional-inputs.test.ts b/packages/fuel-gauge/src/predicate-conditional-inputs.test.ts index 8eae297f731..976a41a59ff 100644 --- a/packages/fuel-gauge/src/predicate-conditional-inputs.test.ts +++ b/packages/fuel-gauge/src/predicate-conditional-inputs.test.ts @@ -1,4 +1,4 @@ -import { Predicate, Wallet, ScriptTransactionRequest, bn } from 'fuels'; +import { Wallet, ScriptTransactionRequest, bn } from 'fuels'; import { launchTestNode, ASSET_A, ASSET_B } from 'fuels/test-utils'; import { PredicateConditionalInputsAbi__factory } from '../test/typegen/predicates'; @@ -22,11 +22,8 @@ describe('PredicateConditionalInputs', () => { const amountToTransfer = 1000; - const predicate = new Predicate({ - bytecode: PredicateConditionalInputsAbi__factory.bin, - abi: PredicateConditionalInputsAbi__factory.abi, - provider, - configurableConstants: { MAKER: aliceWallet.address.toB256() }, + const predicate = PredicateConditionalInputsAbi__factory.createInstance(provider, undefined, { + MAKER: aliceWallet.address.toB256(), }); // transfer asset A to predicate so it can transfer to alice @@ -99,11 +96,8 @@ describe('PredicateConditionalInputs', () => { const amountToTransfer = 1000; - const predicate = new Predicate({ - bytecode: PredicateConditionalInputsAbi__factory.bin, - abi: PredicateConditionalInputsAbi__factory.abi, - provider, - configurableConstants: { MAKER: aliceWallet.address.toB256() }, + const predicate = PredicateConditionalInputsAbi__factory.createInstance(provider, undefined, { + MAKER: aliceWallet.address.toB256(), }); // transfer asset A to predicate so it can transfer to alice diff --git a/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts b/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts index 5ccec84657b..58032b34fd8 100644 --- a/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-arguments.test.ts @@ -1,5 +1,4 @@ -import type { BigNumberish } from 'fuels'; -import { toHex, Predicate, Wallet } from 'fuels'; +import { toHex, Wallet } from 'fuels'; import { launchTestNode } from 'fuels/test-utils'; import { @@ -9,7 +8,6 @@ import { PredicateMultiArgsAbi__factory, PredicateU32Abi__factory, } from '../../test/typegen'; -import type { Validation } from '../types/predicate'; import { fundPredicate, assertBalances } from './utils/predicate'; @@ -30,12 +28,9 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[string]>({ - bytecode: PredicateAddressAbi__factory.bin, - abi: PredicateAddressAbi__factory.abi, - provider, - inputData: ['0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a'], - }); + const predicate = PredicateAddressAbi__factory.createInstance(provider, [ + '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a', + ]); // transfer funds to predicate await fundPredicate(fundingWallet, predicate, amountToPredicate, 3); @@ -65,12 +60,9 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[string]>({ - bytecode: PredicateAddressAbi__factory.bin, - abi: PredicateAddressAbi__factory.abi, - provider, - inputData: ['0xbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbada'], - }); + const predicate = PredicateAddressAbi__factory.createInstance(provider, [ + '0xbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbada', + ]); // fund predicate await fundPredicate(fundingWallet, predicate, amountToPredicate); @@ -90,12 +82,7 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[number]>({ - bytecode: PredicateU32Abi__factory.bin, - abi: PredicateU32Abi__factory.abi, - provider, - inputData: [1078], - }); + const predicate = PredicateU32Abi__factory.createInstance(provider, [1078]); const receiver = Wallet.generate({ provider }); @@ -126,12 +113,7 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[number]>({ - bytecode: PredicateU32Abi__factory.bin, - abi: PredicateU32Abi__factory.abi, - provider, - inputData: [100], - }); + const predicate = PredicateU32Abi__factory.createInstance(provider, [100]); // fund predicate await fundPredicate(fundingWallet, predicate, 90_000_00, 3); @@ -156,12 +138,11 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicateInstanceForBalance = new Predicate<[Validation]>({ - bytecode: PredicateMainArgsStructAbi__factory.bin, - abi: PredicateMainArgsStructAbi__factory.abi, + const predicateInstanceForBalance = PredicateMainArgsStructAbi__factory.createInstance( provider, - inputData: [{ has_account: true, total_complete: 100 }], - }); + [{ has_account: true, total_complete: 100 }] + ); + const receiver = Wallet.generate({ provider }); const initialReceiverBalance = await receiver.getBalance(); @@ -174,12 +155,9 @@ describe('Predicate', () => { await fundTx.waitForResult(); // #region predicate-struct-arg - const predicate = new Predicate<[Validation]>({ - bytecode: PredicateMainArgsStructAbi__factory.bin, - abi: PredicateMainArgsStructAbi__factory.abi, - provider, - inputData: [{ has_account: true, total_complete: 100 }], - }); + const predicate = PredicateMainArgsStructAbi__factory.createInstance(provider, [ + { has_account: true, total_complete: 100 }, + ]); const tx = await predicate.transfer( receiver.address, @@ -204,17 +182,9 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[Validation]>({ - bytecode: PredicateMainArgsStructAbi__factory.bin, - abi: PredicateMainArgsStructAbi__factory.abi, - provider, - inputData: [ - { - has_account: false, - total_complete: 0, - }, - ], - }); + const predicate = PredicateMainArgsStructAbi__factory.createInstance(provider, [ + { has_account: false, total_complete: 0 }, + ]); const receiver = Wallet.generate({ provider }); @@ -234,12 +204,7 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate<[BigNumberish[]]>({ - bytecode: PredicateMainArgsVectorAbi__factory.bin, - abi: PredicateMainArgsVectorAbi__factory.abi, - provider, - inputData: [[42]], - }); + const predicate = PredicateMainArgsVectorAbi__factory.createInstance(provider, [[42]]); const receiver = Wallet.generate({ provider }); const initialReceiverBalance = await receiver.getBalance(); @@ -273,12 +238,7 @@ describe('Predicate', () => { const initialReceiverBalance = await receiver.getBalance(); // #region predicate-multi-args - const predicate = new Predicate({ - bytecode: PredicateMultiArgsAbi__factory.bin, - abi: PredicateMultiArgsAbi__factory.abi, - provider, - inputData: [20, 30], - }); + const predicate = PredicateMultiArgsAbi__factory.createInstance(provider, [20, 30]); // fund the predicate await fundPredicate(fundingWallet, predicate, amountToPredicate); @@ -309,12 +269,7 @@ describe('Predicate', () => { const receiver = Wallet.generate({ provider }); const initialReceiverBalance = await receiver.getBalance(); - const predicate = new Predicate({ - bytecode: PredicateMultiArgsAbi__factory.bin, - abi: PredicateMultiArgsAbi__factory.abi, - provider, - inputData: [20, 30], - }); + const predicate = PredicateMultiArgsAbi__factory.createInstance(provider, [20, 30]); // fund predicate await fundPredicate(fundingWallet, predicate, amountToPredicate); @@ -341,12 +296,7 @@ describe('Predicate', () => { wallets: [fundingWallet], } = launched; - const predicate = new Predicate({ - bytecode: PredicateMultiArgsAbi__factory.bin, - abi: PredicateMultiArgsAbi__factory.abi, - provider, - inputData: [20, 20], - }); + const predicate = PredicateMultiArgsAbi__factory.createInstance(provider, [20, 20]); // fund predicate await fundPredicate(fundingWallet, predicate, amountToPredicate);