Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: storage slots auto-load from typegen #3134

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
...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);
}
}
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);
});
});