|
| 1 | +import { getRandomB256 } from 'fuels'; |
| 2 | +import { launchTestNode } from 'fuels/test-utils'; |
| 3 | + |
| 4 | +import { ScriptMainReturnStruct } from '../../test/typegen/scripts/ScriptMainReturnStruct'; |
| 5 | +import type { ScriptMainReturnStructInputs } from '../../test/typegen/scripts/ScriptMainReturnStruct'; |
| 6 | +import { |
| 7 | + ScriptWithComplexArgs, |
| 8 | + type AssetIdInput, |
| 9 | + type ScriptWithComplexArgsInputs, |
| 10 | +} from '../../test/typegen/scripts/ScriptWithComplexArgs'; |
| 11 | +import type { Vec } from '../../test/typegen/scripts/common'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @group browser |
| 15 | + * @group node |
| 16 | + */ |
| 17 | +describe('abi-script', () => { |
| 18 | + describe('decodeArguments', () => { |
| 19 | + it('should decode arguments with a simple script', async () => { |
| 20 | + using launched = await launchTestNode(); |
| 21 | + const { |
| 22 | + wallets: [funder], |
| 23 | + } = launched; |
| 24 | + |
| 25 | + const args: ScriptMainReturnStructInputs = [1, { x: 2 }]; |
| 26 | + |
| 27 | + // Run script |
| 28 | + const script = new ScriptMainReturnStruct(funder); |
| 29 | + const tx = await script.functions.main(...args).call(); |
| 30 | + const { value, transactionResult } = await tx.waitForResult(); |
| 31 | + expect(value).toEqual({ x: 3 }); |
| 32 | + |
| 33 | + // Assert script data |
| 34 | + const scriptData = transactionResult.transaction.scriptData; |
| 35 | + if (!scriptData) { |
| 36 | + throw new Error('No script data'); |
| 37 | + } |
| 38 | + |
| 39 | + // Assert the decoded script data matches the input arguments |
| 40 | + const fn = script.interface.getFunction('main'); |
| 41 | + const decoded = fn.decodeArguments(scriptData); |
| 42 | + expect(decoded).toEqual(args); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should decode arguments with a complex script', async () => { |
| 46 | + using launched = await launchTestNode(); |
| 47 | + |
| 48 | + const { |
| 49 | + wallets: [wallet], |
| 50 | + } = launched; |
| 51 | + |
| 52 | + const arg1 = 100; |
| 53 | + const arg2 = { bits: getRandomB256() }; |
| 54 | + const arg3 = 100; |
| 55 | + const arg4 = [[{ bits: getRandomB256() }, { bits: getRandomB256() }, true]] as Vec< |
| 56 | + [AssetIdInput, AssetIdInput, boolean] |
| 57 | + >; |
| 58 | + const arg5 = { Address: { bits: getRandomB256() } }; |
| 59 | + const arg6 = 100; |
| 60 | + const expected = [ |
| 61 | + expect.toEqualBn(arg1), |
| 62 | + arg2, |
| 63 | + expect.toEqualBn(arg3), |
| 64 | + arg4, |
| 65 | + arg5, |
| 66 | + expect.toEqualBn(arg6), |
| 67 | + ]; |
| 68 | + |
| 69 | + // Run script |
| 70 | + const script = new ScriptWithComplexArgs(wallet); |
| 71 | + const args: ScriptWithComplexArgsInputs = [arg1, arg2, arg3, arg4, arg5, arg6]; |
| 72 | + const tx = await script.functions.main(...args).call(); |
| 73 | + const { value, transactionResult } = await tx.waitForResult(); |
| 74 | + expect(value).toEqual(expected); |
| 75 | + |
| 76 | + // Assert script data |
| 77 | + const scriptData = transactionResult.transaction.scriptData; |
| 78 | + if (!scriptData) { |
| 79 | + throw new Error('No script data'); |
| 80 | + } |
| 81 | + |
| 82 | + // Assert the decoded script data matches the input arguments |
| 83 | + const fn = script.interface.getFunction('main'); |
| 84 | + const decoded = fn.decodeArguments(scriptData); |
| 85 | + expect(decoded).toEqual(expected); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments