EOS smart contract part 1, function and table Oct2018

in #eos6 years ago (edited)

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>

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

你那里天气如何?请支持中文区的见证人, @abit @bobdos @ety001 @justyy @oflyhigh @partiko (首字母顺序),全投,一个都不要漏。如果不想再收到我的留言,请回复“取消”。

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by fundurian from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

wrote a similar contract, need to get to the next step thought. There is an example collection on github.
https://github.com/dannybabbev/eos-smart-contracts/tree/master/contracts

Hi @fundurian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.874 which ranks you at #4098 across all Steem accounts.
Your rank has improved 8 places in the last three days (old rank 4106).

In our last Algorithmic Curation Round, consisting of 391 contributions, your post is ranked at #252.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • The readers like your work!
  • Try to improve on your user engagement! The more interesting interaction in the comments of your post, the better!

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63402.76
ETH 2554.52
USDT 1.00
SBD 2.66