From 73b1bb17c959965b68f71124993ce264c4d7b5fc Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Sat, 5 Oct 2024 09:35:27 +0700 Subject: [PATCH 01/15] docs: cookbook for manually deploying and upgrading a proxy --- .../guide/contracts/proxy-contracts.test.ts | 101 +++++++++++ .../test/fixtures/forc-projects/Forc.toml | 2 + .../forc-projects/counter-v2/Forc.toml | 7 + .../forc-projects/counter-v2/src/main.sw | 52 ++++++ .../forc-projects/counter/src/main.sw | 8 +- .../forc-projects/proxy-contract/Forc.toml | 9 + .../proxy-contract/src/interface.sw | 11 ++ .../forc-projects/proxy-contract/src/main.sw | 160 ++++++++++++++++++ .../src/guide/contracts/proxy-contracts.md | 46 ++++- 9 files changed, 391 insertions(+), 5 deletions(-) create mode 100644 apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw create mode 100644 apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw diff --git a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts new file mode 100644 index 00000000000..4339f68a6e3 --- /dev/null +++ b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts @@ -0,0 +1,101 @@ +import { Provider, Wallet } from 'fuels'; +import { launchTestNode } from 'fuels/test-utils'; + +import { + Counter, + CounterFactory, + CounterV2, + CounterV2Factory, + ProxyContract, + ProxyContractFactory, +} from '../../../test/typegen'; + +describe('Proxy Contracts', () => { + it('deploys and upgrades a contracts using a proxy', async () => { + using launched = await launchTestNode(); + + const { + provider: testProvider, + wallets: [testWallet], + } = launched; + const providerUrl = testProvider.url; + const WALLET_PVT_KEY = testWallet.privateKey; + + // #region proxy-2 + // #import { Provider, Wallet }; + // #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; + // #context import { CounterFactory, Counter, ProxyFactory, CounterV2Factory } from 'path/to/typegen/outputs'; + + const provider = await Provider.create(providerUrl); + const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + + const counterContractFactory = new CounterFactory(wallet); + const { waitForResult: waitForCounterContract } = await counterContractFactory.deploy(); + const { contract: counterContract } = await waitForCounterContract(); + // #endregion proxy-2 + + // #region proxy-3 + // It is important to pass the pass all storage slots to the proxy in order to initialize the storage slots. + const storageSlots = Counter.storageSlots.concat(ProxyContract.storageSlots); + + // These configurables are specific to our recommended SRC14 compliant contract. They must be passed on deploy + // and then `initialize_proxy` must be called to setup the proxy contract. + const configurableConstants = { + INITIAL_TARGET: { bits: counterContract.id.toB256() }, + INITIAL_OWNER: { Initialized: { Address: { bits: wallet.address.toB256() } } }, + }; + + const proxyContractFactory = new ProxyContractFactory(wallet); + const { waitForResult: waitForProxyContract } = await proxyContractFactory.deploy({ + storageSlots, + configurableConstants, + }); + const { contract: proxyContract } = await waitForProxyContract(); + + const { waitForResult: waitForProxyInit } = await proxyContract.functions + .initialize_proxy() + .call(); + await waitForProxyInit(); + // #endregion proxy-3 + + // #region proxy-4 + // Make sure to use only the contract ID of the proxy when instantiating the contract + // as this will remain static even with future upgrades. + const initialContract = new Counter(proxyContract.id, wallet); + + const { waitForResult: waitForIncrement } = await initialContract.functions + .increment_counter(1) + .call(); + await waitForIncrement(); + + const { value: count } = await initialContract.functions.get_count().get(); + // #endregion proxy-4 + + // #region proxy-6 + const { waitForResult: waitForCounterContractV2 } = await CounterV2Factory.deploy(wallet); + const { contract: counterContractV2 } = await waitForCounterContractV2(); + + const { waitForResult: waitForUpdateTarget } = await proxyContract.functions + .set_proxy_target({ bits: counterContractV2.id.toB256() }) + .call(); + + await waitForUpdateTarget(); + // #endregion proxy-6 + + // #region proxy-7 + // Again, we are instantiating the contract with the same proxy ID but using a new contract instance. + const upgradedContract = new CounterV2(proxyContract.id, wallet); + const { waitForResult: waitForSecondIncrement } = await upgradedContract.functions + .increment_counter(1) + .call(); + await waitForSecondIncrement(); + + const { value: increments } = await upgradedContract.functions.get_increments().get(); + const { value: secondCount } = await upgradedContract.functions.get_count().get(); + // #endregion proxy-7 + + expect(count.toNumber()).toBe(1); + expect(secondCount.toNumber()).toBe(2); + expect(increments.toNumber()).toBe(1); + }); +}); diff --git a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml index ded6b45dc75..a1b46956fad 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml @@ -1,6 +1,7 @@ [workspace] members = [ "counter", + "counter-v2", "echo-enum", "liquidity-pool", "log-values", @@ -34,4 +35,5 @@ members = [ "input-output-types", "bytecode-input", "configurable-pin", + "proxy-contract", ] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml new file mode 100644 index 00000000000..f43fc0bb453 --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "counter-v2" + +[dependencies] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw new file mode 100644 index 00000000000..9c92f4bab17 --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw @@ -0,0 +1,52 @@ +// #region proxy-5 +contract; + +abi Counter { + #[storage(read)] + fn get_count() -> u64; + + #[storage(read)] + fn get_increments() -> u64; + + #[storage(write, read)] + fn increment_counter(amount: u64) -> u64; + + #[storage(write, read)] + fn decrement_counter(amount: u64) -> u64; +} + +storage { + counter: u64 = 0, + increments: u64 = 0, +} + +impl Counter for Contract { + #[storage(read)] + fn get_count() -> u64 { + storage.counter.try_read().unwrap_or(0) + } + + #[storage(read)] + fn get_increments() -> u64 { + storage.increments.try_read().unwrap_or(0) + } + + #[storage(write, read)] + fn increment_counter(amount: u64) -> u64 { + let current = storage.counter.try_read().unwrap_or(0); + storage.counter.write(current + amount); + + let current_iteration: u64 = storage.increments.try_read().unwrap_or(0); + storage.increments.write(current_iteration + 1); + + storage.counter.read() + } + + #[storage(write, read)] + fn decrement_counter(amount: u64) -> u64 { + let current = storage.counter.read(); + storage.counter.write(current - amount); + storage.counter.read() + } +} +// #endregion proxy-5 diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw index fe7abebcfab..b002dbf35ac 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw @@ -1,3 +1,4 @@ +// #region proxy-1 contract; abi Counter { @@ -18,20 +19,21 @@ storage { impl Counter for Contract { #[storage(read)] fn get_count() -> u64 { - storage.counter.read() + storage.counter.try_read().unwrap_or(0) } #[storage(write, read)] fn increment_counter(amount: u64) -> u64 { - let current = storage.counter.read(); + let current = storage.counter.try_read().unwrap_or(0); storage.counter.write(current + amount); storage.counter.read() } #[storage(write, read)] fn decrement_counter(amount: u64) -> u64 { - let current = storage.counter.read(); + let current = storage.counter.try_read().unwrap_or(0); storage.counter.write(current - amount); storage.counter.read() } } +// #endregion proxy-1 diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml new file mode 100644 index 00000000000..6c05298d399 --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "proxy-contract" + +[dependencies] +standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" } +sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" } \ No newline at end of file diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw new file mode 100644 index 00000000000..a3e938df629 --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw @@ -0,0 +1,11 @@ +library; + +use standards::src5::State; + +abi OwnedProxy { + #[storage(write)] + fn initialize_proxy(); + + #[storage(write)] + fn set_proxy_owner(new_proxy_owner: State); +} diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw new file mode 100644 index 00000000000..71911dbeeea --- /dev/null +++ b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw @@ -0,0 +1,160 @@ +contract; + +mod interface; + +use interface::OwnedProxy; +use ::sway_libs::{ + ownership::errors::InitializationError, + upgradability::{ + _proxy_owner, + _proxy_target, + _set_proxy_owner, + _set_proxy_target, + only_proxy_owner, + }, +}; +use standards::{src14::{SRC14, SRC14Extension}, src5::State}; +use std::execution::run_external; + +configurable { + /// The initial value of `storage::SRC14.target`. + INITIAL_TARGET: Option = None, + /// The initial value of `storage::SRC14.proxy_owner`. + INITIAL_OWNER: State = State::Uninitialized, +} + +storage { + SRC14 { + /// The [ContractId] of the target contract. + /// + /// # Additional Information + /// + /// `target` is stored at sha256("storage_SRC14_0") + target in 0x7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55: Option = None, + /// The [State] of the proxy owner. + /// + /// # Additional Information + /// + /// `proxy_owner` is stored at sha256("storage_SRC14_1") + proxy_owner in 0xbb79927b15d9259ea316f2ecb2297d6cc8851888a98278c0a2e03e1a091ea754: State = State::Uninitialized, + }, +} + +impl SRC14 for Contract { + /// Change the target contract of the proxy contract. + /// + /// # Additional Information + /// + /// This method can only be called by the `proxy_owner`. + /// + /// # Arguments + /// + /// * `new_target`: [ContractId] - The new proxy contract to which all fallback calls will be passed. + /// + /// # Reverts + /// + /// * When not called by `proxy_owner`. + /// + /// # Number of Storage Accesses + /// + /// * Reads: `1` + /// * Write: `1` + #[storage(read, write)] + fn set_proxy_target(new_target: ContractId) { + only_proxy_owner(); + _set_proxy_target(new_target); + } + + /// Returns the target contract of the proxy contract. + /// + /// # Returns + /// + /// * [Option] - The new proxy contract to which all fallback calls will be passed or `None`. + /// + /// # Number of Storage Accesses + /// + /// * Reads: `1` + #[storage(read)] + fn proxy_target() -> Option { + _proxy_target() + } +} + +impl SRC14Extension for Contract { + /// Returns the owner of the proxy contract. + /// + /// # Returns + /// + /// * [State] - Represents the state of ownership for this contract. + /// + /// # Number of Storage Accesses + /// + /// * Reads: `1` + #[storage(read)] + fn proxy_owner() -> State { + _proxy_owner() + } +} + +impl OwnedProxy for Contract { + /// Initializes the proxy contract. + /// + /// # Additional Information + /// + /// This method sets the storage values using the values of the configurable constants `INITIAL_TARGET` and `INITIAL_OWNER`. + /// This then allows methods that write to storage to be called. + /// This method can only be called once. + /// + /// # Reverts + /// + /// * When `storage::SRC14.proxy_owner` is not [State::Uninitialized]. + /// + /// # Number of Storage Accesses + /// + /// * Writes: `2` + #[storage(write)] + fn initialize_proxy() { + require( + _proxy_owner() == State::Uninitialized, + InitializationError::CannotReinitialized, + ); + + storage::SRC14.target.write(INITIAL_TARGET); + storage::SRC14.proxy_owner.write(INITIAL_OWNER); + } + + /// Changes proxy ownership to the passed State. + /// + /// # Additional Information + /// + /// This method can be used to transfer ownership between Identities or to revoke ownership. + /// + /// # Arguments + /// + /// * `new_proxy_owner`: [State] - The new state of the proxy ownership. + /// + /// # Reverts + /// + /// * When the sender is not the current proxy owner. + /// * When the new state of the proxy ownership is [State::Uninitialized]. + /// + /// # Number of Storage Accesses + /// + /// * Reads: `1` + /// * Writes: `1` + #[storage(write)] + fn set_proxy_owner(new_proxy_owner: State) { + _set_proxy_owner(new_proxy_owner); + } +} + +/// Loads and runs the target contract's code within the proxy contract's context. +/// +/// # Additional Information +/// +/// Used when a method that does not exist in the proxy contract is called. +#[fallback] +#[storage(read)] +fn fallback() { + run_external(_proxy_target().expect("FallbackError::TargetNotSet")) +} diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 4af77760cff..646867994a6 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -2,9 +2,51 @@ Automatic deployment of proxy contracts can be enabled in `Forc.toml`. -Once that is in place, [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy) will take care of it. +We recommend that you use [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy) to deploy and upgrade your contract using a proxy as it will take care of everything for you. However, if you want to deploy a proxy contract manually, you can follow the guide below. -## Docs +## Manually Deploying and Upgrading by Proxy + +As mentioned above, we recommend using [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy) to deploy and upgrade your contract as everything is handled under the hood. But the below guide will detail this process should you want to implement it yourself. + +We recommend using the [SRC14 compliant owned proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) as the underlying proxy and that is the one we will use in this guide, as is the one used by [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy). + +The overall process is as follows: + +1. Deploy your contract +1. Deploy the proxy contract +1. Set the target of the proxy contract to your deployed contract +1. Make calls to the contract via the proxy contract ID +1. Upgrade the contract by deploying a new version of the contract and updating the target of the proxy contract + +> **Note**: When new storage slots are added to the contract, they must be initialized in the proxy contract before they can be read from. This can be done by first writing to the new storage slot in the proxy contract. Failure to do so will result in the transaction being reverted. + +For example, lets imagine we want to deploy the following counter contract: + +<<< @/../../docs-snippets/test/fixtures/forc-projects/counter/src/main.sw#proxy-1{rs:line-numbers} + +Let's deploy and interact with it by proxy. First let's setup the environment and deploy the counter contract: + +<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-2{ts:line-numbers} + +Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) and initialize it by setting it's target to the counter target ID. + +<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-3{ts:line-numbers} + +Finally, we can call our counter contract using the contract ID of the proxy. + +<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-4{ts:line-numbers} + +Now let's make some changes to our initial counter contract by adding an additional storage slot to track the number of increments and a new get method that retrieves it's value: + +<<< @/../../docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw#proxy-5{rs:line-numbers} + +We can deploy it and update the target of the proxy like so: + +<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-6{ts:line-numbers} + +Then, we can instantiate our upgraded contract via the same proxy contract ID: + +<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-7{ts:line-numbers} For more info, please check these docs: From 0e795f7355aa8ec35cbbb3fe1a353e54fb42cf68 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 9 Oct 2024 13:39:17 +0700 Subject: [PATCH 02/15] chore: add missing test groups --- .../docs-snippets/src/guide/contracts/proxy-contracts.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts index 4339f68a6e3..ec1956b51ff 100644 --- a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts +++ b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts @@ -10,6 +10,10 @@ import { ProxyContractFactory, } from '../../../test/typegen'; +/** + * @group node + * @group browser + */ describe('Proxy Contracts', () => { it('deploys and upgrades a contracts using a proxy', async () => { using launched = await launchTestNode(); From 36c0e24b22fc148fcefdcaf642473616ea9204b4 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 9 Oct 2024 13:40:39 +0700 Subject: [PATCH 03/15] chore: changeset --- .changeset/friendly-cooks-appear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/friendly-cooks-appear.md diff --git a/.changeset/friendly-cooks-appear.md b/.changeset/friendly-cooks-appear.md new file mode 100644 index 00000000000..76352b2c198 --- /dev/null +++ b/.changeset/friendly-cooks-appear.md @@ -0,0 +1,5 @@ +--- +"@internal/benchmarks": patch +--- + +docs: proxy contract cookbook From 238fd5b9d4953d7697b11732f5b7559cff49c1f3 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 9 Oct 2024 13:44:24 +0700 Subject: [PATCH 04/15] chore: update changeset --- .changeset/friendly-cooks-appear.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/friendly-cooks-appear.md b/.changeset/friendly-cooks-appear.md index 76352b2c198..872063faaca 100644 --- a/.changeset/friendly-cooks-appear.md +++ b/.changeset/friendly-cooks-appear.md @@ -1,5 +1,4 @@ --- -"@internal/benchmarks": patch --- docs: proxy contract cookbook From 545923287ad1a7268ee84d1a3c0426037314d5d1 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 9 Oct 2024 13:55:36 +0700 Subject: [PATCH 05/15] chore: forc format --- .../test/fixtures/forc-projects/proxy-contract/Forc.toml | 2 +- scripts/forc-check.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml index 6c05298d399..24babe34583 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml @@ -6,4 +6,4 @@ name = "proxy-contract" [dependencies] standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" } -sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" } \ No newline at end of file +sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" } diff --git a/scripts/forc-check.sh b/scripts/forc-check.sh index bf090ec15e7..861932bfd9d 100755 --- a/scripts/forc-check.sh +++ b/scripts/forc-check.sh @@ -26,7 +26,7 @@ for forc_toml in $forc_tomls; do authors=$(grep "authors =" Forc.toml) if [[ "$authors" != "$expected_authors" ]]; then - ERROR=1 + ERRORED=1 echo -e "authors field should be: ${RED}$expected_authors] ${NC} but is ${RED}$authors ${NC}" fi From 360b704ef3d9598de1f2c40c76578833f4cfa525 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 10 Oct 2024 00:14:15 +0700 Subject: [PATCH 06/15] chore: update doc Co-authored-by: Dhaiwat --- apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts index ec1956b51ff..f3624e431b7 100644 --- a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts +++ b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts @@ -39,7 +39,7 @@ describe('Proxy Contracts', () => { // #endregion proxy-2 // #region proxy-3 - // It is important to pass the pass all storage slots to the proxy in order to initialize the storage slots. + // It is important to pass all storage slots to the proxy in order to initialize the storage slots. const storageSlots = Counter.storageSlots.concat(ProxyContract.storageSlots); // These configurables are specific to our recommended SRC14 compliant contract. They must be passed on deploy From d7c0b0ccf1d25cea33418184e04f232c2d04cb21 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 10 Oct 2024 00:14:38 +0700 Subject: [PATCH 07/15] chore: update doc Co-authored-by: Dhaiwat --- apps/docs/src/guide/contracts/proxy-contracts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 646867994a6..dc62f9ab9d8 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -28,7 +28,7 @@ Let's deploy and interact with it by proxy. First let's setup the environment an <<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-2{ts:line-numbers} -Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) and initialize it by setting it's target to the counter target ID. +Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) and initialize it by setting its target to the counter target ID. <<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-3{ts:line-numbers} From 445c93c4995c5e2600f11d60f5f3f12e91a23954 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 10 Oct 2024 00:14:55 +0700 Subject: [PATCH 08/15] chore: update doc Co-authored-by: Dhaiwat --- apps/docs/src/guide/contracts/proxy-contracts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index dc62f9ab9d8..51734aed1cb 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -36,7 +36,7 @@ Finally, we can call our counter contract using the contract ID of the proxy. <<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-4{ts:line-numbers} -Now let's make some changes to our initial counter contract by adding an additional storage slot to track the number of increments and a new get method that retrieves it's value: +Now let's make some changes to our initial counter contract by adding an additional storage slot to track the number of increments and a new get method that retrieves its value: <<< @/../../docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw#proxy-5{rs:line-numbers} From 8f9c67c66cf47536f6fd43fa7952d97b7da2ecd2 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Tue, 15 Oct 2024 22:31:21 +0700 Subject: [PATCH 09/15] docs: use src 14 commit hash for doc --- apps/docs/src/guide/contracts/proxy-contracts.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 51734aed1cb..5fd7a9614c4 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -8,7 +8,7 @@ We recommend that you use [fuels deploy](https://docs.fuel.network/docs/fuels-ts As mentioned above, we recommend using [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy) to deploy and upgrade your contract as everything is handled under the hood. But the below guide will detail this process should you want to implement it yourself. -We recommend using the [SRC14 compliant owned proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) as the underlying proxy and that is the one we will use in this guide, as is the one used by [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy). +We recommend using the [SRC14 compliant owned proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/174f5ed9c79c23a6aaf5db906fe27ecdb29c22eb/src14/owned_proxy/contract/out/release) as the underlying proxy and that is the one we will use in this guide, as is the one used by [fuels deploy](https://docs.fuel.network/docs/fuels-ts/fuels-cli/commands/#fuels-deploy). The overall process is as follows: @@ -28,7 +28,7 @@ Let's deploy and interact with it by proxy. First let's setup the environment an <<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-2{ts:line-numbers} -Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/master/src14/owned_proxy) and initialize it by setting its target to the counter target ID. +Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/174f5ed9c79c23a6aaf5db906fe27ecdb29c22eb/src14/owned_proxy/contract/out/release) and initialize it by setting its target to the counter target ID. <<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-3{ts:line-numbers} From 38a125f8c5edde5fd88dbcca7a4e60d1fc58c55c Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 17 Oct 2024 11:58:53 +0700 Subject: [PATCH 10/15] chore: migrate to v2 snippet --- .../guide/contracts/proxy-contracts.test.ts | 105 ------------------ .../src/cookbook/proxy-contracts.ts | 92 +++++++++++++++ apps/docs-snippets2/sway/Forc.toml | 2 +- .../sway}/counter-v2/Forc.toml | 0 .../sway}/counter-v2/src/main.sw | 10 +- apps/docs-snippets2/sway/counter/src/main.sw | 16 ++- .../sway/proxy}/Forc.toml | 2 +- .../sway/proxy}/src/interface.sw | 0 .../sway/proxy}/src/main.sw | 0 .../src/guide/contracts/proxy-contracts.md | 12 +- 10 files changed, 119 insertions(+), 120 deletions(-) delete mode 100644 apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts create mode 100644 apps/docs-snippets2/src/cookbook/proxy-contracts.ts rename apps/{docs-snippets/test/fixtures/forc-projects => docs-snippets2/sway}/counter-v2/Forc.toml (100%) rename apps/{docs-snippets/test/fixtures/forc-projects => docs-snippets2/sway}/counter-v2/src/main.sw (80%) rename apps/{docs-snippets/test/fixtures/forc-projects/proxy-contract => docs-snippets2/sway/proxy}/Forc.toml (91%) rename apps/{docs-snippets/test/fixtures/forc-projects/proxy-contract => docs-snippets2/sway/proxy}/src/interface.sw (100%) rename apps/{docs-snippets/test/fixtures/forc-projects/proxy-contract => docs-snippets2/sway/proxy}/src/main.sw (100%) diff --git a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts deleted file mode 100644 index f3624e431b7..00000000000 --- a/apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { Provider, Wallet } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { - Counter, - CounterFactory, - CounterV2, - CounterV2Factory, - ProxyContract, - ProxyContractFactory, -} from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Proxy Contracts', () => { - it('deploys and upgrades a contracts using a proxy', async () => { - using launched = await launchTestNode(); - - const { - provider: testProvider, - wallets: [testWallet], - } = launched; - const providerUrl = testProvider.url; - const WALLET_PVT_KEY = testWallet.privateKey; - - // #region proxy-2 - // #import { Provider, Wallet }; - // #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; - // #context import { CounterFactory, Counter, ProxyFactory, CounterV2Factory } from 'path/to/typegen/outputs'; - - const provider = await Provider.create(providerUrl); - const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); - - const counterContractFactory = new CounterFactory(wallet); - const { waitForResult: waitForCounterContract } = await counterContractFactory.deploy(); - const { contract: counterContract } = await waitForCounterContract(); - // #endregion proxy-2 - - // #region proxy-3 - // It is important to pass all storage slots to the proxy in order to initialize the storage slots. - const storageSlots = Counter.storageSlots.concat(ProxyContract.storageSlots); - - // These configurables are specific to our recommended SRC14 compliant contract. They must be passed on deploy - // and then `initialize_proxy` must be called to setup the proxy contract. - const configurableConstants = { - INITIAL_TARGET: { bits: counterContract.id.toB256() }, - INITIAL_OWNER: { Initialized: { Address: { bits: wallet.address.toB256() } } }, - }; - - const proxyContractFactory = new ProxyContractFactory(wallet); - const { waitForResult: waitForProxyContract } = await proxyContractFactory.deploy({ - storageSlots, - configurableConstants, - }); - const { contract: proxyContract } = await waitForProxyContract(); - - const { waitForResult: waitForProxyInit } = await proxyContract.functions - .initialize_proxy() - .call(); - await waitForProxyInit(); - // #endregion proxy-3 - - // #region proxy-4 - // Make sure to use only the contract ID of the proxy when instantiating the contract - // as this will remain static even with future upgrades. - const initialContract = new Counter(proxyContract.id, wallet); - - const { waitForResult: waitForIncrement } = await initialContract.functions - .increment_counter(1) - .call(); - await waitForIncrement(); - - const { value: count } = await initialContract.functions.get_count().get(); - // #endregion proxy-4 - - // #region proxy-6 - const { waitForResult: waitForCounterContractV2 } = await CounterV2Factory.deploy(wallet); - const { contract: counterContractV2 } = await waitForCounterContractV2(); - - const { waitForResult: waitForUpdateTarget } = await proxyContract.functions - .set_proxy_target({ bits: counterContractV2.id.toB256() }) - .call(); - - await waitForUpdateTarget(); - // #endregion proxy-6 - - // #region proxy-7 - // Again, we are instantiating the contract with the same proxy ID but using a new contract instance. - const upgradedContract = new CounterV2(proxyContract.id, wallet); - const { waitForResult: waitForSecondIncrement } = await upgradedContract.functions - .increment_counter(1) - .call(); - await waitForSecondIncrement(); - - const { value: increments } = await upgradedContract.functions.get_increments().get(); - const { value: secondCount } = await upgradedContract.functions.get_count().get(); - // #endregion proxy-7 - - expect(count.toNumber()).toBe(1); - expect(secondCount.toNumber()).toBe(2); - expect(increments.toNumber()).toBe(1); - }); -}); diff --git a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts new file mode 100644 index 00000000000..0eb4c1d7e33 --- /dev/null +++ b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts @@ -0,0 +1,92 @@ +// #region proxy-1 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { + Counter, + CounterFactory, + CounterV2, + CounterV2Factory, + Proxy, + ProxyFactory, +} from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const counterContractFactory = new CounterFactory(wallet); +const { waitForResult: waitForCounterContract } = + await counterContractFactory.deploy(); +const { contract: counterContract } = await waitForCounterContract(); +// #endregion proxy-2 + +// #region proxy-3 +// It is important to pass all storage slots to the proxy in order to initialize the storage slots. +const storageSlots = Counter.storageSlots.concat(Proxy.storageSlots); + +// These configurables are specific to our recommended SRC14 compliant contract. They must be passed on deploy +// and then `initialize_proxy` must be called to setup the proxy contract. +const configurableConstants = { + INITIAL_TARGET: { bits: counterContract.id.toB256() }, + INITIAL_OWNER: { + Initialized: { Address: { bits: wallet.address.toB256() } }, + }, +}; + +const proxyContractFactory = new ProxyFactory(wallet); +const { waitForResult: waitForProxyContract } = + await proxyContractFactory.deploy({ + storageSlots, + configurableConstants, + }); +const { contract: proxyContract } = await waitForProxyContract(); + +const { waitForResult: waitForProxyInit } = await proxyContract.functions + .initialize_proxy() + .call(); +await waitForProxyInit(); +// #endregion proxy-3 + +// #region proxy-4 +// Make sure to use only the contract ID of the proxy when instantiating the contract +// as this will remain static even with future upgrades. +const initialContract = new Counter(proxyContract.id, wallet); + +const { waitForResult: waitForIncrement } = await initialContract.functions + .increment_count(1) + .call(); +await waitForIncrement(); + +const { value: count } = await initialContract.functions.get_count().get(); +// #endregion proxy-4 + +// #region proxy-6 +const { waitForResult: waitForCounterContractV2 } = + await CounterV2Factory.deploy(wallet); +const { contract: counterContractV2 } = await waitForCounterContractV2(); + +const { waitForResult: waitForUpdateTarget } = await proxyContract.functions + .set_proxy_target({ bits: counterContractV2.id.toB256() }) + .call(); + +await waitForUpdateTarget(); +// #endregion proxy-6 + +// #region proxy-7 +// Again, we are instantiating the contract with the same proxy ID but using a new contract instance. +const upgradedContract = new CounterV2(proxyContract.id, wallet); +const { waitForResult: waitForSecondIncrement } = + await upgradedContract.functions.increment_count(1).call(); +await waitForSecondIncrement(); + +const { value: increments } = await upgradedContract.functions + .get_increments() + .get(); +const { value: secondCount } = await upgradedContract.functions + .get_count() + .get(); +// #endregion proxy-7 + +console.log('count', count.toNumber()); +console.log('secondCount', secondCount.toNumber()); +console.log('increments', increments.toNumber()); diff --git a/apps/docs-snippets2/sway/Forc.toml b/apps/docs-snippets2/sway/Forc.toml index fd431b1e348..c507fe240f7 100644 --- a/apps/docs-snippets2/sway/Forc.toml +++ b/apps/docs-snippets2/sway/Forc.toml @@ -1,2 +1,2 @@ [workspace] -members = ["counter", "script-sum"] +members = ["counter", "counter-v2", "proxy", "script-sum"] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml b/apps/docs-snippets2/sway/counter-v2/Forc.toml similarity index 100% rename from apps/docs-snippets/test/fixtures/forc-projects/counter-v2/Forc.toml rename to apps/docs-snippets2/sway/counter-v2/Forc.toml diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw b/apps/docs-snippets2/sway/counter-v2/src/main.sw similarity index 80% rename from apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw rename to apps/docs-snippets2/sway/counter-v2/src/main.sw index 9c92f4bab17..7a7b7acb59a 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw +++ b/apps/docs-snippets2/sway/counter-v2/src/main.sw @@ -9,10 +9,10 @@ abi Counter { fn get_increments() -> u64; #[storage(write, read)] - fn increment_counter(amount: u64) -> u64; + fn increment_count(amount: u64) -> u64; #[storage(write, read)] - fn decrement_counter(amount: u64) -> u64; + fn decrement_count(amount: u64) -> u64; } storage { @@ -32,7 +32,7 @@ impl Counter for Contract { } #[storage(write, read)] - fn increment_counter(amount: u64) -> u64 { + fn increment_count(amount: u64) -> u64 { let current = storage.counter.try_read().unwrap_or(0); storage.counter.write(current + amount); @@ -43,8 +43,8 @@ impl Counter for Contract { } #[storage(write, read)] - fn decrement_counter(amount: u64) -> u64 { - let current = storage.counter.read(); + fn decrement_count(amount: u64) -> u64 { + let current = storage.counter.try_read().unwrap_or(0); storage.counter.write(current - amount); storage.counter.read() } diff --git a/apps/docs-snippets2/sway/counter/src/main.sw b/apps/docs-snippets2/sway/counter/src/main.sw index e3e3beddcd5..8ae267a19e4 100644 --- a/apps/docs-snippets2/sway/counter/src/main.sw +++ b/apps/docs-snippets2/sway/counter/src/main.sw @@ -1,3 +1,4 @@ +// #region proxy-1 contract; abi Counter { @@ -6,6 +7,9 @@ abi Counter { #[storage(write, read)] fn increment_count(amount: u64) -> u64; + + #[storage(write, read)] + fn decrement_count(amount: u64) -> u64; } storage { @@ -15,13 +19,21 @@ storage { impl Counter for Contract { #[storage(read)] fn get_count() -> u64 { - storage.counter.read() + storage.counter.try_read().unwrap_or(0) } #[storage(write, read)] fn increment_count(amount: u64) -> u64 { - let current = storage.counter.read(); + let current = storage.counter.try_read().unwrap_or(0); storage.counter.write(current + amount); storage.counter.read() } + + #[storage(write, read)] + fn decrement_count(amount: u64) -> u64 { + let current = storage.counter.try_read().unwrap_or(0); + storage.counter.write(current - amount); + storage.counter.read() + } } +// #endregion proxy-1 diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml b/apps/docs-snippets2/sway/proxy/Forc.toml similarity index 91% rename from apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml rename to apps/docs-snippets2/sway/proxy/Forc.toml index 24babe34583..9ee514576e3 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/Forc.toml +++ b/apps/docs-snippets2/sway/proxy/Forc.toml @@ -2,7 +2,7 @@ authors = ["Fuel Labs "] entry = "main.sw" license = "Apache-2.0" -name = "proxy-contract" +name = "proxy" [dependencies] standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" } diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw b/apps/docs-snippets2/sway/proxy/src/interface.sw similarity index 100% rename from apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/interface.sw rename to apps/docs-snippets2/sway/proxy/src/interface.sw diff --git a/apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw b/apps/docs-snippets2/sway/proxy/src/main.sw similarity index 100% rename from apps/docs-snippets/test/fixtures/forc-projects/proxy-contract/src/main.sw rename to apps/docs-snippets2/sway/proxy/src/main.sw diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 5fd7a9614c4..49171dae01c 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -22,7 +22,7 @@ The overall process is as follows: For example, lets imagine we want to deploy the following counter contract: -<<< @/../../docs-snippets/test/fixtures/forc-projects/counter/src/main.sw#proxy-1{rs:line-numbers} +<<< @/../../docs-snippets2/sway/counter/src/main.sw#proxy-1{rs:line-numbers} Let's deploy and interact with it by proxy. First let's setup the environment and deploy the counter contract: @@ -30,23 +30,23 @@ Let's deploy and interact with it by proxy. First let's setup the environment an Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/174f5ed9c79c23a6aaf5db906fe27ecdb29c22eb/src14/owned_proxy/contract/out/release) and initialize it by setting its target to the counter target ID. -<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-3{ts:line-numbers} Finally, we can call our counter contract using the contract ID of the proxy. -<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-4{ts:line-numbers} Now let's make some changes to our initial counter contract by adding an additional storage slot to track the number of increments and a new get method that retrieves its value: -<<< @/../../docs-snippets/test/fixtures/forc-projects/counter-v2/src/main.sw#proxy-5{rs:line-numbers} +<<< @/../../docs-snippets2/sway/counter-v2/src/main.sw#proxy-5{rs:line-numbers} We can deploy it and update the target of the proxy like so: -<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-6{ts:line-numbers} Then, we can instantiate our upgraded contract via the same proxy contract ID: -<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-7{ts:line-numbers} For more info, please check these docs: From a17bb8ee93d359338748d0f5c4a01891e1c23b33 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 17 Oct 2024 12:03:12 +0700 Subject: [PATCH 11/15] chore: restore v1 snippets files --- apps/docs-snippets/test/fixtures/forc-projects/Forc.toml | 2 -- .../test/fixtures/forc-projects/counter/src/main.sw | 8 +++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml index a1b46956fad..ded6b45dc75 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/Forc.toml @@ -1,7 +1,6 @@ [workspace] members = [ "counter", - "counter-v2", "echo-enum", "liquidity-pool", "log-values", @@ -35,5 +34,4 @@ members = [ "input-output-types", "bytecode-input", "configurable-pin", - "proxy-contract", ] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw index b002dbf35ac..fe7abebcfab 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw @@ -1,4 +1,3 @@ -// #region proxy-1 contract; abi Counter { @@ -19,21 +18,20 @@ storage { impl Counter for Contract { #[storage(read)] fn get_count() -> u64 { - storage.counter.try_read().unwrap_or(0) + storage.counter.read() } #[storage(write, read)] fn increment_counter(amount: u64) -> u64 { - let current = storage.counter.try_read().unwrap_or(0); + let current = storage.counter.read(); storage.counter.write(current + amount); storage.counter.read() } #[storage(write, read)] fn decrement_counter(amount: u64) -> u64 { - let current = storage.counter.try_read().unwrap_or(0); + let current = storage.counter.read(); storage.counter.write(current - amount); storage.counter.read() } } -// #endregion proxy-1 From dea7aeac33a2b2551eb9ff407a58cab672a822f5 Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 17 Oct 2024 12:10:10 +0700 Subject: [PATCH 12/15] chore: fix snippet path --- apps/docs/src/guide/contracts/proxy-contracts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 49171dae01c..2dd6d5c27b4 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -26,7 +26,7 @@ For example, lets imagine we want to deploy the following counter contract: Let's deploy and interact with it by proxy. First let's setup the environment and deploy the counter contract: -<<< @/../../docs-snippets/src/guide/contracts/proxy-contracts.test.ts#proxy-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-2{ts:line-numbers} Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/174f5ed9c79c23a6aaf5db906fe27ecdb29c22eb/src14/owned_proxy/contract/out/release) and initialize it by setting its target to the counter target ID. From 93b04721f43f05db92315d7a47ae90b148bd710f Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Thu, 17 Oct 2024 12:14:37 +0700 Subject: [PATCH 13/15] chore: fix test region --- apps/docs-snippets2/src/cookbook/proxy-contracts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts index 0eb4c1d7e33..572ebe04858 100644 --- a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts +++ b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts @@ -1,4 +1,4 @@ -// #region proxy-1 +// #region proxy-2 import { Provider, Wallet } from 'fuels'; import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; From 2290e746503aa5a39b84498927b4079b8fc818ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Thu, 17 Oct 2024 10:43:09 -0300 Subject: [PATCH 14/15] multilning doc comments --- .../src/cookbook/proxy-contracts.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts index 572ebe04858..7a16d7b81b2 100644 --- a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts +++ b/apps/docs-snippets2/src/cookbook/proxy-contracts.ts @@ -21,11 +21,17 @@ const { contract: counterContract } = await waitForCounterContract(); // #endregion proxy-2 // #region proxy-3 -// It is important to pass all storage slots to the proxy in order to initialize the storage slots. +/** + * It is important to pass all storage slots to the proxy in order to + * initialize the storage slots. + */ const storageSlots = Counter.storageSlots.concat(Proxy.storageSlots); -// These configurables are specific to our recommended SRC14 compliant contract. They must be passed on deploy -// and then `initialize_proxy` must be called to setup the proxy contract. +/** + * These configurables are specific to our recommended SRC14 compliant + * contract. They must be passed on deploy and then `initialize_proxy` + * must be called to setup the proxy contract. + */ const configurableConstants = { INITIAL_TARGET: { bits: counterContract.id.toB256() }, INITIAL_OWNER: { @@ -48,8 +54,10 @@ await waitForProxyInit(); // #endregion proxy-3 // #region proxy-4 -// Make sure to use only the contract ID of the proxy when instantiating the contract -// as this will remain static even with future upgrades. +/** + * Make sure to use only the contract ID of the proxy when instantiating + * the contract as this will remain static even with future upgrades. + */ const initialContract = new Counter(proxyContract.id, wallet); const { waitForResult: waitForIncrement } = await initialContract.functions @@ -73,7 +81,10 @@ await waitForUpdateTarget(); // #endregion proxy-6 // #region proxy-7 -// Again, we are instantiating the contract with the same proxy ID but using a new contract instance. +/** + * Again, we are instantiating the contract with the same proxy ID + * but using a new contract instance. + */ const upgradedContract = new CounterV2(proxyContract.id, wallet); const { waitForResult: waitForSecondIncrement } = await upgradedContract.functions.increment_count(1).call(); From b966a26f69bb5bbbfc7f52856980d73b92055eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Torres?= <30977845+Torres-ssf@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:21:58 -0300 Subject: [PATCH 15/15] moving snippet to another place --- .../proxy-contracts.ts | 65 +++++++++---------- .../src/guide/contracts/proxy-contracts.md | 10 +-- 2 files changed, 37 insertions(+), 38 deletions(-) rename apps/docs-snippets2/src/{cookbook => contracts}/proxy-contracts.ts (55%) diff --git a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts b/apps/docs-snippets2/src/contracts/proxy-contracts.ts similarity index 55% rename from apps/docs-snippets2/src/cookbook/proxy-contracts.ts rename to apps/docs-snippets2/src/contracts/proxy-contracts.ts index 7a16d7b81b2..1000503ae06 100644 --- a/apps/docs-snippets2/src/cookbook/proxy-contracts.ts +++ b/apps/docs-snippets2/src/contracts/proxy-contracts.ts @@ -15,9 +15,8 @@ const provider = await Provider.create(LOCAL_NETWORK_URL); const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); const counterContractFactory = new CounterFactory(wallet); -const { waitForResult: waitForCounterContract } = - await counterContractFactory.deploy(); -const { contract: counterContract } = await waitForCounterContract(); +const deploy = await counterContractFactory.deploy(); +const { contract: counterContract } = await deploy.waitForResult(); // #endregion proxy-2 // #region proxy-3 @@ -40,17 +39,17 @@ const configurableConstants = { }; const proxyContractFactory = new ProxyFactory(wallet); -const { waitForResult: waitForProxyContract } = - await proxyContractFactory.deploy({ - storageSlots, - configurableConstants, - }); -const { contract: proxyContract } = await waitForProxyContract(); - -const { waitForResult: waitForProxyInit } = await proxyContract.functions +const proxyDeploy = await proxyContractFactory.deploy({ + storageSlots, + configurableConstants, +}); + +const { contract: proxyContract } = await proxyDeploy.waitForResult(); +const { waitForResult } = await proxyContract.functions .initialize_proxy() .call(); -await waitForProxyInit(); + +await waitForResult(); // #endregion proxy-3 // #region proxy-4 @@ -58,26 +57,25 @@ await waitForProxyInit(); * Make sure to use only the contract ID of the proxy when instantiating * the contract as this will remain static even with future upgrades. */ -const initialContract = new Counter(proxyContract.id, wallet); +const proxiedContract = new Counter(proxyContract.id, wallet); -const { waitForResult: waitForIncrement } = await initialContract.functions - .increment_count(1) - .call(); -await waitForIncrement(); +const incrementCall = await proxiedContract.functions.increment_count(1).call(); +await incrementCall.waitForResult(); -const { value: count } = await initialContract.functions.get_count().get(); +const { value: count } = await proxiedContract.functions.get_count().get(); // #endregion proxy-4 +console.log('count:', count.toNumber() === 1); + // #region proxy-6 -const { waitForResult: waitForCounterContractV2 } = - await CounterV2Factory.deploy(wallet); -const { contract: counterContractV2 } = await waitForCounterContractV2(); +const deployV2 = await CounterV2Factory.deploy(wallet); +const { contract: contractV2 } = await deployV2.waitForResult(); -const { waitForResult: waitForUpdateTarget } = await proxyContract.functions - .set_proxy_target({ bits: counterContractV2.id.toB256() }) +const updateTargetCall = await proxyContract.functions + .set_proxy_target({ bits: contractV2.id.toB256() }) .call(); -await waitForUpdateTarget(); +await updateTargetCall.waitForResult(); // #endregion proxy-6 // #region proxy-7 @@ -86,18 +84,19 @@ await waitForUpdateTarget(); * but using a new contract instance. */ const upgradedContract = new CounterV2(proxyContract.id, wallet); -const { waitForResult: waitForSecondIncrement } = - await upgradedContract.functions.increment_count(1).call(); -await waitForSecondIncrement(); + +const incrementCall2 = await upgradedContract.functions + .increment_count(1) + .call(); + +await incrementCall2.waitForResult(); const { value: increments } = await upgradedContract.functions .get_increments() .get(); -const { value: secondCount } = await upgradedContract.functions - .get_count() - .get(); + +const { value: count2 } = await upgradedContract.functions.get_count().get(); // #endregion proxy-7 -console.log('count', count.toNumber()); -console.log('secondCount', secondCount.toNumber()); -console.log('increments', increments.toNumber()); +console.log('secondCount', count2.toNumber() === 2); +console.log('increments', increments); diff --git a/apps/docs/src/guide/contracts/proxy-contracts.md b/apps/docs/src/guide/contracts/proxy-contracts.md index 2dd6d5c27b4..ff7151cec42 100644 --- a/apps/docs/src/guide/contracts/proxy-contracts.md +++ b/apps/docs/src/guide/contracts/proxy-contracts.md @@ -26,15 +26,15 @@ For example, lets imagine we want to deploy the following counter contract: Let's deploy and interact with it by proxy. First let's setup the environment and deploy the counter contract: -<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/proxy-contracts.ts#proxy-2{ts:line-numbers} Now let's deploy the [SRC14 compliant proxy contract](https://github.com/FuelLabs/sway-standard-implementations/tree/174f5ed9c79c23a6aaf5db906fe27ecdb29c22eb/src14/owned_proxy/contract/out/release) and initialize it by setting its target to the counter target ID. -<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/proxy-contracts.ts#proxy-3{ts:line-numbers} Finally, we can call our counter contract using the contract ID of the proxy. -<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/proxy-contracts.ts#proxy-4{ts:line-numbers} Now let's make some changes to our initial counter contract by adding an additional storage slot to track the number of increments and a new get method that retrieves its value: @@ -42,11 +42,11 @@ Now let's make some changes to our initial counter contract by adding an additio We can deploy it and update the target of the proxy like so: -<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/proxy-contracts.ts#proxy-6{ts:line-numbers} Then, we can instantiate our upgraded contract via the same proxy contract ID: -<<< @/../../docs-snippets2/src/cookbook/proxy-contracts.ts#proxy-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/proxy-contracts.ts#proxy-7{ts:line-numbers} For more info, please check these docs: