Decentralized App development on Tezos for beginners part 4

Let’s deploy our first Michelson contract on Tezos.

catsigma
2 min readMar 3, 2018

Updated @ 2018.03.03 (outdated, waiting for the formal release of zeronet)

Series parts

  1. Set up your own Tezos node and learn the basics concepts.
  2. Meet the Tezos smart contract language — Michelson.
  3. Use Liquidity to easily write Michelson contracts.
  4. Let’s deploy our first Michelson contract on Tezos.
  5. Build a website to interact with our Tezos contract.

----------------------------------------------------------------------------------------- — -

Last time, we designed an arithmetic calculation contract in Liquidity.

type method = 
| Addition
| Multiplication
| Subtraction
| Division
let%entry main
(parameter : nat * nat * method)
(storage : nat)
: nat * nat =
let (a, b, m) = parameter in
let result = match m with
| Addition -> a + b

| Multiplication -> a * b
| Subtraction ->
(match%nat a - b with | Plus x -> x | Minus _ -> 0p)
| Division ->
match a / b with
| None -> Current.fail ()
| Some x -> x.(0)
in
(result, result)

Now let’s deploy our contract.

1) Compile the Liquidity contract to Michelson contract.

./liquidity-mini demo.liq

Then we get a file named demo.liq.tz.

2) Originate this contract.

./alphanet.sh client originate contract my_demo1 for my_identity transferring 2.01 from my_account running container:demo.liq.tz -init '0'

Let’s break down this word by word. my_demo1 is an alias for the deployed contract, but the alias can only be used by the local Tezos client. 2.01 is the lowest amount for deploying a contract currently. container: means you are using a docker image version of alphanet. -init is the initial storage you want to set for the contract. '0' is the initial storage data. More data literals can be found here.

Once the contract is deployed. You can try to call it.

3) Call the contract(same as transfer to the contract).

./alphanet.sh client transfer 0 from my_account to my_demo1 -arg '(Pair 5 (Pair 8 (Right (Left Unit))))'

0 means that we don’t transfer XTZ to the contract, but still, it will run. -arg is the input argument you want to call with. (Pair 5 (Pair 8 (Right (Left Unit)))) directly fits the type predefined for the parameter in our demo contract. The type of

type method = 
| Addition
| Multiplication
| Subtraction
| Division

will be compiled to

(or
unit
(or
unit <- use (Right (Left Unit)) to fit this type
(or
unit
unit)))

4) Let’s check if the transaction is completed locally.

./alphanet.sh client get storage for my_demo1

It should print 40. By locally, I mean the transaction might haven’t been baked(like mining in Bitcoin) yet, so the command here only checks the result of how the transaction modified the local chain. If your node is syncing with the whole system, then several minutes later it will be baked into the alphanet.

-------------------------------------------------------------------------------------------------------

Next time, we’ll discuss how Tezos contracts work in real world and a demo website will be built.

Donation BTC: 1L7oCqy7GHx7EiQc9SPFputCWftfaoT3kB

View the next part…Build a website to interact with our Tezos contract.

--

--