One of the advantages of smart contracts on a blockchain is that they could eliminate trusted third parties. The use of escrow mechanisms leads to increased trust in processes where currency is exchanged for goods. For Ethereum-based payments, standard solutions are available¹.
However, the basic ERC20 contract of Ethereum does not offer this functionality out of the box. Payable functions only allow receiving Ether and no custom-made ERC20 tokens, so we had to build a solution ourselves.
We created a blockchain-based webshop that accepts payments in ERC20 tokens. To improve trust in the exchange of products and assets, a customer would like to use escrow functionality for his or her orders in the webshop. The escrow contract should be able to release the tokens to the webshop or to refund the token to the customer.
We developed a smart contract
escrow.sol that creates a payment in which the orderId is stored, along with the customer’s (ERC20) address and the payment amount. New payments are kept in a key-value pair, implemented as a mapping of orderId to a payment together with its status, with initial status set to ‘Pending’.
Since this escrow functionality is a service offered by our webshop, we decided not to implement a timed escrow, but customers could actively apply for a refund (e.g. when the delivery takes too long in their opinion), to ensure human interaction between the webshop owner and the customer. The refund request is approved by the webshop owner, who is the owner of the payment address.
The initial payment status ‘Pending’ will in time be changed to ‘Refunded’ (the customer received its refund) or ‘Completed’ (the webshop received its payment), depending on the actions of the customer.
The complete code is shown below.
ERC20 escrow contract
This solution was developed by Sander van Laar and Jordy van Vorselen for SynTouch in The Netherlands.