I want to implement, in my smart contract(C++), a function that forwards EOS token to another account. This is actually a basic question, but is that possible actually? Please, could you share the code? Found nothing on the official reference: https://developers.eos.io/eosio-cpp/reference#transactions.

  • You want to transfer token from on account to another or you want assign EOS token to your account? – Nirdesh Kumar Choudhary Sep 25 at 5:14

First of all you need to provide the permission of eosio.code. After that you can use the transfer action of eosio.token contract.

before assigning tokens see if both .abi and .wast file are there, if they are not you need to generate .abi and .wast file of eosio.token

that can be done like this:

# eosiocpp -o ./contracts/eosio.token/eosio.token.wast ./contracts/eosio.token/eosio.token.cpp
# eosiocpp -g ./contracts/eosio.token/eosio.token.abi ./contracts/eosio.token/eosio.token.cpp

after that set you eosio.token contract(check you file paths carefully )

cleos set contract eosio.token ./contracts/eosio.token/

now our eosio.token is set

First of all create some tokens , that can be done like this:

cleos push action eosio.token create '{"issuer":"eosio.token","maximum_supply":"1000000.0000 EOS","can_freeze":"0","can_recall":"0","can_whitelist":"0"}' -p eosio.token

Second assign tokens to eosio.token and to account you want like this:

cleos push action eosio.token issue '{"to":"eosio.token","quantity":"2000.0000 EOS","memo":"first issued"}' -p eosio.token

cleos push action eosio.token issue '{"to":"your_account","quantity":"1000.0000 EOS","memo":"first issued"}' -p eosio.token

Transfer using cleos :

cleos push action eosio.token transfer '{"from":"your_account","to":"your_friend_account","quantity":"20.0000 EOS","memo":"my first transfer"}' -p your_account

Transfer within contract:

void Mycontract::yourcontract_action(account_name from,
                                      account_name to,
                                      asset quantity,
                                      string memo)
{
    eosio::transaction txn{};
    txn.actions.emplace_back(
        eosio::permission_level(from, N(active)),
        N(eosio.token),
        N(transfer),
        std::make_tuple(from, to, quantity, memo));
    txn.send(eosio::string_to_name(memo.c_str()), from);
}

Use the following code in your smart contract to initiate a transfer of EOS tokens.

asset quantity = asset( 10000, symbol_type(S(4, EOS)) ); // equals to 1 EOS

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

Also note that you will need grant eosio.code to your active permission.

cleos set account permission <your_account> active '{"threshold" : 1, "keys" : [{"key": "<your_public_key>", "weight": 1}], "accounts" : [{"permission":{"actor":"<your_account>","permission":"eosio.code"},"weight":1}], "waits":[]}}' owner -p <your_account>@owner

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.