Delegating other accounts to spend your token
In addition to the regular transfer function, ERC20 provides an option to allow other accounts to spend on your behalf. This is made possible by allocating an amount they can spend from your account. This recipe focuses on implementing such approval methods from the token standard.
Getting ready
You need to have a development environment that supports solidity programming and an Ethereum network to deploy and test your code.
How to do it…
- ERC20 includes a few functions that enable a user to transfer on behalf of someone else. You can allocate the tokens using the
approve
function and transfers can be carried out using thetransferFrom
function. - Use a nested mapping to track allocations. Use the first key to denote the actual owner of the tokens and use the second key to store the address that has permission to spend those tokens. Use the final value to store the amount allocated. Create a mapping that follows this structure:
mapping (address => mapping...