Skip to content

Commit

Permalink
fix: incorrect function coder offset usage (#3589)
Browse files Browse the repository at this point in the history
* fix: incorrect offset

* chore: changeset

* chore: added missing @groups

* chore: change name of variable

* chore: added expectations for the script result
  • Loading branch information
petertonysmith94 authored Jan 16, 2025
1 parent 71691a2 commit 69e1ba8
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-carrots-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": patch
---

fix: incorrect function coder offset usage
4 changes: 2 additions & 2 deletions packages/abi-coder/src/FunctionFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ export class FunctionFragment {
const result = this.jsonFnOld.inputs.reduce(
(obj: { decoded: unknown[]; offset: number }, input) => {
const coder = AbiCoder.getCoder(this.jsonAbiOld, input, { encoding: this.encoding });
const [decodedValue, decodedValueByteSize] = coder.decode(bytes, obj.offset);
const [decodedValue, decodedOffset] = coder.decode(bytes, obj.offset);

return {
decoded: [...obj.decoded, decodedValue],
offset: obj.offset + decodedValueByteSize,
offset: decodedOffset,
};
},
{ decoded: [], offset: 0 }
Expand Down
88 changes: 88 additions & 0 deletions packages/fuel-gauge/src/abi/abi-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { getRandomB256 } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { ScriptMainReturnStruct } from '../../test/typegen/scripts/ScriptMainReturnStruct';
import type { ScriptMainReturnStructInputs } from '../../test/typegen/scripts/ScriptMainReturnStruct';
import {
ScriptWithComplexArgs,
type AssetIdInput,
type ScriptWithComplexArgsInputs,
} from '../../test/typegen/scripts/ScriptWithComplexArgs';
import type { Vec } from '../../test/typegen/scripts/common';

/**
* @group browser
* @group node
*/
describe('abi-script', () => {
describe('decodeArguments', () => {
it('should decode arguments with a simple script', async () => {
using launched = await launchTestNode();
const {
wallets: [funder],
} = launched;

const args: ScriptMainReturnStructInputs = [1, { x: 2 }];

// Run script
const script = new ScriptMainReturnStruct(funder);
const tx = await script.functions.main(...args).call();
const { value, transactionResult } = await tx.waitForResult();
expect(value).toEqual({ x: 3 });

// Assert script data
const scriptData = transactionResult.transaction.scriptData;
if (!scriptData) {
throw new Error('No script data');
}

// Assert the decoded script data matches the input arguments
const fn = script.interface.getFunction('main');
const decoded = fn.decodeArguments(scriptData);
expect(decoded).toEqual(args);
});

it('should decode arguments with a complex script', async () => {
using launched = await launchTestNode();

const {
wallets: [wallet],
} = launched;

const arg1 = 100;
const arg2 = { bits: getRandomB256() };
const arg3 = 100;
const arg4 = [[{ bits: getRandomB256() }, { bits: getRandomB256() }, true]] as Vec<
[AssetIdInput, AssetIdInput, boolean]
>;
const arg5 = { Address: { bits: getRandomB256() } };
const arg6 = 100;
const expected = [
expect.toEqualBn(arg1),
arg2,
expect.toEqualBn(arg3),
arg4,
arg5,
expect.toEqualBn(arg6),
];

// Run script
const script = new ScriptWithComplexArgs(wallet);
const args: ScriptWithComplexArgsInputs = [arg1, arg2, arg3, arg4, arg5, arg6];
const tx = await script.functions.main(...args).call();
const { value, transactionResult } = await tx.waitForResult();
expect(value).toEqual(expected);

// Assert script data
const scriptData = transactionResult.transaction.scriptData;
if (!scriptData) {
throw new Error('No script data');
}

// Assert the decoded script data matches the input arguments
const fn = script.interface.getFunction('main');
const decoded = fn.decodeArguments(scriptData);
expect(decoded).toEqual(expected);
});
});
});
1 change: 1 addition & 0 deletions packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ members = [
"script-with-more-configurable",
"script-with-vector",
"script-with-options",
"script-with-complex-args",
"script-with-vector-advanced",
"script-with-vector-mixed",
"small-bytes",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "script-with-complex-args"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
script;

use std::bytes::Bytes;

type PoolId = (AssetId, AssetId, bool);

type ScriptResult = (u64, AssetId, u64, Vec<PoolId>, Identity, u32);

fn main(
amount_in: u64,
asset_in: AssetId,
amount_out_min: u64,
pools: Vec<PoolId>,
recipient: Identity,
deadline: u32,
) -> ScriptResult {
return (amount_in, asset_in, amount_out_min, pools, recipient, deadline);
}

0 comments on commit 69e1ba8

Please sign in to comment.