EOS smart contract - From helloworld to hellomoney

in #development6 years ago (edited)

EOS smart contract - From helloworld to hellomoney

Today, we're going to show you how to transfer token inside the EOS smart contract.

Create source code

Create helloworld contract

$ eosiocpp -n helloworld
created helloworld from skeleton

Here is the source code of helloworld.cpp

#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) )

Add header file

#include <eosiolib/asset.hpp>

Add amount to hi() function

void hi( account_name to, const asset& quantity ) {

Add some security check

        require_auth( _self );
        eosio_assert( quantity.is_valid(), "invalid token" );
        eosio_assert( quantity.amount > 0, "must be positive quantity" );

        require_recipient( _self );
        require_recipient( to );

Add in contract transaction

        action(
            permission_level{ _self, N(active) },
            N(eosio.token), N(transfer),
            std::make_tuple(_self, to, quantity, std::string("hello money"))
        ).send();

Then hi() function become

    /// @abi action hi
    void hi( account_name to, const asset& quantity ) {
        require_auth( _self );
        eosio_assert( quantity.is_valid(), "invalid token" );
        eosio_assert( quantity.amount > 0, "must be positive quantity" );

        require_recipient( _self );
        require_recipient( to );

        action(
            permission_level{ _self, N(active) },
            N(eosio.token), N(transfer),
            std::make_tuple(_self, to, quantity, std::string("hello money"))
        ).send();
        print( "Hello, here is some money for ", name{to} );
    }

Run it with jungle testnet

Buy ram

cleos -u http://jungle.cryptolions.io:18888 system buyram -k eosonetest2 eosonetest2 1000

Push contract to account

cleos -u http://jungle.cryptolions.io:18888 set contract eosonetest2 ../helloworld -p eosonetest2

Set eosio.code permission

cleos -u http://jungle.cryptolions.io:18888 set account permission eosonetest2 active '{"threshold": 1,"keys": [{"key": "EOS8xxxxxxxxxxxxxxx","weight": 1}],"accounts": [{"permission":{"actor":"eosonetest2","permission":"eosio.code"},"weight":1}]}' owner -p eosonetest2@owner

Test output on testnet

Before

@joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest1 EOS
7970.3411 EOS
@joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest2 EOS
6733.2414 EOS

Send hello money

@joel-Lenovo-Y520:helloworld $ cleos -u http://jungle.cryptolions.io:18888 push action eosonetest2 hi '["eosonetest1", "103.0000 EOS"]' -p eosonetest2
executed transaction: 71fxxxxxxxxxab706f2xxx9c15678d51f7xxx  120 bytes  2483 us
#   eosonetest2 <= eosonetest2::hi              {"to":"eosonetest1","quantity":"103.0000 EOS"}
>> Hello, here is some money for eosonetest1
#   eosonetest1 <= eosonetest2::hi              {"to":"eosonetest1","quantity":"103.0000 EOS"}
#   eosio.token <= eosio.token::transfer        {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"}
#   eosonetest2 <= eosio.token::transfer        {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"}
#   eosonetest1 <= eosio.token::transfer        {"from":"eosonetest2","to":"eosonetest1","quantity":"103.0000 EOS","memo":"hello money"}
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

After

@joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest1 EOS
8073.3411 EOS
@joel-Lenovo-Y520:helloworld $ cleos -u https://jungle.eosio.cr:443 get currency balance eosio.token eosonetest2 EOS
6630.2414 EOS

Sort:  

吃了吗?新人吗?《steemit指南》拿一份吧,以免迷路; 另外一定要去 @team-cn 的新手村看看,超级热闹的大家庭。如果不想再收到我的留言,请回复“取消”。

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 69692.63
ETH 3775.19
USDT 1.00
SBD 3.76