0

I am trying to create my own ERC223 token. There is not much information online on how to use new functions in ERC223 or I just don't understand it. Github of ERC223 says to implement the IERC223Recipient contract to handle cases when tokens are sent to a contract.

    function transfer(address _to, uint _value, bytes calldata _data) external override returns (bool)
    {
        require(_value > 0);

        if(isContract(_to)) 
        {
            IERC223Recipient receiver = IERC223Recipient(_to);
            receiver.tokenReceived(msg.sender, _value, _data);
        }

        balances[msg.sender] = SafeMath.sub(balances[msg.sender], _value);
        balances[_to] = SafeMath.add(balances[_to], _value);
        emit Transfer(msg.sender, _to, _value);
        emit TransferData(_data);

        return true; 
    }

I am not quite sure what _data is in this case. I watched some videos online and they say that this is bytes of a function. I don't understand which function and where to get it.

Once tokenReceived is called this happens:

    struct ERC223TransferInfo
    {
        address token_contract;
        address sender;
        uint256 value;
        bytes   data;
    }
    
    ERC223TransferInfo private tkn;
        
    /**
    * @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 tokenReceived(address _from, uint _value, bytes memory _data) public virtual
    {
        /**
         * @dev Note that inside of the token transaction handler the actual sender of token transfer is accessible via the tkn.sender variable
         * (analogue of msg.sender for Ether transfers)
         * 
         * tkn.value - is the amount of transferred tokens
         * tkn.data  - is the "metadata" of token transfer
         * tkn.token_contract is most likely equal to msg.sender because the token contract typically invokes this function
        */
        tkn.token_contract = msg.sender;
        tkn.sender         = _from;
        tkn.value          = _value;
        tkn.data           = _data;
        
        // ACTUAL CODE
    }

where tnk is struct above. The developer provides the code and leaves the // ACTUAL CODE comment that confuses me a lot.

I hope it is not a dumb question. I am very stuck on how to use the new ERC223 functions

Thank you!

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Danny711
  • 69
  • 1
  • 10

1 Answers1

0

In ethereum smart contracts, if receive or fallback functions are not defined, sending ether to this contract will fail and you will lose the tokens. ERC223 will make sure that contract that you are sending funds is capable of receiving funds.

Another use case is to make sure that account addresses (externally owned accounts) are executing mint, transfer or burn functions. Because those functions should not be executed by contract addresses. So this function if(isContract(_to)) in your contract is checking if the to address is a contract address or not. this is done by low level solidity function extcodesize. How to find out if an Ethereum address is a contract?

_data is metadata that you want to pass. it can be anything. You can leave it blank by passing "". Or you could pass msg.data

msg.data is the raw data that's sent in the transaction. It's an encoding of the function selector and then any parameters that you're passing to that function.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202