Skip to content

Commit

Permalink
chore: migrate to v2 snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Oct 17, 2024
1 parent 8f9c67c commit 38a125f
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 120 deletions.
105 changes: 0 additions & 105 deletions apps/docs-snippets/src/guide/contracts/proxy-contracts.test.ts

This file was deleted.

92 changes: 92 additions & 0 deletions apps/docs-snippets2/src/cookbook/proxy-contracts.ts
Original file line number Diff line number Diff line change
@@ -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());
2 changes: 1 addition & 1 deletion apps/docs-snippets2/sway/Forc.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["counter", "script-sum"]
members = ["counter", "counter-v2", "proxy", "script-sum"]
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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()
}
Expand Down
16 changes: 14 additions & 2 deletions apps/docs-snippets2/sway/counter/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// #region proxy-1
contract;

abi Counter {
Expand All @@ -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 {
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Fuel Labs <contact@fuel.sh>"]
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" }
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions apps/docs/src/guide/contracts/proxy-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ 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:

<<< @/../../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/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:

Expand Down

0 comments on commit 38a125f

Please sign in to comment.