From baca88dabe9f25efeb68fce7ac6cd61ab353e883 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Wed, 21 Feb 2024 13:22:04 -0500 Subject: [PATCH] More info on calling entrypoints and encoding params (#295) * Example of calling FA2 transfer entrypoint * How to get the param schema * Video walkthrough --- docs/dApps/sending-transactions.md | 107 ++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 3 deletions(-) diff --git a/docs/dApps/sending-transactions.md b/docs/dApps/sending-transactions.md index fda2b0529..0ed227daa 100644 --- a/docs/dApps/sending-transactions.md +++ b/docs/dApps/sending-transactions.md @@ -2,7 +2,7 @@ title: Sending transactions authors: "Tim McMackin" last_update: - date: 7 November 2023 + date: 1 February 2024 --- @@ -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.