From d123917773c1df58eb6684de12076232c3185657 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 10:51:56 +0100 Subject: [PATCH 1/9] feat: deprecate v1 encoding support for configurables --- packages/abi-coder/src/Interface.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index 46a9602df7d..65b68e18ac8 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -99,7 +99,6 @@ export class Interface { return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, { isRightPadded: true, - encoding: this.jsonAbi.encoding, }); } From 8156fa958187fb420eb6a06047307386fbded35c Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 10:52:18 +0100 Subject: [PATCH 2/9] test: multiple encoding version support for params and configurables --- .../src/experimental-contract.test.ts | 26 +++++++++++++++++-- packages/fuel-gauge/src/utils.ts | 11 ++++++-- .../contract-echo/src/main.sw | 19 ++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index 96c24fbfaeb..807a2676903 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -1,11 +1,12 @@ import { readFileSync } from 'fs'; -import type { Contract } from 'fuels'; +import type { Contract, ContractFactory } from 'fuels'; import { bn } from 'fuels'; import { join } from 'path'; -import { setup } from './utils'; +import { setup, setupFactory } from './utils'; let contractInstance: Contract; +let contractFactory: ContractFactory; const U8_MAX = 2 ** 8 - 1; const U16_MAX = 2 ** 16 - 1; @@ -29,6 +30,7 @@ describe('Experimental Contract', () => { const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); contractInstance = await setup({ contractBytecode, abi }); + contractFactory = await setupFactory({ contractBytecode, abi }); }); it('echos mixed struct with all types', async () => { @@ -86,4 +88,24 @@ describe('Experimental Contract', () => { 'The transaction reverted because a "require" statement has thrown "This is a revert error".' ); }); + + it('echos configurable (both encoding versions)', async () => { + const param = [1, 2, 3, 'four']; + const { value } = await contractInstance.functions.echo_configurable(param).call(); + + expect(value).toStrictEqual(param); + + const configurableParam = [5, 6, 7, 'fuel']; + const configurableConstants = { + CONF: configurableParam, + }; + const configurableContractInstance = await contractFactory.deployContract({ + configurableConstants, + }); + const { value: configurableValue } = await configurableContractInstance.functions + .echo_configurable(configurableParam) + .call(); + + expect(configurableValue).toStrictEqual(configurableParam); + }); }); diff --git a/packages/fuel-gauge/src/utils.ts b/packages/fuel-gauge/src/utils.ts index cd6250a2fe8..fc7bbd00d62 100644 --- a/packages/fuel-gauge/src/utils.ts +++ b/packages/fuel-gauge/src/utils.ts @@ -9,13 +9,14 @@ let contractInstance: Contract; const deployContract = async ( factory: ContractFactory, provider: Provider, - useCache: boolean = true + useCache: boolean = true, + configurableConstants?: { [name: string]: unknown } ) => { if (contractInstance && useCache) { return contractInstance; } const { minGasPrice } = provider.getGasConfig(); - contractInstance = await factory.deployContract({ gasPrice: minGasPrice }); + contractInstance = await factory.deployContract({ configurableConstants, gasPrice: minGasPrice }); return contractInstance; }; @@ -36,6 +37,7 @@ export type SetupConfig = { contractBytecode: BytesLike; abi: JsonAbi | Interface; cache?: boolean; + configurableConstants?: { [name: string]: unknown }; }; export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => { @@ -46,6 +48,11 @@ export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => { return contract; }; +export const setupFactory = async ({ contractBytecode, abi }: SetupConfig) => { + const wallet = await createWallet(); + return new ContractFactory(contractBytecode, abi, wallet); +}; + export const createSetupConfig = (defaultConfig: SetupConfig) => async (config?: Partial) => setup({ 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 8d81efbf771..76b057489c3 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 @@ -6,6 +6,7 @@ use std::option::Option; use std::bytes::Bytes; use std::vec::Vec; + enum NativeEnum { Checked: (), Pending: (), @@ -65,9 +66,14 @@ struct MixedStruct { str_slice: str, } +configurable { + CONF: (u8, u16, u32, str[4]) = (1, 2, 3, __to_str_array("four")), +} + abi MyContract { fn echo_struct(param: MixedStruct) -> MixedStruct; fn test_revert() -> bool; + fn echo_configurable(param: (u8, u16, u32, str[4])) -> (u8, u16, u32, str[4]); } impl MyContract for Contract { @@ -79,4 +85,17 @@ impl MyContract for Contract { require(false, "This is a revert error"); true } + + fn echo_configurable(param: (u8, u16, u32, str[4])) -> (u8, u16, u32, str[4]) { + assert_eq(param.0, CONF.0); + assert_eq(param.1, CONF.1); + assert_eq(param.2, CONF.2); + + let param_str: str = from_str_array(param.3); + let conf_str: str = from_str_array(CONF.3); + assert_eq(param_str, conf_str); + + + CONF + } } From 7c1957e3fa9b09214981fd9d4f7ce3e730da20be Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 10:52:59 +0100 Subject: [PATCH 3/9] chore: changeset --- .changeset/brave-rice-march.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/brave-rice-march.md diff --git a/.changeset/brave-rice-march.md b/.changeset/brave-rice-march.md new file mode 100644 index 00000000000..495219e2c2a --- /dev/null +++ b/.changeset/brave-rice-march.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/abi-coder": minor +--- + +Deprecate multiple encoding version support for configurables From 52bc27e266a85fe1637cfe3559460ce5826131df Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:00:27 +0100 Subject: [PATCH 4/9] chore: update changeset --- .changeset/brave-rice-march.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/brave-rice-march.md b/.changeset/brave-rice-march.md index 495219e2c2a..76b12855134 100644 --- a/.changeset/brave-rice-march.md +++ b/.changeset/brave-rice-march.md @@ -2,4 +2,4 @@ "@fuel-ts/abi-coder": minor --- -Deprecate multiple encoding version support for configurables +feat!: deprecate multiple encoding version support for configurable constants From b7f790564ba93ae7898a1c777e6ce353f0861ea1 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:02:38 +0100 Subject: [PATCH 5/9] chore: lint --- .../forc-projects-experimental/contract-echo/src/main.sw | 1 - 1 file changed, 1 deletion(-) 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 76b057489c3..074ae8d14f3 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 @@ -94,7 +94,6 @@ impl MyContract for Contract { let param_str: str = from_str_array(param.3); let conf_str: str = from_str_array(CONF.3); assert_eq(param_str, conf_str); - CONF } From b3b9384d71b59a8be927720a67b61961bca7a31d Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:08:06 +0100 Subject: [PATCH 6/9] chore: forc format --- .../forc-projects-experimental/contract-echo/src/main.sw | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 074ae8d14f3..9defecdf883 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 @@ -6,7 +6,6 @@ use std::option::Option; use std::bytes::Bytes; use std::vec::Vec; - enum NativeEnum { Checked: (), Pending: (), @@ -94,7 +93,7 @@ impl MyContract for Contract { let param_str: str = from_str_array(param.3); let conf_str: str = from_str_array(CONF.3); assert_eq(param_str, conf_str); - + CONF } } From 595866f448d8dae993bc5648cc193ea682f61b83 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:29:52 +0100 Subject: [PATCH 7/9] feat: use v0 encoding constant for encode configuralbes --- packages/abi-coder/src/Interface.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index 65b68e18ac8..b6acdf248fc 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -7,6 +7,7 @@ import { AbiCoder } from './AbiCoder'; import { FunctionFragment } from './FunctionFragment'; import type { InputValue } from './encoding/coders/AbstractCoder'; import type { JsonAbi, JsonAbiConfigurable } from './types/JsonAbi'; +import { ENCODING_V0 } from './utils/constants'; import { findTypeById } from './utils/json-abi'; export class Interface { @@ -99,6 +100,7 @@ export class Interface { return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, { isRightPadded: true, + encoding: ENCODING_V0, }); } From df8d9a88ef8ebe7c6735ec053d54750de01670ce Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:38:57 +0100 Subject: [PATCH 8/9] chore: add doc for configurable encoding support Co-authored-by: Anderson Arboleya --- 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 b6acdf248fc..e275e422901 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -100,6 +100,7 @@ export class Interface { return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, { isRightPadded: true, + // TODO: Review support for configurables in v1 encoding when it becomes available encoding: ENCODING_V0, }); } From 169489dcc1837d3df8b48d75fb7af282d8b67a8b Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 11 Apr 2024 11:43:21 +0100 Subject: [PATCH 9/9] chore: remove fuel gauge factory helper functions --- packages/fuel-gauge/src/experimental-contract.test.ts | 8 ++++---- packages/fuel-gauge/src/utils.ts | 11 ++--------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/fuel-gauge/src/experimental-contract.test.ts b/packages/fuel-gauge/src/experimental-contract.test.ts index 807a2676903..e2078715611 100644 --- a/packages/fuel-gauge/src/experimental-contract.test.ts +++ b/packages/fuel-gauge/src/experimental-contract.test.ts @@ -1,9 +1,9 @@ import { readFileSync } from 'fs'; -import type { Contract, ContractFactory } from 'fuels'; -import { bn } from 'fuels'; +import type { Contract } from 'fuels'; +import { ContractFactory, bn } from 'fuels'; import { join } from 'path'; -import { setup, setupFactory } from './utils'; +import { setup } from './utils'; let contractInstance: Contract; let contractFactory: ContractFactory; @@ -30,7 +30,7 @@ describe('Experimental Contract', () => { const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8')); contractInstance = await setup({ contractBytecode, abi }); - contractFactory = await setupFactory({ contractBytecode, abi }); + contractFactory = new ContractFactory(contractBytecode, abi, contractInstance.account); }); it('echos mixed struct with all types', async () => { diff --git a/packages/fuel-gauge/src/utils.ts b/packages/fuel-gauge/src/utils.ts index fc7bbd00d62..cd6250a2fe8 100644 --- a/packages/fuel-gauge/src/utils.ts +++ b/packages/fuel-gauge/src/utils.ts @@ -9,14 +9,13 @@ let contractInstance: Contract; const deployContract = async ( factory: ContractFactory, provider: Provider, - useCache: boolean = true, - configurableConstants?: { [name: string]: unknown } + useCache: boolean = true ) => { if (contractInstance && useCache) { return contractInstance; } const { minGasPrice } = provider.getGasConfig(); - contractInstance = await factory.deployContract({ configurableConstants, gasPrice: minGasPrice }); + contractInstance = await factory.deployContract({ gasPrice: minGasPrice }); return contractInstance; }; @@ -37,7 +36,6 @@ export type SetupConfig = { contractBytecode: BytesLike; abi: JsonAbi | Interface; cache?: boolean; - configurableConstants?: { [name: string]: unknown }; }; export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => { @@ -48,11 +46,6 @@ export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => { return contract; }; -export const setupFactory = async ({ contractBytecode, abi }: SetupConfig) => { - const wallet = await createWallet(); - return new ContractFactory(contractBytecode, abi, wallet); -}; - export const createSetupConfig = (defaultConfig: SetupConfig) => async (config?: Partial) => setup({