Skip to content

Commit

Permalink
Topic on calling contracts from dApps (#139)
Browse files Browse the repository at this point in the history
* Topic on calling contracts from dApps

* Don't need to escape URLs anymore

* restore this note about default behavior

* Taquito first, then Beacon

* Link to octez docs

* Other clarifications and tweaks; save origination for later

* Format for updated date

* Clarify this note and rework intro section

* You can also use the Taquito Contract API to send tez in a similar way.

* This note is not needed

* Simpler flow of code with awaits
  • Loading branch information
timothymcmackin authored Nov 16, 2023
1 parent 5d0b7d2 commit 2806df8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 6 deletions.
136 changes: 136 additions & 0 deletions docs/dApps/sending-transactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: Sending transactions
authors: "Tim McMackin"
last_update:
date: 7 November 2023
---
<!-- TODO originating contracts: https://tezostaquito.io/docs/originate -->

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](../smart-contracts/logic/operations).

## Taquito

You can use the Taquito SDK to send transactions from JavaScript/TypeScript applications.
For more information about Taquito, see [Taquito](./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:

```typescript
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](https://tezostaquito.io/docs/making_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:

```javascript
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](../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.

## 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:

```javascript
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:

```javascript
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](../developing/octez-client/transactions).
6 changes: 0 additions & 6 deletions docs/smart-contracts/logic/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ These operations can include:
Only the first type is technically a transaction, but the terms "operation" and "transaction" are often used interchangeably in courses, documentation, and tools.
Don't worry too much about the difference.

:::note Sending tez
Sending tez to an address is just a special case of calling a smart contract (via the `default` entrypoint).
However, some languages have a specific syntax for simply sending tez to a contract that is different from the syntax to call an entrypoint.
A call to a smart contract always includes a transfer of a certain amount of tez, even if that amount is zero.
:::

## Order of execution

The code of a contract never directly executes an operation or a transfer of tez.
Expand Down
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const sidebars = {
'dApps/samples',
// 'dApps/creating', //TODO
'dApps/wallets',
'dApps/sending-transactions',
'dApps/taquito',
'dApps/unity',
// 'dApps/frameworks', // TODO
Expand Down

0 comments on commit 2806df8

Please sign in to comment.