Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect function coder offset usage #3589

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
87 changes: 87 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,87 @@
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;

// 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(true);

// 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([
expect.toEqualBn(arg1),
arg2,
expect.toEqualBn(arg3),
arg4,
arg5,
expect.toEqualBn(arg6),
]);
});
});
});
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,16 @@
script;

use std::bytes::Bytes;

pub type PoolId = (AssetId, AssetId, bool);

fn main(
amount_in: u64,
asset_in: AssetId,
amount_out_min: u64,
pools: Vec<PoolId>,
recipient: Identity,
deadline: u32,
) -> bool {
return true;
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading