Table of Contents

LikeCoin transfer delegation

Table of Contents

To use ERC-20 token as transaction fee

If you tried the peer to peer LikeCoin transfer functionality in like.co, you may wonder why you do not need to pay the Ethereum transaction fee in order to transfer LikeCoin as an ERC-20 token.

LikeCoin transfer delegation
Sending 100LIKE with 0.0011252ETH transaction fee.

For an ordinary ERC-20 token transfer, you can use MetaMask to do so.

You may notice that you do need to pay 0.0011252ETH for the transaction fee.

Let’s take a look on normal ERC-20 transfer in the smart contract:

// LikeCoin.sol
function _transfer(address _from,
address _to,
uint256 _value)
internal returns (bool success) {
_tryUnlockBalance(_from);
require(_from != 0x0);
require(_to != 0x0);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
Transfer(_from, _to, _value);
return true;
}// LikeCoin.sol
function transfer(address _to,
uint256 _value)
public returns (bool success) {
return _transfer(msg.sender, _to, _value);
}

It is simple, only check both _from and _to is not 0x0 and then substrate the amount from _from and add back the amount to _to.

However, when you use transfer delegation, says you want to send 100LIKE to me, you will see:

LikeCoin transfer delegation
Peer to peer LikeCoin transfer page from like.co.

After you click confirm, you only need to sign a signature in MetaMask and 100LIKE will be transferred to my Ethereum address without paying any Ether.

LikeCoin transfer delegation
Signing signature for confirming LikeCoin transfer.

Let’s take a look on transferDelegated in the smart contract to see what is the magic here:

// LikeCoin.sol
function transferDelegated(address _from,
address _to,
uint256 _value,
uint256 _maxReward,
uint256 _claimedReward,
uint256 _nonce,
bytes _signature)
isDelegated(_from, _maxReward, _claimedReward, _nonce)
public returns (bool success) {
require(signatureChecker.checkTransferDelegated(_from,
_to,
_value,
_maxReward,
_nonce,
_signature));
return _transfer(_from, _to, _value);
}// LikeCoin.sol
modifier isDelegated(address _from, uint256 _maxReward, uint256 _claimedReward, uint256 _nonce) {
require(allowDelegate);
require(_from != 0x0);
require(_claimedReward <= _maxReward);
require(!usedNonce[_from][_nonce]);
usedNonce[_from][_nonce] = true;
require(_transfer(_from, msg.sender, _claimedReward));
_;
}// SignatureCheckerImpl.sol
function checkTransferDelegated(address _from,
address _to,
uint256 _value,
uint256 _maxReward,
uint256 _nonce,
bytes _signature)
public constant returns (bool) {
bytes32 hash = keccak256(transferDelegatedHash,
keccak256(msg.sender,
"transferDelegated",
_to,
_value,
_maxReward,
_nonce));
var (v, r, s) = _bytesToSignature(_signature);
return ecrecover(hash, v, r, s) == _from;
}

The only different between transfer and transferDelegated is the checkTransferDelegated from signatureChecker and the modifier isDelegated. First of all, we get the signature from you which is signing from a method name transferDelegated, a to, a value, a maxReward and a nonce. We use the checkTransferDelegated to check whether those data are really signing from your address by ecrecover. Once we confirm this transaction is signed by the owner, we first deduct _claimedReward’s amount of LikeCoin from _from (the signature signer) to msg.sender (LikeCoin Foundation as the delegatee) as a LikeCoin transaction fee. After that, simply run the normal _transfer to transfer from _from to _to.

Therefore, the delegator (you) can specify the maximum transaction fee (`_maxReward`) will pay for this transfer delegation and of course the delegatee (LikeCoin Foundation in this case) with pay the Ether transaction fee to issue this transaction to Ethereum network. And that is why from the view of the delegator, he/she pays the transaction fee by the LikeCoin (ERC-20 token) only.

However, you seem do not need to pay the transaction fee by LikeCoin even when using like.co’s peer to peer transfer functionality. It is because we set the _maxReward to 0, that’s the reason.

Related links: 以太坊代幣付款委託(Cantonese speech) | Make it pay | Introduction to LikeChain | Chinese version