/eos-smart-contract-info

Shortcut to getting started with EOS Smart Contracts

Primary LanguageShell

Getting started with EOS Smart Contracts

There are two options that I'll go over for getting nodeos up and running.

  1. Docker (Recommended)
  2. Local Automated Build

Install Docker

Click here to install Docker

EOSIO Dev Docker image

Run the following commands:

  • docker pull eosio/eos-dev
  • sudo docker run --rm --name eosio -d -p 8888:8888 -p 9876:9876 -v /tmp/work:/work -v /tmp/eosio:/mnt/dev/data eosio/eos-dev /bin/bash -c "nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::wallet_plugin --plugin eosio::producer_plugin --plugin eosio::history_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --plugin eosio::http_plugin -d /mnt/dev/data --http-server-address=0.0.0.0:8888 --access-control-allow-origin=* --contracts-console"
  • To follow the blocks being produced run the following: sudo docker logs -f eosio

You can now visit http://localhost:8888/v1/chain/get_info and should JSON data describing the chain.

NODEOS

System requirements for building a local copy:

  1. Amazon 2017.09 and higher.
  2. Centos 7.
  3. Fedora 25 and higher (Fedora 27 recommended).
  4. Mint 18.
  5. Ubuntu 16.04 (Ubuntu 16.10 recommended).
  6. MacOS Darwin 10.12 and higher (MacOS 10.13.x recommended).
  • git clone https://github.com/EOSIO/eos --recursive
  • cd eos
  • ./eosio_build.sh
  • cd build
  • sudo make install
  • Start your local blockchain
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin 

CLEOS

Command-line tool for manging wallets and interacting with the blockchain

Wallets and Accounts

Create a wallet and save your master password: cleos wallet create

Create another wallet with a name and save your master password: cleos wallet create -n devcoffee

List all your wallets cleos wallet list

Lock a specific wallet cleos wallet lock -n devcoffee

Unlock a specific wallet cleos wallet unlock -n devcoffee --password [YOUR_MASTER_PASSWORD]

Create a private and public key (Make sure to save the keys somewhere secure) cleos create key

Import the private key into your wallet cleos wallet import -n devcoffee [YOUR_MASTER_PASSWORD]

Creating accounts

Create an account name to use for our smart contract cleos create account eosio user [PUBLIC_KEY]

List all of your accounts cleos get accounts [PUBLIC_KEY]

Create your first Smart Contract

Creating a smart contract is rather simple. The library eosio has a lot of functions that make things easy.

  • Command for creating your first smart contract: eosiocpp -n ping

  • cd into the ping directory: cd ping

  • Now open up the code with your favorite text editor

  • You should see the following boilerplate code

#include <eosiolib/eosio.hpp>

using namespace eosio;

class hello : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};

EOSIO_ABI( hello, (hi) )
  • Paste the following to ping.cpp
#include <eosiolib/eosio.hpp>

using namespace eosio;

class pingpong : public eosio::contract {
  public:
      using contract::contract;

      /// @abi action
      void ping( account_name user ) {
         print( "Pong, ", name{user} );
      }
};

EOSIO_ABI( pingpong, (ping) )
  • Change Hello to Pong for funz.
  • Create a new account for the smart contract (Make sure your wallet is unlocked) cleos create account eosio ping.code [PUBLIC_KEY]
  • Create a new file called compile.sh in the ping directory. Paste the following code in the file:
eosiocpp -o ping.wast ping.cpp
eosiocpp -g ping.abi ping.cpp
cleos set contract ping.code ../ping -p ping.code
  • Now run sh compile.sh. This will compile your smart contract and publish it to your local blockchain.
  • You can interact with your smart contract by running: cleos push action ping.code ping '["user"]' -p user
  • Congratulations! You just deployed and executed your first smart contract.

Helper scripts

I wrote two scripts to help with typing repetitive commands

  • compile.sh : Put this script in the root of any new smart contract directory that you create. Anytime you want to compile and publish the smart contract just run sh compile.sh or ./compile.sh. (Replace the ping.code variable with whatever account you want to develop on)
  • createWalletAccount.sh : This script will create a new wallet with a public/private keypair and it creates an associated account name as well. To run this script just type sh createWalletAccount.sh or ./createWalletAccount.sh.

Open to PRs

I'll be adding on to this repo in the coming weeks. If anyone wants to join me go a head and create a pull request and I'll be more than happy to review/merge it!