I am looking for an alternative to the payable
function of Ethereum in EOSIO. I want to invoke an action when EOS is sent to the contract. Is it possible?
2 Answers
Yes, compared to Ethereum, rather than having payable
you would set up your contract to listen to an action, in particular transfer
from eosio.token
.
The listener will reference a specific action to execute in your contract you would use to define what logic/code should happen.
In the EOSIO world, it's better practice to minimise the amount of logic done in a transfer listener. One technique might be to maintain a balance of how much EOS a user has deposited. Then, use a completely different action for the user to engage in order to buy something from the contract etc.
Another catch is that the transfer listener cannot charge the sender RAM, so if you want to create a record in a multi-index container your contract must pay for it.
Using the better practice method, you would have one action which bills the user to create the record, then the transfer listener which credits the sending account 1.0000 EOS
. Then another action which bills the user taking away his 1.0000 EOS
balance.
A simple example of an eosio.token
transfer
comes generated for you in the https://github.com/MrToph/generator-eos project.