From c68b6d6450cf4de9c5e384cbd0c3c04c6d713689 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 5 Mar 2024 11:14:14 +0000 Subject: [PATCH 01/70] chore: use temp sway and forc branch --- packages/forc/VERSION | 2 +- .../test/fixtures/forc-projects-experimental/Forc.toml | 3 +++ packages/versions/src/lib/getBuiltinVersions.ts | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/forc/VERSION b/packages/forc/VERSION index 1e0c609c978..adfd70389fb 100644 --- a/packages/forc/VERSION +++ b/packages/forc/VERSION @@ -1 +1 @@ -0.51.1 +git:xunilrj/encoding-contract-call \ No newline at end of file diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 3d346cb2d65..0fe7d04045f 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,2 +1,5 @@ [workspace] members = ["logging"] + +[patch.'https://github.com/fuellabs/sway'] +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/encoding-contract-call" } \ No newline at end of file diff --git a/packages/versions/src/lib/getBuiltinVersions.ts b/packages/versions/src/lib/getBuiltinVersions.ts index 330d609db46..2807df3a453 100644 --- a/packages/versions/src/lib/getBuiltinVersions.ts +++ b/packages/versions/src/lib/getBuiltinVersions.ts @@ -1,6 +1,6 @@ export function getBuiltinVersions() { return { - FORC: '0.51.1', + FORC: 'git:xunilrj/encoding-contract-call', FUEL_CORE: '0.22.1', FUELS: '0.76.0', }; From 365bfd151d5fe435f149ae6c5aa6b5b065dac3ab Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 5 Mar 2024 11:26:31 +0000 Subject: [PATCH 02/70] lint: forc --- .../test/fixtures/forc-projects-experimental/Forc.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 0fe7d04045f..97b02174e15 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -2,4 +2,4 @@ members = ["logging"] [patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/encoding-contract-call" } \ No newline at end of file +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/encoding-contract-call" } From 68e91c5e25f218465b51bc0b0105325c624b173c Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 5 Mar 2024 11:27:26 +0000 Subject: [PATCH 03/70] chore: temp disable forc format --- .github/workflows/test.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0174bd7513c..1eb533b6f93 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,8 +43,9 @@ jobs: - name: Verify package.json integrity run: pnpm lint:package-jsons - - name: Forc Format Check - run: pnpm forc:check + # Temp disable + # - name: Forc Format Check + # run: pnpm forc:check # linting of some tests depends on pretest being run so that it generates the necessary files - name: Pretest From 0a101f7fed29e1f60395221e6bbc73b2d68db1e6 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Fri, 8 Mar 2024 14:30:31 +0100 Subject: [PATCH 04/70] feat: validate u8 decode in script --- packages/abi-coder/src/FunctionFragment.ts | 4 +- packages/forc/VERSION | 2 +- .../src/experimental-logging.test.ts | 4 +- .../src/experimental-script.test.ts | 39 +++++++++++++++++++ .../forc-projects-experimental/Forc.toml | 4 +- .../script/Forc.toml | 7 ++++ .../script/src/main.sw | 6 +++ 7 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 packages/fuel-gauge/src/experimental-script.test.ts create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index 3d946977eca..5c00a9119c6 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -206,7 +206,9 @@ export class FunctionFragment< } const bytes = arrayify(data); - const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output); + const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output, { + encoding: this.jsonAbi.encoding, + }); return coder.decode(bytes, 0) as [DecodedValue | undefined, number]; } diff --git a/packages/forc/VERSION b/packages/forc/VERSION index adfd70389fb..7e34ee1b9e0 100644 --- a/packages/forc/VERSION +++ b/packages/forc/VERSION @@ -1 +1 @@ -git:xunilrj/encoding-contract-call \ No newline at end of file +git:xunilrj/fix-new-encoding-abi \ No newline at end of file diff --git a/packages/fuel-gauge/src/experimental-logging.test.ts b/packages/fuel-gauge/src/experimental-logging.test.ts index a54a67609c2..45f8d3c6077 100644 --- a/packages/fuel-gauge/src/experimental-logging.test.ts +++ b/packages/fuel-gauge/src/experimental-logging.test.ts @@ -17,10 +17,10 @@ const B512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; beforeAll(async () => { - const contractName = 'logging'; + const projectName = 'logging'; const path = join( __dirname, - `../test/fixtures/forc-projects-experimental/${contractName}/out/release/${contractName}` + `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` ); const contractBytecode = readFileSync(`${path}.bin`); const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts new file mode 100644 index 00000000000..fa4e0578399 --- /dev/null +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -0,0 +1,39 @@ +import { readFileSync } from 'fs'; +import type { BN } from 'fuels'; +import { Script } from 'fuels'; +import { join } from 'path'; + +import { createWallet } from './utils'; + +let script: Script; +let gasPrice: BN; + +const U8_MAX = 2 ** 8 - 1; +const U16_MAX = 2 ** 16 - 1; +const U32_MAX = 2 ** 32 - 1; + +beforeAll(async () => { + const projectName = 'script'; + const path = join( + __dirname, + `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` + ); + const bytes = readFileSync(`${path}.bin`); + const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); + + const wallet = await createWallet(); + + script = new Script(bytes, abi, wallet); + + ({ minGasPrice: gasPrice } = wallet.provider.getGasConfig()); +}); + +/** + * @group node + */ +describe('Experimental Logging', () => { + it('prints u8', async () => { + const { value } = await script.functions.main().call(); + expect(value).toBe(U8_MAX); + }); +}); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 97b02174e15..5110e98ed9d 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["logging"] +members = ["logging", "script"] [patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/encoding-contract-call" } +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-new-encoding-abi" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml new file mode 100644 index 00000000000..937fd72852b --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "script" + +[dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw new file mode 100644 index 00000000000..d430b202104 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -0,0 +1,6 @@ +script; + + +fn main() -> u8 { + 255 +} From 0cfb8db1d5b3997da4e523b55255b9622ed3d316 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 15:06:59 +0100 Subject: [PATCH 05/70] test: u8 u16 tuple --- packages/fuel-gauge/src/experimental-script.test.ts | 10 +++++----- .../forc-projects-experimental/script/src/main.sw | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index fa4e0578399..39f99d3de6b 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -8,9 +8,9 @@ import { createWallet } from './utils'; let script: Script; let gasPrice: BN; -const U8_MAX = 2 ** 8 - 1; -const U16_MAX = 2 ** 16 - 1; -const U32_MAX = 2 ** 32 - 1; +const U8_MAX = 255; +const U16_MAX = 65535; +const U32_MAX = 4294967295; beforeAll(async () => { const projectName = 'script'; @@ -32,8 +32,8 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it('prints u8', async () => { + it('prints u8 u16 tuple', async () => { const { value } = await script.functions.main().call(); - expect(value).toBe(U8_MAX); + expect(value).toStrictEqual([U8_MAX, U16_MAX]); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index d430b202104..919a3cde52b 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,6 @@ script; -fn main() -> u8 { - 255 +fn main() -> (u8, u16) { + (255, 65535) } From 328920783662826a1233c83cc01ba65ad9cf5130 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 15:11:35 +0100 Subject: [PATCH 06/70] test: u8 u16 u32 tuple --- packages/fuel-gauge/src/experimental-script.test.ts | 4 ++-- .../fixtures/forc-projects-experimental/script/src/main.sw | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 39f99d3de6b..be038e90d8e 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -32,8 +32,8 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it('prints u8 u16 tuple', async () => { + it('prints u8 u16 u32 tuple', async () => { const { value } = await script.functions.main().call(); - expect(value).toStrictEqual([U8_MAX, U16_MAX]); + expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX]); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 919a3cde52b..9a724c40650 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,6 @@ script; -fn main() -> (u8, u16) { - (255, 65535) +fn main() -> (u8, u16, u32) { + (255, 65535, 4294967295) } From 9fe2cfcfa644d4a3aa17fdede8704f3b5478371f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 15:17:39 +0100 Subject: [PATCH 07/70] test: u8 u16 u32 u64 tuple --- packages/fuel-gauge/src/experimental-script.test.ts | 7 ++++--- .../fixtures/forc-projects-experimental/script/src/main.sw | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index be038e90d8e..9cc1568fc8f 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -1,6 +1,6 @@ import { readFileSync } from 'fs'; import type { BN } from 'fuels'; -import { Script } from 'fuels'; +import { Script, bn } from 'fuels'; import { join } from 'path'; import { createWallet } from './utils'; @@ -11,6 +11,7 @@ let gasPrice: BN; const U8_MAX = 255; const U16_MAX = 65535; const U32_MAX = 4294967295; +export const U64_MAX = bn(2).pow(64).sub(1); beforeAll(async () => { const projectName = 'script'; @@ -32,8 +33,8 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it('prints u8 u16 u32 tuple', async () => { + it('prints u8 u16 u32 u64 tuple', async () => { const { value } = await script.functions.main().call(); - expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX]); + expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX, bn(U64_MAX)]); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 9a724c40650..89ca5d8c928 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,6 @@ script; -fn main() -> (u8, u16, u32) { - (255, 65535, 4294967295) +fn main() -> (u8, u16, u32, u64) { + (255, 65535, 4294967295, 18446744073709551615) } From f13094b10e5d11d4c90eb556d062d624ee34dcee Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 15:26:50 +0100 Subject: [PATCH 08/70] test: u8 u16 u32 u64 bool tuple --- packages/fuel-gauge/src/experimental-script.test.ts | 4 ++-- .../fixtures/forc-projects-experimental/script/src/main.sw | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 9cc1568fc8f..e9f55b15fb6 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -33,8 +33,8 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it('prints u8 u16 u32 u64 tuple', async () => { + it('prints u8 u16 u32 u64 bool tuple', async () => { const { value } = await script.functions.main().call(); - expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX, bn(U64_MAX)]); + expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX, bn(U64_MAX), true, false]); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 89ca5d8c928..6b918a888cd 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,6 @@ script; -fn main() -> (u8, u16, u32, u64) { - (255, 65535, 4294967295, 18446744073709551615) +fn main() -> (u8, u16, u32, u64, bool, bool) { + (255, 65535, 4294967295, 18446744073709551615, true, false) } From a7e0a3bddc2074a4b8f486963d26eeb882206a94 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 17:21:19 +0100 Subject: [PATCH 09/70] test: b256 script decode --- .../src/experimental-script.test.ts | 32 +++++++++++++++++-- .../script/src/main.sw | 29 +++++++++++++++-- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index e9f55b15fb6..9636bb80a79 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -12,6 +12,7 @@ const U8_MAX = 255; const U16_MAX = 65535; const U32_MAX = 4294967295; export const U64_MAX = bn(2).pow(64).sub(1); +export const U256_MAX = bn(2).pow(256).sub(1); beforeAll(async () => { const projectName = 'script'; @@ -33,8 +34,35 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it('prints u8 u16 u32 u64 bool tuple', async () => { + it.skip('prints u8', async () => { const { value } = await script.functions.main().call(); - expect(value).toStrictEqual([U8_MAX, U16_MAX, U32_MAX, bn(U64_MAX), true, false]); + expect(value).toStrictEqual(U8_MAX); + }); + + it.skip('prints u16', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual(U16_MAX); + }); + + it.skip('prints u32', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual(U32_MAX); + }); + + it.skip('prints u64', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual(U64_MAX); + }); + + it.skip('prints u256', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual(U256_MAX); + }); + + it('prints b256', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual( + '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6' + ); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 6b918a888cd..8fb5116f68a 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,29 @@ script; +// fn main() -> u8 { +// 255 +// } -fn main() -> (u8, u16, u32, u64, bool, bool) { - (255, 65535, 4294967295, 18446744073709551615, true, false) -} +// fn main() -> u16 { +// 65535 +// } + +// fn main() -> u32 { +// 4294967295 +// } + +// fn main() -> u64 { +// 18446744073709551615 +// } + +// fn main() -> u256 { +// 18446744073709551615 +// } + +// fn main() -> bool { +// true +// } + +// fn main() -> b256 { +// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 +// } \ No newline at end of file From 89f63cc75be4da514643ca37ac8b8d0330c7639f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sun, 10 Mar 2024 17:41:39 +0100 Subject: [PATCH 10/70] test: b512 scrip --- packages/fuel-gauge/src/experimental-script.test.ts | 10 +++++++++- .../forc-projects-experimental/script/src/main.sw | 10 ++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 9636bb80a79..c5c5e83b3be 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -2,6 +2,7 @@ import { readFileSync } from 'fs'; import type { BN } from 'fuels'; import { Script, bn } from 'fuels'; import { join } from 'path'; +import { b } from 'vitest/dist/suite-MFRDkZcV'; import { createWallet } from './utils'; @@ -59,10 +60,17 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual(U256_MAX); }); - it('prints b256', async () => { + it.skip('prints b256', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual( '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6' ); }); + + it('echos b512', async () => { + const b512 = + '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; + const { value } = await script.functions.main(b512).call(); + expect(value).toStrictEqual(b512); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 8fb5116f68a..4098fa4b165 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,5 +1,7 @@ script; +use std::b512::B512; + // fn main() -> u8 { // 255 // } @@ -17,13 +19,13 @@ script; // } // fn main() -> u256 { -// 18446744073709551615 +// 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256 // } // fn main() -> bool { // true // } -// fn main() -> b256 { -// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 -// } \ No newline at end of file +fn main(input: B512) -> B512 { + input +} \ No newline at end of file From afdf766da1aee37935400734bc1873940b4e1213 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 11 Mar 2024 16:24:58 +0000 Subject: [PATCH 11/70] chore: reanme script functions --- .../script/src/main.sw | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 4098fa4b165..a7100a57e1c 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -2,30 +2,30 @@ script; use std::b512::B512; -// fn main() -> u8 { +// fn print_u8() -> u8 { // 255 // } -// fn main() -> u16 { +// fn print_u16() -> u16 { // 65535 // } -// fn main() -> u32 { +// fn print_u32() -> u32 { // 4294967295 // } -// fn main() -> u64 { +// fn print_u64() -> u64 { // 18446744073709551615 // } -// fn main() -> u256 { +// fn print_u256() -> u256 { // 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256 // } -// fn main() -> bool { +// fn print_bool() -> bool { // true // } -fn main(input: B512) -> B512 { - input -} \ No newline at end of file +// fn echo_b512(input: B512) -> B512 { +// input +// } \ No newline at end of file From 532260901f86964aa34bebd162bf78eb17fa0c42 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 11 Mar 2024 16:26:01 +0000 Subject: [PATCH 12/70] feat: b256 script decode --- .../fixtures/forc-projects-experimental/script/src/main.sw | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index a7100a57e1c..925dd58c7ba 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -26,6 +26,10 @@ use std::b512::B512; // true // } +// fn echo_b256() -> b256 { +// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 +// } + // fn echo_b512(input: B512) -> B512 { // input // } \ No newline at end of file From e19268e5db7f78a8d9795ed64ac5d5fc8880338c Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 11 Mar 2024 16:33:05 +0000 Subject: [PATCH 13/70] feat: str script decode --- packages/fuel-gauge/src/experimental-script.test.ts | 7 ++++++- .../fixtures/forc-projects-experimental/script/src/main.sw | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index c5c5e83b3be..3ccd57e73a5 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -67,10 +67,15 @@ describe('Experimental Logging', () => { ); }); - it('echos b512', async () => { + it.skip('echos b512', async () => { const b512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; const { value } = await script.functions.main(b512).call(); expect(value).toStrictEqual(b512); }); + + it('echos string', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual('fuel'); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 925dd58c7ba..51c08a8cdef 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -32,4 +32,9 @@ use std::b512::B512; // fn echo_b512(input: B512) -> B512 { // input -// } \ No newline at end of file +// } + +fn main() -> str[4] { + let my_string: str[4] = __to_str_array("fuel"); + my_string +} \ No newline at end of file From 81223de64d448af34e317015f0ceef0a78dedcc3 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 11 Mar 2024 16:54:08 +0000 Subject: [PATCH 14/70] feat: std str script decode --- packages/fuel-gauge/src/experimental-script.test.ts | 7 ++++++- .../forc-projects-experimental/script/src/main.sw | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 3ccd57e73a5..01e5a4e30c2 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -74,8 +74,13 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual(b512); }); - it('echos string', async () => { + it.skip('echos string', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual('fuel'); }); + + it('echoes std string', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual('Hello World'); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 51c08a8cdef..495a1b54c08 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -1,6 +1,7 @@ script; use std::b512::B512; +use std::string::String; // fn print_u8() -> u8 { // 255 @@ -34,7 +35,12 @@ use std::b512::B512; // input // } -fn main() -> str[4] { - let my_string: str[4] = __to_str_array("fuel"); +// fn echo_str_array() -> str[4] { +// let my_string: str[4] = __to_str_array("fuel"); +// my_string +// } + +fn main() -> String { + let my_string: String = String::from_ascii_str("Hello World"); my_string } \ No newline at end of file From 647449eff56016c8428b730943a776a1eedd67f3 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 11 Mar 2024 17:16:52 +0000 Subject: [PATCH 15/70] feat: str slice script decode --- packages/fuel-gauge/src/experimental-script.test.ts | 13 +++++++++++-- .../forc-projects-experimental/script/src/main.sw | 12 ++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 01e5a4e30c2..4e6b34f5ab2 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -2,7 +2,6 @@ import { readFileSync } from 'fs'; import type { BN } from 'fuels'; import { Script, bn } from 'fuels'; import { join } from 'path'; -import { b } from 'vitest/dist/suite-MFRDkZcV'; import { createWallet } from './utils'; @@ -79,8 +78,18 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual('fuel'); }); - it('echoes std string', async () => { + it.skip('echoes std string', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual('Hello World'); }); + + /** + * Currently throws, need to confirm with Daniel + */ + it.skip('logs str slice', async () => { + const expected = 'fuel'; + + const { value } = await script.functions.main(expected).call(); + expect(value).toStrictEqual(expected); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 495a1b54c08..611facd8336 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -40,7 +40,11 @@ use std::string::String; // my_string // } -fn main() -> String { - let my_string: String = String::from_ascii_str("Hello World"); - my_string -} \ No newline at end of file +// fn main() -> String { +// let my_string: String = String::from_ascii_str("Hello World"); +// my_string +// } + +// fn main(slice: str) -> str { +// slice +// } \ No newline at end of file From 42b0b5483cd00d90cc99b13a14fcaa8253dc408f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 12 Mar 2024 09:50:33 +0000 Subject: [PATCH 16/70] feat: struct enum vec script decode --- .../src/experimental-script.test.ts | 15 +++++++++-- .../script/src/main.sw | 27 ++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 4e6b34f5ab2..6dcd5e566bb 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -73,12 +73,12 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual(b512); }); - it.skip('echos string', async () => { + it.skip('prints string', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual('fuel'); }); - it.skip('echoes std string', async () => { + it.skip('prints std string', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual('Hello World'); }); @@ -92,4 +92,15 @@ describe('Experimental Logging', () => { const { value } = await script.functions.main(expected).call(); expect(value).toStrictEqual(expected); }); + + it('prints struct enum u8 vec', async () => { + const { value } = await script.functions.main().call(); + expect(value).toStrictEqual({ + x: 5, + y: 128, + state: { Pending: '()' }, + grades: [1, 4, 6, 22], + tag: 'fuel', + }); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 611facd8336..9192579b248 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -3,6 +3,19 @@ script; use std::b512::B512; use std::string::String; +enum MyEnum { + Checked: (), + Pending: (), +} + +struct MyStruct { + x: u8, + y: u8, + state: MyEnum, + grades: [u8; 4], + tag: str[4], +} + // fn print_u8() -> u8 { // 255 // } @@ -47,4 +60,16 @@ use std::string::String; // fn main(slice: str) -> str { // slice -// } \ No newline at end of file +// } + +fn main() -> MyStruct { + let my_struct = MyStruct { + x: 5, + y: 128, + state: MyEnum::Pending, + grades: [1,4,6,22], + tag: __to_str_array("fuel"), + }; + + my_struct +} \ No newline at end of file From c9b3ac9531b49643a1e60297f50d5b7b840439a9 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 12 Mar 2024 10:56:21 +0000 Subject: [PATCH 17/70] test: fix struct --- packages/fuel-gauge/src/experimental-script.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 6dcd5e566bb..693f04426af 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -98,7 +98,7 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual({ x: 5, y: 128, - state: { Pending: '()' }, + state: 'Pending', grades: [1, 4, 6, 22], tag: 'fuel', }); From daa3f2f90ca7368dbd4de5de15604ac67df011f8 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 12 Mar 2024 12:11:49 +0000 Subject: [PATCH 18/70] test: use more complex struct --- .../src/experimental-script.test.ts | 32 +++++-- .../script/src/main.sw | 95 +++++++------------ 2 files changed, 59 insertions(+), 68 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 693f04426af..d18d99f738c 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -11,8 +11,10 @@ let gasPrice: BN; const U8_MAX = 255; const U16_MAX = 65535; const U32_MAX = 4294967295; -export const U64_MAX = bn(2).pow(64).sub(1); -export const U256_MAX = bn(2).pow(256).sub(1); +const U64_MAX = bn(2).pow(64).sub(1); +const U256_MAX = bn(2).pow(256).sub(1); +const B512 = + '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; beforeAll(async () => { const projectName = 'script'; @@ -67,10 +69,8 @@ describe('Experimental Logging', () => { }); it.skip('echos b512', async () => { - const b512 = - '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; - const { value } = await script.functions.main(b512).call(); - expect(value).toStrictEqual(b512); + const { value } = await script.functions.main(B512).call(); + expect(value).toStrictEqual(B512); }); it.skip('prints string', async () => { @@ -93,7 +93,7 @@ describe('Experimental Logging', () => { expect(value).toStrictEqual(expected); }); - it('prints struct enum u8 vec', async () => { + it.skip('prints struct enum u8 vec', async () => { const { value } = await script.functions.main().call(); expect(value).toStrictEqual({ x: 5, @@ -103,4 +103,22 @@ describe('Experimental Logging', () => { tag: 'fuel', }); }); + + it('prints mixed struct', async () => { + const { value } = await script.functions.main(B512).call(); + expect(value).toStrictEqual({ + a: 5, + b: 65535, + c: 4294967295, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + }); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 9192579b248..53212e49ad0 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -3,73 +3,46 @@ script; use std::b512::B512; use std::string::String; -enum MyEnum { +enum NativeEnum { Checked: (), Pending: (), } -struct MyStruct { - x: u8, - y: u8, - state: MyEnum, - grades: [u8; 4], - tag: str[4], +enum MixedEnum { + Value: bool, + Data: u16, } -// fn print_u8() -> u8 { -// 255 -// } - -// fn print_u16() -> u16 { -// 65535 -// } - -// fn print_u32() -> u32 { -// 4294967295 -// } - -// fn print_u64() -> u64 { -// 18446744073709551615 -// } - -// fn print_u256() -> u256 { -// 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256 -// } - -// fn print_bool() -> bool { -// true -// } - -// fn echo_b256() -> b256 { -// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 -// } - -// fn echo_b512(input: B512) -> B512 { -// input -// } - -// fn echo_str_array() -> str[4] { -// let my_string: str[4] = __to_str_array("fuel"); -// my_string -// } - -// fn main() -> String { -// let my_string: String = String::from_ascii_str("Hello World"); -// my_string -// } - -// fn main(slice: str) -> str { -// slice -// } +struct MixedStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, +} -fn main() -> MyStruct { - let my_struct = MyStruct { - x: 5, - y: 128, - state: MyEnum::Pending, - grades: [1,4,6,22], - tag: __to_str_array("fuel"), +fn main(param_one: B512) -> MixedStruct { + let my_struct = MixedStruct { + a: 5, + b: 65535, + c: 4294967295, + d: 18446744073709551615, + e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256, + f: 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6, + g: param_one, + native: NativeEnum::Pending, + mixed: MixedEnum::Value(true), + grades: [1, 4, 6, 22], + fuel: __to_str_array("fuel"), + hello: String::from_ascii_str("Hello World"), }; - + my_struct -} \ No newline at end of file +} From 275d969e866fc0f9a7d0eb322decdcba4cd330ed Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 12 Mar 2024 12:15:07 +0000 Subject: [PATCH 19/70] chore: add todo to mixed struct test --- packages/fuel-gauge/src/experimental-script.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index d18d99f738c..49c924308d8 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -104,6 +104,11 @@ describe('Experimental Logging', () => { }); }); + /** + * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, vec + * + * todo: option, bytes, slice, tuple, deep + */ it('prints mixed struct', async () => { const { value } = await script.functions.main(B512).call(); expect(value).toStrictEqual({ From 693aa380052a0da3249828fae6e6cbdb21302270 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 12 Mar 2024 14:45:29 +0000 Subject: [PATCH 20/70] test: add option to complex struct --- packages/fuel-gauge/src/experimental-script.test.ts | 6 ++++-- .../fixtures/forc-projects-experimental/script/src/main.sw | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 49c924308d8..0ff70ee2e54 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -105,9 +105,9 @@ describe('Experimental Logging', () => { }); /** - * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, vec + * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option * - * todo: option, bytes, slice, tuple, deep + * todo: bytes, slice, tuple, vec, deep */ it('prints mixed struct', async () => { const { value } = await script.functions.main(B512).call(); @@ -124,6 +124,8 @@ describe('Experimental Logging', () => { grades: [1, 4, 6, 22], fuel: 'fuel', hello: 'Hello World', + opt: 42, + nada: undefined, }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 53212e49ad0..48631266fb9 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -2,6 +2,7 @@ script; use std::b512::B512; use std::string::String; +use std::option::Option; enum NativeEnum { Checked: (), @@ -26,6 +27,8 @@ struct MixedStruct { grades: [u8; 4], fuel: str[4], hello: String, + opt: Option, + nada: Option, } fn main(param_one: B512) -> MixedStruct { @@ -42,6 +45,8 @@ fn main(param_one: B512) -> MixedStruct { grades: [1, 4, 6, 22], fuel: __to_str_array("fuel"), hello: String::from_ascii_str("Hello World"), + opt: Some(42), + nada: None, }; my_struct From 47ca00ec19ca9cd0728533d192ff79ddc096b821 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 14:42:59 +0000 Subject: [PATCH 21/70] feat: support bytes script decode and fix option --- .../src/encoding/coders/v0/OptionCoder.ts | 3 +- .../src/encoding/coders/v1/OptionCoder.ts | 41 +++++++++++++++++++ .../src/encoding/strategies/getCoderV1.ts | 2 +- packages/forc/VERSION | 2 +- .../src/experimental-script.test.ts | 1 + .../forc-projects-experimental/Forc.toml | 2 +- .../script/src/main.sw | 15 +++++-- 7 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts diff --git a/packages/abi-coder/src/encoding/coders/v0/OptionCoder.ts b/packages/abi-coder/src/encoding/coders/v0/OptionCoder.ts index 20646fb8f4f..f333d2dea02 100644 --- a/packages/abi-coder/src/encoding/coders/v0/OptionCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/OptionCoder.ts @@ -23,8 +23,7 @@ export class OptionCoder> extends EnumCode } decode(data: Uint8Array, offset: number): [DecodedValueOf, number] { - // An empty option will be one less than the expected encoded length - if (data.length < this.encodedLength - 1) { + if (data.length < this.encodedLength) { throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid option data size.`); } diff --git a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts new file mode 100644 index 00000000000..f333d2dea02 --- /dev/null +++ b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts @@ -0,0 +1,41 @@ +import { ErrorCode, FuelError } from '@fuel-ts/errors'; + +import type { Coder } from '../AbstractCoder'; + +import type { InputValueOf, DecodedValueOf } from './EnumCoder'; +import { EnumCoder } from './EnumCoder'; + +type SwayOption = { None: [] } | { Some: T }; +export type Option = T | undefined; + +export class OptionCoder> extends EnumCoder { + encode(value: InputValueOf): Uint8Array { + const result = super.encode(this.toSwayOption(value) as unknown as InputValueOf); + return result; + } + + toSwayOption(input: InputValueOf): SwayOption { + if (input !== undefined) { + return { Some: input }; + } + + return { None: [] }; + } + + decode(data: Uint8Array, offset: number): [DecodedValueOf, number] { + if (data.length < this.encodedLength) { + throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid option data size.`); + } + + const [decoded, newOffset] = super.decode(data, offset); + return [this.toOption(decoded) as DecodedValueOf, newOffset]; + } + + toOption(output?: DecodedValueOf): Option { + if (output && 'Some' in output) { + return output.Some; + } + + return undefined; + } +} diff --git a/packages/abi-coder/src/encoding/strategies/getCoderV1.ts b/packages/abi-coder/src/encoding/strategies/getCoderV1.ts index db74dc6c361..690e140af79 100644 --- a/packages/abi-coder/src/encoding/strategies/getCoderV1.ts +++ b/packages/abi-coder/src/encoding/strategies/getCoderV1.ts @@ -32,11 +32,11 @@ import { ArrayCoder } from '../coders/v0/ArrayCoder'; import { B256Coder } from '../coders/v0/B256Coder'; import { B512Coder } from '../coders/v0/B512Coder'; import { BigNumberCoder } from '../coders/v0/BigNumberCoder'; -import { OptionCoder } from '../coders/v0/OptionCoder'; import { BooleanCoder } from '../coders/v1/BooleanCoder'; import { ByteCoder } from '../coders/v1/ByteCoder'; import { EnumCoder } from '../coders/v1/EnumCoder'; import { NumberCoder } from '../coders/v1/NumberCoder'; +import { OptionCoder } from '../coders/v1/OptionCoder'; import { RawSliceCoder } from '../coders/v1/RawSliceCoder'; import { StdStringCoder } from '../coders/v1/StdStringCoder'; import { StringCoder } from '../coders/v1/StringCoder'; diff --git a/packages/forc/VERSION b/packages/forc/VERSION index 7e34ee1b9e0..4208c92bc74 100644 --- a/packages/forc/VERSION +++ b/packages/forc/VERSION @@ -1 +1 @@ -git:xunilrj/fix-new-encoding-abi \ No newline at end of file +git:xunilrj/fix-predicate-entry \ No newline at end of file diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 0ff70ee2e54..71e34d65ebd 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -126,6 +126,7 @@ describe('Experimental Logging', () => { hello: 'Hello World', opt: 42, nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 5110e98ed9d..c168417ed21 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -2,4 +2,4 @@ members = ["logging", "script"] [patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-new-encoding-abi" } +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-predicate-entry" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 48631266fb9..50a1ae2c67f 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -3,6 +3,7 @@ script; use std::b512::B512; use std::string::String; use std::option::Option; +use std::bytes::Bytes; enum NativeEnum { Checked: (), @@ -29,9 +30,16 @@ struct MixedStruct { hello: String, opt: Option, nada: Option, + bytes: Bytes, } fn main(param_one: B512) -> MixedStruct { + let mut my_bytes = Bytes::new(); + + my_bytes.push(40u8); + my_bytes.push(41u8); + my_bytes.push(42u8); + let my_struct = MixedStruct { a: 5, b: 65535, @@ -45,9 +53,10 @@ fn main(param_one: B512) -> MixedStruct { grades: [1, 4, 6, 22], fuel: __to_str_array("fuel"), hello: String::from_ascii_str("Hello World"), - opt: Some(42), - nada: None, + opt: Option::Some(42), + nada: Option::None, + bytes: my_bytes, }; my_struct -} +} \ No newline at end of file From cc89150e8f9f89be528df0d0a78752c0fbc36f1e Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 14:48:58 +0000 Subject: [PATCH 22/70] feat: support tuple script decode --- packages/abi-coder/src/FunctionFragment.ts | 1 + packages/fuel-gauge/src/experimental-script.test.ts | 5 +++-- .../fixtures/forc-projects-experimental/script/src/main.sw | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index 6d8904ce9a5..4720ac99c2f 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -206,6 +206,7 @@ export class FunctionFragment< } const bytes = arrayify(data); + console.log('here'); const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output, { encoding: this.jsonAbi.encoding, }); diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 71e34d65ebd..7764f8bd518 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -105,9 +105,9 @@ describe('Experimental Logging', () => { }); /** - * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option + * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option, bytes, tuple * - * todo: bytes, slice, tuple, vec, deep + * todo: slice, vec, deep */ it('prints mixed struct', async () => { const { value } = await script.functions.main(B512).call(); @@ -127,6 +127,7 @@ describe('Experimental Logging', () => { opt: 42, nada: undefined, bytes: Uint8Array.from([40, 41, 42]), + tuple: [255, 65535, 4294967295, 'fuel'], }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 50a1ae2c67f..5782a9d8ea1 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -31,6 +31,7 @@ struct MixedStruct { opt: Option, nada: Option, bytes: Bytes, + tuple: (u8, u16, u32, str[4]), } fn main(param_one: B512) -> MixedStruct { @@ -56,6 +57,7 @@ fn main(param_one: B512) -> MixedStruct { opt: Option::Some(42), nada: Option::None, bytes: my_bytes, + tuple: (255, 65535, 4294967295, __to_str_array("fuel")) }; my_struct From 8bf6a06d55988283f3b337fd4c9e9ddedca30225 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 15:36:16 +0000 Subject: [PATCH 23/70] feat: vec decode script --- packages/fuel-gauge/src/experimental-script.test.ts | 6 ++++-- .../forc-projects-experimental/script/src/main.sw | 12 ++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 7764f8bd518..4c35decd0e7 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -105,9 +105,10 @@ describe('Experimental Logging', () => { }); /** - * done: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option, bytes, tuple + * decode: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option, bytes, tuple, vec + * encode: bytes * - * todo: slice, vec, deep + * todo: slice, deep */ it('prints mixed struct', async () => { const { value } = await script.functions.main(B512).call(); @@ -128,6 +129,7 @@ describe('Experimental Logging', () => { nada: undefined, bytes: Uint8Array.from([40, 41, 42]), tuple: [255, 65535, 4294967295, 'fuel'], + vec_u8: [40, 41, 42], }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index 5782a9d8ea1..d036b7f1ff7 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -4,6 +4,7 @@ use std::b512::B512; use std::string::String; use std::option::Option; use std::bytes::Bytes; +use std::vec::Vec; enum NativeEnum { Checked: (), @@ -32,6 +33,7 @@ struct MixedStruct { nada: Option, bytes: Bytes, tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, } fn main(param_one: B512) -> MixedStruct { @@ -41,6 +43,11 @@ fn main(param_one: B512) -> MixedStruct { my_bytes.push(41u8); my_bytes.push(42u8); + let mut my_vec_u8 = Vec::new(); + my_vec_u8.push(40u8); + my_vec_u8.push(41u8); + my_vec_u8.push(42u8); + let my_struct = MixedStruct { a: 5, b: 65535, @@ -57,8 +64,9 @@ fn main(param_one: B512) -> MixedStruct { opt: Option::Some(42), nada: Option::None, bytes: my_bytes, - tuple: (255, 65535, 4294967295, __to_str_array("fuel")) + tuple: (255, 65535, 4294967295, __to_str_array("fuel")), + vec_u8: my_vec_u8, }; my_struct -} \ No newline at end of file +} From e6518c61f84c879c6a21e2012c423b33c11c73d0 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 15:38:55 +0000 Subject: [PATCH 24/70] feat: vec decode script --- packages/fuel-gauge/src/experimental-script.test.ts | 1 + .../test/fixtures/forc-projects-experimental/script/src/main.sw | 2 ++ 2 files changed, 3 insertions(+) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 4c35decd0e7..17bee79a21c 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -130,6 +130,7 @@ describe('Experimental Logging', () => { bytes: Uint8Array.from([40, 41, 42]), tuple: [255, 65535, 4294967295, 'fuel'], vec_u8: [40, 41, 42], + vec_to_slice: [40, 41, 42], }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw index d036b7f1ff7..812160e3f57 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw @@ -34,6 +34,7 @@ struct MixedStruct { bytes: Bytes, tuple: (u8, u16, u32, str[4]), vec_u8: Vec, + vec_to_slice: raw_slice, } fn main(param_one: B512) -> MixedStruct { @@ -66,6 +67,7 @@ fn main(param_one: B512) -> MixedStruct { bytes: my_bytes, tuple: (255, 65535, 4294967295, __to_str_array("fuel")), vec_u8: my_vec_u8, + vec_to_slice: my_vec_u8.as_raw_slice(), }; my_struct From 6715cfd35af9a6e1e06550807f01cdf704ef0bb2 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 16:05:06 +0000 Subject: [PATCH 25/70] feat: validate all script decode types --- .../src/experimental-script.test.ts | 98 ++++--------------- .../forc-projects-experimental/Forc.toml | 2 +- .../{script => script-print}/Forc.toml | 2 +- .../{script => script-print}/src/main.sw | 42 +++++++- 4 files changed, 63 insertions(+), 81 deletions(-) rename packages/fuel-gauge/test/fixtures/forc-projects-experimental/{script => script-print}/Forc.toml (83%) rename packages/fuel-gauge/test/fixtures/forc-projects-experimental/{script => script-print}/src/main.sw (58%) diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 17bee79a21c..53d2ff8e242 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -17,7 +17,7 @@ const B512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; beforeAll(async () => { - const projectName = 'script'; + const projectName = 'script-print'; const path = join( __dirname, `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` @@ -36,81 +36,7 @@ beforeAll(async () => { * @group node */ describe('Experimental Logging', () => { - it.skip('prints u8', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual(U8_MAX); - }); - - it.skip('prints u16', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual(U16_MAX); - }); - - it.skip('prints u32', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual(U32_MAX); - }); - - it.skip('prints u64', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual(U64_MAX); - }); - - it.skip('prints u256', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual(U256_MAX); - }); - - it.skip('prints b256', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual( - '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6' - ); - }); - - it.skip('echos b512', async () => { - const { value } = await script.functions.main(B512).call(); - expect(value).toStrictEqual(B512); - }); - - it.skip('prints string', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual('fuel'); - }); - - it.skip('prints std string', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual('Hello World'); - }); - - /** - * Currently throws, need to confirm with Daniel - */ - it.skip('logs str slice', async () => { - const expected = 'fuel'; - - const { value } = await script.functions.main(expected).call(); - expect(value).toStrictEqual(expected); - }); - - it.skip('prints struct enum u8 vec', async () => { - const { value } = await script.functions.main().call(); - expect(value).toStrictEqual({ - x: 5, - y: 128, - state: 'Pending', - grades: [1, 4, 6, 22], - tag: 'fuel', - }); - }); - - /** - * decode: u8, u16, u32, u64, u256, b256, b512, string, stdString, struct, enum, array, option, bytes, tuple, vec - * encode: bytes - * - * todo: slice, deep - */ - it('prints mixed struct', async () => { + it('prints mixed struct with all types', async () => { const { value } = await script.functions.main(B512).call(); expect(value).toStrictEqual({ a: 5, @@ -130,7 +56,25 @@ describe('Experimental Logging', () => { bytes: Uint8Array.from([40, 41, 42]), tuple: [255, 65535, 4294967295, 'fuel'], vec_u8: [40, 41, 42], - vec_to_slice: [40, 41, 42], + deep: { + a: 5, + b: 65535, + c: 4294967295, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [255, 65535, 4294967295, 'fuel'], + vec_u8: [40, 41, 42], + }, }); }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index c168417ed21..c0d5f2bd8b2 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["logging", "script"] +members = ["logging", "script-print"] [patch.'https://github.com/fuellabs/sway'] std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-predicate-entry" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/Forc.toml similarity index 83% rename from packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml rename to packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/Forc.toml index 937fd72852b..647a1bd0549 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/Forc.toml @@ -2,6 +2,6 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "script" +name = "script-print" [dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw similarity index 58% rename from packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw rename to packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw index 812160e3f57..67f47be397d 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw @@ -16,6 +16,26 @@ enum MixedEnum { Data: u16, } +struct DeeperStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, +} + struct MixedStruct { a: u8, b: u16, @@ -34,7 +54,7 @@ struct MixedStruct { bytes: Bytes, tuple: (u8, u16, u32, str[4]), vec_u8: Vec, - vec_to_slice: raw_slice, + deep: DeeperStruct, } fn main(param_one: B512) -> MixedStruct { @@ -67,7 +87,25 @@ fn main(param_one: B512) -> MixedStruct { bytes: my_bytes, tuple: (255, 65535, 4294967295, __to_str_array("fuel")), vec_u8: my_vec_u8, - vec_to_slice: my_vec_u8.as_raw_slice(), + deep: DeeperStruct { + a: 5, + b: 65535, + c: 4294967295, + d: 18446744073709551615, + e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256, + f: 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6, + g: param_one, + native: NativeEnum::Pending, + mixed: MixedEnum::Value(true), + grades: [1, 4, 6, 22], + fuel: __to_str_array("fuel"), + hello: String::from_ascii_str("Hello World"), + opt: Option::Some(42), + nada: Option::None, + bytes: my_bytes, + tuple: (255, 65535, 4294967295, __to_str_array("fuel")), + vec_u8: my_vec_u8, + }, }; my_struct From 547c9a4dd31b9f7898de9e87bc95c030d2603567 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 16:07:08 +0000 Subject: [PATCH 26/70] chore: re enable forc format --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 1eb533b6f93..fd4922fbde4 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -44,8 +44,8 @@ jobs: run: pnpm lint:package-jsons # Temp disable - # - name: Forc Format Check - # run: pnpm forc:check + - name: Forc Format Check + run: pnpm forc:check # linting of some tests depends on pretest being run so that it generates the necessary files - name: Pretest From c50fbbb2e869ddcd341160b7ad2fa8748c53ab3b Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 17:51:48 +0000 Subject: [PATCH 27/70] feat: encode number struct script --- packages/abi-coder/src/FunctionFragment.ts | 2 +- .../src/experimental-script.test.ts | 55 +++++++++++-------- .../forc-projects-experimental/Forc.toml | 2 +- .../script-echo/Forc.toml | 7 +++ .../script-echo/src/main.sw | 29 ++++++++++ .../script-print/src/main.sw | 4 +- 6 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index 4720ac99c2f..bdce54eeb9e 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -113,6 +113,7 @@ export class FunctionFragment< const coders = nonEmptyInputs.map((t) => AbiCoder.getCoder(this.jsonAbi, t, { isRightPadded: nonEmptyInputs.length > 1, + encoding: this.jsonAbi.encoding, }) ); @@ -206,7 +207,6 @@ export class FunctionFragment< } const bytes = arrayify(data); - console.log('here'); const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output, { encoding: this.jsonAbi.encoding, }); diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index 53d2ff8e242..f6aae00412f 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -1,13 +1,11 @@ import { readFileSync } from 'fs'; -import type { BN } from 'fuels'; import { Script, bn } from 'fuels'; import { join } from 'path'; import { createWallet } from './utils'; -let script: Script; -let gasPrice: BN; - +let echoScript: Script; +let printScript: Script; const U8_MAX = 255; const U16_MAX = 65535; const U32_MAX = 4294967295; @@ -16,32 +14,32 @@ const U256_MAX = bn(2).pow(256).sub(1); const B512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; -beforeAll(async () => { - const projectName = 'script-print'; +const getScript = async (name: string) => { + const wallet = await createWallet(); const path = join( __dirname, - `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` + `../test/fixtures/forc-projects-experimental/${name}/out/release/${name}` ); const bytes = readFileSync(`${path}.bin`); const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); + return new Script(bytes, abi, wallet); +}; - const wallet = await createWallet(); - - script = new Script(bytes, abi, wallet); - - ({ minGasPrice: gasPrice } = wallet.provider.getGasConfig()); +beforeAll(async () => { + printScript = await getScript('script-print'); + echoScript = await getScript('script-echo'); }); /** * @group node */ -describe('Experimental Logging', () => { +describe('Experimental Script', () => { it('prints mixed struct with all types', async () => { - const { value } = await script.functions.main(B512).call(); + const { value } = await printScript.functions.main(B512).call(); expect(value).toStrictEqual({ - a: 5, - b: 65535, - c: 4294967295, + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, d: U64_MAX, e: U256_MAX, f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', @@ -54,12 +52,12 @@ describe('Experimental Logging', () => { opt: 42, nada: undefined, bytes: Uint8Array.from([40, 41, 42]), - tuple: [255, 65535, 4294967295, 'fuel'], + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], vec_u8: [40, 41, 42], deep: { - a: 5, - b: 65535, - c: 4294967295, + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, d: U64_MAX, e: U256_MAX, f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', @@ -72,9 +70,22 @@ describe('Experimental Logging', () => { opt: 42, nada: undefined, bytes: Uint8Array.from([40, 41, 42]), - tuple: [255, 65535, 4294967295, 'fuel'], + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], vec_u8: [40, 41, 42], }, }); }); + + it('echos number struct', async () => { + const struct = { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + }; + + const { value } = await echoScript.functions.main(struct).call(); + expect(value).toStrictEqual(struct); + }); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index c0d5f2bd8b2..731e57e4a00 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["logging", "script-print"] +members = ["logging", "script-print", "script-echo"] [patch.'https://github.com/fuellabs/sway'] std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-predicate-entry" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/Forc.toml new file mode 100644 index 00000000000..34b6fef5427 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "script-echo" + +[dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw new file mode 100644 index 00000000000..a89dde5dcd2 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw @@ -0,0 +1,29 @@ +script; + +use std::b512::B512; +use std::string::String; +use std::option::Option; +use std::bytes::Bytes; +use std::vec::Vec; + +enum NativeEnum { + Checked: (), + Pending: (), +} + +enum MixedEnum { + Value: bool, + Data: u16, +} + +struct MixedStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, +} + +fn main(param: MixedStruct) -> MixedStruct { + param +} diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw index 67f47be397d..aae7f1ba7a9 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw @@ -70,7 +70,7 @@ fn main(param_one: B512) -> MixedStruct { my_vec_u8.push(42u8); let my_struct = MixedStruct { - a: 5, + a: 255, b: 65535, c: 4294967295, d: 18446744073709551615, @@ -88,7 +88,7 @@ fn main(param_one: B512) -> MixedStruct { tuple: (255, 65535, 4294967295, __to_str_array("fuel")), vec_u8: my_vec_u8, deep: DeeperStruct { - a: 5, + a: 255, b: 65535, c: 4294967295, d: 18446744073709551615, From 9195757108ba888d76ecf60eae5ed012f8297361 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 14 Mar 2024 18:34:36 +0000 Subject: [PATCH 28/70] feat: support script encode --- .../src/encoding/coders/v0/ByteCoder.test.ts | 9 ----- .../src/encoding/coders/v0/ByteCoder.ts | 11 +++---- .../src/encoding/coders/v1/ByteCoder.ts | 10 ++---- .../src/encoding/coders/v1/StructCoder.ts | 3 +- .../src/experimental-script.test.ts | 31 +++++++++++++++++ .../script-echo/src/main.sw | 33 +++++++++++++++++++ 6 files changed, 73 insertions(+), 24 deletions(-) diff --git a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts index 3e9b9fb1760..f87f8b7e0e6 100644 --- a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts +++ b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts @@ -38,15 +38,6 @@ describe('ByteCoder', () => { expect(actual).toStrictEqual(expected); }); - it('should throw when value to encode is not array', async () => { - const coder = new ByteCoder(); - const nonArrayInput = { ...[1] }; - await expectToThrowFuelError( - () => coder.encode(nonArrayInput), - new FuelError(ErrorCode.ENCODE_ERROR, 'Expected array value.') - ); - }); - it('should decode a byte', () => { const coder = new ByteCoder(); const input = new Uint8Array([ diff --git a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts index 32f2d891271..9efcbb7553d 100644 --- a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts @@ -15,11 +15,7 @@ export class ByteCoder extends Coder { super('struct', 'struct Bytes', BASE_VECTOR_OFFSET); } - encode(value: number[]): Uint8Array { - if (!Array.isArray(value)) { - throw new FuelError(ErrorCode.ENCODE_ERROR, `Expected array value.`); - } - + encode(value: number[] | Uint8Array): Uint8Array { const parts: Uint8Array[] = []; // pointer (ptr) @@ -42,8 +38,9 @@ export class ByteCoder extends Coder { return concatWithDynamicData(parts); } - #getPaddedData(value: number[]): Uint8Array { - const data: Uint8Array[] = [Uint8Array.from(value)]; + #getPaddedData(value: number[] | Uint8Array): Uint8Array { + const bytes = value instanceof Uint8Array ? value : new Uint8Array(value); + const data: Uint8Array[] = [Uint8Array.from(bytes)]; const paddingLength = (WORD_SIZE - (value.length % WORD_SIZE)) % WORD_SIZE; if (paddingLength) { diff --git a/packages/abi-coder/src/encoding/coders/v1/ByteCoder.ts b/packages/abi-coder/src/encoding/coders/v1/ByteCoder.ts index 1996a395458..285ec8dd986 100644 --- a/packages/abi-coder/src/encoding/coders/v1/ByteCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/ByteCoder.ts @@ -11,13 +11,9 @@ export class ByteCoder extends Coder { super('struct', 'struct Bytes', WORD_SIZE); } - encode(value: number[]): Uint8Array { - if (!Array.isArray(value)) { - throw new FuelError(ErrorCode.ENCODE_ERROR, `Expected array value.`); - } - - const bytes = new Uint8Array(value); - const lengthBytes = new BigNumberCoder('u64').encode(value.length); + encode(value: number[] | Uint8Array): Uint8Array { + const bytes = value instanceof Uint8Array ? value : new Uint8Array(value); + const lengthBytes = new BigNumberCoder('u64').encode(bytes.length); return new Uint8Array([...lengthBytes, ...bytes]); } diff --git a/packages/abi-coder/src/encoding/coders/v1/StructCoder.ts b/packages/abi-coder/src/encoding/coders/v1/StructCoder.ts index f9337e3a8bb..55f96895b28 100644 --- a/packages/abi-coder/src/encoding/coders/v1/StructCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/StructCoder.ts @@ -3,7 +3,8 @@ import { concatBytes } from '@fuel-ts/utils'; import type { TypesOfCoder } from '../AbstractCoder'; import { Coder } from '../AbstractCoder'; -import { OptionCoder } from '../v0/OptionCoder'; + +import { OptionCoder } from './OptionCoder'; type InputValueOf> = { [P in keyof TCoders]: TypesOfCoder['Input']; diff --git a/packages/fuel-gauge/src/experimental-script.test.ts b/packages/fuel-gauge/src/experimental-script.test.ts index f6aae00412f..bd0534ed9b6 100644 --- a/packages/fuel-gauge/src/experimental-script.test.ts +++ b/packages/fuel-gauge/src/experimental-script.test.ts @@ -83,6 +83,37 @@ describe('Experimental Script', () => { c: U32_MAX, d: U64_MAX, e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + deep: { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + }, }; const { value } = await echoScript.functions.main(struct).call(); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw index a89dde5dcd2..7a274d55355 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-echo/src/main.sw @@ -16,12 +16,45 @@ enum MixedEnum { Data: u16, } +struct DeeperStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, +} + struct MixedStruct { a: u8, b: u16, c: u32, d: u64, e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, + deep: DeeperStruct, } fn main(param: MixedStruct) -> MixedStruct { From 4d621e272c436aa44b614272b699134b8fc00d74 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Fri, 15 Mar 2024 10:43:42 +0000 Subject: [PATCH 29/70] feat: predicate u8 echo --- .../src/experimental-predicate.test.ts | 42 +++++++++++++++++++ .../forc-projects-experimental/Forc.toml | 2 +- .../predicate-echo/Forc.toml | 7 ++++ .../predicate-echo/src/main.sw | 5 +++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/fuel-gauge/src/experimental-predicate.test.ts create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts new file mode 100644 index 00000000000..80cc774bb45 --- /dev/null +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -0,0 +1,42 @@ +import { readFileSync } from 'fs'; +import type { WalletLocked, WalletUnlocked } from 'fuels'; +import { BaseAssetId, Predicate, bn } from 'fuels'; +import { join } from 'path'; + +import { fundPredicate, setupWallets } from './predicate/utils/predicate'; + +let wallet: WalletUnlocked; +let receiver: WalletLocked; +let predicate: Predicate<[number]>; +const U8_MAX = 255; +const U16_MAX = 65535; +const U32_MAX = 4294967295; +const U64_MAX = bn(2).pow(64).sub(1); +const U256_MAX = bn(2).pow(256).sub(1); +const B512 = + '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; + +beforeAll(async () => { + [wallet, receiver] = await setupWallets(); + const name = 'predicate-echo'; + const path = join( + __dirname, + `../test/fixtures/forc-projects-experimental/${name}/out/release/${name}` + ); + const bytes = readFileSync(`${path}.bin`); + const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); + + predicate = new Predicate<[number]>(bytes, wallet.provider, abi); + await fundPredicate(wallet, predicate, 100_000); +}); + +/** + * @group node + */ +describe('Experimental Predicate', () => { + it('echos u8', async () => { + predicate.setData(U8_MAX); + const tx = await predicate.transfer(receiver.address, 100, BaseAssetId); + await tx.waitForResult(); + }); +}); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 731e57e4a00..28cfce79f82 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["logging", "script-print", "script-echo"] +members = ["predicate-echo"] [patch.'https://github.com/fuellabs/sway'] std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-predicate-entry" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml new file mode 100644 index 00000000000..b489516d114 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Daniel Bate"] +entry = "main.sw" +license = "Apache-2.0" +name = "predicate-echo" + +[dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw new file mode 100644 index 00000000000..91880af1617 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw @@ -0,0 +1,5 @@ +predicate; + +fn main(x: u8) -> bool { + x == 255 +} From 1734e31b33f27bd5f1685ad4c06cab0020491424 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 18 Mar 2024 11:48:50 +0000 Subject: [PATCH 30/70] feat: support predicate all types --- .../src/experimental-predicate.test.ts | 45 ++++++++++++- .../predicate-echo/src/main.sw | 64 ++++++++++++++++++- 2 files changed, 104 insertions(+), 5 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts index 80cc774bb45..3a1e146413f 100644 --- a/packages/fuel-gauge/src/experimental-predicate.test.ts +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -26,7 +26,7 @@ beforeAll(async () => { const bytes = readFileSync(`${path}.bin`); const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); - predicate = new Predicate<[number]>(bytes, wallet.provider, abi); + predicate = new Predicate(bytes, wallet.provider, abi); await fundPredicate(wallet, predicate, 100_000); }); @@ -34,8 +34,47 @@ beforeAll(async () => { * @group node */ describe('Experimental Predicate', () => { - it('echos u8', async () => { - predicate.setData(U8_MAX); + it('echos struct', async () => { + const struct = { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + deep: { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + }, + }; + + predicate.setData(struct); const tx = await predicate.transfer(receiver.address, 100, BaseAssetId); await tx.waitForResult(); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw index 91880af1617..2e13f3d402f 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/src/main.sw @@ -1,5 +1,65 @@ predicate; -fn main(x: u8) -> bool { - x == 255 +use std::b512::B512; +use std::string::String; +use std::option::Option; +use std::bytes::Bytes; +use std::vec::Vec; + +enum NativeEnum { + Checked: (), + Pending: (), +} + +enum MixedEnum { + Value: bool, + Data: u16, +} + +struct DeeperStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, +} + +struct MixedStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, + deep: DeeperStruct, +} + +fn main(param: MixedStruct) -> bool { + if (param.deep.d == 18446744073709551615) { + return true; + } + return false; } From c92f6d8a3e693af2aa1ba95dad4c45e68a75ba10 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 18 Mar 2024 16:41:15 +0000 Subject: [PATCH 31/70] chore: use other sway branch --- packages/forc/VERSION | 2 +- packages/fuel-gauge/src/experimental-contract.test.ts | 0 .../test/fixtures/forc-projects-experimental/Forc.toml | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 packages/fuel-gauge/src/experimental-contract.test.ts diff --git a/packages/forc/VERSION b/packages/forc/VERSION index 4208c92bc74..f8e8ae130bc 100644 --- a/packages/forc/VERSION +++ b/packages/forc/VERSION @@ -1 +1 @@ -git:xunilrj/fix-predicate-entry \ No newline at end of file +git:xunilrj/remove-method-name-log \ No newline at end of file diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 28cfce79f82..cba917e6af6 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["predicate-echo"] +members = ["predicate-echo", "script-echo", "script-print", "logging"] [patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/fix-predicate-entry" } +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } From 816684e40a1d6a927c4d90f77e787018bbf03d98 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 20 Mar 2024 13:31:55 +0000 Subject: [PATCH 32/70] chore: fix author for predicate echo toml --- .../forc-projects-experimental/predicate-echo/Forc.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml index b489516d114..a17df04822d 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/predicate-echo/Forc.toml @@ -1,5 +1,5 @@ [project] -authors = ["Daniel Bate"] +authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" name = "predicate-echo" From 13c38bfb2ee6010474eb700328ffac38ec36b79d Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 09:49:51 +0000 Subject: [PATCH 33/70] feat: remove size validation from option coder --- packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts index f333d2dea02..8228521983c 100644 --- a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts @@ -1,5 +1,3 @@ -import { ErrorCode, FuelError } from '@fuel-ts/errors'; - import type { Coder } from '../AbstractCoder'; import type { InputValueOf, DecodedValueOf } from './EnumCoder'; @@ -23,10 +21,6 @@ export class OptionCoder> extends EnumCode } decode(data: Uint8Array, offset: number): [DecodedValueOf, number] { - if (data.length < this.encodedLength) { - throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid option data size.`); - } - const [decoded, newOffset] = super.decode(data, offset); return [this.toOption(decoded) as DecodedValueOf, newOffset]; } From 78e898af8f96b1f1d47c2be416a64ea9d30733f3 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 09:50:20 +0000 Subject: [PATCH 34/70] feat: add v1 encoding for configurables --- packages/abi-coder/src/Interface.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index 67b6cdbd2e7..def710dc10d 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -123,6 +123,7 @@ export class Interface { return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, { isRightPadded: true, + encoding: this.jsonAbi.encoding, }); } From d1c3551721dec705d99744cc07e70e1fd631e38a Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 13:27:45 +0000 Subject: [PATCH 35/70] feat: support v1 encoding in contracts for all types --- packages/abi-coder/src/FunctionFragment.ts | 21 +- packages/abi-coder/src/index.ts | 2 + .../src/experimental-contract.test.ts | 78 ++++++++ .../src/experimental-logging.test.ts | 180 ++++------------- .../forc-projects-experimental/Forc.toml | 2 +- .../contract-echo/Forc.toml | 7 + .../contract-echo/src/main.sw | 68 +++++++ packages/program/src/contract-call-script.ts | 184 +++++++++++++----- .../src/functions/base-invocation-scope.ts | 2 + packages/program/src/types.ts | 2 + 10 files changed, 347 insertions(+), 199 deletions(-) create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/Forc.toml create mode 100644 packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index bdce54eeb9e..175eafcc875 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -11,13 +11,15 @@ import type { DecodedValue, InputValue } from './encoding/coders/AbstractCoder'; import { ByteCoder } from './encoding/coders/v0/ByteCoder'; import { TupleCoder } from './encoding/coders/v0/TupleCoder'; import { VecCoder } from './encoding/coders/v0/VecCoder'; +import { StdStringCoder } from './encoding/coders/v1/StdStringCoder'; +import { TupleCoder as TupleCoderV1 } from './encoding/coders/v1/TupleCoder'; import type { JsonAbi, JsonAbiArgument, JsonAbiFunction, JsonAbiFunctionAttribute, } from './types/JsonAbi'; -import { OPTION_CODER_TYPE } from './utils/constants'; +import { ENCODING_V1, OPTION_CODER_TYPE } from './utils/constants'; import type { Uint8ArrayWithDynamicData } from './utils/utilities'; import { isPointerType, unpackDynamicData, findOrThrow, isHeapType } from './utils/utilities'; @@ -27,6 +29,8 @@ export class FunctionFragment< > { readonly signature: string; readonly selector: string; + readonly selectorBytes: Uint8Array; + readonly encoding?: string; readonly name: string; readonly jsonFn: JsonAbiFunction; readonly attributes: readonly JsonAbiFunctionAttribute[]; @@ -44,6 +48,8 @@ export class FunctionFragment< this.name = name; this.signature = FunctionFragment.getSignature(this.jsonAbi, this.jsonFn); this.selector = FunctionFragment.getFunctionSelector(this.signature); + this.selectorBytes = new StdStringCoder().encode(name); + this.encoding = this.jsonAbi.encoding; this.isInputDataPointer = this.#isInputDataPointer(); this.outputMetadata = { isHeapType: this.#isOutputDataHeap(), @@ -113,13 +119,14 @@ export class FunctionFragment< const coders = nonEmptyInputs.map((t) => AbiCoder.getCoder(this.jsonAbi, t, { isRightPadded: nonEmptyInputs.length > 1, - encoding: this.jsonAbi.encoding, + encoding: this.encoding, }) ); - const coder = new TupleCoder(coders); - const results: Uint8ArrayWithDynamicData = coder.encode(shallowCopyValues); - + if (this.encoding === ENCODING_V1) { + return new TupleCoderV1(coders).encode(shallowCopyValues); + } + const results: Uint8ArrayWithDynamicData = new TupleCoder(coders).encode(shallowCopyValues); return unpackDynamicData(results, offset, results.byteLength); } @@ -183,7 +190,7 @@ export class FunctionFragment< const result = nonEmptyInputs.reduce( (obj: { decoded: unknown[]; offset: number }, input) => { - const coder = AbiCoder.getCoder(this.jsonAbi, input); + const coder = AbiCoder.getCoder(this.jsonAbi, input, { encoding: this.encoding }); const [decodedValue, decodedValueByteSize] = coder.decode(bytes, obj.offset); return { @@ -208,7 +215,7 @@ export class FunctionFragment< const bytes = arrayify(data); const coder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output, { - encoding: this.jsonAbi.encoding, + encoding: this.encoding, }); return coder.decode(bytes, 0) as [DecodedValue | undefined, number]; diff --git a/packages/abi-coder/src/index.ts b/packages/abi-coder/src/index.ts index 21707ad02e1..3085bd112ca 100644 --- a/packages/abi-coder/src/index.ts +++ b/packages/abi-coder/src/index.ts @@ -23,5 +23,7 @@ export { ASSET_ID_LEN, CONTRACT_ID_LEN, calculateVmTxMemory, + ENCODING_V0, + ENCODING_V1, } from './utils/constants'; export { BigNumberCoder } from './encoding/coders/v0/BigNumberCoder'; diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index e69de29bb2d..1758f453dc1 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -0,0 +1,78 @@ +import { randomBytes } from 'crypto'; +import { readFileSync } from 'fs'; +import type { BN, Contract } from 'fuels'; +import { bn, hexlify } from 'fuels'; +import { join } from 'path'; + +import { setup } from './utils'; + +let contractInstance: Contract; +let gasPrice: BN; + +const U8_MAX = 2 ** 8 - 1; +const U16_MAX = 2 ** 16 - 1; +const U32_MAX = 2 ** 32 - 1; +const U64_MAX = bn(2).pow(64).sub(1); +const U256_MAX = bn(2).pow(256).sub(1); +const B512 = + '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; + +beforeAll(async () => { + const projectName = 'contract-echo'; + const path = join( + __dirname, + `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` + ); + const contractBytecode = readFileSync(`${path}.bin`); + const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); + + contractInstance = await setup({ contractBytecode, abi }); + + ({ minGasPrice: gasPrice } = contractInstance.provider.getGasConfig()); +}); + +describe('Experimental Contract', () => { + it('echos mixed struct with all types', async () => { + const struct = { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + deep: { + a: U8_MAX, + b: U16_MAX, + c: U32_MAX, + d: U64_MAX, + e: U256_MAX, + f: '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6', + g: B512, + native: 'Pending', + mixed: { Value: true }, + grades: [1, 4, 6, 22], + fuel: 'fuel', + hello: 'Hello World', + opt: 42, + nada: undefined, + bytes: Uint8Array.from([40, 41, 42]), + tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], + vec_u8: [40, 41, 42], + }, + }; + + const { value } = await contractInstance.functions.echo_struct(struct).call(); + expect(value).toStrictEqual(struct); + }); +}); diff --git a/packages/fuel-gauge/src/experimental-logging.test.ts b/packages/fuel-gauge/src/experimental-logging.test.ts index 45f8d3c6077..a5f0dd72003 100644 --- a/packages/fuel-gauge/src/experimental-logging.test.ts +++ b/packages/fuel-gauge/src/experimental-logging.test.ts @@ -37,10 +37,7 @@ describe('Experimental Logging', () => { it('logs u8', async () => { const expected = U8_MAX; - const { logs } = await contractInstance.functions - .log_u8(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u8(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -48,10 +45,7 @@ describe('Experimental Logging', () => { it('logs u16', async () => { const expected = U16_MAX; - const { logs } = await contractInstance.functions - .log_u16(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u16(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -59,10 +53,7 @@ describe('Experimental Logging', () => { it('logs u32', async () => { const expected = U32_MAX; - const { logs } = await contractInstance.functions - .log_u32(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u32(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -70,10 +61,7 @@ describe('Experimental Logging', () => { it('logs u8 u16 u32 multiple params', async () => { const expected = [U8_MAX, U16_MAX, U32_MAX]; - const { value, logs } = await contractInstance.functions - .log_u8_u16_u32(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { value, logs } = await contractInstance.functions.log_u8_u16_u32(...expected).call(); expect(value).toEqual(expected); expect(logs).toEqual(expected); @@ -82,10 +70,7 @@ describe('Experimental Logging', () => { it('logs u64', async () => { const expected = U32_MAX + 1; - const { logs } = await contractInstance.functions - .log_u64(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u64(expected).call(); expect(bn(logs[0]).toNumber()).toStrictEqual(expected); }); @@ -101,10 +86,7 @@ describe('Experimental Logging', () => { it('logs u64 u8 multiple params', async () => { const expected = [U32_MAX + 1, U8_MAX]; - const { logs } = await contractInstance.functions - .log_u64_u8(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u64_u8(...expected).call(); expect(bn(logs[0]).toNumber()).toStrictEqual(expected[0]); expect(logs[1]).toEqual(expected[1]); @@ -113,10 +95,7 @@ describe('Experimental Logging', () => { it('logs boolean', async () => { const expected = true; - const { logs } = await contractInstance.functions - .log_boolean(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_boolean(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -124,10 +103,7 @@ describe('Experimental Logging', () => { it('logs boolean boolean multiple params', async () => { const expected = [true, false]; - const { logs } = await contractInstance.functions - .log_boolean_boolean(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_boolean_boolean(...expected).call(); expect(logs).toEqual(expected); }); @@ -135,10 +111,7 @@ describe('Experimental Logging', () => { it('logs number boolean mixed params', async () => { const expected = [U32_MAX, true]; - const { logs } = await contractInstance.functions - .log_number_boolean(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_number_boolean(...expected).call(); expect(logs).toEqual(expected); }); @@ -146,10 +119,7 @@ describe('Experimental Logging', () => { it('logs b256', async () => { const expected = B256; - const { logs } = await contractInstance.functions - .log_b256(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_b256(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -157,10 +127,7 @@ describe('Experimental Logging', () => { it('logs b512', async () => { const expected = B512; - const { logs } = await contractInstance.functions - .log_b512(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_b512(expected).call(); expect(logs).toStrictEqual([expected]); }); @@ -168,10 +135,7 @@ describe('Experimental Logging', () => { it('logs b256 b512 multiple params', async () => { const expected = [B256, B512]; - const { logs } = await contractInstance.functions - .log_b256_b512(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_b256_b512(...expected).call(); expect(logs).toEqual(expected); }); @@ -179,21 +143,7 @@ describe('Experimental Logging', () => { it('logs u8 vec', async () => { const expected = [U8_MAX, 1, U8_MAX, 5]; - const { logs } = await contractInstance.functions - .log_vec_u8(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); - - expect(logs).toEqual([expected]); - }); - - it('logs b256 vec', async () => { - const expected = [hexlify(randomBytes(32)), hexlify(randomBytes(32))]; - - const { logs } = await contractInstance.functions - .log_vec_b256(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_vec_u8(expected).call(); expect(logs).toEqual([expected]); }); @@ -201,20 +151,17 @@ describe('Experimental Logging', () => { it('logs bytes', async () => { const expected = [40, 41, 42]; - const { logs } = await contractInstance.functions - .log_bytes(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_bytes(expected).call(); expect(logs).toEqual([Uint8Array.from(expected)]); }); it('logs StdString', async () => { - const expected = 'hello world'; + const expected = 'fuel'; const { logs } = await contractInstance.functions .log_std_string(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) + .txParams({ gasLimit: 1_000_000, gasPrice }) .call(); expect(logs).toEqual([expected]); @@ -223,10 +170,7 @@ describe('Experimental Logging', () => { it('logs u16 bytes multiple params', async () => { const expected = [U16_MAX, [40, 41, 42]]; - const { logs } = await contractInstance.functions - .log_u16_bytes(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u16_bytes(...expected).call(); expect(logs).toEqual([U16_MAX, Uint8Array.from([40, 41, 42])]); }); @@ -234,10 +178,7 @@ describe('Experimental Logging', () => { it('logs u8 array', async () => { const expected = [U8_MAX, 5, U8_MAX]; - const { logs } = await contractInstance.functions - .log_u8_array(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u8_array(expected).call(); expect(logs).toEqual([expected]); }); @@ -245,10 +186,7 @@ describe('Experimental Logging', () => { it('logs u16 array', async () => { const expected = [U16_MAX, 5, U16_MAX]; - const { logs } = await contractInstance.functions - .log_u16_array(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u16_array(expected).call(); expect(logs).toEqual([expected]); }); @@ -256,10 +194,7 @@ describe('Experimental Logging', () => { it('log u16 u8 array multiple params', async () => { const expected = [U16_MAX, [U8_MAX, 5, U8_MAX]]; - const { logs } = await contractInstance.functions - .log_u16_u8_array(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u16_u8_array(...expected).call(); expect(logs).toEqual(expected); }); @@ -267,10 +202,7 @@ describe('Experimental Logging', () => { it('logs string', async () => { const expected = 'fuel'; - const { logs } = await contractInstance.functions - .log_str_4(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_str_4(expected).call(); expect(logs).toEqual([expected]); }); @@ -278,10 +210,7 @@ describe('Experimental Logging', () => { it('logs u8 string multiple params', async () => { const expected = [U8_MAX, 'at']; - const { logs } = await contractInstance.functions - .log_u8_str_2(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u8_str_2(...expected).call(); expect(logs).toEqual(expected); }); @@ -289,10 +218,7 @@ describe('Experimental Logging', () => { it('logs u8 u16 tuple', async () => { const expected = [U8_MAX, U16_MAX]; - const { logs } = await contractInstance.functions - .log_u8_u16_tuple(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_u8_u16_tuple(expected).call(); expect(logs).toEqual([expected]); }); @@ -301,15 +227,9 @@ describe('Experimental Logging', () => { const expectedFoo = { Foo: U32_MAX }; const expectedBar = { Bar: true }; - const { logs: logsFoo } = await contractInstance.functions - .log_enum(expectedFoo) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsFoo } = await contractInstance.functions.log_enum(expectedFoo).call(); - const { logs: logsBar } = await contractInstance.functions - .log_enum(expectedBar) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsBar } = await contractInstance.functions.log_enum(expectedBar).call(); expect(logsFoo).toEqual([expectedFoo]); expect(logsBar).toEqual([expectedBar]); @@ -319,15 +239,9 @@ describe('Experimental Logging', () => { const expectedFoo = 'Foo'; const expectedBar = 'Bar'; - const { logs: logsFoo } = await contractInstance.functions - .log_native_enum(expectedFoo) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsFoo } = await contractInstance.functions.log_native_enum(expectedFoo).call(); - const { logs: logsBar } = await contractInstance.functions - .log_native_enum(expectedBar) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsBar } = await contractInstance.functions.log_native_enum(expectedBar).call(); expect(logsFoo).toEqual([expectedFoo]); expect(logsBar).toEqual([expectedBar]); @@ -336,10 +250,7 @@ describe('Experimental Logging', () => { it('logs boolean enum multiple params', async () => { const expected = [true, { Foo: U32_MAX }]; - const { logs } = await contractInstance.functions - .log_boolean_enum(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_boolean_enum(...expected).call(); expect(logs).toEqual(expected); }); @@ -350,10 +261,7 @@ describe('Experimental Logging', () => { b: U16_MAX, }; - const { logs } = await contractInstance.functions - .log_struct(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_struct(expected).call(); expect(logs).toEqual([expected]); }); @@ -370,10 +278,7 @@ describe('Experimental Logging', () => { }, ]; - const { logs } = await contractInstance.functions - .log_struct_vec(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_struct_vec(expected).call(); expect(logs).toEqual([expected]); }); @@ -387,10 +292,7 @@ describe('Experimental Logging', () => { true, ]; - const { logs } = await contractInstance.functions - .log_struct_boolean(...expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_struct_boolean(...expected).call(); expect(logs).toEqual(expected); }); @@ -399,15 +301,9 @@ describe('Experimental Logging', () => { const expectedSome = U8_MAX; const expectedNone = undefined; - const { logs: logsSome } = await contractInstance.functions - .log_option_u8(expectedSome) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsSome } = await contractInstance.functions.log_option_u8(expectedSome).call(); - const { logs: logsNone } = await contractInstance.functions - .log_option_u8(expectedNone) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs: logsNone } = await contractInstance.functions.log_option_u8(expectedNone).call(); expect(logsSome).toEqual([expectedSome]); expect(logsNone).toEqual([expectedNone]); @@ -416,10 +312,7 @@ describe('Experimental Logging', () => { it('logs raw slice', async () => { const expected = [40, 41, 42]; - const { logs } = await contractInstance.functions - .log_raw_slice(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_raw_slice(expected).call(); expect(logs).toEqual([expected]); }); @@ -428,10 +321,7 @@ describe('Experimental Logging', () => { it.skip('logs str slice', async () => { const expected = 'fuel'; - const { logs } = await contractInstance.functions - .log_str_slice(expected) - .txParams({ gasPrice, gasLimit: 10_000 }) - .call(); + const { logs } = await contractInstance.functions.log_str_slice(expected).call(); expect(logs).toEqual([expected]); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index cba917e6af6..20198ca9d6c 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,5 @@ [workspace] -members = ["predicate-echo", "script-echo", "script-print", "logging"] +members = ["predicate-echo", "script-echo", "script-print", "logging", "contract-echo"] [patch.'https://github.com/fuellabs/sway'] std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/Forc.toml new file mode 100644 index 00000000000..57b95ffcfef --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "contract-echo" + +[dependencies] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw new file mode 100644 index 00000000000..64193a611d7 --- /dev/null +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw @@ -0,0 +1,68 @@ +contract; + +use std::b512::B512; +use std::string::String; +use std::option::Option; +use std::bytes::Bytes; +use std::vec::Vec; + +enum NativeEnum { + Checked: (), + Pending: (), +} + +enum MixedEnum { + Value: bool, + Data: u16, +} + +struct DeeperStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, +} + +struct MixedStruct { + a: u8, + b: u16, + c: u32, + d: u64, + e: u256, + f: b256, + g: B512, + native: NativeEnum, + mixed: MixedEnum, + grades: [u8; 4], + fuel: str[4], + hello: String, + opt: Option, + nada: Option, + bytes: Bytes, + tuple: (u8, u16, u32, str[4]), + vec_u8: Vec, + deep: DeeperStruct, +} + +abi MyContract { + fn echo_struct(param: MixedStruct) -> MixedStruct; +} + +impl MyContract for Contract { + fn echo_struct(param: MixedStruct) -> MixedStruct { + param + } +} diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index 768be03e84a..a3c0c36e553 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -1,5 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { WORD_SIZE, B256Coder, ASSET_ID_LEN, BigNumberCoder } from '@fuel-ts/abi-coder'; +import { CONTRACT_ID_LEN } from '@fuel-ts/abi-coder'; +import { ENCODING_V1 } from '@fuel-ts/abi-coder'; import type { CallResult, TransactionResultCallReceipt, @@ -37,6 +39,11 @@ type CallOutputInfo = { encodedLength: number; }; +type ContractCallScriptFn = ( + call: ContractCall, + segmentOffset: number +) => { scriptData: Uint8Array[]; callParamOffsets: CallOpcodeParamsOffset }; + const DEFAULT_OPCODE_PARAMS: CallOpcodeParamsOffset = { assetIdOffset: 0, amountOffset: 0, @@ -204,6 +211,126 @@ const getFunctionOutputInfos = (functionScopes: InvocationScopeLike[]): CallOutp }; }); +/** + * + * @param call + * @param segmentOffset + * @returns + */ +export const getScriptDataV0 = ( + call: ContractCall, + segmentOffset: number +): { scriptData: Uint8Array[]; callParamOffsets: CallOpcodeParamsOffset } => { + const scriptData: Uint8Array[] = []; + let gasForwardedSize: number = 0; + + const callParamOffsets: CallOpcodeParamsOffset = { + amountOffset: segmentOffset, + assetIdOffset: segmentOffset + WORD_SIZE, + gasForwardedOffset: call.gas ? segmentOffset + WORD_SIZE + ASSET_ID_LEN : 0, + callDataOffset: segmentOffset + WORD_SIZE + ASSET_ID_LEN + gasForwardedSize, + }; + + /// script data, consisting of the following items in the given order: + /// 1. Amount to be forwarded `(1 * `[`WORD_SIZE`]`)` + scriptData.push(new BigNumberCoder('u64').encode(call.amount || 0)); + /// 2. Asset ID to be forwarded ([`AssetId::LEN`]) + scriptData.push(new B256Coder().encode(call.assetId?.toString() || BaseAssetId)); + /// 3. Contract ID ([`ContractId::LEN`]); + scriptData.push(call.contractId.toBytes()); + /// 4. Function selector `(1 * `[`WORD_SIZE`]`)` + scriptData.push(new BigNumberCoder('u64').encode(call.fnSelector)); + /// 5. Gas to be forwarded `(1 * `[`WORD_SIZE`]`)` + if (call.gas) { + scriptData.push(new BigNumberCoder('u64').encode(call.gas)); + + gasForwardedSize = WORD_SIZE; + } + + /// 6. Calldata offset (optional) `(1 * `[`WORD_SIZE`]`)` + // If the method call takes custom inputs or has more than + // one argument, we need to calculate the `call_data_offset`, + // which points to where the data for the custom types start in the + // transaction. If it doesn't take any custom inputs, this isn't necessary. + if (call.isInputDataPointer) { + const pointerInputOffset = segmentOffset + POINTER_DATA_OFFSET + gasForwardedSize; + scriptData.push(new BigNumberCoder('u64').encode(pointerInputOffset)); + } + + /// 7. Encoded arguments (optional) (variable length) + const args = arrayify(call.data); + scriptData.push(args); + + return { + scriptData, + callParamOffsets, + }; +}; + +/** + * + * @param call + * @param segmentOffset + * @returns + */ +export const getScriptDataV1 = ( + call: ContractCall, + segmentOffset: number +): { scriptData: Uint8Array[]; callParamOffsets: CallOpcodeParamsOffset } => { + const scriptData: Uint8Array[] = []; + const callSegmentOffset = segmentOffset + WORD_SIZE; + let gasForwardedSize: number = 0; + + // 1. Amount + scriptData.push(new BigNumberCoder('u64').encode(call.amount || 0)); + // 2. Asset ID + scriptData.push(new B256Coder().encode(call.assetId?.toString() || BaseAssetId)); + // 3. Gas to be forwarded + if (call.gas) { + scriptData.push(new BigNumberCoder('u64').encode(call.gas)); + gasForwardedSize = WORD_SIZE; + } + + const callParamOffsets: CallOpcodeParamsOffset = { + amountOffset: callSegmentOffset, + assetIdOffset: callSegmentOffset + WORD_SIZE, + gasForwardedOffset: callSegmentOffset + WORD_SIZE + ASSET_ID_LEN, + callDataOffset: callSegmentOffset + WORD_SIZE + ASSET_ID_LEN + gasForwardedSize, + }; + const encodedSelectorOffset = + callParamOffsets.callDataOffset + CONTRACT_ID_LEN + WORD_SIZE + WORD_SIZE; + const customInputOffset = encodedSelectorOffset + call.fnSelectorBytes.length; + const bytes = arrayify(call.data); + + // 4. Contract ID + scriptData.push(call.contractId.toBytes()); + // 5. Function selector offset + scriptData.push(new BigNumberCoder('u64').encode(encodedSelectorOffset)); + // 6. CallData offset + scriptData.push(new BigNumberCoder('u64').encode(customInputOffset)); + // 7. Function selector + scriptData.push(call.fnSelectorBytes); + // 8. Encoded arguments + scriptData.push(bytes); + + return { + scriptData, + callParamOffsets, + }; +}; + +/** + * + * @param encoding + * @returns + */ +export const getScriptDataForEncoding = (encoding?: string): ContractCallScriptFn => { + if (encoding === ENCODING_V1) { + return getScriptDataV1; + } + return getScriptDataV0; +}; + export const getContractCallScript = ( functionScopes: InvocationScopeLike[], maxInputs: BN @@ -216,78 +343,43 @@ export const getContractCallScript = ( ), (contractCalls): EncodedScriptCall => { const TOTAL_CALLS = contractCalls.length; + if (TOTAL_CALLS === 0) { return { data: new Uint8Array(), script: new Uint8Array() }; } - // Calculate instructions length for call instructions + // Get total data offset AFTER all scripts const callInstructionsLength = getCallInstructionsLength(contractCalls); - // pad length + // Pad length const paddingLength = (8 - (callInstructionsLength % 8)) % 8; const paddedInstructionsLength = callInstructionsLength + paddingLength; - // get total data offset AFTER all scripts + // Base offset const dataOffset = calculateScriptDataBaseOffset(maxInputs.toNumber()) + paddedInstructionsLength; // The data for each call is ordered into segments const paramOffsets: CallOpcodeParamsOffset[] = []; - let segmentOffset = dataOffset; - // the data about the contract output const outputInfos: CallOutputInfo[] = []; + let segmentOffset = dataOffset; const scriptData: Uint8Array[] = []; for (let i = 0; i < TOTAL_CALLS; i += 1) { const call = contractCalls[i]; + const { scriptData: callScriptData, callParamOffsets } = getScriptDataForEncoding( + call.encoding + )(call, segmentOffset); + // store output and param offsets for asm instructions later outputInfos.push({ isHeap: call.isOutputDataHeap, encodedLength: call.outputEncodedLength, }); - - let gasForwardedSize = 0; - - paramOffsets.push({ - amountOffset: segmentOffset, - assetIdOffset: segmentOffset + WORD_SIZE, - gasForwardedOffset: call.gas ? segmentOffset + WORD_SIZE + ASSET_ID_LEN : 0, - callDataOffset: segmentOffset + WORD_SIZE + ASSET_ID_LEN + gasForwardedSize, - }); - - /// script data, consisting of the following items in the given order: - /// 1. Amount to be forwarded `(1 * `[`WORD_SIZE`]`)` - scriptData.push(new BigNumberCoder('u64').encode(call.amount || 0)); - /// 2. Asset ID to be forwarded ([`AssetId::LEN`]) - scriptData.push(new B256Coder().encode(call.assetId?.toString() || BaseAssetId)); - /// 3. Contract ID ([`ContractId::LEN`]); - scriptData.push(call.contractId.toBytes()); - /// 4. Function selector `(1 * `[`WORD_SIZE`]`)` - scriptData.push(new BigNumberCoder('u64').encode(call.fnSelector)); - /// 5. Gas to be forwarded `(1 * `[`WORD_SIZE`]`)` - if (call.gas) { - scriptData.push(new BigNumberCoder('u64').encode(call.gas)); - - gasForwardedSize = WORD_SIZE; - } - - /// 6. Calldata offset (optional) `(1 * `[`WORD_SIZE`]`)` - // If the method call takes custom inputs or has more than - // one argument, we need to calculate the `call_data_offset`, - // which points to where the data for the custom types start in the - // transaction. If it doesn't take any custom inputs, this isn't necessary. - if (call.isInputDataPointer) { - const pointerInputOffset = segmentOffset + POINTER_DATA_OFFSET + gasForwardedSize; - scriptData.push(new BigNumberCoder('u64').encode(pointerInputOffset)); - } - - /// 7. Encoded arguments (optional) (variable length) - const args = arrayify(call.data); - scriptData.push(args); - - // move offset for next call + scriptData.push(concat(callScriptData)); + paramOffsets.push(callParamOffsets); segmentOffset = dataOffset + concat(scriptData).byteLength; } diff --git a/packages/program/src/functions/base-invocation-scope.ts b/packages/program/src/functions/base-invocation-scope.ts index d50d3f65e53..089c96215c2 100644 --- a/packages/program/src/functions/base-invocation-scope.ts +++ b/packages/program/src/functions/base-invocation-scope.ts @@ -39,6 +39,8 @@ function createContractCall(funcScope: InvocationScopeLike, offset: number): Con return { contractId: (program as AbstractContract).id, fnSelector: func.selector, + fnSelectorBytes: func.selectorBytes, + encoding: func.encoding, data, isInputDataPointer: func.isInputDataPointer, isOutputDataHeap: func.outputMetadata.isHeapType, diff --git a/packages/program/src/types.ts b/packages/program/src/types.ts index 15ad034bfa4..7ad14510cce 100644 --- a/packages/program/src/types.ts +++ b/packages/program/src/types.ts @@ -12,6 +12,8 @@ export type ContractCall = { contractId: AbstractAddress; data: BytesLike; fnSelector: string; + fnSelectorBytes: Uint8Array; + encoding?: string; isInputDataPointer: boolean; isOutputDataHeap: boolean; outputEncodedLength: number; From 39eb40fd164d641870c5e2841ebf2fe26eb0a6d6 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 15:12:41 +0000 Subject: [PATCH 36/70] chore: forc format --- .../test/fixtures/forc-projects-experimental/Forc.toml | 8 +++++++- .../forc-projects-experimental/script-print/src/main.sw | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index 20198ca9d6c..a22cfcf5baf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,11 @@ [workspace] -members = ["predicate-echo", "script-echo", "script-print", "logging", "contract-echo"] +members = [ + "predicate-echo", + "script-echo", + "script-print", + "logging", + "contract-echo", +] [patch.'https://github.com/fuellabs/sway'] std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw index aae7f1ba7a9..9b97833f77f 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/script-print/src/main.sw @@ -16,7 +16,7 @@ enum MixedEnum { Data: u16, } -struct DeeperStruct { +struct DeeperStruct { a: u8, b: u16, c: u32, From fb209ad3264531a36fdc3d4e66c17bbba27255cd Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 16:26:37 +0000 Subject: [PATCH 37/70] feat: support str slice in v1 --- .../abi-coder/src/encoding/strategies/getCoderV1.ts | 10 +++------- packages/fuel-gauge/src/experimental-contract.test.ts | 10 ++++++++-- packages/fuel-gauge/src/experimental-logging.test.ts | 3 +-- .../contract-echo/src/main.sw | 7 +++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/abi-coder/src/encoding/strategies/getCoderV1.ts b/packages/abi-coder/src/encoding/strategies/getCoderV1.ts index 690e140af79..c9163f2630f 100644 --- a/packages/abi-coder/src/encoding/strategies/getCoderV1.ts +++ b/packages/abi-coder/src/encoding/strategies/getCoderV1.ts @@ -39,6 +39,7 @@ import { NumberCoder } from '../coders/v1/NumberCoder'; import { OptionCoder } from '../coders/v1/OptionCoder'; import { RawSliceCoder } from '../coders/v1/RawSliceCoder'; import { StdStringCoder } from '../coders/v1/StdStringCoder'; +import { StrSliceCoder } from '../coders/v1/StrSliceCoder'; import { StringCoder } from '../coders/v1/StringCoder'; import { StructCoder } from '../coders/v1/StructCoder'; import { TupleCoder } from '../coders/v1/TupleCoder'; @@ -79,6 +80,8 @@ export const getCoder: GetCoderFn = ( return new ByteCoder(); case STD_STRING_CODER_TYPE: return new StdStringCoder(); + case STR_SLICE_CODER_TYPE: + return new StrSliceCoder(); default: break; } @@ -149,13 +152,6 @@ export const getCoder: GetCoderFn = ( return new TupleCoder(coders as Coder[]); } - if (resolvedAbiType.type === STR_SLICE_CODER_TYPE) { - throw new FuelError( - ErrorCode.INVALID_DATA, - 'String slices can not be decoded from logs. Convert the slice to `str[N]` with `__to_str_array`' - ); - } - throw new FuelError( ErrorCode.CODER_NOT_FOUND, `Coder not found: ${JSON.stringify(resolvedAbiType)}.` diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index 1758f453dc1..d140fad928d 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -1,7 +1,6 @@ -import { randomBytes } from 'crypto'; import { readFileSync } from 'fs'; import type { BN, Contract } from 'fuels'; -import { bn, hexlify } from 'fuels'; +import { bn } from 'fuels'; import { join } from 'path'; import { setup } from './utils'; @@ -51,6 +50,7 @@ describe('Experimental Contract', () => { bytes: Uint8Array.from([40, 41, 42]), tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], vec_u8: [40, 41, 42], + str_slice: 'fuel', deep: { a: U8_MAX, b: U16_MAX, @@ -75,4 +75,10 @@ describe('Experimental Contract', () => { const { value } = await contractInstance.functions.echo_struct(struct).call(); expect(value).toStrictEqual(struct); }); + + it('extracts str slice from revert', async () => { + await expect(contractInstance.functions.test_revert().call()).rejects.toThrow( + /The script reverted with reason RequireFailed/ + ); + }); }); diff --git a/packages/fuel-gauge/src/experimental-logging.test.ts b/packages/fuel-gauge/src/experimental-logging.test.ts index a5f0dd72003..750c945057a 100644 --- a/packages/fuel-gauge/src/experimental-logging.test.ts +++ b/packages/fuel-gauge/src/experimental-logging.test.ts @@ -317,8 +317,7 @@ describe('Experimental Logging', () => { expect(logs).toEqual([expected]); }); - // Requires v1 encoding to be supported for contract calls - it.skip('logs str slice', async () => { + it('logs str slice', async () => { const expected = 'fuel'; const { logs } = await contractInstance.functions.log_str_slice(expected).call(); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw index 64193a611d7..39d2137cef1 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw @@ -55,14 +55,21 @@ struct MixedStruct { tuple: (u8, u16, u32, str[4]), vec_u8: Vec, deep: DeeperStruct, + str_slice: str, } abi MyContract { fn echo_struct(param: MixedStruct) -> MixedStruct; + fn test_revert() -> bool; } impl MyContract for Contract { fn echo_struct(param: MixedStruct) -> MixedStruct { param } + + fn test_revert() -> bool { + require(false, "This is a revert error"); + true + } } From db59129ab6c72480f26061464dd33024b2ea3c79 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 16:42:47 +0000 Subject: [PATCH 38/70] feat: add test for nested heap --- packages/fuel-gauge/src/experimental-contract.test.ts | 4 ++++ .../test/fixtures/forc-projects-experimental/Forc.toml | 4 ---- .../forc-projects-experimental/contract-echo/src/main.sw | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index d140fad928d..bbec190ae32 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -69,6 +69,10 @@ describe('Experimental Contract', () => { bytes: Uint8Array.from([40, 41, 42]), tuple: [U8_MAX, U16_MAX, U32_MAX, 'fuel'], vec_u8: [40, 41, 42], + even_deeper: { + nested_vec: [1, 2, 3, 4, 5], + nested_str: 'fuel labs', + }, }, }; diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index a22cfcf5baf..a9593f50f2a 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,9 +1,5 @@ [workspace] members = [ - "predicate-echo", - "script-echo", - "script-print", - "logging", "contract-echo", ] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw index 39d2137cef1..8bac0478b88 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw @@ -16,6 +16,11 @@ enum MixedEnum { Data: u16, } +struct EvenDeeperStruct { + nested_vec: Vec, + nested_str: str, +} + struct DeeperStruct { a: u8, b: u16, @@ -34,6 +39,7 @@ struct DeeperStruct { bytes: Bytes, tuple: (u8, u16, u32, str[4]), vec_u8: Vec, + even_deeper: EvenDeeperStruct, } struct MixedStruct { From d16ba80053b8def286b292b3e92304efc9255508 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 16:47:29 +0000 Subject: [PATCH 39/70] chore: fix experimental forc workspace --- .../test/fixtures/forc-projects-experimental/Forc.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index a9593f50f2a..a22cfcf5baf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -1,5 +1,9 @@ [workspace] members = [ + "predicate-echo", + "script-echo", + "script-print", + "logging", "contract-echo", ] From 302ecb3fe28662a0137b912535e4a1a06667e139 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 16:50:49 +0000 Subject: [PATCH 40/70] feat: test for raw slice --- packages/fuel-gauge/src/experimental-contract.test.ts | 1 + .../forc-projects-experimental/contract-echo/src/main.sw | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index bbec190ae32..f189dd0c20e 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -72,6 +72,7 @@ describe('Experimental Contract', () => { even_deeper: { nested_vec: [1, 2, 3, 4, 5], nested_str: 'fuel labs', + nested_raw: [88, 89, 90, 100], }, }, }; diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw index 8bac0478b88..8d81efbf771 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/contract-echo/src/main.sw @@ -19,6 +19,7 @@ enum MixedEnum { struct EvenDeeperStruct { nested_vec: Vec, nested_str: str, + nested_raw: raw_slice, } struct DeeperStruct { From 348f2977df7d89cb5bfedd912312da1daa97b53e Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 17:10:35 +0000 Subject: [PATCH 41/70] chore: changeset --- .changeset/curly-feet-melt.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/curly-feet-melt.md diff --git a/.changeset/curly-feet-melt.md b/.changeset/curly-feet-melt.md new file mode 100644 index 00000000000..4afe887671a --- /dev/null +++ b/.changeset/curly-feet-melt.md @@ -0,0 +1,6 @@ +--- +"@fuel-ts/abi-coder": minor +"@fuel-ts/program": minor +--- + +feat!: support v1 encoding in program types From 351b94ee8faeaa873c0bd2c17ec6d27362a00ab6 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 17:30:44 +0000 Subject: [PATCH 42/70] chore: remove comment --- .github/workflows/test.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5f4e6c9bd36..1c695494418 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,7 +43,6 @@ jobs: - name: Verify package.json integrity run: pnpm lint:package-jsons - # Temp disable - name: Forc Format Check run: pnpm forc:check From e64be8c9d3a970450068953bb9d04a1148a14d13 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 17:47:48 +0000 Subject: [PATCH 43/70] chore: doc blocks --- packages/program/src/contract-call-script.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index a3c0c36e553..13c56a34727 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -212,10 +212,11 @@ const getFunctionOutputInfos = (functionScopes: InvocationScopeLike[]): CallOutp }); /** + * Obtains script data for a contract call according to the V0 specification. * - * @param call - * @param segmentOffset - * @returns + * @param call - the contract call to obtain for script data for. + * @param segmentOffset - the segment to generate pointers and offset data from. + * @returns the populated script data and call parameter offsets. */ export const getScriptDataV0 = ( call: ContractCall, @@ -268,10 +269,11 @@ export const getScriptDataV0 = ( }; /** + * Obtains script data for a contract call according to the V1 specification. * - * @param call - * @param segmentOffset - * @returns + * @param call - the contract call to obtain for script data for. + * @param segmentOffset - the segment to generate pointers and offset data from. + * @returns the populated script data and call parameter offsets. */ export const getScriptDataV1 = ( call: ContractCall, @@ -320,9 +322,10 @@ export const getScriptDataV1 = ( }; /** + * Retrieves a script data function for a specific encoding version. * - * @param encoding - * @returns + * @param encoding - the encoding version used for the contract call. + * @returns an appropriate script data function. */ export const getScriptDataForEncoding = (encoding?: string): ContractCallScriptFn => { if (encoding === ENCODING_V1) { From c04ac6545c0a59385d61f676e943f3f56efb64f9 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 21 Mar 2024 17:58:32 +0000 Subject: [PATCH 44/70] chore: fix changeset --- .changeset/curly-feet-melt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/curly-feet-melt.md b/.changeset/curly-feet-melt.md index 4afe887671a..44d4d075b74 100644 --- a/.changeset/curly-feet-melt.md +++ b/.changeset/curly-feet-melt.md @@ -3,4 +3,4 @@ "@fuel-ts/program": minor --- -feat!: support v1 encoding in program types +feat!: support `v1` encoding in program types From 6d7c20c9842dedbd3124859f8a10dd7c9973de14 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 15:09:04 +0000 Subject: [PATCH 45/70] feat: upgrade to forc 0.52.0 --- packages/forc/VERSION_EXPERIMENTAL | 2 +- .../src/experimental-predicate.test.ts | 42 +++++++++++-------- .../forc-projects-experimental/Forc.toml | 3 -- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/packages/forc/VERSION_EXPERIMENTAL b/packages/forc/VERSION_EXPERIMENTAL index fb6e7a44dd5..655b90034ac 100644 --- a/packages/forc/VERSION_EXPERIMENTAL +++ b/packages/forc/VERSION_EXPERIMENTAL @@ -1 +1 @@ -0.51.1 \ No newline at end of file +0.52.0 \ No newline at end of file diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts index 3a1e146413f..388f5bd817a 100644 --- a/packages/fuel-gauge/src/experimental-predicate.test.ts +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -1,5 +1,5 @@ import { readFileSync } from 'fs'; -import type { WalletLocked, WalletUnlocked } from 'fuels'; +import type { JsonAbi, Provider, WalletLocked, WalletUnlocked } from 'fuels'; import { BaseAssetId, Predicate, bn } from 'fuels'; import { join } from 'path'; @@ -7,7 +7,10 @@ import { fundPredicate, setupWallets } from './predicate/utils/predicate'; let wallet: WalletUnlocked; let receiver: WalletLocked; -let predicate: Predicate<[number]>; +let bytecode: Buffer; +let abi: JsonAbi; +let provider: Provider; + const U8_MAX = 255; const U16_MAX = 65535; const U32_MAX = 4294967295; @@ -16,24 +19,22 @@ const U256_MAX = bn(2).pow(256).sub(1); const B512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; -beforeAll(async () => { - [wallet, receiver] = await setupWallets(); - const name = 'predicate-echo'; - const path = join( - __dirname, - `../test/fixtures/forc-projects-experimental/${name}/out/release/${name}` - ); - const bytes = readFileSync(`${path}.bin`); - const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); - - predicate = new Predicate(bytes, wallet.provider, abi); - await fundPredicate(wallet, predicate, 100_000); -}); - /** * @group node */ describe('Experimental Predicate', () => { + beforeAll(async () => { + [wallet, receiver] = await setupWallets(); + const name = 'predicate-echo'; + const path = join( + __dirname, + `../test/fixtures/forc-projects-experimental/${name}/out/release/${name}` + ); + bytecode = readFileSync(`${path}.bin`); + abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); + provider = wallet.provider; + }); + it('echos struct', async () => { const struct = { a: U8_MAX, @@ -74,7 +75,14 @@ describe('Experimental Predicate', () => { }, }; - predicate.setData(struct); + const predicate = new Predicate({ + bytecode, + provider, + abi, + inputData: struct, + }); + await fundPredicate(wallet, predicate, 100_000); + const tx = await predicate.transfer(receiver.address, 100, BaseAssetId); await tx.waitForResult(); }); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index a22cfcf5baf..dc76225a689 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -6,6 +6,3 @@ members = [ "logging", "contract-echo", ] - -[patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } From 8960f54ce66f189806b33d70402898722b4f6db4 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 15:16:25 +0000 Subject: [PATCH 46/70] chore: linting --- packages/fuel-gauge/src/experimental-contract.test.ts | 5 +---- packages/fuel-gauge/src/experimental-logging.test.ts | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index f189dd0c20e..6b8641770a3 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -1,12 +1,11 @@ import { readFileSync } from 'fs'; -import type { BN, Contract } from 'fuels'; +import type { Contract } from 'fuels'; import { bn } from 'fuels'; import { join } from 'path'; import { setup } from './utils'; let contractInstance: Contract; -let gasPrice: BN; const U8_MAX = 2 ** 8 - 1; const U16_MAX = 2 ** 16 - 1; @@ -26,8 +25,6 @@ beforeAll(async () => { const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); contractInstance = await setup({ contractBytecode, abi }); - - ({ minGasPrice: gasPrice } = contractInstance.provider.getGasConfig()); }); describe('Experimental Contract', () => { diff --git a/packages/fuel-gauge/src/experimental-logging.test.ts b/packages/fuel-gauge/src/experimental-logging.test.ts index 750c945057a..368d926ac8a 100644 --- a/packages/fuel-gauge/src/experimental-logging.test.ts +++ b/packages/fuel-gauge/src/experimental-logging.test.ts @@ -1,7 +1,6 @@ -import { randomBytes } from 'crypto'; import { readFileSync } from 'fs'; import type { BN, Contract } from 'fuels'; -import { bn, hexlify } from 'fuels'; +import { bn } from 'fuels'; import { join } from 'path'; import { setup } from './utils'; From 2fdb48ddb21ee5248034d3ba268c90ec95bbb8d9 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 15:24:20 +0000 Subject: [PATCH 47/70] chore: linting --- packages/program/src/contract-call-script.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index 13c56a34727..cafed0c26e1 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -1,7 +1,12 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { WORD_SIZE, B256Coder, ASSET_ID_LEN, BigNumberCoder } from '@fuel-ts/abi-coder'; -import { CONTRACT_ID_LEN } from '@fuel-ts/abi-coder'; -import { ENCODING_V1 } from '@fuel-ts/abi-coder'; +import { + WORD_SIZE, + B256Coder, + ASSET_ID_LEN, + BigNumberCoder, + CONTRACT_ID_LEN, + ENCODING_V1, +} from '@fuel-ts/abi-coder'; import type { CallResult, TransactionResultCallReceipt, From 8ecd5c9390dd831c74a2cc7abdefb0949fd5617d Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 15:30:30 +0000 Subject: [PATCH 48/70] chore: add missing test groups --- .../src/experimental-contract.test.ts | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index 6b8641770a3..36dbf9f9806 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -15,19 +15,22 @@ const U256_MAX = bn(2).pow(256).sub(1); const B512 = '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; -beforeAll(async () => { - const projectName = 'contract-echo'; - const path = join( - __dirname, - `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` - ); - const contractBytecode = readFileSync(`${path}.bin`); - const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); +/** + * @group node + */ +describe('Experimental Contract', () => { + beforeAll(async () => { + const projectName = 'contract-echo'; + const path = join( + __dirname, + `../test/fixtures/forc-projects-experimental/${projectName}/out/release/${projectName}` + ); + const contractBytecode = readFileSync(`${path}.bin`); + const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); - contractInstance = await setup({ contractBytecode, abi }); -}); + contractInstance = await setup({ contractBytecode, abi }); + }); -describe('Experimental Contract', () => { it('echos mixed struct with all types', async () => { const struct = { a: U8_MAX, From b80d39e237b61db576d621f0bc6324adda58a7bf Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 15:40:43 +0000 Subject: [PATCH 49/70] feat: support forc 0.51.1 --- packages/forc/VERSION_EXPERIMENTAL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/forc/VERSION_EXPERIMENTAL b/packages/forc/VERSION_EXPERIMENTAL index 655b90034ac..fb6e7a44dd5 100644 --- a/packages/forc/VERSION_EXPERIMENTAL +++ b/packages/forc/VERSION_EXPERIMENTAL @@ -1 +1 @@ -0.52.0 \ No newline at end of file +0.51.1 \ No newline at end of file From 0f6ee0a1d7b749241f26b171d50ef8246930acee Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 28 Mar 2024 17:13:52 +0000 Subject: [PATCH 50/70] chore: support forc .52.0 with sway patch branch --- packages/forc/VERSION_EXPERIMENTAL | 2 +- .../test/fixtures/forc-projects-experimental/Forc.toml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/forc/VERSION_EXPERIMENTAL b/packages/forc/VERSION_EXPERIMENTAL index fb6e7a44dd5..655b90034ac 100644 --- a/packages/forc/VERSION_EXPERIMENTAL +++ b/packages/forc/VERSION_EXPERIMENTAL @@ -1 +1 @@ -0.51.1 \ No newline at end of file +0.52.0 \ No newline at end of file diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index dc76225a689..a22cfcf5baf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -6,3 +6,6 @@ members = [ "logging", "contract-echo", ] + +[patch.'https://github.com/fuellabs/sway'] +std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } From 2c7a48e7087a28336e1b671d418240b7eea5c6ec Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Fri, 29 Mar 2024 14:18:15 +0000 Subject: [PATCH 51/70] chore: support forc 52 in experimetnal --- .../test/fixtures/forc-projects-experimental/Forc.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml index a22cfcf5baf..dc76225a689 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/Forc.toml @@ -6,6 +6,3 @@ members = [ "logging", "contract-echo", ] - -[patch.'https://github.com/fuellabs/sway'] -std = { git = "https://github.com/fuellabs/sway", branch = "xunilrj/remove-method-name-log" } From fe5d6a24808f947ba70f1f813215599a4d9bc473 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 1 Apr 2024 15:32:21 +0100 Subject: [PATCH 52/70] chore: pass predicate arguments as array --- packages/fuel-gauge/src/experimental-predicate.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts index 388f5bd817a..42c37847e08 100644 --- a/packages/fuel-gauge/src/experimental-predicate.test.ts +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -79,7 +79,7 @@ describe('Experimental Predicate', () => { bytecode, provider, abi, - inputData: struct, + inputData: [struct], }); await fundPredicate(wallet, predicate, 100_000); From 7ebd3b652ade660af374100eff7aef08abb6dfd5 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 1 Apr 2024 15:32:58 +0100 Subject: [PATCH 53/70] chore: type hit predicate test --- packages/fuel-gauge/src/experimental-predicate.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts index 42c37847e08..ac347683783 100644 --- a/packages/fuel-gauge/src/experimental-predicate.test.ts +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -75,7 +75,7 @@ describe('Experimental Predicate', () => { }, }; - const predicate = new Predicate({ + const predicate = new Predicate<[typeof struct]>({ bytecode, provider, abi, From fab876331159b3b782437ef872b42f6d077639d1 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Mon, 1 Apr 2024 15:36:15 +0100 Subject: [PATCH 54/70] chore: regen changeset --- .changeset/{curly-feet-melt.md => light-bikes-cover.md} | 1 + 1 file changed, 1 insertion(+) rename .changeset/{curly-feet-melt.md => light-bikes-cover.md} (82%) diff --git a/.changeset/curly-feet-melt.md b/.changeset/light-bikes-cover.md similarity index 82% rename from .changeset/curly-feet-melt.md rename to .changeset/light-bikes-cover.md index 44d4d075b74..2b633e66931 100644 --- a/.changeset/curly-feet-melt.md +++ b/.changeset/light-bikes-cover.md @@ -1,5 +1,6 @@ --- "@fuel-ts/abi-coder": minor +"@fuel-ts/forc": minor "@fuel-ts/program": minor --- From 204f4810acd7045110e2d73832c6091f2a93259f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 13:26:30 +0100 Subject: [PATCH 55/70] chore: redudant array conversion in byte coder --- packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts index 9efcbb7553d..444d3877a31 100644 --- a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.ts @@ -39,8 +39,7 @@ export class ByteCoder extends Coder { } #getPaddedData(value: number[] | Uint8Array): Uint8Array { - const bytes = value instanceof Uint8Array ? value : new Uint8Array(value); - const data: Uint8Array[] = [Uint8Array.from(bytes)]; + const data = value instanceof Uint8Array ? [value] : [new Uint8Array(value)]; const paddingLength = (WORD_SIZE - (value.length % WORD_SIZE)) % WORD_SIZE; if (paddingLength) { From c1f9a79ac0119ef0d2a4f1d0b98ea0b10f153850 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 13:32:07 +0100 Subject: [PATCH 56/70] feat: add private function to v1 option coder Co-authored-by: Peter Smith --- packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts index 8228521983c..20a7a658c0f 100644 --- a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts @@ -12,7 +12,7 @@ export class OptionCoder> extends EnumCode return result; } - toSwayOption(input: InputValueOf): SwayOption { + private toSwayOption(input: InputValueOf): SwayOption { if (input !== undefined) { return { Some: input }; } From e8c95bdc004ed4dd2341e83ef1d090893a2ad110 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 13:34:19 +0100 Subject: [PATCH 57/70] feat: add private function to v1 option coder Co-authored-by: Peter Smith --- packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts index 20a7a658c0f..45f16d8035c 100644 --- a/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/OptionCoder.ts @@ -25,7 +25,7 @@ export class OptionCoder> extends EnumCode return [this.toOption(decoded) as DecodedValueOf, newOffset]; } - toOption(output?: DecodedValueOf): Option { + private toOption(output?: DecodedValueOf): Option { if (output && 'Some' in output) { return output.Some; } From f6a4658b7ccb8811e4aa2f9db899b20ccce6c6c2 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 14:08:02 +0100 Subject: [PATCH 58/70] chore: format contract call script comments --- packages/program/src/contract-call-script.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index cafed0c26e1..1720dda81e8 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -237,33 +237,28 @@ export const getScriptDataV0 = ( callDataOffset: segmentOffset + WORD_SIZE + ASSET_ID_LEN + gasForwardedSize, }; - /// script data, consisting of the following items in the given order: - /// 1. Amount to be forwarded `(1 * `[`WORD_SIZE`]`)` + // 1. Amount scriptData.push(new BigNumberCoder('u64').encode(call.amount || 0)); - /// 2. Asset ID to be forwarded ([`AssetId::LEN`]) + // 2. Asset ID scriptData.push(new B256Coder().encode(call.assetId?.toString() || BaseAssetId)); - /// 3. Contract ID ([`ContractId::LEN`]); + // 3. Contract ID scriptData.push(call.contractId.toBytes()); - /// 4. Function selector `(1 * `[`WORD_SIZE`]`)` + // 4. Function selector scriptData.push(new BigNumberCoder('u64').encode(call.fnSelector)); - /// 5. Gas to be forwarded `(1 * `[`WORD_SIZE`]`)` + // 5. Gas to be forwarded if (call.gas) { scriptData.push(new BigNumberCoder('u64').encode(call.gas)); gasForwardedSize = WORD_SIZE; } - /// 6. Calldata offset (optional) `(1 * `[`WORD_SIZE`]`)` - // If the method call takes custom inputs or has more than - // one argument, we need to calculate the `call_data_offset`, - // which points to where the data for the custom types start in the - // transaction. If it doesn't take any custom inputs, this isn't necessary. + // 6. Calldata offset if (call.isInputDataPointer) { const pointerInputOffset = segmentOffset + POINTER_DATA_OFFSET + gasForwardedSize; scriptData.push(new BigNumberCoder('u64').encode(pointerInputOffset)); } - /// 7. Encoded arguments (optional) (variable length) + // 7. Encoded arguments (optional) (variable length) const args = arrayify(call.data); scriptData.push(args); From 5f37abd7e0e1e1b6ba260c2d2e596a9bc2b8f308 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 17:14:38 +0100 Subject: [PATCH 59/70] feat: add assertion to predicate test --- .../fuel-gauge/src/experimental-predicate.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-predicate.test.ts b/packages/fuel-gauge/src/experimental-predicate.test.ts index ac347683783..9f564156db3 100644 --- a/packages/fuel-gauge/src/experimental-predicate.test.ts +++ b/packages/fuel-gauge/src/experimental-predicate.test.ts @@ -81,9 +81,17 @@ describe('Experimental Predicate', () => { abi, inputData: [struct], }); - await fundPredicate(wallet, predicate, 100_000); - const tx = await predicate.transfer(receiver.address, 100, BaseAssetId); + const initialBalance = await receiver.getBalance(BaseAssetId); + expect(initialBalance).toStrictEqual(bn(0)); + + const amountToReceiver = 100; + + await fundPredicate(wallet, predicate, 100_000); + const tx = await predicate.transfer(receiver.address, amountToReceiver, BaseAssetId); await tx.waitForResult(); + + const finalBalance = await receiver.getBalance(BaseAssetId); + expect(finalBalance).toStrictEqual(bn(amountToReceiver)); }); }); From 7fd52bae69e305f4ddac484362c0d945a5232f8a Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 17:47:36 +0100 Subject: [PATCH 60/70] feat: add default encoding to function fragment --- packages/abi-coder/src/FunctionFragment.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index 175eafcc875..0397dd9e9bc 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -19,7 +19,7 @@ import type { JsonAbiFunction, JsonAbiFunctionAttribute, } from './types/JsonAbi'; -import { ENCODING_V1, OPTION_CODER_TYPE } from './utils/constants'; +import { ENCODING_V0, ENCODING_V1, OPTION_CODER_TYPE } from './utils/constants'; import type { Uint8ArrayWithDynamicData } from './utils/utilities'; import { isPointerType, unpackDynamicData, findOrThrow, isHeapType } from './utils/utilities'; @@ -30,7 +30,7 @@ export class FunctionFragment< readonly signature: string; readonly selector: string; readonly selectorBytes: Uint8Array; - readonly encoding?: string; + readonly encoding: string; readonly name: string; readonly jsonFn: JsonAbiFunction; readonly attributes: readonly JsonAbiFunctionAttribute[]; @@ -49,7 +49,7 @@ export class FunctionFragment< this.signature = FunctionFragment.getSignature(this.jsonAbi, this.jsonFn); this.selector = FunctionFragment.getFunctionSelector(this.signature); this.selectorBytes = new StdStringCoder().encode(name); - this.encoding = this.jsonAbi.encoding; + this.encoding = this.jsonAbi.encoding ?? ENCODING_V0; this.isInputDataPointer = this.#isInputDataPointer(); this.outputMetadata = { isHeapType: this.#isOutputDataHeap(), From 1f64bd8fec5dbfb20e8cc241f8af880b9ba48b5b Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 21:04:50 +0100 Subject: [PATCH 61/70] feat: add type hint to getScriptDatav0 Co-authored-by: Peter Smith --- packages/program/src/contract-call-script.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index 1720dda81e8..fa3565d1fa3 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -223,7 +223,7 @@ const getFunctionOutputInfos = (functionScopes: InvocationScopeLike[]): CallOutp * @param segmentOffset - the segment to generate pointers and offset data from. * @returns the populated script data and call parameter offsets. */ -export const getScriptDataV0 = ( +export const getScriptDataV0: ContractCallScriptFn = ( call: ContractCall, segmentOffset: number ): { scriptData: Uint8Array[]; callParamOffsets: CallOpcodeParamsOffset } => { From 163d1403749b3bdd743441b6405c2bddc1b11d77 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 21:04:58 +0100 Subject: [PATCH 62/70] feat: add type hint to getScriptDatav1 Co-authored-by: Peter Smith --- packages/program/src/contract-call-script.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/program/src/contract-call-script.ts b/packages/program/src/contract-call-script.ts index fa3565d1fa3..6b2d0c35490 100644 --- a/packages/program/src/contract-call-script.ts +++ b/packages/program/src/contract-call-script.ts @@ -275,7 +275,7 @@ export const getScriptDataV0: ContractCallScriptFn = ( * @param segmentOffset - the segment to generate pointers and offset data from. * @returns the populated script data and call parameter offsets. */ -export const getScriptDataV1 = ( +export const getScriptDataV1: ContractCallScriptFn = ( call: ContractCall, segmentOffset: number ): { scriptData: Uint8Array[]; callParamOffsets: CallOpcodeParamsOffset } => { From 29cbe1bc8d8d137f94a4f7d7201344dd39f0d830 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 2 Apr 2024 21:16:10 +0100 Subject: [PATCH 63/70] feat: add byte coder tests for byte array --- .../src/encoding/coders/v0/ByteCoder.test.ts | 14 ++++++++++++++ .../src/encoding/coders/v1/ByteCoder.test.ts | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts index f87f8b7e0e6..d54e552c6b1 100644 --- a/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts +++ b/packages/abi-coder/src/encoding/coders/v0/ByteCoder.test.ts @@ -24,6 +24,20 @@ describe('ByteCoder', () => { expect(actual).toStrictEqual(expected); }); + it('should encode a byte [byte array]', () => { + const coder = new ByteCoder(); + const expected: Uint8ArrayWithDynamicData = new Uint8Array([ + 0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 3, + ]); + expected.dynamicData = { + 0: new Uint8Array([1, 2, 3, 0, 0, 0, 0, 0]), + }; + + const actual = coder.encode(Uint8Array.from([1, 2, 3])); + + expect(actual).toStrictEqual(expected); + }); + it('should encode a byte [full word]', () => { const coder = new ByteCoder(); const expected: Uint8ArrayWithDynamicData = new Uint8Array([ diff --git a/packages/abi-coder/src/encoding/coders/v1/ByteCoder.test.ts b/packages/abi-coder/src/encoding/coders/v1/ByteCoder.test.ts index 2bb95e0bcd6..062ed94d151 100644 --- a/packages/abi-coder/src/encoding/coders/v1/ByteCoder.test.ts +++ b/packages/abi-coder/src/encoding/coders/v1/ByteCoder.test.ts @@ -17,6 +17,13 @@ describe('ByteCoder', () => { expect(actual).toStrictEqual(expected); }); + it('should encode a byte [byte array]', () => { + const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 3]); + const actual = coder.encode(Uint8Array.from([1, 2, 3])); + + expect(actual).toStrictEqual(expected); + }); + it('should encode a byte [full word]', () => { const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 8, 1, 2, 3, 4, 5, 6, 7, 8]); const actual = coder.encode([1, 2, 3, 4, 5, 6, 7, 8]); From 8c938e8f25fc3d62503c99501d6caf5c2f17f9fd Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 12:59:32 +0100 Subject: [PATCH 64/70] feat: implement encoding version type --- packages/abi-coder/src/FunctionFragment.ts | 3 ++- .../abi-coder/src/encoding/strategies/getCoderForEncoding.ts | 3 ++- packages/abi-coder/src/index.ts | 1 + packages/abi-coder/src/types/EncodingOptions.ts | 4 +++- packages/abi-coder/src/types/JsonAbi.ts | 4 +++- packages/abi-coder/src/utils/constants.ts | 1 + packages/program/src/types.ts | 4 ++-- 7 files changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/abi-coder/src/FunctionFragment.ts b/packages/abi-coder/src/FunctionFragment.ts index 0397dd9e9bc..73ff018532c 100644 --- a/packages/abi-coder/src/FunctionFragment.ts +++ b/packages/abi-coder/src/FunctionFragment.ts @@ -19,6 +19,7 @@ import type { JsonAbiFunction, JsonAbiFunctionAttribute, } from './types/JsonAbi'; +import type { EncodingVersion } from './utils/constants'; import { ENCODING_V0, ENCODING_V1, OPTION_CODER_TYPE } from './utils/constants'; import type { Uint8ArrayWithDynamicData } from './utils/utilities'; import { isPointerType, unpackDynamicData, findOrThrow, isHeapType } from './utils/utilities'; @@ -30,7 +31,7 @@ export class FunctionFragment< readonly signature: string; readonly selector: string; readonly selectorBytes: Uint8Array; - readonly encoding: string; + readonly encoding: EncodingVersion; readonly name: string; readonly jsonFn: JsonAbiFunction; readonly attributes: readonly JsonAbiFunctionAttribute[]; diff --git a/packages/abi-coder/src/encoding/strategies/getCoderForEncoding.ts b/packages/abi-coder/src/encoding/strategies/getCoderForEncoding.ts index affeb32ed07..ca4f2b35aa9 100644 --- a/packages/abi-coder/src/encoding/strategies/getCoderForEncoding.ts +++ b/packages/abi-coder/src/encoding/strategies/getCoderForEncoding.ts @@ -1,6 +1,7 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { GetCoderFn } from '../../types/GetCoder'; +import type { EncodingVersion } from '../../utils/constants'; import { ENCODING_V0, ENCODING_V1 } from '../../utils/constants'; import { getCoder as getCoderV0 } from './getCoderV0'; @@ -13,7 +14,7 @@ import { getCoder as getCoderV1 } from './getCoderV1'; * @throws for an unsupported encoding version. * @returns the appropriate encoding strategy. */ -export function getCoderForEncoding(encoding: string = ENCODING_V0): GetCoderFn { +export function getCoderForEncoding(encoding: EncodingVersion = ENCODING_V0): GetCoderFn { switch (encoding) { case ENCODING_V1: return getCoderV1; diff --git a/packages/abi-coder/src/index.ts b/packages/abi-coder/src/index.ts index 3085bd112ca..b0bc0217fe9 100644 --- a/packages/abi-coder/src/index.ts +++ b/packages/abi-coder/src/index.ts @@ -23,6 +23,7 @@ export { ASSET_ID_LEN, CONTRACT_ID_LEN, calculateVmTxMemory, + type EncodingVersion, ENCODING_V0, ENCODING_V1, } from './utils/constants'; diff --git a/packages/abi-coder/src/types/EncodingOptions.ts b/packages/abi-coder/src/types/EncodingOptions.ts index a0b4290a3d8..7577288937f 100644 --- a/packages/abi-coder/src/types/EncodingOptions.ts +++ b/packages/abi-coder/src/types/EncodingOptions.ts @@ -1,3 +1,5 @@ +import type { EncodingVersion } from '../utils/constants'; + /** * These are configurable options to be used when encoding. * @@ -56,7 +58,7 @@ * fuel specs (https://github.com/FuelLabs/fuel-specs/blob/master/src/abi/argument-encoding.md#version-1 */ export type EncodingOptions = { - encoding?: string; + encoding?: EncodingVersion; isSmallBytes?: boolean; isRightPadded?: boolean; }; diff --git a/packages/abi-coder/src/types/JsonAbi.ts b/packages/abi-coder/src/types/JsonAbi.ts index fd417d6cbcc..8e261f8d379 100644 --- a/packages/abi-coder/src/types/JsonAbi.ts +++ b/packages/abi-coder/src/types/JsonAbi.ts @@ -3,12 +3,14 @@ * https://github.com/FuelLabs/fuel-specs/blob/master/specs/protocol/abi.md#json-abi-format */ +import type { EncodingVersion } from '../utils/constants'; + export interface JsonAbi { readonly types: readonly JsonAbiType[]; readonly loggedTypes: readonly JsonAbiLoggedType[]; readonly functions: readonly JsonAbiFunction[]; readonly configurables: readonly JsonAbiConfigurable[]; - readonly encoding?: string; + readonly encoding?: EncodingVersion; } export interface JsonAbiType { diff --git a/packages/abi-coder/src/utils/constants.ts b/packages/abi-coder/src/utils/constants.ts index bc5f05bc6ee..4210da14451 100644 --- a/packages/abi-coder/src/utils/constants.ts +++ b/packages/abi-coder/src/utils/constants.ts @@ -25,6 +25,7 @@ export const genericRegEx = /^generic (?\w+)$/; /** * Encoding versions */ +export type EncodingVersion = '0' | '1'; export const ENCODING_V0 = '0'; export const ENCODING_V1 = '1'; /** diff --git a/packages/program/src/types.ts b/packages/program/src/types.ts index d71858b1c60..d010f896644 100644 --- a/packages/program/src/types.ts +++ b/packages/program/src/types.ts @@ -1,4 +1,4 @@ -import type { FunctionFragment, JsonAbi } from '@fuel-ts/abi-coder'; +import type { FunctionFragment, JsonAbi, EncodingVersion } from '@fuel-ts/abi-coder'; import type { CoinQuantity, CoinQuantityLike } from '@fuel-ts/account'; import type { AbstractProgram, AbstractAddress, BytesLike } from '@fuel-ts/interfaces'; import type { BigNumberish } from '@fuel-ts/math'; @@ -13,7 +13,7 @@ export type ContractCall = { data: BytesLike; fnSelector: string; fnSelectorBytes: Uint8Array; - encoding?: string; + encoding?: EncodingVersion; isInputDataPointer: boolean; isOutputDataHeap: boolean; outputEncodedLength: number; From 9d6f3214292ce07605de804a2e391d5457b6592f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 15:43:18 +0100 Subject: [PATCH 65/70] feat: correctly set enum name --- packages/abi-coder/src/encoding/coders/v0/EnumCoder.ts | 2 +- packages/abi-coder/src/encoding/coders/v1/EnumCoder.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/abi-coder/src/encoding/coders/v0/EnumCoder.ts b/packages/abi-coder/src/encoding/coders/v0/EnumCoder.ts index 160dea65083..17afb3cb597 100644 --- a/packages/abi-coder/src/encoding/coders/v0/EnumCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v0/EnumCoder.ts @@ -37,7 +37,7 @@ export class EnumCoder> extends Coder< (max, coder) => Math.max(max, coder.encodedLength), 0 ); - super('enum', `enum ${name}`, caseIndexCoder.encodedLength + encodedValueSize); + super(`enum ${name}`, `enum ${name}`, caseIndexCoder.encodedLength + encodedValueSize); this.name = name; this.coders = coders; this.#caseIndexCoder = caseIndexCoder; diff --git a/packages/abi-coder/src/encoding/coders/v1/EnumCoder.ts b/packages/abi-coder/src/encoding/coders/v1/EnumCoder.ts index 7f9d5d4f205..680bc1e01e6 100644 --- a/packages/abi-coder/src/encoding/coders/v1/EnumCoder.ts +++ b/packages/abi-coder/src/encoding/coders/v1/EnumCoder.ts @@ -36,7 +36,7 @@ export class EnumCoder> extends Coder< (max, coder) => Math.max(max, coder.encodedLength), 0 ); - super('enum', `enum ${name}`, caseIndexCoder.encodedLength + encodedValueSize); + super(`enum ${name}`, `enum ${name}`, caseIndexCoder.encodedLength + encodedValueSize); this.name = name; this.coders = coders; this.#caseIndexCoder = caseIndexCoder; From d323cc4c33d0c541c7555ca32e6625c9b45b5cc6 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 15:50:14 +0100 Subject: [PATCH 66/70] chore: rebuild From 7b967104d04fd6b439b28505c23709ad8ddbc28f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 17:50:03 +0100 Subject: [PATCH 67/70] feat: use forc 52.1 for experimental projects --- packages/forc/VERSION_EXPERIMENTAL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/forc/VERSION_EXPERIMENTAL b/packages/forc/VERSION_EXPERIMENTAL index 655b90034ac..d06c2d46465 100644 --- a/packages/forc/VERSION_EXPERIMENTAL +++ b/packages/forc/VERSION_EXPERIMENTAL @@ -1 +1 @@ -0.52.0 \ No newline at end of file +0.52.1 \ No newline at end of file From 239011baaf8fd2bb7fce59f51c6e387546282c17 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 17:55:12 +0100 Subject: [PATCH 68/70] chore: update link ABI Co-authored-by: Peter Smith --- packages/abi-coder/src/types/JsonAbi.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/abi-coder/src/types/JsonAbi.ts b/packages/abi-coder/src/types/JsonAbi.ts index 8e261f8d379..e936819d861 100644 --- a/packages/abi-coder/src/types/JsonAbi.ts +++ b/packages/abi-coder/src/types/JsonAbi.ts @@ -1,6 +1,6 @@ /** * Types for Fuel JSON ABI Format as defined on: - * https://github.com/FuelLabs/fuel-specs/blob/master/specs/protocol/abi.md#json-abi-format + * https://github.com/FuelLabs/fuel-specs/blob/master/src/abi/json-abi-format.md */ import type { EncodingVersion } from '../utils/constants'; From 19740157f6244095905cf9a6c9fdec46f938bb21 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 3 Apr 2024 18:33:08 +0100 Subject: [PATCH 69/70] test: add b256 b512 vec tests --- .../fuel-gauge/src/experimental-logging.test.ts | 16 ++++++++++++++++ .../logging/src/main.sw | 10 +++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-logging.test.ts b/packages/fuel-gauge/src/experimental-logging.test.ts index 368d926ac8a..12e372a57b7 100644 --- a/packages/fuel-gauge/src/experimental-logging.test.ts +++ b/packages/fuel-gauge/src/experimental-logging.test.ts @@ -123,6 +123,14 @@ describe('Experimental Logging', () => { expect(logs).toStrictEqual([expected]); }); + it('logs b256 vec', async () => { + const expected = [B256, B256]; + + const { logs } = await contractInstance.functions.log_vec_b256(expected).call(); + + expect(logs).toEqual([expected]); + }); + it('logs b512', async () => { const expected = B512; @@ -131,6 +139,14 @@ describe('Experimental Logging', () => { expect(logs).toStrictEqual([expected]); }); + it('logs b512 vec', async () => { + const expected = [B512, B512]; + + const { logs } = await contractInstance.functions.log_vec_b512(expected).call(); + + expect(logs).toEqual([expected]); + }); + it('logs b256 b512 multiple params', async () => { const expected = [B256, B512]; diff --git a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/logging/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/logging/src/main.sw index 610ca6eb35d..0a2aa368788 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects-experimental/logging/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects-experimental/logging/src/main.sw @@ -37,7 +37,8 @@ abi LoggingContract { fn log_b512(a: B512) -> B512; fn log_b256_b512(a: b256, b: B512) -> (b256, B512); fn log_vec_u8(a: Vec) -> Vec; - fn log_vec_b256(a: Vec) -> Vec; + fn log_vec_b256(a: Vec); + fn log_vec_b512(a: Vec); fn log_u16_vec_u8(a: u16, b: Vec) -> (u16, Vec); fn log_bytes(a: Bytes) -> Bytes; fn log_u16_bytes(a: u16, b: Bytes) -> (u16, Bytes); @@ -139,9 +140,12 @@ impl LoggingContract for Contract { a } - fn log_vec_b256(a: Vec) -> Vec { + fn log_vec_b256(a: Vec) { + log(a); + } + + fn log_vec_b512(a: Vec) { log(a); - a } fn log_bytes(a: Bytes) -> Bytes { From 12e725cc3e4534a4402124bfee3ac5d5b9a98e1f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 4 Apr 2024 15:30:53 +0100 Subject: [PATCH 70/70] test: fix revert assertion --- packages/fuel-gauge/src/experimental-contract.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index 36dbf9f9806..96c24fbfaeb 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -83,7 +83,7 @@ describe('Experimental Contract', () => { it('extracts str slice from revert', async () => { await expect(contractInstance.functions.test_revert().call()).rejects.toThrow( - /The script reverted with reason RequireFailed/ + 'The transaction reverted because a "require" statement has thrown "This is a revert error".' ); }); });