Skip to content

Commit

Permalink
fix: storage slots auto-load from typegen (#3134)
Browse files Browse the repository at this point in the history
* chore: added test around the bug

* fix: overloaded deploy method in typegen contract factory

* chore: changeset

* chore: lint
  • Loading branch information
petertonysmith94 authored Sep 9, 2024
1 parent 41ea8d0 commit ccfa699
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-terms-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

fix: storage slots auto-load from typegen
17 changes: 11 additions & 6 deletions packages/abi-typegen/src/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
@@ -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}}";
Expand All @@ -15,15 +15,20 @@ export class {{capitalizedName}}Factory extends ContractFactory {
super(bytecode, {{capitalizedName}}.abi, accountOrProvider);
}

deploy<TContract extends Contract = Contract>(
deployOptions?: DeployContractOptions
): Promise<DeployContractResult<TContract>> {
return super.deploy({
storageSlots: {{capitalizedName}}.storageSlots,
...deployOptions,
});
}

static async deploy (
wallet: Account,
options: DeployContractOptions = {}
): Promise<DeployContractResult<{{capitalizedName}}>> {
const factory = new {{capitalizedName}}Factory(wallet);

return factory.deploy({
storageSlots: {{capitalizedName}}.storageSlots,
...options,
});
return factory.deploy(options);
}
}
17 changes: 11 additions & 6 deletions packages/abi-typegen/test/fixtures/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,15 +25,20 @@ export class MyContractFactory extends ContractFactory {
super(bytecode, MyContract.abi, accountOrProvider);
}

deploy<TContract extends Contract = Contract>(
deployOptions?: DeployContractOptions
): Promise<DeployContractResult<TContract>> {
return super.deploy({
storageSlots: MyContract.storageSlots,
...deployOptions,
});
}

static async deploy (
wallet: Account,
options: DeployContractOptions = {}
): Promise<DeployContractResult<MyContract>> {
const factory = new MyContractFactory(wallet);

return factory.deploy({
storageSlots: MyContract.storageSlots,
...options,
});
return factory.deploy(options);
}
}
60 changes: 59 additions & 1 deletion packages/fuel-gauge/src/storage-test-contract.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});

0 comments on commit ccfa699

Please sign in to comment.