EOS.IO takes smart contracts to the next level, operating as a smart contract platform. In this article we will learn its basics.
Running our private node
In order to deploy our first “Hello world!” smart contract we are going to need at least one EOS node running. We will communicate with this node via CLI and provide the smart contract to be deployed. Please note that this requires to have EOS installed. You can see how to do that here.Start your node
You can start your node using this single command:$ nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
This will show an output as the following
Cleos and wallet creation
cleos command is our friend. This CLI application allows us to communicate with our node, create wallets, ask for balance, transfer money, and so much more.In order set a contract to an account, we need to have the required permissions. Since in this example we are going to be using the eosio account (the EOS’s root account), we already have these permissions. But we do need to sign the transaction, and to do this, we need eosio’s private key. To use this private key we need to ensure we have it in our wallet, and for this, my friends… we need a wallet.
$ cleos wallet create
The above command creates our default wallet, the standard output then showing its password. Save it! You will need this password in order to unlock the wallet.
Importing keys
You can think of a wallet as a keyring, it keeps all of our keys organized in one place. So, this is why we create it, to keep our eosio’s private key to sign our transactions validating that we have the required permissions. Now, we import the eosio’s private key.$ cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
The one above is the public domain key. If you have changed the eosio’s public key, replace it with the corresponding private key.
Hello World!
Having the required key in our wallet, we can now focus on our smart contract code. For the sake of simplicity, we will write a simple Hello World smart contract.Let’s create a hello.cpp file with the following content:
1 #include <  eosiolib/eosio.hpp >
2 using namespace eosio;
3
4 class hello : public eosio::contract {
5    public:
6    using contract::contract;
7
8    /// @abi action
9    void hi() {
10      print( "Hello world!");
11   }
12 };
13
14 EOSIO_ABI( hello, (hi) )
Code analysis
1 #include
Is the header for EOS’s smart contracts.
4 class hello : public eosio::contract { Declares hello class as a eosio::contract derived.
8 /// @abi action
9 void hi() {
Declares hi() function as part of the contract’s ABI.
10 print( "Hello World!");
Prints Hello World! on calling hi.
14 EOSIO_ABI( hello, (hi) ) It is the ABI declaration of our smart contract, so it must include all the actions (class methods) that we want to potentially get called on it. It is the json to binary (and reverse) bridge. This macro also calls apply() on the contract.
Compiling
The next step is to compile the contract. For this we will use the EOS’s eosiocpp script with the following parameters:$ eosiocpp -o hello.wast hello.cpp
This will compile our C++ code into WebAssembly, resulting on a hello.wast file.
And then
$ eosiocpp -g hello.abi hello.cpp
Which will create a hello.abi file with the ABI declaration of our contract.
Both file are required to deploy the Hello Word! smart contract.
Deploying the smart contract
Finally, the last step is to deploy our smart contract to the blockchain. The command to do this has the following format:cleos set contract
So we have:
$ cleos set contract eosio . hello.wast hello.abi
Verifying
We can verify that our smart contract is responding to us by calling on this:$ cleos push action eosio hi ’’ -p eosio
Here we send an action to eosio, the user we have previously deployed the contract with, calling the action hi without parameters. Then we sign on with our private key (-p eosio), used earlier in one of our previous steps.
Summary
Creating a smart contract for EOS is easy. You must have a wallet, and have imported the user-key signing the transaction. Then just write the smart contract, compile and deploy it to the blockchain.And that’s all, folks!
No comments:
Post a Comment