Skip to content

Commit

Permalink
docs: multiple clauses read (#910)
Browse files Browse the repository at this point in the history
* docs: multiple clauses read

* refactor: removing comment

* docs: replacing Vechain with VeChain

---------

Co-authored-by: Fabio Rigamonti <[email protected]>
  • Loading branch information
Valazan and fabiorigam authored May 24, 2024
1 parent 8241d93 commit 3c52e8d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
58 changes: 57 additions & 1 deletion docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Contract> => {
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',
Expand All @@ -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]);
```
18 changes: 18 additions & 0 deletions docs/examples/contracts/contract-create-ERC20-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions docs/examples/contracts/contract-delegation-ERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -64,8 +67,6 @@ const setupERC20Contract = async (): Promise<Contract> => {
// 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',
Expand Down
14 changes: 12 additions & 2 deletions docs/templates/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
[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)

0 comments on commit 3c52e8d

Please sign in to comment.