ERC 223 Token Standard and Implementation
kiran kumar
Data Scientist at Confidential, HealthCareInnovator ! Winner @ Enter The Travel-Verse Hackathon!AmazonsmbhavHackathon
The new ERC223 token standard introduces improvements and capabilities, addressing some of the most significant ERC20 pain points, especially when interacting with other smart contracts.Even though we may not be able to keep Kin on the public Ethereum blockchain because of issues with scalability, I truly wish it was possible to upgrade our current ERC20 token smart contract to an ERC223 token standard and benefit from the new features. But alas, contract upgrades aren’t supported.
ERC20 token standard suffers Set Backs and major issues , that caused loss of approximately $3,000,000 at the moment . The main and the most important is lack of event handling mechanism of ERC20 standard. ERC223 is a superset of the ERC20 token standard. It is a step forward towards economic abstraction at the application/contract level allowing the use of tokens as first class value transfer assets in smart contract development. It is also a more safe standard as it doesn't allow token transfers to contracts that don't support token receiving and handling.
Current implementation
ERC223 contracts are separated in 3 parts:
- Interface: The standard itself. The minimal common API ERC223 tokens and receivers to interact with each other.
- Receiver interface: A dummy receiver that is intended to accept ERC223 tokens.
- Proposed Method : The approach as to how implement .
Interface
pragma solidity ^0.5.0;
contract ERC223Interface {
uint public totalSupply;
function balanceOf(address who) public view returns (uint);
function transfer(address to, uint value) public returns (bool success);
function transfer(address to, uint value, bytes memory data) public returns (bool success); event Transfer(address indexed from, address indexed to, uint value, bytes data);}
Receiving Interface
pragma solidity ^0.5.0;
/** * @title Contract that will work with ERC223 tokens. */
contract ERC223ReceivingContract {
/** * @dev Standard ERC223 function that will handle incoming token transfers. * * @param _from Token sender address. * @param _value Amount of tokens. * @param _data Transaction metadata. */
function tokenFallback(address _from, uint _value, bytes memory _data) public;}
Proposed Solution
pragma solidity ^0.5.0;
import './ERC223_interface.sol';
import './ERC223_receiving_contract.sol';
import '././SafeMath.sol';
/**
* @title Reference implementation of the ERC223 standard token.
*/
contract ERC223Token is ERC223Interface {
using SafeMath for uint;
mapping(address => uint) balances; // List of user balances.
/**
* @dev Transfer the specified amount of tokens to the specified address.
* Invokes the `tokenFallback` function if the recipient is a contract.
* The token transfer fails if the recipient is a contract
* but does not implement the `tokenFallback` function
* or the fallback function to receive funds.
*
* @param _to Receiver address.
* @param _value Amount of tokens that will be transferred.
* @param _data Transaction metadata.
*/
function transfer(address _to, uint _value, bytes memory _data) public returns (bool success){
// Standard function transfer similar to ERC20 transfer with no _data .
// Added due to backwards compatibility reasons .
uint codeLength;
assembly {
// Retrieve the size of the code on target address, this needs assembly .
codeLength := extcodesize(_to)
}
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
if(codeLength>0) {
ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
receiver.tokenFallback(msg.sender, _value, _data);
}
emit Transfer(msg.sender, _to, _value, _data);
return true;
}
/**
* @dev Transfer the specified amount of tokens to the specified address.
* This function works the same with the previous one
* but doesn't contain `_data` param.
* Added due to backwards compatibility reasons.
*
* @param _to Receiver address.
* @param _value Amount of tokens that will be transferred.
*/
function transfer(address _to, uint _value) public returns (bool success){
uint codeLength;
bytes memory empty = hex"000000000";
assembly {
// Retrieve the size of the code on target address, this needs assembly .
codeLength := extcodesize(_to)
}
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
if(codeLength>0) {
ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
receiver.tokenFallback(msg.sender, _value, empty);
}
emit Transfer(msg.sender, _to, _value, empty);
return true;
}
Advantages of ERC223 Token Standard
Following are the 3 key advantages of upgrading to ERC223 standard:
- Eliminates the problem of lost tokens which happens during the transfer of ERC20 tokens to a contract (when people mistakenly use the instructions for sending tokens to a wallet). ERC223 allows users to send their tokens to either wallet or contract with the same function transfer, thereby eliminating the potential for confusion and lost tokens.
- Allows developers to handle incoming token transactions, and reject non-supported tokens. In this case, you won’t lose the tokens as it will be refunded back to you minus the Gas, something that is not possible with ERC20.
- Energy savings: The transfer of ERC223 tokens to a contract is a one-step process rather than 2 step process (for ERC20), and this means two times less Gas and no extra blockchain bloating. This, as a result, also lowers the transaction fees one pays for the transfer of tokens.
DBA,D.Lit.,PhD, PDPS (Oxford, UK), PGCL (IIM-L) , PBM (IIT-M), PGCEI (LSB,UK), PGCEIB (GER) , PGCUT (AUS) , M.Phil.(Gold Medalist) ,KSET, MBA (Gold Medalist)? ,B.Sc.-Hons (UK)| Associate Professor & Director - MBA @ AIT
5 年M m