Our developer team set a goal of enabling the support of the ERC20 token in Plasma. But to do this, we had to establish an algorithm for receiving ERC20 tokens during the deposit. Everything is simple with ETH: the user can request the ‘deposit’ function with an ETH transaction.
As a rule, ERC20 token users exchange tokens using the `transfer` function of a smart contract here.
- ```
- /**
- * @dev transfer token for a specified address
- * @param _to the address to transfer to.
- * @param _value The amount to be transferred.
- */
- function transfer(address _to, uint256 _value) public returns (bool) {
- require(_to != address(0));
- require(_value <= balances[msg.sender]);
- // SafeMath.sub will throw if there is not enough balance.
- balances[msg.sender] = balances[msg.sender].sub(_value);
- balances[_to] = balances[_to].add(_value);
- Transfer(msg.sender, _to, _value);
- return true;
- }
- ```
As you can see from the code, only the owner of ERC20 tokens can send them to another address.
Algorithm for Transferring ERC20 Tokens
We can take advantage of two great features provided in ERC20 and, in particular, in the Opporty smart contract,
`approve` here.
- ```
- /**
- * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
- *
- * @param _spender The address which will spend the funds.
- * @param _value The amount of tokens to be spent.
- */
- function approve(address _spender, uint256 _value) public returns (bool) {
- allowed[msg.sender][_spender] = _value;
- Approval(msg.sender, _spender, _value);
- return true;
- }
- ```
The token owner requests this function and thus signals to the blockchain that they allow the transfer of a certain number of ERC20 tokens to a specific address.
`transferFrom` here.
- ```
- /**
- * @dev Transfer tokens from one address to another
- * @param _from address The address which you want to send tokens from
- * @param _to address The address which you want to transfer to
- * @param _value uint256 the amount of tokens to be transferred
- */
- function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
- require(_to != address(0));
- require(_value <= balances[_from]);
- require(_value <= allowed[_from][msg.sender]);
- balances[_from] = balances[_from].sub(_value);
- balances[_to] = balances[_to].add(_value);
- allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
- Transfer(_from, _to, _value);
- return true;
- }
- ```
This function is requested by the party that wants to receive tokens.
Therefore, we get the following algorithm,
- The user requests the `approve` function of the ERC20 smart contract, specifies the address of the Plasma Cash smart contract and the amount of ERC20 tokens they want to transfer to the deposit.
- The user requests the `deposit` function of the Plasma Cash smart contract (we will provide more information about that later), which in turn requests the `transferFrom` function of the ERC20 smart contract.
- The user withdraws the Plasma token in the standard way: using the `transfer` function of the ERC20 smart contract, the Plasma Cash smart contract transfers tokens to the receiver's address.
As evident from the above algorithm, we need to refine the Plasma Cash smart contract.
Refining the Plasma Cash Smart Contract
We add an ERC20 smart contract declaration,
- ```
- contract ERC20 {
- function totalSupply() public constant returns (uint);
- function balanceOf(address tokenOwner) public constant returns (uint balance);
- function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
- function transfer(address to, uint tokens) public returns (bool success);
- function approve(address spender, uint tokens) public returns (bool success);
- function transferFrom(address from, address to, uint tokens) public returns (bool success);
- event Transfer(address indexed from, address indexed to, uint tokens);
- event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
- }
- ```
Next, we establish a new token structure that will support both ETH coins and ERC20 tokens.

Join the conversation! Your thoughts help the community grow.