Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-5528, which enables ERC-20 token refunds through escrow smart contracts.

An escrow service is an intermediary that facilitates secure transactions. It receives funds from one party and releases them to another once agreed conditions are met.

Traditional finance commonly relies on a trusted individual or institution, such as a bank, to act as the intermediary. In DeFi, a smart contract can perform that role.

For more information, see the following resources.

https://spendingcrypto.com/crypto-and-blockchain-legals/cryptocurrency-escrow-service-providers/

https://medium.com/@spendingcrypto/what-are-crypto-web3-escrow-services-and-how-do-they-work-d28452df1ff1

https://docs.replit.com/tutorials/web3/escrow-contract-with-solidity#escrow-protocol-design

This article translates, summarizes, and supplements the material linked below.

Overview

The standard extends EIP-20 and defines the following escrow workflow.

  1. The seller issues a token.
  2. The seller creates an escrow smart contract specifying details such as token contract addresses, the lock period, exchange rate, and additional success conditions.
  3. The seller funds the escrow contract with the seller's tokens.
  4. The buyers fund the escrow using the buyer-side token specified in advance.
  5. Once the escrow is determined to have succeeded, the seller withdraws the buyers' tokens, and the buyers withdraw the seller's tokens according to the exchange rate.
  6. If escrow fails or remains in progress, buyers can withdraw or obtain refunds of the tokens they deposited, subject to the escrow policy.

Motivation

Ordinary cryptocurrency payments generally cannot be automatically reversed by the sender once settled.

Trusted escrow services address this problem in traditional finance. In decentralized systems, smart contracts can implement escrow without requiring a third-party arbitrator for every operation.

This standard defines an interface for escrow contracts that can return tokens to their original wallets when the escrow does not complete successfully.

If you are unfamiliar with smart contract interfaces, see the following article.

https://chaldene.net/erc721#st-toc-h-5

Specification

The escrow system involves two types of contracts.

Payable Contract

Sellers and buyers use the escrow mechanism to fund their token positions. The payable token contract implements the relevant extensions to the EIP-20 interface.

Escrow Contract

The escrow contract defines the escrow policy and holds the payable contracts' tokens for the relevant period. It does not need to override the EIP-20 interface itself.

Methods

constructor

The escrow policy is configured when the contract is deployed.

Required policy information

  • Seller's token contract address.
  • Buyer's token contract address.

Optional policy information

  • Escrow period.
  • Maximum or minimum number of investors.
  • Maximum or minimum token funding amount.
  • Exchange rate between seller and buyer tokens.
  • User KYC verification requirements.

escrowFund

Funds escrow with _value tokens using the address parameter _to.

For the escrow contract

  • _to must identify the user.
  • msg.sender must be a payable contract address.
  • The applicable escrow policy must be validated.

For the payable contract

  • Its _to argument must identify the escrow contract.
  • It must call the corresponding function on the escrow contract interface.
  • When forwarding that call, it passes the original msg.sender as the user address so that the escrow contract can identify the funding participant.
javascript
function escrowFund(address _to, uint256 _value) public returns (bool)

escrowRefund

Refunds _value tokens using the address parameter _from.

For the escrow contract

  • _from must identify the user.
  • msg.sender must be a payable contract address.
  • The applicable refund policy must be validated.

For the payable contract

  • Its _from argument must identify the escrow contract.
  • It must call the corresponding function on the escrow contract interface.
  • When forwarding that call, it passes the original msg.sender as the user address so that the escrow contract can identify the participant requesting a refund.
javascript
function escrowRefund(address _from, uint256 _value) public returns (bool)

escrowWithdraw

Withdraws funds from the escrow contract.

For the escrow contract

  • Verify that the escrow process has completed successfully.
  • Transfer the relevant seller-side and buyer-side token balances to the entitled participants.
  • Implementing the withdrawal method on the payable contract is optional.
javascript
function escrowWithdraw() public returns (bool)

Example Interface

javascript
pragma solidity ^0.4.20;

interface IERC5528 {

    function escrowFund(address _to, uint256 _value) public returns (bool);

    function escrowRefund(address _from, uint256 _value) public returns (bool);

    function escrowWithdraw() public returns (bool);

}

