paint-brush
Tezos smart contracts with ReasonML, Docker and a sandboxed nodeby@matej.sima
1,677 reads
1,677 reads

Tezos smart contracts with ReasonML, Docker and a sandboxed node

by Matej ŠimaJanuary 27th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

You’ll deploy a simple <a href="https://tezos.com/get-started/" target="_blank">Tezos</a> smart contract written in <a href="https://reasonml.github.io" target="_blank">ReasonML</a>, using <a href="https://github.com/maht0rz/tezos-environment-manager" target="_blank">tezos-environment-manager</a>. You’ll call the smart contract using <a href="https://www.npmjs.com/package/sotez" target="_blank">sotez</a> to verify the deployment. All of this will happen in a local, private and sandboxed Tezos <a href="https://hackernoon.com/tagged/blockchain" target="_blank">blockchain</a>.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - Tezos smart contracts with ReasonML, Docker and a sandboxed node
Matej Šima HackerNoon profile picture

This article will walk you trough using tezos-environment-manager to implement smart contracts in ReasonML.

https://tezos.com

What will you learn?

You’ll deploy a simple Tezos smart contract written in ReasonML, using tezos-environment-manager. You’ll call the smart contract using sotez to verify the deployment. All of this will happen in a local, private and sandboxed Tezos blockchain.

What is Tezos?

Tezos is a blockchain technology, or in other words — a platform that allows you to build decentralised applications by implementing a blockchain ledger and smart contract support. You can learn more here.

Setting up Tezos Environment Manager

Tezos Environment Manager (TEM), is a toolkit for building decentralised applications and smart contracts on top of Tezos blockchain. To start a project based off TEM, you have to clone it’s source repository first:

Make sure you have docker 🐳 installed & running before running any TEM commands. ⚠️ Don’t forget to cd tezos-environment-manager as well.

After we have cloned TEM successfully, we can begin using it by selecting an environment we want to develop our contract in.

Build our docker images next, please be patient as this command will compile all the necessary tools to make our setup work. It can take several minutes to build the images.

After the images are built, we want to get going by starting a private & sandboxed tezos-node.

If everything went as expected, we’ll now have a running tezos-node, operating in private / sandboxed mode, with RPC exposed at port 18731.

Interacting with sandboxed tezos-node

In our setup, we can access the node’s RPC interface at port 18731, which is pretty useful itself, but we don’t really want to write RPC calls ourself, libraries exist to do that for us. One of the ways to interact with the node is a tezos-client. You can launch an interactive shell, with tezos-client preconfigured, using the following command:

You can find an extensive manual on all supported tezos-client commands here

Let’s check if we have any accounts available to use (hint: we don’t)

Because this is a private sandboxed network, there really isn’t a faucet that’d give us an account with free cryptocurrency (tez), that we could use. Luckily TEM provides a command, to load up few accounts that exist on the sandboxed network and have tez that we can use.

After successful initialisation, we can try checking for known addresses again.

Hurray, we’ve got a bunch of accounts full of tez, that we can use to develop our application / smart contract!

The simple smart contract

Smart contracts are basically distributed back-end applications that are capable of storing information / executing transactions on the blockchain itself, there are costs associated with deploying and executing smart contract’s code. Let’s take a look at our very own smart contract:

Our language of choice is ReasonML, but native tezos smart contracts are written in Michelson. We’re using Liquidity, which takes our OCaml/ReasonML code and compiles it to Michelson.

ReasonML is a syntax for OCaml, it can be converted into OCaml and back pretty easily

What does our smart contract do? It stores an integer, initialized as 0. It specifies one %entry point called main. When called, it increments current storage by the amount (int) provided. For more information on how contract methods can be called, check here.

Goal of this tutorial, is purely to deploy our contract, so let’s get back on track.

In order to deploy our contract using Liquidity, we have to convert it to OCaml/Liqudity first. This is done using refmt, which comes as part of the tooling provided by reason-cli. Conversion can be done as following:

simple.liq is the converted simple.re smart contract

Deploying the smart contract

To deploy a smart contract, we have to forge an operation, coming from one of the known accounts / addresses we have configured in tezos-client. We have to provide a fee & initial contract’s tez amount too. Forging an operation will result in creating contract_deployment_op.bytes, that will store out op’s bytes that we need to sign.

Signing a forged operation can be done using tezos-client, and it has to be done by using the private key of an account specified as --source when the operation was forged.

We can use the above generated signature, to inject an operation into our running tezos-node

Now our operation has been injected, and we’ll have to wait until a new blockchain block is created/baked, in order to use our smart contract!

Verifying the deployment

To verify that our deployment was successful, we can check for a receipt.

Whoops! Our operation isn’t included in the blockchain yet. It is because in the sandboxed environment, we’re in charge of baking new blocks for ourselves as well, so let’s do that before we proceed.

Now we can check for if our operation has been already included in the blockchain:

Hurray! Our operation — meaning our smart contract deployment was indeed included in the newly baked block, cool huh?

Operation receipt includes one vital piece of information, which is the originated address of our smart contract, which we’ll use later on to get the current storage value.

Optional: Running the javascript/sotez example

Make sure to have a recent version of NodeJS installed in order to run this example

Within TEM’s repository there’s an _examples_ folder, which contains a script called _contract.js_.

In order to run it, exit your tezos-client shell by typing exit and pressing return.

Inside the examples directory, run npm install, update your contract address in contract.js (KT1…) and run the script with npm run contract.

Hurray, you’ve successfully deployed a smart contract, and retrieved it’s current storage.

Tutorial steps as a GIF