EOS smart contract part 3 sending transaction from smart contract

Words
273
Reading
2 min
Listen
Play
8y


Hello steemit friends so this is part 3 of EOS smart contract where i attempt to send money from smart contract to another account example let say i want to send 0.0003 EOS from helloworld11 smart contract to account fundurianbbb, should be simple right?

not really, because it is not as straight forward as you think

First, you need to give eosio.code permission to yourself, with the following syntax

cleos set account permission helloworld11 active '{"threshold": 1,"keys": [{"key": "EOS8cCC4QN56dX873qyrBjzCPHvQDjqhSzuiwSffgRXYusHggiuUQ","weight": 1}],"accounts": [{"permission":{"actor":"helloworld11","permission":"eosio.code"},"weight":1}]}' owner -p helloworld11

this mean that you want to allow helloworld11 to execute the eosio.code to transfer money from this account "helloworld11" to any other account

then you can use below syntax in your smart contract to send money

action(permission_level{ _self, N(active) },
             N(eosio.token), N(transfer),
              std::make_tuple(_self, user, tax, mymemo5)
        ).send();

What this mean is you want to send money from _self (which is helloworld11) to user , amount = tax and with your own memo

full function code look like below

 [[eosio::action]]
        void sendeos(const account_name user,const asset &quantity)
       {
        std::string mymemo5 = "auto send you money";
        asset tax(3, S(4, EOS));
       action(permission_level{ _self, N(active) },
               N(eosio.token), N(transfer),
               std::make_tuple(_self, user, tax, mymemo5)
        ).send();
        }

so you can see that mymemo5 is string hardcoded with value "auto send you money" while tax is an asset

When i put asset tax(3, S(4, EOS));

it mean tax = 0.0003 EOS

because symbol = EOS, with 4 decimal point

so after compiling the code you can call this smart contract function with below syntax

cleos push action helloworld11 sendeos '{"user":"fundurianbbb","quantity":"0.0001 EOS"}' -p fundurianaaa

(this will send 0.0003 EOS from "helloworld11" to account "fundurianbbb")

and if you use below syntax

cleos push action helloworld11 sendeos '{"user":"fundurianaaa","quantity":"0.0001 EOS"}' -p fundurianaaa

(this will send 0.0003 EOS from "helloworld11" to account "fundurianaaa")

below is some syntax to help you along the way
if you want to convert string to account name you can do so in following syntax

std::string mymemo1  = "fundurianaaa";
account_name myname = eosio::string_to_name(mymemo1.c_str());

if you want to convert string to integer you can do so in following syntax

  int haha = atoi(mystring.c_str());

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/18/eos-smart-contract-part-3-sending-transaction-from-smart-contract/ </em><hr/></center>

EOS smart contract part 3 sending transaction from smart contract | Ecency