contract PayableContract is IERC5528, IERC20 {
    /*
      General ERC20 implementations
    */

    function _transfer(address from, address to, uint256 amount) internal {
        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        _balances[from] = fromBalance - amount;
        _balances[to] += amount;
    }

    function transfer(address to, uint256 amount) public returns (bool) {
        address owner = msg.sender;
        _transfer(owner, to, amount);
        return true;
    }

    function escrowFund(address _to, uint256 _value) public returns (bool){
        bool res = IERC5528(to).escrowFund(msg.sender, amount);
        require(res, "Fund Failed");
        _transfer(msg.sender, to, amount);
        return true;
    }

    function escrowRefund(address _from, uint256 _value) public returns (bool){
        bool res = IERC5528(_from).escrowRefund(msg.sender, _value);
        require(res, "Refund Failed");
        _transfer(_from, msg.sender, _value);
        return true;
    }
}

contract EscrowContract is IERC5528 {

    enum State { Inited, Running, Success, Closed }
    struct BalanceData {
        address addr;
        uint256 amount;
    }

    address _addrSeller;
    address _addrBuyer;
    BalanceData _fundSeller;
    BalanceData _fundBuyer;
    EscrowStatus _status;

    constructor(address sellerContract, address buyerContract){
        _addrSeller = sellerContract;
        _addrBuyer = buyerContract;
        _status = State.Inited;
    }

    function escrowFund(address _to, uint256 _value) public returns (bool){
        if(msg.sender == _addrSeller){
            require(_status.state == State.Running, "must be running state");
            _fundSeller.addr = _to;
            _fundSeller.amount = _value;
            _status = State.Success;
        }else if(msg.sender == _addrBuyer){
            require(_status.state == State.Inited, "must be init state");
            _fundBuyer.addr = _to;
            _fundBuyer.amount = _value;
            _status = State.Running;
        }else{
            require(false, "Invalid to address");
        }
        return true;
    }

    function escrowRefund(address _from, uint256 amount) public returns (bool){
        require(_status.state == State.Running, "refund is only available on running state");
        require(msg.sender == _addrBuyer, "invalid caller for refund");
        require(_fundBuyer.addr == _from, "only buyer can refund");
        require(_fundBuyer.amount >= amount, "buyer fund is not enough to refund");
        _fundBuyer.amount = _fundBuyer.amount - amount
        return true;
    }

    function escrowWithdraw() public returns (bool){
        require(_status.state == State.Success, "withdraw is only available on success state");
        uint256 common = MIN(_fundBuyer.amount, _fundSeller.amount);

        if(common > 0){
            _fundBuyer.amount = _fundBuyer.amount - common;
            _fundSeller.amount = _fundSeller.amount - common;

            // Exchange
            IERC5528(_addrSeller).transfer(_fundBuyer.addr, common);
            IERC5528(_addrBuyer).transfer(_fundSeller.addr, common);

            // send back the remaining balances
            if(_fundBuyer.amount > 0){
                IERC5528(_addrBuyer).transfer(_fundBuyer.addr, _fundBuyer.amount);
            }
            if(_fundSeller.amount > 0){
                IERC5528(_addrSeller).transfer(_fundSeller.addr, _fundSeller.amount);
            }
        }

        _status = State.Closed;
    }

}

Additional Notes

The three functions, escrowFund, escrowRefund, and escrowWithdraw, build on the token transfer mechanism of EIP-20.

The escrowFund function transfers tokens into escrow. The escrow contract can record the funding state and reject deposits that do not satisfy its policy.

The escrowRefund function can be called while escrow remains in progress or after it fails, according to the applicable conditions.

The escrowWithdraw function allows sellers and buyers to receive tokens from the escrow account. When escrow completes successfully, the seller receives the buyer-side tokens and buyers receive the seller-side tokens.

Backward Compatibility

Payable contracts implementing the extension are intended to remain backward-compatible with EIP-20.

Test Cases

The original article describes an example unit test using Truffle.

The test defines the following conditions for exchanging seller-side and buyer-side tokens.

  • The exchange rate is 1 to 1.
  • Escrow completes successfully when the number of buyers reaches 2.
  • Before the success condition is met, buyers can request refunds or withdraw their deposited tokens according to the policy.

Conclusion

ERC-5528 defines an interface for ERC-20 escrow operations, including refunds.

I plan to add further implementation details in the future.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777