0

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.

2

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
2

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);
}
  • 1
    Note that the N macro was replaced, so more current code will look like this: permission_level{user, "active"_n}, "eosio.token"_n, "transfer"_n – qwertyboy Jan 23 '19 at 12:10
  • on CDT 1.6.3 this fails to compile: "error: no member named 'string_to_name' in namespace 'eosio'" -- how is this done now? – ekkis Dec 14 '19 at 2:00
  • and... the first parameter to .send() is supposed to be the ID of sender, so why is the value of the memo field being used here?? – ekkis Dec 14 '19 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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