Originating (Deploying) Contracts
Taquito can originate (create or deploy) Smart Contracts to the Tezos Blockchain.
Example demonstrating origination of a contract
In this example, we will originate the popular multi-sig contract available here.
Since version 6.3.2, Taquito allows encoding and decoding between "plain" Michelson and JSON Michelson. Smart Contracts' origination is now more straightforward than it was because it is no longer required to do the tezos-client command-line to convert & expand "plain" Michelson to JSON Michelson. You can now pass JSON Michelson and "plain" Michelson using the
code
parameter of theoriginate
method.
Originate the contract using Taquito
Here are three examples of originating a contract using Taquito. The first example initializes the storage of the contract using a familiar-looking javascript object. The second and third demonstrates the use of plain Michelson and JSON Michelson. The first method is preferred, but if you have a reason to circumvent the convenient storage API, you can do so.
We will show these three examples using the Contract API
and the Wallet API.
The new Taquito Wallet API interacts with wallets, supporting Beacon, the TZIP-10 standard.
Note: To run the
Wallet API
examples, you can install a wallet extension to your browser. For example, the Beacon Extension can be download here.
- Contract API
- Wallet API
This requires a signer to be configured, ie:
import { importKey } from '@taquito/signer';import { TezosToolkit } from '@taquito/taquito';const Tezos = new TezosToolkit('https://ghostnet.ecadinfra.com');importKey(Tezos, "p2sk2obfVMEuPUnadAConLWk7Tf4Dt3n4svSgJwrgpamRqJXvaYcg1")
import { BeaconWallet } from '@taquito/beacon-wallet';import { TezosToolkit } from '@taquito/taquito';const Tezos = new TezosToolkit('https://ghostnet.ecadinfra.com');const option = { name: "nameOfWallet", network: { type: 'ghostnet' }, enableMetrics: true};const wallet = new BeaconWallet(option);await wallet.client.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET,async (account) => {// An active account has been set, update the dApp UIconsole.log(`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `, account);},await wallet.requestPermissions();Tezos.setWalletProvider(wallet);
a. Initializing storage using a Plain Old JavaScript Object
You can pass your initial storage as a JavaScript object to the storage:
property. Taquito will encode your JavaScript object into a Michelson expression.
This JavaScript object :
{ stored_counter: 0,threshold: 1,keys: ['edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t'] }
Is equivalent to this Michelson expression :
(Pair 0 (Pair 1 { "edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t" }))
As you can see, the property names are discarded. The order of your properties is crucial!
- Contract API
- Wallet API
b. Initializing storage using a plain Michelson Expression for initial storage
When using the Michelson expression for initial storage, we need to use the init
parameter instead of the storage
object.
- Contract API
- Wallet API
c. Initializing storage using a JSON encoded Michelson Expression for initial storage
- Contract API
- Wallet API
Originate multiple contracts using Taquito
It is also possible to use Taquito to originate multiple contracts in one operation. The origination operations must be batched with the Batch API and after the contracts have been originated, the addresses will be available in an array returned by the getOriginatedContractAddresses
method of the operation object:
const batch = Tezos.contract.batch().withOrigination({balance: '1',code: CODE_1,storage: 0,}).withOrigination({balance: '2',code: CODE_2,storage: 0,});const op = await batch.send();await op.confirmation();const addresses = op.getOriginatedContractAddresses();