Could you please enlighten me on how approve and allowance functions work using a usecase? I dont understand the underlying operations of these functions. Especially, when a token owner delegates the right to spend his token by other user: how the ownership sign-off works, is this be done automatically?
1 Answer
The original use case from back 2016 is that back in a day, smart contracts had no way to interact to incoming token transfer. (This has been now fixed with ERC-777 token standard and send()
).
For you to interact with a smart contract with an ERC-20 token
You need to
approve()
amount of tokens on a smart contract addressYou call a smart function that then in a turn calls
token.transferFrom()
function to take over the tokens from you and do something with them
allowance()
is simply a function to check what's the current approve()
limit.
-
Thanks Mikko! This means, the contract that I approve would use my address to sign-off tokens which require signing-off using my private key right? Does the signing-off happen seamlessly?– Muni V KCommented May 20, 2020 at 17:27
-
After the
approve()
has been done the smart contract is free to use the tokens and one can consider "the posession of" tokens to be on the smart contract.approve()
is the only transaction that depends on your private key. Smart contract can control the tokens until you cancel toapprove()
by setting it to zero. Commented May 21, 2020 at 10:53