Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: multiple clauses read #910

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,38 @@ Vechain allows for the delegation of contract calls, enabling developers to exec
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
12 changes: 11 additions & 1 deletion docs/templates/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ Vechain allows for the delegation of contract calls, enabling developers to exec

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

Valazan marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading