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 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); } } diff --git a/packages/fuel-gauge/src/storage-test-contract.test.ts b/packages/fuel-gauge/src/storage-test-contract.test.ts index 256cf3462f9..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 } from 'fuels'; +import { toHex, ContractFactory, 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 }) => ({ + 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); + }); });