Token Standards - I
When someone who learns the Solidity language wants to develop a smart contract with this knowledge, they first need to code it. For this coding, they can examine the source codes of projects that have already gone live (uploaded to Mainnet). One thing that can be a guide for these reviews is EIPs (Ethereum Development Recommendations). The ERC standards are the simplest and fastest of these standards. Examples of these standards are ERC20, ERC721, ERC1155 standards.
Let's take a detailed look at the information about which standards and coding the tokens we use in the real world have and how we can encode our own tokens using these standards.
ERC20 Token Standard
We also have the ability to encode our own tokens using the ERC20 Token standard. You can get detailed information about this standard by reading Ethereum's EIP-20 page and you can see in detail how this standard has evolved into what we use today.
This ERC20 standard has the following features.
totalSupply()
Returns how much of the ERC20 Token we are developing is in circulation, or what the maximum amount in circulation can be.
balanceOf(account)
Returns the balance of the account address given as a parameter.
transfer(recipient, amount)
This method transfers the amount of tokens given as the amount parameter to the address given as the recipient parameter. This method is used in cases where the triggering party transfers from its own balance.
allowance(owner, spender):
This method returns how much the proxy address allowed by the account address is authorized to spend. The parameter given as owner is our account address that assigns a proxy, and the spender is the proxy that the account address assigns to spend its balance.
approve(spender, amount)
This method is the method to set the account address to be able to spend from its balance. It is the method that sets the approval of how much spending authorization is given to the proxy address passed as the spender parameter by the triggerer of this method with the amount parameter.
transferFrom(sender, recipient, amount)
This method transfers the amount sent as amount from the sender address given as a parameter to the address given as the recipient parameter where the transfer will be made. This method is used to perform transactions with the help of another contract or a designated proxy account.
name()
This method returns the name of the ERC20 token.
symbol()
This method displays the symbol of the ERC20 token.
decimals()
This method returns the number of digits in the decimal part of the ERC20 token. Since there is no float or double data type, this method shows how many total digits the number should be after the comma.
event Transfer(address indexed _from, address indexed _to, uint256 _value)
This is the event that should be triggered when the transfer operation takes place.
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
Event that should be triggered when spending authorization is given.
ERC721 Token Standard
This standard is also known as NFT (Non-Fungible Token). Projects such as CryptoKitties, CryptoPunks, Loot Project are the pioneering projects that use this standard. Let's examine the methods and structures required to code this standard together.
The ERC721 standard has the following features.
balanceOf(owner)
Returns the balance of the owner address given as a parameter.
ownerOf(tokenId)
Returns the ownership information of the tokenId value given as a parameter.
safeTransferFrom(from, to, tokenId, data)
This method transfers the ownership of the tokenId value of the from address given as a parameter to the address given as to. If additional information is needed, this method is called by assigning the data parameter.
safeTransferFrom(from, to, tokenId)
It transfers the ownership of the NFT with the value "tokenId" belonging to the from account given as a parameter to the account given as "to".
transferFrom(from, to, tokenId)
It transfers the ownership of the NFT with the value "tokenId" belonging to the "from" account given as a parameter to the account given as "to". This method is used when the user wants to check whether the transaction has been performed or not.
approve(approved, tokenId)
It is the method in which authorization is made on the "tokenId" ownership of the "approved" account given as a parameter.
setApprovalForAll(operator, approved)
It is the method in which the "operator" account address given as a parameter is set according to the value of the "approved" parameter that it can or cannot perform transactions on all NFTs belonging to the address that calls this method.If "approved" is true, authorization is done, if false, authorization is canceled if it was done before.
getApproved(tokenId)
It is checked whether any account address is authorized for the NFT with the "tokenId" value given as a parameter and if there is authorization, the address of the authorized account is returned.
isApprovedForAll(owner, operator)
It is checked whether there is a proxy account assigned for the "owner" account given as a parameter and returns true if there is authorization.Otherwise it should return false."
onERC721Received(operator, from, tokenId, data)
method in ERC721TokenReceiver Interface:This method is used in the methods starting with "safe" mentioned above.Through this method, it is used to check if the transferred ownership change is correct.
Methods in the ERC721 Metadata Interface:
name()
Returns the name of the NFT.
symbol()
Returns the symbol of the NFT.
tokenURI(tokenId)
Returns the URI containing the metadata of the "tokenId" value given as a parameter.
This returned value can be used by third party applications to access the metadata file containing the NFT's image and other information. In on-chain projects such as Loot Project, this information is returned to us directly.
Methods in the ERC721Enumerable Interface:
totalSupply()
Returns the total supply of the NFT.
tokenByIndex(index)
It is the method that returns the tokenId value of the NFT at the given index value. Here, the index value to be given as a parameter should be checked at most "totalSupply -1" and an error should be thrown if it does not match this situation.
tokenOfOwnerByIndex(owner, index)
It is the method that returns the tokenId value if the NFT in the given index value belongs to the "owner" account address given as a parameter.
Source: