Sending transactions
After connecting to a wallet, dApps can call smart contract entrypoints and make transactions with that wallet.
These calls can include:
-
Sending tez to an account or smart contract
When a dApp sends tez, it removes the tez from the source account and adds it to the target account. When you send tez to a smart contract's address without calling an entrypoint, the smart contract behaves as though you called its
default
entrypoint. Some tools have a specific syntax for sending tez to a contract that is different from the syntax to call an entrypoint, so check your tool's documentation for how to send tez to a contract. -
Calling a smart contract entrypoint
When a dApp calls a smart contract, it passes an argument in Michelson format that includes the name of the entrypoint and the parameters to pass to it. Most tools compile this argument for you, so you can call the entrypoint and pass parameters as though you were calling a function. A call to a smart contract entrypoint always includes a transfer of tez, even if the amount is zero.
-
Originating a smart contract
Tools can originate a smart contract from source code.
For information about calling contracts from other contracts, see Operations.
Taquito
You can use the Taquito SDK to send transactions from JavaScript/TypeScript applications. For more information about Taquito, see Taquito.
Sending tez
To send tez with Taquito, connect to the user's wallet and use the Tezos.wallet.transfer
method, as in this example:
import { TezosToolkit } from "@taquito/taquito";
const Tezos = new TezosToolkit(rpcUrl);
Tezos.setWalletProvider(wallet);
await Tezos.wallet.transfer({
amount: sendAmount,
to: targetAccount,
})
.send()
.then((op) => {
console.log(`Waiting for ${op.opHash} to be confirmed...`);
return op.confirmation(2).then(() => op.opHash);
})
.catch((error) => console.log(`Error: ${JSON.stringify(error, null, 2)}`));
You can also use the Taquito Contract API to send tez in a similar way. For more information, see Transfers in the Taquito documentation.
Calling contracts
Taquito offers several different ways to send transactions from JavaScript/TypeScript code. One way is to create a Taquito object that represents the contract. That contract object contains a method that corresponds to each entrypoint in the contract.
For example, this code calls an entrypoint named "doSomething." It passes parameters in the order that the contract expects them:
import { TezosToolkit } from "@taquito/taquito";
const Tezos = new TezosToolkit(rpcUrl);
Tezos.setWalletProvider(wallet);
const contract = await Tezos.wallet.at(contractAddress);
try {
const op = await contract.methods.doSomething('Param 1', 25).send();
console.log(`Waiting for ${op.opHash} to be confirmed...`);
await op.confirmation(2);
} catch (error) {
console.log(`Error: ${JSON.stringify(error, null, 2)}`)
}
For examples of calling smart contracts, see tutorials such as Build your first app on Tezos or Create a contract and web app that mints NFTs.
For more information about using Taquito, see Smart contracts in the Taquito documentation.
Beacon
You can use the Beacon SDK to send transactions from JavaScript/TypeScript code.
Sending tez with Beacon
To send tez with Beacon, use the requestOperation
method, as in this example:
const response = await dAppClient.requestOperation({
operationDetails: [
{
kind: TezosOperationType.TRANSACTION,
destination: targetAddress, // Address of the target account
amount: sendAmount, // Amount to send in mutez
},
],
})
Calling contracts with Beacon
To call contracts with Beacon, use the requestOperation
method and pass the address of the contract, the entrypoint to call, and the parameters to include, as in this example:
import { TezosOperationType } from '@airgap/beacon-sdk'
const result = await dAppClient.requestOperation({
operationDetails: [
{
kind: TezosOperationType.TRANSACTION,
amount: '0',
destination: CONTRACT_ADDRESS,
parameters: {
entrypoint: 'mint',
value: {
int: 3,
},
},
},
],
})
Octez
The Octez command-line client can send tez and call contracts from the command line. See Interacting with contracts.