James Bachini

ReasonLIGO | Building Smart Contracts & Dapps On Tezos

ReasonLigo Tezos

Tezos smart contracts are compiled from a custom low level language called Michelson. For web developers however learning a new low level programming language is a barrier to entry… Enter LIGO and specifically ReasonLigo.

ReasonLIGO  is the most accessible way to develop smart contracts for web developers. ReasonLIGO is based on ReasonML which is based on Typescript which is based on Javascript. It’s a bit convoluted but basically if you can read Javascript you’ll probably understand ReasonLIGO and be able to create smart contracts. Let’s take a look at a simple ReasonLIGO smart contract.

So this contract is storing a number and providing two functions to increase the number or decrease it.

If you are coming from a Javascript background the main difference is that this is strongly typed. That means that when you define a variable you have to describe the type such as string/integar/object etc.

You’ll notice the big arrow function main is similar in construction to a modern ES6 Javascript function and this will be the main entry point when invoking or using the contract after it has been deployed.

Deploying A ReasonLIGO Contract

Let’s try deploying this contract to the babylon testnet using the LIGO playground IDE.

https://ide.ligolang.org/

Open up this link and select ReasonLIGO from the list of options. This is a development playground where you can test, compile, deploy and evaluate smart contracts.

Select Deploy > Run and your smart contract will be deployed to the blockchain.

Deploying a ReasonLIGO Smart Contract To Tezos Testnet

Next step is getting this working using your own credentials inside a dapp.

First job is to get some credentials and testnet tokens.

https://faucet.tzalpha.net/

If that isn’t working just Google “Tezos testnet faucet”.

You should get a JSON credentials set. Lets run some Javascript to initiate the account and deploy a smart contract.

I’m going to be doing this in Node.js but you can also run it in a HTML page. See code further down for examples.

In this example we use an external library called ConseilJS to interact with a public Tezos node. We use this to initiate our test account using the credentials from the faucet and then deploy a very simple smart contract.

Full docs for ConseilJS are here: https://cryptonomic.github.io/ConseilJS/#/

Let’s compile the contract using the IDE. Select Compile and then tick the “Output Michelson as JSON” box, then click Run. Copy and paste the result into the script above between the backticks at “const contract = “;”

Now lets run the script:

npm install conseiljs

node conseilTest.js

In the output you should get an operation hash which can be entered on the block explorer here: https://better-call.dev/

Remember to change the network from mainnet to babylon testnet.

You’ve now successfully created a smart contract in ReasonLIGO, compiled it to a Michelson JSON file and then deployed it in a Javascript application.

Next job is to invoke or use the smart contract.

Enter the contract address from the block explorer in the first line. This increments the stored number by 5. We need the tezosNode and keystore (generated from initaccount()) data a well from the previous code.

Each time we call the above code we are interacting with our smart contract. Any functions we have as part of the “main” entry point in ReasonLIGO can be called in this way.

ReasonLIGO A Beginners Guide

Here are some code snippets which should help with getting started developing smart contracts with ReasonLIGO

Conclusion

ReasonLIGO is a coding language which can be used to develop Tezos blockchain applications. It’s Javascript-esque format make it a good choice for developers coming from a web dev background.


Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.


Posted

in

, , ,

by