Skip to content

Commit

Permalink
docs: add docs around custom storage slots (#1126)
Browse files Browse the repository at this point in the history
* docs: add docs around custom storage slots

* fix import

* fix incorrect path
  • Loading branch information
Dhaiwat10 committed Jul 25, 2023
1 parent 1b6541b commit b2937d7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .changeset/quiet-eggs-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ export default defineConfig({
text: 'Configurable Constants',
link: '/guide/contracts/configurable-constants',
},
{
text: 'Storage Slots',
link: '/guide/contracts/storage-slots',
},
{
text: 'Logs',
link: '/guide/contracts/logs',
Expand Down
11 changes: 11 additions & 0 deletions apps/docs/src/guide/contracts/storage-slots.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Storage Slots

When deploying a contract, you can specify the custom storage slots that you want to use.

<<< @/../../../packages/fuel-gauge/src/storage-test-contract.test.ts#contract-deployment-storage-slots{ts:line-numbers}

In the above example, we directly imported the storage slots from a JSON file generated by the Sway compiler.

Instead of importing from a file, you can also specify the custom storage slots directly in your code:

<<< @/../../../packages/fuel-gauge/src/storage-test-contract.test.ts#contract-deployment-storage-slots-inline{ts:line-numbers}
53 changes: 50 additions & 3 deletions packages/fuel-gauge/src/storage-test-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import { join } from 'path';
import abi from '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test-abi.json';
import storageSlots from '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test-storage_slots.json';

const binPath = join(
__dirname,
'../fixtures/forc-projects/storage-test-contract/out/debug/storage-test.bin'
);

const setup = async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
// Create wallet
const wallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);

// Deploy contract
const bytecode = readFileSync(
join(__dirname, '../fixtures/forc-projects/storage-test-contract/out/debug/storage-test.bin')
);
const bytecode = readFileSync(binPath);
// #region contract-deployment-storage-slots
// #context import storageSlots from '../your-sway-project/out/debug/your-sway-project-storage_slots.json';

const factory = new ContractFactory(bytecode, abi, wallet);
const contract = await factory.deployContract({
storageSlots,
});
// #endregion contract-deployment-storage-slots

return contract;
};
Expand All @@ -36,4 +43,44 @@ describe('StorageTestContract', () => {
const { value: count } = await contract.functions.counter().get();
expect(count.toHex()).toEqual(toHex(1337));
});

it('can increment counter - using custom inline storage slots', async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const wallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);
const bytecode = readFileSync(binPath);
const factory = new ContractFactory(bytecode, abi, wallet);
// #region contract-deployment-storage-slots-inline
const contract = await factory.deployContract({
storageSlots: [
{
key: '02dac99c283f16bc91b74f6942db7f012699a2ad51272b15207b9cc14a70dbae',
value: '0000000000000001000000000000000000000000000000000000000000000000',
},
{
key: '6294951dcb0a9111a517be5cf4785670ff4e166fb5ab9c33b17e6881b48e964f',
value: '0000000000000001000000000000003200000000000000000000000000000000',
},
{
key: 'b48b753af346966d0d169c0b2e3234611f65d5cfdb57c7b6e7cd6ca93707bee0',
value: '000000000000001e000000000000000000000000000000000000000000000000',
},
{
key: 'de9090cb50e71c2588c773487d1da7066d0c719849a7e58dc8b6397a25c567c0',
value: '0000000000000014000000000000000000000000000000000000000000000000',
},
{
key: 'f383b0ce51358be57daa3b725fe44acdb2d880604e367199080b4379c41bb6ed',
value: '000000000000000a000000000000000000000000000000000000000000000000',
},
],
});
// #endregion contract-deployment-storage-slots-inline
const { value: initializeResult } = await contract.functions.initialize_counter(1300).call();
expect(initializeResult.toHex()).toEqual(toHex(1300));
const { value: incrementResult } = await contract.functions.increment_counter(37).call();
expect(incrementResult.toHex()).toEqual(toHex(1337));

const { value: count } = await contract.functions.counter().get();
expect(count.toHex()).toEqual(toHex(1337));
});
});

0 comments on commit b2937d7

Please sign in to comment.