- What are the benefits of setting a token standard like ERC20?
If everybody would use their own functions it would be very deifficult to comunicate beetwen the wallets, so usinf the standard functions makes more easily writing and understanding the smart contract among community. With standard exchanges can support the tokens easily.
- What functions are in the ERC20 Token Standard Interface and what do they do?
ERC-20 token contracts come with a number of functions to allow users to find out the balances of accounts as well as to transfer them from one account to another under varying conditions. These functions are described below.
The balanceOf()
function provides the number of tokens held by a given address. Note that anyone can query any addressâ balance, as all data on the blockchain is public.
There are two ways to send tokens from one address to another. The transfer()
function transfers a number of tokens directly from the message sender to another address. Note that there are no checks made on the recipient address, so it is incumbent on the sender to ensure that the recipient is as intended.
Although transfer()
is fine for sending tokens from one user to another it doesnât work so well when tokens are being used to pay for a function in a smart contract. This is because at the time the smart contract runs it has no access to details of which addresses transferred funds where and so cannot assert that the user calling the contract has paid the required amount of funds to operate the contract.
Imagine that there is a contract Doer
deployed on the network. Doer
has a function doSomething()
that requires 10 Do tokens to operate. Joe wants to call doSomething()
and has 50 Do tokens in his account. How can Joe pay Doer so that it can run doSomething()
successfully?
approve()
and transferFrom()
are two functions that allow the above scenario to work using a two-step process. In the first step a token holder gives another address (usually of a smart contract) approval to transfer up to a certain number of tokens, known as an allowance. The token holder uses approve()
to provide this information.
Once an allowance has been created the smart contract can take up to the allowed number of tokens from a userâs allowance as part of the contractâs operation. Continuing the example, Joe can now call doSomething()
and doSomething()
can use transferFrom()
to take 10 Do tokens from Joeâs account and carry on its work. If Joe doesnât have 10 tokens in their account, or their allowance is less than 10 tokens, doSomething()
will fail.
The allowance()
function provides the number of tokens allowed to be transferred from a given address by another given address. Note that anyone can query any addressâ allowance, as all data on the blockchain is public. It is important to understand that allowances are âsoftâ, in that both individual and cumulative allowances can exceed an addressâ balance. In the approval table shown above, for example, the holder 0x2299âŚ3ab70x2299âŚ3ab7 has approved a transfer of up to 500 tokens but their balance as seen previously is only 90 tokens. Any contract using allowance()
must additionally consider the balance of the holder when calculating the number of tokens available to it.