EOS smart contract part 1, function and table Oct2018

fundurian(69)
Published in
#eos
Words
266
Reading
2 min
Listen
Play
8y

Screen Shot 2018-10-16 at 11.08.42 AM.png
Hello Steemit friends, all the EOS tutorial out there is outdated because EOS is evolving very fast for example, last time you can compile using

eosiocpp -o hello.wast hello.cpp

eosiocpp -g hello.abi hello.cpp

now it doesnt work anymore because there is a new compiler called eosio.cdt, so you have to install the new compiler via

git clone --recursive https://github.com/eosio/eosio.cdt

then build it via

./build.sh

finall install it via

./install.sh

after you done the installation you can compile your file using the new syntax

eosio-cpp -o hello.wasm hello.cpp --abigen

Let us build our first contract with 1 function and 1 table

hello.cpp

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;

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

      [[eosio::action]]
      void hi( account_name user ) {
         print( "Hello,STUPID ", name{user} );
      }

  private:


      struct [[eosio::table]] mystruct
      {
         uint64_t     key;
         uint64_t     secondid;
         std::string  name;
         std::string  account;

         uint64_t primary_key() const { return key; } // getter for primary key
         uint64_t by_id() const {return secondid; } // getter for additional key
      };

        typedef eosio::multi_index<N(mystruct), mystruct> datastore;



};

EOSIO_ABI( hello, (hi) )

Noticed on top of the function you must put [[eosio::action]] and on top of the table you must put [[eosio::table]], otherwise the compiler will not add your function and table to the ABI file, the old tutorial will not put both of this and you cannot find both when you try to run your smart contract

now that you have the code save inside hello.cpp, you can compile it using code below

eosio-cpp -o hello.wasm hello.cpp --abigen

then you can deploy to any EOS account you want, example i deploy this contract to my EOS account "helloworld11")

cleos set contract helloworld11 ../hello -p helloworld11@active

once the deploy is successful, you can play with the contract calling the function via

cleos push action helloworld11 hi '{"user":"fundurianaaa"}' -p fundurianaaa

(it will output

> Hello,STUPID fundurianaaa

)

if the print is not showing you need to go ~/.local/share/eosio/nodeos/config/config.ini

put contracts-console = true

reboot your node via
pkill nodeos

restart your node via

nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin

finally you can also query your blockchain table via below code

cleos get table helloworld11 helloworld11 mystruct

thats all for today, thanks for reading

  <br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : http://fundurian.vornix.blog/2018/10/16/eos-smart-contract-part-1/ </em><hr/></center>

EOS smart contract part 1, function and table Oct2018 | Ecency