-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incorrect function coder offset usage (#3589)
* fix: incorrect offset * chore: changeset * chore: added missing @groups * chore: change name of variable * chore: added expectations for the script result
- Loading branch information
1 parent
71691a2
commit 69e1ba8
Showing
6 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/fuel-gauge/test/fixtures/forc-projects/script-with-complex-args/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
18 changes: 18 additions & 0 deletions
18
packages/fuel-gauge/test/fixtures/forc-projects/script-with-complex-args/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |