create account

EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS) by eos-asia

View this thread on: hive.blogpeakd.comecency.com
· @eos-asia · (edited)
$2.89
EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)
![example](https://github.com/tylerdiaz/ping-eos/raw/master/example.gif)

**Info:** This post is the first in a series of posts made to help EOS smart contracts developers go from beginner-to-production. If you have any suggestions of topics you’d like to a deep-dive on, please leave it in the comments! For the full code of this tutorial, you can [visit the repository](https://github.com/tylerdiaz/ping-eos). Now, onto the program.

With all the excitement around EOS, an area that remains challenging for most developers looking to become involved is getting started with smart contracts. There are generally two hurdles for new developers to overcome: getting your tooling setup, and knowing how to write the smart contract itself.

EOS smart contracts are written in C++ and compile into Web Assembly. [Dan Larimer](https://steemit.com/eos/@dan/eos-example-exchange-contract-and-benefits-of-c) chose C++ to take advantage of its type and templating system which makes for safer contracts, and adds that because smart-contracts have short runtimes, most of the memory concerns fall away.

## Setting up
Part of the challenge in working with EOS is setting up the local blockchain to work against. Luckily, EOS offers some facilities for [setting up your local EOS environment].(https://github.com/EOSIO/eos/wiki/Local-Environment#getting-the-code) For this guide, we’ll be using `EOSIO Dawn 3.0`.

A summary of that guide can be condensed into a few key commands:
```bash
$ git clone https://github.com/EOSIO/eos --recursive
$ cd eos
$ ./eosio_build.sh
$ cd build && make install
$ cd programs/nodeos
$ ./nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin --access-control-allow-origin=*
```

The installation will take some time, but is straightforward. Once you have a local EOS blockchain up and running locally, you’re ready to start. Throughout the guide, we’ll reference some utilities like  `cleos` and `eosiocpp`. You can find these inside your `eos/programs` folder.

## Building a ping smart contract
For this tutorial we’ll create and deploy the “Hello World” of distributed systems: ping/pong. For the uninitiated, we will send the server a  “ping” command, and it will respond with “pong”. A smart contract is composed of a few things: C++ code, an ABI (Application Binary Interface), and a WAST (Web Assembly Text file) based on the C++ code. Here’s what that looks like:
![photo_2018-04-25_12-46-24.jpg](https://steemitimages.com/DQmbMSJ59gSCdwRBwwhCiumngTJCBUjWLEPPQopa54wd9ee/photo_2018-04-25_12-46-24.jpg)

### Implementing ping
After we’ve got our tooling environment setup, let’s get into the contract! To write the ping smart contract, we only need a contract that implements one action: `ping`. All this method needs to do is print “Pong” in return. 

Create a folder called `ping` inside `contracts`, and a file `ping/ping.cpp`:
```c++
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

class ping_contract : public eosio::contract {
  public:
      using eosio::contract::contract;
      void ping(account_name receiver) {
         eosio::print("Pong");
      }
};

EOSIO_ABI( ping_contract, (ping) )
```

The idea here is to have a simple and small example you can test with to become more familiar with the moving pieces. Let’s break down what’s going on here:
1. We include some definitions to write our contract.
2. We create a new contract class that inherits from the [eos::contract](https://github.com/EOSIO/eos/blob/8425ff88a7f712c7df46b979de0e6e7de512f569/contracts/eosiolib/contract.hpp).
3. We create a method that prints “Pong”
4. The last line is a [macro that was recently added](https://github.com/EOSIO/eos/pull/2051), it saves us the effort of maintaining our own hand-written ABIs by generating one based on the methods we pass into the second parameter.

### Building your contract
The EOS block producers don’t run C++ code when executing smart contracts, they expect web-assembly. EOS offers a tool called `eosiocpp` for converting your C++ code to Web Assembly Text. Let’s do that now with 
`eosiocpp -o ping.wast ping.cpp`. This step will generate some warnings, but we can disregard those for now.

Next, we need the Application Binary Interface. Essentially, the ABI for your smart contract that will describe the methods and their corresponding signatures. Since we added the `EOSIO_ABI` macro at the end of our file, instead of writing one by hand, we can simply generate it with: `eosiocpp -g ping.abi ping.cpp`

At this point, your folder should look like:
```
├── ping.abi
├── ping.cpp
└── ping.wast
```

## Deploying to your local network
Now that we have all the materials necessary for our smart contract, let’s get to deploying it. Make sure you’ve got a wallet created `cleos wallet create` and ensure it’s unlocked by running `cleos wallet unlock` and typing your wallet password if prompted. We’ll deploy our smart contract under another account.

For this, we’ll need to create a new key-pair, let's do that by running: `cleos create key`. This will generate a random public and private key for you. Throughout the rest of the tutorial, be sure to replace any indications of [public_key]/[private_key] with the values you've just received.

Import the private key to your current unlocked account wallet: `cleos wallet import [private_key]`
Set up an account for the contract with the public key: `cleos create account eosio ping.ctr [owner_key: public_key] [active_key: public_key]`
- Link the contract with your newly created account `cleos set contract ping.ctr ../ping -p ping.ctr`

## Interacting with ping
Once your new contract is deployed, it’s time to interact with it! Let's create a tester account with the same keys to run the transaction: `cleos create account eosio tester [public_key] [public_key]`

Now we can test it out on the command line:
```bash
$ cleos push action ping.ctr ping '["tester"]' -p tester
executed transaction: e89ebeaad8f3623e42d966f62d4d0adbf6a7412b6bb4d7be61f04a22d3cd485e  232 bytes  102400 cycles
#  ping.ctr <= ping.ctr::ping           {"account":"tester"}
>> Received ping
```

It works!

This is exciting for us programmers, but most of your users won’t setup their command line to interact with your smart contract. So let’s bring this interaction to an interface they’re more familiar with: their browser. 

### Interacting through the browser
To interact with EOS from the frontend, we’ll use  [EOS.js](https://github.com/EOSIO/eosjs). Since we’re using dawn3 on our EOS backed, we need to make sure to use the `dawn3` branch when installing: `npm install eosjs@dawn3`.

We start off by configuring:
```javascript
Eos = require('eosjs')

eos = EOS.Localnet({
  keyProvider: ['{replace_with_your_private_key}'],
  httpEndpoint: 'http://127.0.0.1:8888'
})
```

Once configured, we have to specify a few details:
```javascript
eos.contract('ping.ctr').then((contract) => {
  contract.ping("tester", { authorization: ['tester'] }).then((res) => {
    console.log(res)
  })
})
```

Notice `ping.ctr`, which matches the name of the contract we deployed earlier. Once we fetch the contract interface (or ABI) we can interact with it as though it were native Javascript. The contract returns a promise with the transaction details included in the resolve function. This is the core idea of interacting with our ping smart contract from the frontend. 

To see this in a larger context with a working example, check out the [frontend code in this repository](https://github.com/tylerdiaz/ping-eos/tree/master/frontend).


Follow us on: 
[Twitter](https://twitter.com/EOSAsia_one)
[Medium](https://medium.com/@eosasia)
[Website](https://www.eosasia.one/)
[Github](https://github.com/eos-asia)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoreos-asia
permlinkeos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos
categorydevs
json_metadata{"tags":["devs","eos","blockchain","developers","dapp"],"image":["https://github.com/tylerdiaz/ping-eos/raw/master/example.gif","https://steemitimages.com/DQmbMSJ59gSCdwRBwwhCiumngTJCBUjWLEPPQopa54wd9ee/photo_2018-04-25_12-46-24.jpg"],"links":["https://github.com/tylerdiaz/ping-eos","https://steemit.com/eos/@dan/eos-example-exchange-contract-and-benefits-of-c","https://github.com/EOSIO/eos/wiki/Local-Environment#getting-the-code","https://github.com/EOSIO/eos/blob/8425ff88a7f712c7df46b979de0e6e7de512f569/contracts/eosiolib/contract.hpp","https://github.com/EOSIO/eos/pull/2051","https://github.com/EOSIO/eosjs","https://github.com/tylerdiaz/ping-eos/tree/master/frontend","https://twitter.com/EOSAsia_one","https://medium.com/@eosasia","https://www.eosasia.one/","https://github.com/eos-asia"],"app":"steemit/0.1","format":"markdown"}
created2018-04-25 08:40:24
last_update2018-04-26 23:26:27
depth0
children11
last_payout2018-05-02 08:40:24
cashout_time1969-12-31 23:59:59
total_payout_value2.198 HBD
curator_payout_value0.690 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,782
author_reputation205,922,811,942
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id52,023,991
net_rshares447,405,847,085
author_curate_reward""
vote details (31)
@aclarkuk82 ·
Fantastic post, great work.  Keep this kind of thing coming
👍  
properties (23)
authoraclarkuk82
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180506t234126800z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-05-06 23:41:27
last_update2018-05-06 23:41:27
depth1
children0
last_payout2018-05-13 23:41:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length59
author_reputation901,192,097,946
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,279,395
net_rshares451,635,879
author_curate_reward""
vote details (1)
@aclarkuk82 ·
Also, I would love to see a working solution for "authenticating" users against the blockchain rather than entering a user/password.  Not sure if that is possible just yet but I thought I would ask.
👍  
properties (23)
authoraclarkuk82
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180506t234556994z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-05-06 23:45:57
last_update2018-05-06 23:45:57
depth1
children0
last_payout2018-05-13 23:45:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length198
author_reputation901,192,097,946
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,279,854
net_rshares442,418,820
author_curate_reward""
vote details (1)
@csx-eos ·
Thank you for sharing this.
👍  
properties (23)
authorcsx-eos
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180510t171517566z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-05-10 17:15:21
last_update2018-05-10 17:15:21
depth1
children0
last_payout2018-05-17 17:15:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation539,320,185,055
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,992,204
net_rshares239,433,876
author_curate_reward""
vote details (1)
@cutemachine ·
Just read this article / video about [EOS](https://busy.org/@julianhosp/eos-simply-explained-too-late-to-buy). I thought you might be interested. Cheers.
properties (22)
authorcutemachine
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180513t184428292z
categorydevs
json_metadata{"tags":["devs"],"community":"busy","app":"busy/2.4.0"}
created2018-05-13 18:44:30
last_update2018-05-13 18:44:30
depth1
children0
last_payout2018-05-20 18:44:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length153
author_reputation13,229,471,185,839
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id55,506,990
net_rshares0
@dreambaked ·
really helpful for getting my head round the front end integration, thanks!
👍  
properties (23)
authordreambaked
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180426t102642479z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-04-26 10:26:42
last_update2018-04-26 10:26:42
depth1
children0
last_payout2018-05-03 10:26:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length75
author_reputation1,800,691,307,585
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id52,234,072
net_rshares0
author_curate_reward""
vote details (1)
@eosgo · (edited)
Thoroughly enjoyed this post, thank you for helping beginners interact with the most powerful blockchain project available. It is very satisfying seeing the ping!
properties (22)
authoreosgo
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180426t051241938z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-04-26 05:12:45
last_update2018-04-26 05:13:30
depth1
children0
last_payout2018-05-03 05:12:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length162
author_reputation19,975,856,950,324
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id52,192,807
net_rshares0
@hhezi ·
really awesome tutorial! thanks for sharing, and post more like this
properties (22)
authorhhezi
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180709t205150242z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-07-09 20:51:48
last_update2018-07-09 20:51:48
depth1
children1
last_payout2018-07-16 20:51:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length68
author_reputation104,477,872
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,078,123
net_rshares0
@redris ·
$0.05
Did you get it to work? I am stuck on "cleos push action ping.ctr ping '["tester"]' -p tester" because I never get the "recieved ping" message
👍  
properties (23)
authorredris
permlinkre-hhezi-re-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180720t215922628z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-07-20 21:59:24
last_update2018-07-20 21:59:24
depth2
children0
last_payout2018-07-27 21:59:24
cashout_time1969-12-31 23:59:59
total_payout_value0.038 HBD
curator_payout_value0.011 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length142
author_reputation51,310,500,722
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,411,956
net_rshares24,486,513,678
author_curate_reward""
vote details (1)
@interwebcoding ·
change:
 __eosio::account_history_api_plugin__  -->  __eosio::history_api_plugin__
properties (22)
authorinterwebcoding
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180523t142602430z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-05-23 14:26:03
last_update2018-05-23 14:26:03
depth1
children0
last_payout2018-05-30 14:26:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length82
author_reputation837,467,726
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id57,275,467
net_rshares0
@redris ·
$0.05
Are you returning a "pong" or "Received ping" message in the console? The ping.cpp file does not match the ping.cpp file in the tutorial.
👍  
properties (23)
authorredris
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180720t220737302z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-07-20 22:07:39
last_update2018-07-20 22:07:39
depth1
children0
last_payout2018-07-27 22:07:39
cashout_time1969-12-31 23:59:59
total_payout_value0.052 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length137
author_reputation51,310,500,722
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,412,528
net_rshares24,986,238,447
author_curate_reward""
vote details (1)
@redris ·
$0.05
Why is "ping.ctr" written twice here?..."cleos set contract ping.ctr ../ping -p ping.ctr"
👍  
properties (23)
authorredris
permlinkre-eos-asia-eos-smart-contracts-part-1-getting-started-ping-equivalent-in-eos-20180721t152246713z
categorydevs
json_metadata{"tags":["devs"],"app":"steemit/0.1"}
created2018-07-21 15:22:51
last_update2018-07-21 15:22:51
depth1
children0
last_payout2018-07-28 15:22:51
cashout_time1969-12-31 23:59:59
total_payout_value0.052 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length89
author_reputation51,310,500,722
root_title"EOS smart contracts, Part 1: Getting started (Ping equivalent in EOS)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,487,577
net_rshares24,986,238,447
author_curate_reward""
vote details (1)