-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into st/chore/fix-ci-markdown-lint-check
- Loading branch information
Showing
39 changed files
with
1,498 additions
and
1,110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/recipes": patch | ||
--- | ||
|
||
docs: proxy contract cookbook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
apps/docs/src/guide/contracts/snippets/proxy-contracts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// #region proxy-2 | ||
import { | ||
Provider, | ||
Wallet, | ||
Src14OwnedProxy, | ||
Src14OwnedProxyFactory, | ||
} from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../env'; | ||
import { | ||
Counter, | ||
CounterFactory, | ||
CounterV2, | ||
CounterV2Factory, | ||
} from '../../../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
const counterContractFactory = new CounterFactory(wallet); | ||
const deploy = await counterContractFactory.deploy(); | ||
const { contract: counterContract } = await deploy.waitForResult(); | ||
// #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 = counterContractFactory.storageSlots.concat( | ||
Src14OwnedProxy.storageSlots | ||
); | ||
/** | ||
* These configurables are specific to our recommended SRC14 compliant | ||
* contract. They must be passed on deployment 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 Src14OwnedProxyFactory(wallet); | ||
const proxyDeploy = await proxyContractFactory.deploy({ | ||
storageSlots, | ||
configurableConstants, | ||
}); | ||
|
||
const { contract: proxyContract } = await proxyDeploy.waitForResult(); | ||
const { waitForResult } = await proxyContract.functions | ||
.initialize_proxy() | ||
.call(); | ||
|
||
await waitForResult(); | ||
// #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 proxiedContract = new Counter(proxyContract.id, wallet); | ||
|
||
const incrementCall = await proxiedContract.functions.increment_count(1).call(); | ||
await incrementCall.waitForResult(); | ||
|
||
const { value: count } = await proxiedContract.functions.get_count().get(); | ||
// #endregion proxy-4 | ||
|
||
console.log('count:', count.toNumber() === 1); | ||
|
||
// #region proxy-6 | ||
const deployV2 = await CounterV2Factory.deploy(wallet); | ||
const { contract: contractV2 } = await deployV2.waitForResult(); | ||
|
||
const updateTargetCall = await proxyContract.functions | ||
.set_proxy_target({ bits: contractV2.id.toB256() }) | ||
.call(); | ||
|
||
await updateTargetCall.waitForResult(); | ||
// #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 incrementCall2 = await upgradedContract.functions | ||
.increment_count(1) | ||
.call(); | ||
|
||
await incrementCall2.waitForResult(); | ||
|
||
const { value: increments } = await upgradedContract.functions | ||
.get_increments() | ||
.get(); | ||
|
||
const { value: count2 } = await upgradedContract.functions.get_count().get(); | ||
// #endregion proxy-7 | ||
|
||
console.log('secondCount', count2.toNumber() === 2); | ||
console.log('increments', increments); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "counter-v2" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_count(amount: u64) -> u64; | ||
|
||
#[storage(write, read)] | ||
fn decrement_count(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_count(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_count(amount: u64) -> u64 { | ||
let current = storage.counter.try_read().unwrap_or(0); | ||
storage.counter.write(current - amount); | ||
storage.counter.read() | ||
} | ||
} | ||
// #endregion proxy-5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { hexlify, randomBytes, Src14OwnedProxy, Src14OwnedProxyFactory } from 'fuels'; | ||
import { launchTestNode } from 'fuels/test-utils'; | ||
|
||
/** | ||
* @group node | ||
* @group browser | ||
*/ | ||
describe('recipes', () => { | ||
it('deploy and interact with Src14OwnedProxy', async () => { | ||
using launched = await launchTestNode(); | ||
|
||
const { | ||
wallets: [wallet], | ||
} = launched; | ||
|
||
const targetAddress = hexlify(randomBytes(32)); | ||
const configurableConstants = { | ||
INITIAL_TARGET: { bits: targetAddress }, | ||
INITIAL_OWNER: { Initialized: { Address: { bits: wallet.address.toB256() } } }, | ||
}; | ||
|
||
const proxyFactory = new Src14OwnedProxyFactory(wallet); | ||
const { waitForResult: waitForProxyDeploy } = await proxyFactory.deploy({ | ||
configurableConstants, | ||
}); | ||
const { contract: proxyContract } = await waitForProxyDeploy(); | ||
const { waitForResult: waitForProxyInit } = await proxyContract.functions | ||
.initialize_proxy() | ||
.call(); | ||
await waitForProxyInit(); | ||
const proxyAddress = proxyContract.id.toB256(); | ||
|
||
const { waitForResult: waitForFirstTarget } = await proxyContract.functions | ||
.proxy_target() | ||
.call(); | ||
const firstTarget = await waitForFirstTarget(); | ||
expect(firstTarget.value.bits).toEqual(targetAddress); | ||
|
||
const anotherProxy = new Src14OwnedProxy(proxyAddress, wallet); | ||
const { waitForResult: waitForAnotherTarget } = await anotherProxy.functions | ||
.proxy_target() | ||
.call(); | ||
const anotherTarget = await waitForAnotherTarget(); | ||
expect(anotherTarget.value.bits).toEqual(targetAddress); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.