ApproveAndCall function for ERC20
Due to one of the known problems of ERC20 EventHandling standard, there is no token receiving processing function in the token contract, smart contract will not be able to record how many, when and from whom tokens were received. This leads to the irretrievable loss of a huge amount of money.
Since it was not possible to send tokens directly to contracts, it was necessary to do it in 2 stages: Approve and TransferFrom. Approve – means to approve a certain number of tokens for withdrawal to a certain wallet. TransferFrom – means to transfer tokens approved for withdrawal.
This leads to the following set of actions:
1) Tokens holder initiates Approve of his tokens to smart contract’s address.
2) Tokens holder initiates a function in the contract, within which TransferFrom is performed to the contract address and the received tokens are successfully processed by the contract.
As you can see, everything is quite complicated and is carried out only in two transactions. The introduction of function ApproveAndCall into tokens ERC20 became a solution to smart contracts operation. The additional function ApproveAndCall allows you to simplify the above procedure and do it by only 1 transaction: initiation of the function ApproveAndCall inside which both Approve tokens and transferFrom will occur.
Example of ApproveAndCall implementing in code:
… As you can see, the contract must have the receiveApproval function for sending tokens to a smart contract. The transaction is made in one stage: initiation of the ApproveAndCall function in the token contract with the smart contract’s address where the funds are sent (spender), the number of tokens sent (amount) and additional data, if required (extraData).
If you have any questions or are interested in the development/audit of smart contracts, please contact us and we will answer all your questions.
Sincerely, Grox Solutions.