Interacting with the Tezos blockchain can be done using to the Tezos CLI. However, it is not suitable for Dapps since it needs to be integrated into web interfaces.
Fortunately, the Tezos ecosystem offers libraries in several languages that enable developers to build efficient Dapps. Taquito is one of these: it is a Typescript library developed and maintained by ECAD Labs. This library offers developers all of the everyday interactions with the blockchain: retrieving information about a Tezos network, sending a transaction, contract origination and interactions such as calling an entrypoint and fetching the storage, delegation, fetching metadata, etc.
A full reference is available here.
In this chapter we will use Taquito to interact with the deployed Raffle smart contract.
The Taquito library is made of several modules:
- @taquito/taquito: High-level functionalities that build upon the other packages in the Tezos Typescript Library Suite.
- @taquito/ledger-signer: Provides ledger signing functionality.
- @taquito/rpc: Provides low-level methods and types to invoke RPC calls from a Nomadic Tezos RPC node.
- @taquito/utils: Converts Michelson data and types into convenient JS/TS objects.
- @taquito/michel-codec: Michelson parser/validator/formatter.
- @taquito/local-forging: Provide local forging functionality.
- @taquito/signer: Provide signing functionality.
The main module is
@taquito/taquito, it will be used for most actions. The other modules are used by the
@taquito/taquito methods as complementary features.
Let's initialize a Typescript project and install taquito:
main.ts file will import an
App class from
src/app.ts and run its
Let's create the
App class with a
main method. We import the
TezosToolkit class to check if
@taquito/taquito is indeed installed:
Let's run it with:
If Taquito is correctly installed, this should not raise any exception.
We first need to configure Taquito with an RPC URL (to communicate with a Tezos node). To do that we use the
TezosToolkit: it is the "facade class that surfaces all of the libraries capability and allow its configuration". When created, it accepts an RPC URL. Here, we will use the Florence testnet RPC URL offered for free by smartpy.io at https://florencenet.smartpy.io/
Taquito is already ready for some actions: it can retrieve all the information about the Tezos network, the accounts, the smart contracts.
For instance, let's retrieve the balance of an account, with the
Let's call this method for the address:
Let's run it:
We can also retrieve the metadata and storage of a contract.
Let's run it for the simple
Counter contract on Florencenet.
The output is:
Taquito can also sign and send transactions, but it needs a private key to do that. Let's retrieve a faucet file from faucet.tzalpha.net/ and put it in the project directory.
Every implicit address must be activated on a public network. Let's activate ours on Florencenet.
First, we need to install the
@taquito/signer module, used to sign the transactions.
We will use the
InMemorySigner: it loads a private key in memory and uses it to sign transactions.
Storing private keys in memory is suitable for development workflows but risky for production use-cases! Use the
InMemorySignerappropriately given your risk profile.
First, we need to set the signer of our TezosToolkit using
setSignerProvider. The signer will load a private key from our faucet using the
We can now create an
activateAccount method that uses the signer to activate our address.
Let's call it from our
You can now see your activated account on an explorer (https://florence.tzstats.com/ for instance).
Now that Taquito is configured with an activated account, we can send transactions. Let's send some Tez to another address.
Transactions can be sent with
this.tezos.contract.transfer. It returns a
TransactionOperation contains the information about this transaction. It also has a
confirmation method. This method can wait for several confirmations on demand.
Let's create a
sendTz method that sends an
amount of Tez to the recipient
Let's call it from our
Let's run it from the console:
We can now check the transaction on an explorer: https://florence.tzstats.com/ooYGXazAECCMTehpfsPWo6JxavJs2a5KYP6a1i6eU5ofATWnHbH
Taquito can call smart contracts as well. We will use the Counter contract. If you need to know what are the available entrypoints, you can use the
getContractEntrypoints defined in the Contract data subsection.
Let's call the
increment entrypoint. It takes a single int as input.
To do so, we need:
- to get the contract with
this.tezos.contract.at(contract). It returns a
- get the entrypoints. For this
methodsproperty contraining the entrypoints
- get the increment entrypoint with
methods.increment(2)to increment the counter by
- send the contract call and inspect the transaction with
- wait for a chosen number of confirmations, let's say
Let's call it from our
send() function can take an object with fields as an input, such as
amount (which defines an amount sent with the contract call),
Let's consider this Dapp:
This is basically a concatenation of the Counter example and the Transfer example. Now, let's consider a use-case where we need to send these two transactions at the same time (and maybe additional contract calls, originations or transfer transactions). One could be tempted to make those calls one after the other like this:
We basically make a contract call then try to send some funds to an address. Here is the output:
The meaningful part is
Counter 334156 already used for contract tz1YWK1gDPQx9N1Jh4JnmVre7xN6xhGGM4uC. Each transaction in our Dapp is performed asynchronously: the application makes the contract call to the
increment entrypoint, but did not wait for the confirmation to made the transfer transaction. The contract call transaction was still in the mempool when the transfer transaction was sent. Thus, it failed.
However, Taquito offers a
batch method, which enables Dapps to send several transactions at once.
To do so, we need to:
- retrieve the contract that we want to call,
- call the batch method,
- send the transactions batch,
- wait for their confirmation.
Here is an example:
Our three transfer transactions and our contract call are now indeed batched together in an operation.
Taquito facilitates developers' interactions with the Tezos network. It can read data from a blockchain, send transactions, originate a contract, etc.
However, Dapps require the ability to manage keys. In our example, there was only a single key to manage. In production Dapps, each user will want to use a key that they owns. That is where wallets come into play. Most Tezos wallets are built upon Taquito and make Dapps more user-friendly and accessible. Let's take a deeper look at wallets in the next chapter.