Coding a DeFi Arbitrage Bot

9 min readOct 29, 2020

In our previous article, we introduced Decentralized Finance as a whole, casting light upon the basic building blocks of this new ecosystem and the main strategies used to earn passive income, mainly “yield farming” and “arbitrage”.

In this article we are going to guide the readers through a step by step tutorial for building an arbitrage trading bot that works with decentralized exchanges, the bot will also get flashloans in order to borrow funds to use for arbitrage. The full working code can be found on’s gihub repository, Extropy will publish a follow-up article that will examine the code in depth.

What is Arbitrage

Arbitrage is the purchase and sale of an asset in order to profit from a difference in the asset’s price between marketplaces.

An example arbitrage strategy in DeFI would be to buy ETH in exchange for USDT on a decentralized such as Kyber and sell it immediately afterwards on another decentralized exchange such as Uniswap at a higher price, thus making a profit in USDT, i.e. you end up with more USDT in your wallet than you had before the arbitrage. The difficulty in arbitrage lies in finding a price discrepancy (spread) for the same trading pair across two different exchanges. For an overview of the main arbitrage strategies please refer to our previous article.

Why Arbitrage on DeFi

Before we begin, let’s explore in more detail the idea of arbitrage and why this already established practice in the centralized finance world has reached a whole new level in DeFi mainly thanks to decentralized exchanges and flashloans.

When people hear of the term arbitrage they usually associate it with trading, or rather the practice of trying to predict the markets in order to make a profit. The best thing about arbitrage as a passive income strategy, is that it does not require any kind of prediction algorithm or stop-loss strategy, but rather it deals with finding profitable opportunities in the present moment before they disappear or before others do.

Another advantage of doing arbitrage on DEXes (i.e. smart contracts) rather than on centralized exchanges, is that there is no risk of losing money should a sequence of trades not execute as expected; the transactions will be reverted due to lack of funds, because the smart contract isn’t able to repay a flashloan. On the other hand, arbitrage traders on centralized exchanges…


Oxford-based blockchain and zero knowledge consultancy and auditing firm