How to build an Escrow contract with an Ethereum ERC20 token

Sander van Laar
May 2, 2018 · 2 min read

Written by Sander van Laar & Jordy van Vorselen.

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.

Image for post
Image for post

The problem

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.

The solution

We developed a smart contract 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.



Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store