From 73b5e3a098f4fe5fefcbf788c743eec6440eedd3 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 08:22:29 +0100 Subject: [PATCH 1/4] chore: added test around the bug --- .../src/storage-test-contract.test.ts | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/fuel-gauge/src/storage-test-contract.test.ts b/packages/fuel-gauge/src/storage-test-contract.test.ts index 256cf3462f9..d632e5543d2 100644 --- a/packages/fuel-gauge/src/storage-test-contract.test.ts +++ b/packages/fuel-gauge/src/storage-test-contract.test.ts @@ -1,4 +1,4 @@ -import { toHex, ContractFactory } from 'fuels'; +import { toHex, ContractFactory, hexlify, ZeroBytes32 } from 'fuels'; import { launchTestNode } from 'fuels/test-utils'; import { StorageTestContract, StorageTestContractFactory } from '../test/typegen'; @@ -99,4 +99,62 @@ describe('StorageTestContract', () => { const { value: count } = await contract.functions.counter().simulate(); expect(count.toHex()).toEqual(toHex(1337)); }); + + it('should automatically load storage slots', async () => { + const { storageSlots } = StorageTestContract; + const expectedStorageSlots = storageSlots.map(({ key, value }) => ({ + key: `0x${key}`, + value: `0x${value}`, + })); + + using launched = await launchTestNode(); + + const { + wallets: [wallet], + } = launched; + + // via constructor + const storageContractFactory = new StorageTestContractFactory(wallet); + const deployConstructor = await storageContractFactory.deploy(); + const { transactionResult: transactionResultConstructor } = + await deployConstructor.waitForResult(); + expect(transactionResultConstructor.transaction.storageSlots).toEqual(expectedStorageSlots); + + // via static deploy + const deployStatically = await StorageTestContractFactory.deploy(wallet); + const { transactionResult: transactionResultStatically } = + await deployStatically.waitForResult(); + expect(transactionResultStatically.transaction.storageSlots).toEqual(expectedStorageSlots); + }); + + it('should allow for overriding storage slots', async () => { + const { storageSlots } = StorageTestContract; + const expectedStorageSlots = storageSlots.map(({ key, value }) => ({ + key: `0x${key}`, + value: ZeroBytes32, + })); + + using launched = await launchTestNode(); + + const { + wallets: [wallet], + } = launched; + + // via constructor + const storageContractFactory = new StorageTestContractFactory(wallet); + const deployConstructor = await storageContractFactory.deploy({ + storageSlots: expectedStorageSlots, + }); + const { transactionResult: transactionResultConstructor } = + await deployConstructor.waitForResult(); + expect(transactionResultConstructor.transaction.storageSlots).toEqual(expectedStorageSlots); + + // via static deploy + const deployStatically = await StorageTestContractFactory.deploy(wallet, { + storageSlots: expectedStorageSlots, + }); + const { transactionResult: transactionResultStatically } = + await deployStatically.waitForResult(); + expect(transactionResultStatically.transaction.storageSlots).toEqual(expectedStorageSlots); + }); }); From f8a5cd8d1f0b2a411ea1e4d6ec7e14fc3b95847a Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 08:38:31 +0100 Subject: [PATCH 2/4] fix: overloaded deploy method in typegen contract factory --- .../src/templates/contract/factory.hbs | 17 +++++++++++------ .../fixtures/templates/contract/factory.hbs | 17 +++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/abi-typegen/src/templates/contract/factory.hbs b/packages/abi-typegen/src/templates/contract/factory.hbs index 4810bcbba2e..bb033f843a4 100644 --- a/packages/abi-typegen/src/templates/contract/factory.hbs +++ b/packages/abi-typegen/src/templates/contract/factory.hbs @@ -1,6 +1,6 @@ {{header}} -import { ContractFactory, decompressBytecode } from "fuels"; +import { Contract, ContractFactory, decompressBytecode } from "fuels"; import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels"; import { {{capitalizedName}} } from "./{{capitalizedName}}"; @@ -15,15 +15,20 @@ export class {{capitalizedName}}Factory extends ContractFactory { super(bytecode, {{capitalizedName}}.abi, accountOrProvider); } + deploy( + deployOptions?: DeployContractOptions + ): Promise> { + return super.deploy({ + storageSlots: {{capitalizedName}}.storageSlots, + ...deployOptions, + }); + } + static async deploy ( wallet: Account, options: DeployContractOptions = {} ): Promise> { const factory = new {{capitalizedName}}Factory(wallet); - - return factory.deploy({ - storageSlots: {{capitalizedName}}.storageSlots, - ...options, - }); + return factory.deploy(options); } } diff --git a/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs b/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs index fb8630c1371..99cde0a2361 100644 --- a/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs +++ b/packages/abi-typegen/test/fixtures/templates/contract/factory.hbs @@ -10,7 +10,7 @@ Fuel-Core version: 33.33.33 */ -import { ContractFactory, decompressBytecode } from "fuels"; +import { Contract, ContractFactory, decompressBytecode } from "fuels"; import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels"; import { MyContract } from "./MyContract"; @@ -25,15 +25,20 @@ export class MyContractFactory extends ContractFactory { super(bytecode, MyContract.abi, accountOrProvider); } + deploy( + deployOptions?: DeployContractOptions + ): Promise> { + return super.deploy({ + storageSlots: MyContract.storageSlots, + ...deployOptions, + }); + } + static async deploy ( wallet: Account, options: DeployContractOptions = {} ): Promise> { const factory = new MyContractFactory(wallet); - - return factory.deploy({ - storageSlots: MyContract.storageSlots, - ...options, - }); + return factory.deploy(options); } } From 102156f2e622ab8ea2db38f4633a4a273ff35df7 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 08:39:33 +0100 Subject: [PATCH 3/4] chore: changeset --- .changeset/witty-terms-swim.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/witty-terms-swim.md diff --git a/.changeset/witty-terms-swim.md b/.changeset/witty-terms-swim.md new file mode 100644 index 00000000000..246bd037156 --- /dev/null +++ b/.changeset/witty-terms-swim.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/abi-typegen": patch +--- + +fix: storage slots auto-load from typegen From 39493714181ee2a944ced8beaa42a84540c6c7c6 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 9 Sep 2024 08:56:53 +0100 Subject: [PATCH 4/4] chore: lint --- packages/fuel-gauge/src/storage-test-contract.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/src/storage-test-contract.test.ts b/packages/fuel-gauge/src/storage-test-contract.test.ts index d632e5543d2..f428fcdfa82 100644 --- a/packages/fuel-gauge/src/storage-test-contract.test.ts +++ b/packages/fuel-gauge/src/storage-test-contract.test.ts @@ -1,4 +1,4 @@ -import { toHex, ContractFactory, hexlify, ZeroBytes32 } from 'fuels'; +import { toHex, ContractFactory, ZeroBytes32 } from 'fuels'; import { launchTestNode } from 'fuels/test-utils'; import { StorageTestContract, StorageTestContractFactory } from '../test/typegen'; @@ -129,7 +129,7 @@ describe('StorageTestContract', () => { it('should allow for overriding storage slots', async () => { const { storageSlots } = StorageTestContract; - const expectedStorageSlots = storageSlots.map(({ key, value }) => ({ + const expectedStorageSlots = storageSlots.map(({ key }) => ({ key: `0x${key}`, value: ZeroBytes32, }));