1

I'm building a Smart Contract that transfers EOS from my contract account to end user accounts.

I can not seem to find up to date and correct syntax for transferring EOS using eosio.token from within my contract.

All the info I can find, generally, references something like:


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);
}

It is using old syntax and does not appear to function as intended when I try and mold this to my contract.

2 Answers 2

1

Have you tried to replace the from - field with your accounts/contracts name? If you use _self this should be possible.

Checkout this answer from @Jeff Zheng.

If you merge both codes and update macros you get something like this (please correct the code if needed, I didn't compile it):

void Mycontract::yourcontract_action(account_name to,
                                      asset quantity,
                                      string memo)
{
    // maybe add some require_auth here
    eosio::transaction txn{};

    // where quantity should be something like this
    // asset quantity = asset( 10000, symbol_type(S(4, EOS)) ); // equals to 1 EOS

    txn.actions.emplace_back(
        permission_level{_self, "active"_n}, "eosio.token"_n, "transfer"_n, // replaced from
        std::make_tuple(_self, to, quantity, memo)); // replaced 'from'
    txn.send(eosio::string_to_name(memo.c_str()), _self);
}

And take care of the eosio.code permission like mentioned in the other answer.

0

You could use the following syntax as well:

transfer_tokens(buyer, seller, asset(tokensToTransfer, symbol("ABC", 4)), "transfer tokens to seller");
void market::transfer_tokens(name from, name to, asset quantity, string memo)
{
    token::transfer_action transfer("eosio.token"_n, {from, "active"_n});
    transfer.send(from, to, quantity, memo);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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