Shankar Nathan

@shankqr

How to deploy and run a Smart Contract on the EOS Blockchain from zero to hero!

December 23rd 2018

We are going to write a simple smart contract and deploy it to EOS.

More and more dapp’s are now being built on EOS, and at the heart of every dapp is the smart contract: programs that run on the blockchain. When I was learning the ropes around EOS the best guide available to me was the EOS official developer portal: https://developers.eos.io/, however, after compiling the smart contract locally I had limited resources to reference for guidance on deployment on a live environment. Due to this setback, i was challenged to doing more research and findings and in the process, Eureka!, I found several efficient means in getting things done. Hence, I decided to write this guide for everyone beginning their journey into blockchain development and EOS.

Prerequisites:

  1. Install binaries: https://developers.eos.io/eosio-home/docs/setting-up-your-environment
  2. Install the Contract Development Toolkit: https://developers.eos.io/eosio-home/docs/installing-the-contract-development-toolkit

EOS Smart Contract

Let’s take a look at a simple smart contract for EOS.

This is a simple contract written in C++ and it’s comprised as a single file named hello.cpp. Usually for a smart contract there would be 2 files: the header file .hpp file extension and the main file .cpp. The header file is usually where you declare all the public variables and methods of the contract that can be accessed from the outside, however for the sake of simplicity let’s just work with a simple contract that just consist of one main .cpp file.

A glance on the code shows only one method in the class named hi (methods are called actions in EOS by the way). We want to be able to call this method on the blockchain so we put the [[eosio::action]] tag above it. It’s input is the user and all it does is prints out that input when called upon. Go ahead and fire up VS Code (or your favourite code editor), type the code in and save it as hello.cpp

EOS Compiler

Now that we have written our simple contract let’s compile it. EOS compiler compiles your source code (.cpp and .hpp) to web assembly format (.wasm). It also has the option to generate an ABI file (.abi) for your contract. Now you can think of the wasm file as an executable program and the abi file as a description of how to talk to that program.

Fire up your terminal and navigate to your directory that you saved the hello.cpp file. Type the above command to compile and generate the abi as well. You should now see 3 files in your directory.

2 files generated, hello.abi and hello.wasm

EOS Account

We now have the files required to deploy our simple smart contract which are the hello.wasm and hello.abi but to be able to deploy it we will require an account on EOS. I am going to show you how to deploy it on a EOS Testnet but the steps required to deploy it on the Mainnet is similar. The first step now is to create an account on a Testnet, we are going to be using the CryptoKylin Testnet. If you go to their site: https://www.cryptokylin.io, you will find a link on creating an account, but you will be required to download and setup Scatter wallet. I have found a faster way to create an account, open your browser and head to: https://dev4eos.com.

Dev4eos is a cool IDE to run EOS code

Click on the TestNet button on the top right, select an Endpoint with the fastest speed. Write down this endpoint url somewhere, we will be using it through out this guide. Now enter a 12 character account name under Create Account, I my case I entered “youraccname1”. Click on Create Account button and you will get a similar JSON string returned.

Save this string somewhere, because we are going to need the keys to deploy our smart contract latter. Now click on the Faucet button to get free EOS Tokens (yes like free beer).

To confirm your account has been created and credited with the free tokens:

If you would like to deploy a smart contract on the Mainnet then you would need to create an account there. I created my account using https://eos-account-creator.com

Now that you have setup an account, I will work you through the fundamentals of EOS account.

An EOS account is actually a set of private and public key. When we created the account earlier we were given 2 sets of public-private keys. These 2 sets are the Owner and Active keys. The practise is to keep your Owner key far and safe somewhere, and use your Active key to do all the transactions on the blockchain. If you accidentally loose your Active key, you can then use your Owner key to take back control of your account. For the remaining of this guide we will be using your Active key only.

EOS Wallet

cleos is your best friend for EOS

We will be using the wallet that comes with the EOS binaries which is accessible by the command line tool cleos. Open Terminal and type the first line bellow:

We have just created a wallet called kylin1, please safe this password so that we can unlock this wallet in the future. After creating this wallet, it has been locked automatically so let’s unlock it. Enter the password when prompted.

Remember we are now in possession of the Active and Owner keys after creating an account in the previous section. Open up the JSON I asked you to save earlier. Try to locate your “active_key” and look for the private field inside it’s brackets that looks like this:{“private”:“5Hup7GxQ8aNE4HhfLUt4Rb7SN5E7yx2onrJYsg4DFG2qZ3gntwa”}.

Okay done! Give yourself a big pat on your back. We have created a wallet called kylin1 and imported our Active private key into it.

NOTE: We do not need to import the public key because it will be autogenerated from the private key.

Deploying a EOS Smart Contract

Time to deploy! Bring out the guns and the tanks.

We have all the requirements to deploy our smart contract:

  1. Compiled .wasm
  2. .abi
  3. Testnet account with sufficient funds
  4. Wallet with Testnet account key

Unlock you wallet once more and enter the password once prompted. Now type in the command on the first line bellow, replace ‘/Users/shankar/contracts/hello’ with the path to your hello.wasm directory.

Oh no! why wont it deploy my contract? well if you read the Error Details you will see that your account has insufficient ram. This is how EOS works, you need ram to deploy. Let’s go buy ourself some ram..

Ok now we have enough ram, so let’s try to deploy again.

Success! Remember to replace ‘/Users/shankar/contracts/hello’ with the path to your hello.wasm directory. To get your path, you can cd into your directory and type pwd on terminal to print out your full path.

If you are getting this error please keep on trying, and also check that the path to your wasm file is correct.

Executing our EOS Smart Contract

Now that we have our baby deployed and alive on the blockchain, let’s go ahead and execute the hi method on our smart contract.

You will now see that your hi method has been executed and a transaction hash is returned. Hurray! we made it. I am so proud of you for accomplishing your first step towards the future of decentralisation. We are the soldiers building the infrastructure and applications step by step, bit by bit that will power a fully decentralised economy thus unlocking tremendous prosperity to all people on earth.

NOTE: To deploy and run your smart contract on the Mainnet simply replace our testnet url ( https://api.kylin.alohaeos.com:443 ) with any of the api endpoint of the main 21 block producers. https://www.eosdocs.io/resources/apiendpoints/

This is my first time writing on Medium and I hope you enjoyed this. Maybe next time I will write on how to connect React or Vue frontend to a smart contract.

What’s Next

Now that you have a simple Smart Contract running on EOS, how about we connect it to a simple Front End. I have just finished writing about this, click here to view the article.

Follow my on my LinkedIn!

More by Shankar Nathan

More Related Stories