Skip to content

Commit

Permalink
More info on calling entrypoints and encoding params (#295)
Browse files Browse the repository at this point in the history
* Example of calling FA2 transfer entrypoint

* How to get the param schema

* Video walkthrough
  • Loading branch information
timothymcmackin authored Feb 21, 2024
1 parent 3c99614 commit baca88d
Showing 1 changed file with 104 additions and 3 deletions.
107 changes: 104 additions & 3 deletions docs/dApps/sending-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Sending transactions
authors: "Tim McMackin"
last_update:
date: 7 November 2023
date: 1 February 2024
---
<!-- TODO originating contracts: https://tezostaquito.io/docs/originate -->

Expand Down Expand Up @@ -78,14 +78,115 @@ try {
console.log(`Waiting for ${op.opHash} to be confirmed...`);
await op.confirmation(2);
} catch (error) {
console.log(`Error: ${JSON.stringify(error, null, 2)}`)
console.log(`Error: ${JSON.stringify(error, null, 2)}`);
}
```

For examples of calling smart contracts, see tutorials such as [Build a simple web application](../tutorials/build-your-first-app) or [Create a contract and web app that mints NFTs](../tutorials/create-an-nft/nft-taquito).
To call an entrypoint that accepts parameters, you must encode those parameters in the format that the entrypoint requires.

To see the format for these parameters, create a Taquito object that represents the contract and extract its parameter schema, as in the following example:

```javascript
const contract = await Tezos.wallet.at(contractAddress);
const parameterSchema = contract.parameterSchema;
console.log(parameterSchema.ExtractSignatures());
```

The response shows the entrypoints in the contract and the parameters that they accept.

For example, the [FA2](../architecture/tokens/FA2) `transfer` entrypoint appears like this:

```json
[
"transfer",
{
"list": {
"from_": "address",
"txs": {
"list": {
"to_": "address",
"token_id": "nat",
"amount": "nat"
}
}
}
}
]
```

This `transfer` entrypoint accepts an array of token transfers.
Each transfer object includes the address to take the tokens from and an array of accounts to send the tokens to, as in this example:


```javascript
const transactionParams = [
{
from_: sourceAddress,
txs: [
{
to_: targetAddress1,
token_id: 7,
amount: 2,
},
{
to_: targetAddress2,
token_id: 7,
amount: 3,
},
],
},
];
```

To call the `transfer` entrypoint, pass this parameter to the Taquito entrypoint method, as in this example:

```javascript
Tezos.setWalletProvider(wallet);
const contract = await Tezos.wallet.at(contractAddress);

const transactionParams = [
{
from_: sourceAddress,
txs: [
{
to_: targetAddress1,
token_id: 7,
amount: 2,
},
{
to_: targetAddress2,
token_id: 7,
amount: 3,
},
],
},
];

const estimation = await Tezos.estimate.transfer({
to: contractAddress,
amount: 0,
parameter: contract.methods.transfer(transactionParams).toTransferParams().parameter
});

const operation = await contract.methods
.transfer(transactionParams, estimation)
.send();

console.log(`Waiting for ${operation.opHash} to be confirmed...`);

await operation.confirmation(2);

console.log(
`Operation injected: https://ghost.tzstats.com/${operation.opHash}`,
);
```

For more examples of calling smart contracts, see tutorials such as [Build a simple web application](../tutorials/build-your-first-app) or [Create a contract and web app that mints NFTs](../tutorials/create-an-nft/nft-taquito).

For more information about using Taquito, see [Smart contracts](https://tezostaquito.io/docs/smartcontracts) in the Taquito documentation.

For a video walkthrough, see [Interacting with FA2 Contracts Using Taquito](https://www.youtube.com/watch?v=xL6jyW1sqmA).

## Beacon

You can use the Beacon SDK to send transactions from JavaScript/TypeScript code.
Expand Down

0 comments on commit baca88d

Please sign in to comment.