Loopring Arb checker - or trading hours of fun work to save on minutes of tedious calculation

While I’m waiting for the next release of raiden so that I can finish my own release of Hub20, I am left playing with other layer-2 projects. Of those, loopring is certainly the one that has caught most of my attention. It requires people to transfer their tokens to a contract and then lets you keep a “Layer-2 balance”.

Loopring and the mechanics of an AMM

Any transaction that happens on Layer-2 is virtually free, and holding the tokens there cost nothing. Whether you need to send a payment to another Layer-2 account or even back to a different Layer-1, it can be done at much lower cost than a standard on-chain transfer.

That alone should make it interesting, but to make it even more appealing the folks at Loopring created an Automated Market Maker (AMM) exchange: similar to the mechanism popularized by Uniswap, users can provide liquidity in token pairs, and users that want to exchange one token for another (make a swap) can do so at a fee.

Due to the way that an AMM exchange works, it can happen to become unbalanced, i.e: if users buy too much of one side of the pool or if one token of the pair gains/loses value relative to the other, the value in one side of the pool is different from the other. This in turn creates an opportunity for arbitrage: one can buy the token on the side with “excess” supply for a lower price than other pools. In the process, the pool gets re-balanced and peace is restored in the universe - that is at last until the next trader makes it unbalanced again and the dance starts all over.

Enough exposition. How can I make money from it?

When Loopring started accepting more token pairs to be listed on the exchange,they rely on users providing lots of liquidity of all the different tokens. They started providing rewards for people providing liquidity for some tokens that were of interest, but not all pools were benefiting from these incentives.

As I noticed that there were some tokens from solid projects but with low liquidity, I wondered if I could check if how balanced those pools were.

After some hours playing with their API, I hacked up a script that checks that takes a list of tokens available for trade and does the following:

  • Checks if there is a “ETH-” pair
  • Verifies the volume and USD value of each side of token pair
  • In case the pool is unbalanced, calculates how much of any one token is needed to rebalance the pool and the associated costs.
  • If the swap is profitable, prints the result with instructions of what token and how much to trade.

The code is available here and is a simple python script. To use it, just clone the repo, pip install the dependencies listed on requirements.txt and call python loopring.py. An example run would be:

Pay 0.2878332608336053 ETH ($511.2580728904747)
Receive: 190.44220898248264 CRV ($523.7160747018272)
Profit: $12.458 (2.437%)

[<Token: ETH>, <Token: DAI>] is balanced

Pay 0.07103439215219157 ETH ($126.17341837248723)
Receive: 61.38390289979394 GRT ($126.45083997357551)
Profit: $0.277 (0.220%)

[<Token: MKR>, <Token: ETH>] is balanced

[<Token: ETH>, <Token: USDC>] is balanced

Pay 87.2081188782998 ETH ($154901.67699520246)
Receive: 223365.22777104643 LRC ($155110.62166013668)
Profit: $208.945 (0.135%)

[<Token: WBTC>, <Token: ETH>] is balanced

[<Token: LINK>, <Token: ETH>] is balanced

Pay 42.64371343612116 1INCH ($201.7047645528531)
Receive: 0.11383943982241368 ETH ($202.20502819576586)
Profit: $0.500 (0.248%)

Pay 35.65256489452435 UNI ($724.81664430568)
Receive: 0.4083333892596485 ETH ($725.2940160046655)
Profit: $0.477 (0.066%)

Pay 0.3363907664508281 COMP ($149.6198851019993)
Receive: 0.08453349180284586 ETH ($150.1509241449689)
Profit: $0.531 (0.355%)

Pay 2683.6068944875324 PNK ($241.51925329008893)
Receive: 0.1372335288461172 ETH ($243.75831094233874)
Profit: $2.239 (0.927%)

[<Token: RENBTC>, <Token: ETH>] is balanced

Pay 4.064107695371856 AAVE ($1727.3676937638998)
Receive: 1.0481508187995057 ETH ($1861.7569288762459)
Profit: $134.389 (7.780%)

Taking a look at the results, it’s easy to verify that the ETH-AAVE pool is really unbalanced and is now a big opportunity to make some quick cash. As loopring grows, more people will come in, more sophisticated traders will show up and these opportunities will become ever rarer. Enjoy it while it lasts!