2

I am working on a DAPP which requires users to pay a small amount of EOS in order to participate in the DAPP.

In ethereum, every transaction has msg.value which specifies the amount of ether in the request. Is there a similar functionality in EOS as well?

Can the users send EOS token while executing an action? I know that I can define my own apply function and listen to transfer actions but I am actually looking for a way so that the users can send token while executing an action.

2

In EOS, actions don't have tokens associated with them. The tokens are handled through token contracts, for example, the "real" EOS token is managed by the eosio.token contract account.

If you want to allow users to pay for your dapp, they need to call the eosio.token::transfer action. What you need to do is to listen to the eosio.token::transfer action in your smart contract. The transfer action has four fields: (account_name from, account_name to, asset quantity, string memo). How much EOS was sent will be available through quantity.amount.

The code to listen to users sending tokens to your smart contract will look like this:

void on_transfer(const currency::transfer &transfer)
{
    // verify that this is an incoming transfer
    if (transfer.to != N(yourcontract))
        return;

    eosio_assert(transfer.quantity.symbol == S(4, EOS), "must pay with EOS token");
    // transfer.quantity.amount stores the amount
    eosio_assert(transfer.quantity.amount > 0, "deposit amount must be positive");
}

extern "C" void apply(uint64_t receiver, uint64_t code, uint64_t action)
{
    if (code == N(eosio.token) && action == N(transfer))
    {
        on_transfer(unpack_action_data<currency::transfer>());
        return;
    }
}

I am actually looking for a way so that the users can send token while executing an action.

This is not possible, the only way to transfer tokens is through the mentioned transfer action. This is because the token balance for a user is implemented as a table (accounts) in the token contract, and this action is the only one that modifies it. What you could do however is triggering this action on your user's behalf whenever he calls an action on your account, but this requires that the user authorizes your contract to do that.

| improve this answer | |

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.