diff --git a/docs/contracts.md b/docs/contracts.md index 4956f9dcd..4873b24b1 100644 --- a/docs/contracts.md +++ b/docs/contracts.md @@ -97,11 +97,43 @@ This document, designed to be both informative and practical, equips developers ### Overview -Vechain allows for the delegation of contract calls, enabling developers to execute contract functions in which the fees are payed by the delegator. +VeChain allows for the delegation of contract calls, enabling developers to execute contract functions in which the fees are payed by the delegator. Here is an example of how to delegate a contract call: ```typescript { name=contract-delegation-erc20, category=example } +const thorSoloClient = ThorClient.fromUrl(_soloUrl); +const provider = new VechainProvider( + thorSoloClient, + new ProviderInternalBaseWallet([deployerAccount], { + delegator: { + delegatorPrivateKey: delegatorAccount.privateKey + } + }), + true +); +const signer = (await provider.getSigner( + deployerAccount.address +)) as VechainSigner; + +// Defining a function for deploying the ERC20 contract +const setupERC20Contract = async (): Promise => { + const contractFactory = thorSoloClient.contracts.createContractFactory( + VIP180_ABI, + erc20ContractBytecode, + signer + ); + + // Deploying the contract + await contractFactory.startDeployment(); + + // Waiting for the contract to be deployed + return await contractFactory.waitForDeployment(); +}; + +// Setting up the ERC20 contract and getting its address +const contract = await setupERC20Contract(); + // Transferring 10000 tokens to another address with a delegated transaction const transferResult = await contract.transact.transfer( '0x9e7911de289c3c856ce7f421034f66b6cde49c39', @@ -115,3 +147,27 @@ const transactionReceiptTransfer = // Asserting that the transaction has not been reverted expect(transactionReceiptTransfer.reverted).toEqual(false); ``` + +## Multi-Clause Contract Interaction + +### Multiple clauses read + +VeChain supports the execution of multiple clauses in a single transaction, allowing developers to interact with multiple contracts or perform multiple operations within a single transaction. + +Here is an example of how to interact with multiple read clauses in a single transaction: + +```typescript { name=contract-create-erc20-token, category=example } +// Reading data from multiple clauses in a single call +const multipleClausesResult = + await thorSoloClient.contracts.executeMultipleClausesCall([ + contract.clause.totalSupply(), + contract.clause.name(), + contract.clause.symbol(), + contract.clause.decimals() + ]); + +expect(multipleClausesResult[0]).toEqual([unitsUtils.parseUnits('1', 24)]); +expect(multipleClausesResult[1]).toEqual(['SampleToken']); +expect(multipleClausesResult[2]).toEqual(['ST']); +expect(multipleClausesResult[3]).toEqual([18n]); +``` diff --git a/docs/examples/contracts/contract-create-ERC20-token.ts b/docs/examples/contracts/contract-create-ERC20-token.ts index 3b0ac16f2..854c31b0b 100644 --- a/docs/examples/contracts/contract-create-ERC20-token.ts +++ b/docs/examples/contracts/contract-create-ERC20-token.ts @@ -59,3 +59,21 @@ const balance = await contract.read.balanceOf(deployerAccount.address); expect(balance).toEqual([unitsUtils.parseUnits('1', 24)]); // END_SNIPPET: CreateERC20TokenSnippet + +// START_SNIPPET: ERC20MultiClausesReadSnippet + +// Reading data from multiple clauses in a single call +const multipleClausesResult = + await thorSoloClient.contracts.executeMultipleClausesCall([ + contract.clause.totalSupply(), + contract.clause.name(), + contract.clause.symbol(), + contract.clause.decimals() + ]); + +expect(multipleClausesResult[0]).toEqual([unitsUtils.parseUnits('1', 24)]); +expect(multipleClausesResult[1]).toEqual(['SampleToken']); +expect(multipleClausesResult[2]).toEqual(['ST']); +expect(multipleClausesResult[3]).toEqual([18n]); + +// END_SNIPPET: ERC20MultiClausesReadSnippet diff --git a/docs/examples/contracts/contract-delegation-ERC20.ts b/docs/examples/contracts/contract-delegation-ERC20.ts index 6907c1a46..cab337ed5 100644 --- a/docs/examples/contracts/contract-delegation-ERC20.ts +++ b/docs/examples/contracts/contract-delegation-ERC20.ts @@ -32,6 +32,9 @@ const delegatorAccount = { // Create thor client for solo network const _soloUrl = 'http://localhost:8669/'; + +// START_SNIPPET: ERC20FunctionCallDelegatedSnippet + const thorSoloClient = ThorClient.fromUrl(_soloUrl); const provider = new VechainProvider( thorSoloClient, @@ -64,8 +67,6 @@ const setupERC20Contract = async (): Promise => { // Setting up the ERC20 contract and getting its address const contract = await setupERC20Contract(); -// START_SNIPPET: ERC20FunctionCallDelegatedSnippet - // Transferring 10000 tokens to another address with a delegated transaction const transferResult = await contract.transact.transfer( '0x9e7911de289c3c856ce7f421034f66b6cde49c39', diff --git a/docs/templates/contracts.md b/docs/templates/contracts.md index 39c0ae078..31fffd37e 100644 --- a/docs/templates/contracts.md +++ b/docs/templates/contracts.md @@ -48,8 +48,18 @@ This document, designed to be both informative and practical, equips developers ### Overview -Vechain allows for the delegation of contract calls, enabling developers to execute contract functions in which the fees are payed by the delegator. +VeChain allows for the delegation of contract calls, enabling developers to execute contract functions in which the fees are payed by the delegator. Here is an example of how to delegate a contract call: -[ERC20FunctionCallDelegatedSnippet](examples/contracts/contract-delegation-ERC20.ts) \ No newline at end of file +[ERC20FunctionCallDelegatedSnippet](examples/contracts/contract-delegation-ERC20.ts) + +## Multi-Clause Contract Interaction + +### Multiple clauses read + +VeChain supports the execution of multiple clauses in a single transaction, allowing developers to interact with multiple contracts or perform multiple operations within a single transaction. + +Here is an example of how to interact with multiple read clauses in a single transaction: + +[ERC20MultiClausesReadSnippet](examples/contracts/contract-create-ERC20-token.ts) \ No newline at end of file