Skip to main content

Deploying smart contracts

Introduction​

In Tezos, deploying a smart contract is often referred to as “origination”. This process essentially creates a new account that holds the smart contract's script. Contracts originated in this manner have addresses that start with KT1, which distinguishes them from the user accounts with addresses beginning with tz prefixes, such as tz1, tz2, etc.

Prerequisites​

  • Compile your contract and its initial storage
  • Set up an wallet account on Tezos with some tez to pay the fees

Deploying a smart contract​

Generally, there are two methods for deploying your smart contracts: either using the command line in your terminal or deploying through an online IDE.

Deploying via terminal​

The first one is to deploy through your terminal. Here is the syntax for the Tezos command line to deploy a smart contract:

octez-client originate contract <CONTRACT_NAME> transferring <AMOUNT_TEZ> from <FROM_USER> \
running <MICHELSON_FILE> \
--init '<INITIAL_STORAGE>' --burn-cap <GAS_FEE>

where:

  • <CONTRACT_NAME> is the name given to the contract.
  • <MICHELSON_FILE> is the path for the Michelson smart contract code (.tz file).
  • <AMOUNT_TEZ> is the quantity of tez being transferred to the newly deployed contract. If a contract balance reaches 0 then it is deactivated.
  • <FROM_USER> account from which the tez are taken (and transferred to the new contract).
  • <INITIAL_STORAGE> is a Michelson expression. The --init parameter is used to specify the initial state of the storage.
  • <GAS_FEE> is a specified maximal fee the user is willing to pay for this operation (using the --burn-cap parameter).

Deploying via online IDE​

As for deploying through your online IDE, if you are using LIGO or SmartPy programming languages, you can deploy your smart contracts through their respective online IDEs.

Compiling the initial storage value​

When you deploy a contract, you initialize its storage. The initial value of the storage must be a Micheline value, which is the format for variables in Michelson smart contracts. The high-level languages provide tools to compile the initial values of smart contracts into Micheline values.

Compiling LIGO storage values​

For LIGO smart contracts, you can use the ligo compile storage command. For example, assume that a JsLIGO contract has a storage value that includes a list of integers, a string, and an integer:

namespace MyContract {
type storage = [
list<int>,
string,
int,
];

type returnValue = [list<operation>, storage];

@entry
const reset = (_p : unit, _s : storage) : returnValue =>
[list([]), [[],"",0]];
}```

When this contract is compiled to Michelson:

ligo compile contract MyContract.jsligo -o MyContract.tz


the storage line of the contract looks like this:

```michelson
storage (pair (list int) string int) ;

To compile an initial value to this format, you can pass a JsLIGO value to the ligo compile storage command, as in this example:

ligo compile storage -m MyContract MyContract.jsligo '[list([1,2,3,4]), "start", 0]'

The result is the Micheline value, as in this example:

(Pair { 1 ; 2 ; 3 ; 4 } "start" 0)

Then you can use this Micheline value as the initial storage value for the contract:

octez-client originate contract MyContract \
transferring 0 from my_account \
running MyContract.tz --init '(Pair { 1 ; 2 ; 3 ; 4 } "start" 0)' \
--burn-cap 1

Compiling SmartPy storage values​

SmartPy lets you set the initial value of the contract storage in the smart contract code in the __init__ function. For example, this contract defines three storage variables and sets their initial values:

import smartpy as sp

@sp.module
def main():
class MyList(sp.Contract):
def __init__(self):
self.data.ListOfIntegers = [1,2,3,4]
self.data.MyString = "hello"
self.data.MyInteger = 5

@sp.add_test()
def test():
scenario = sp.test_scenario("Example")
c1 = main.MyList()
scenario += c1

Now you can compile and deploy the contract via the online IDE with these starting values.

If you want to deploy the contract with the Octez client, run the test with the command python MyContract.py. This command creates a directory named Example/ (after the name passed to sp.test_scenario()) containing several files, including one that ends in _contract.tz, which contains the compiled Michelson contract, and one that ends in _storage.tz, which contains the Micheline value of the initial storage, as in this example:

(Pair {1; 2; 3; 4} (Pair 5 "hello"))

Then you can use this Micheline value as the initial storage value for the contract:

octez-client originate contract MyList \
transferring 0 from my_account \
running Example/step_001_cont_0_contract.tz --init '(Pair {1; 2; 3; 4} (Pair 5 "hello"))' \
--burn-cap 1

Interacting with the contract​

When you have successfully originated the smart contract and it is included in a baked block, there are two ways to interact with it: through command lines or through a block explorer.

Interacting through command lines​

The first method involves interacting with the contract's entry points using command lines.

For example, supposing you have deplyed the Counter contract in the Smart contract tutorial having an entrypoint called increment, which takes an integer as an argument to add to the number in its storage. Here's how you might invoke this entrypoint:

octez-client call <CONTRACT_NAME> from <YOUR_ACCOUNT_ADDRESS> \
--arg '<ARGUMENT>' \
--entrypoint increment \
--burn-cap <FEE_LIMIT>

Where:

  • <CONTRACT_NAME>: Identifier or the address of the contract that you want to interact with.
  • <YOUR_ACCOUNT_ADDRESS> Your own account address that will initiate the transaction.
  • <ARGUMENT>: Argument that you're passing to the entrypoint, in this case, an integer value. You need to format this according to the expected input in the contract's Michelson code.
  • increment: Entrypoint in the smart contract that you're calling.
  • <FEE_LIMIT>: The maximum fee you are willing to spend for this transaction to be included in the blockchain.

Here's an example with sample values filled in:

octez-client call counter from my_account \
--arg '42' \
--entrypoint increment \
--burn-cap 0.05

Where:

  • counter: Contract address alias.
  • my_account: User's account address.
  • '42': New integer value you wish to pass to the increment entrypoint.
  • 0.05: Maximum amount of tez you're willing to pay in fees.
note

Always ensure that you check the documentation specific to the smart contract you are interacting with, as the expected arguments (--arg) and the name of the entrypoint (--entrypoint) can vary widely depending on the contract's design and purpose.

Interacting via blockchain explorers​

A blockchain explorer is an efficient and user-friendly tool that enables you to interact with deployed contracts. In the Tezos ecosystem, there are two main blockchain explorers:

To interact with a contract, copy its address into one of these blockchain explorers. Below is the user interface for interacting with a contract through Better Call Dev:

UI for Better Call Dev