May 24, 2023: $crvUSD Arbitrage Trading π±πΈ
Tutorial on arb trading through $crvUSD and LLAMMA
Weβre excited to release this short tutorial on how $crvUSD arbitrage trading works.
This educational video dissects how arbitrage trades of $crvUSD happen behind the scenes. Understanding such trading is useful to having a complete understanding of $crvUSD, because the PegKeeper system relies in part on arbitrage traders.
More on PegKeeper from yesterday:
Note that despite the connotations, arb trading can be very risky and this is only recommended for experienced professionals. Frequently arbitrage trades are not possible because another interested party has sniped it first!
The tutorial presumes a basic knowledge of $crvUSD architecture, particularly the LLAMMA (AMM) attached to each collateral type. If you are not familiar with these mechanics, we recommend reviewing the white paper.
This particular tutorial runs these arbitrage calculations on-chain via a helper Vyper contract and a python script to invoke it, which you can find in its entirety in the companion Github repository: https://github.com/curvefi/vyper-tutorial/tree/main/crvusd/arb
The Vyper script contains two important functions we review in the video. First is the `calc_output` view function which can be invoked to determine whether or not an opportunity exists.
The meat of this is the conditional between lines 106 and 121. The script is passed a True/False variable called `liquidation` that determines whether the collateral is being liquidated (in this case converting $sfrxETH to $crvUSD) or de-liquidated ($crvUSD to $sfrxETH). Since good arbitrage trading will begin and end with the same asset, the routes are as documented:
For liquidations, convert $sfrxETH to $crvUSD through Curveβs exchange router, then trade this surplus $crvUSD through the LLAMMA to receive $sfrxETH
For de-liquidations, reverse the process. Trade $sfrxETH to $crvUSD through the LLAMMA, and move the balance through the exchange router.
If either of these processes return a profit, then it can be profitable to actually execute the opportunity.
Execution works using the same logic, but it replaces `view` functions with state changing functions that capture the opportunity.
Itβs important to note that executing so many trades can be gas prohibitive. The included Python script will calculate gas costs, to determine if an arbitrage opportunity is profitable inclusive of gas fees. For this reason, when gas prices are high, arbitrage trades tend to occur less frequently for higher amounts. When gas prices dip, trades may be more common and consist of smaller amounts.
Finally, we explore exactly how such arbitrage trades look when executed on-chain. We consider a successful transaction and break down how it looks on Eigenphiβs visualizer:
As you follow the numbered steps in the above diagram, we see this exact sequence plays out. 45 $sfrxETH is deposited to the contract (0), which is directly exchanged in the LLAMMA (1-2) for 87,480 $crvUSD. This $crvUSD is traded through the Curve router (3-16) to receive 47.0192 $frxETH, which gets converted at Frax (17-19) into 45.2824 $sfrxETH. This works out to a profit of 0.2824 $sfrxETH.
Again note that arbitrage trading is difficult, as other traders are hungry to bite off these opportunities. The scripts presented are educational in nature, but potentially you may find an advantage gas golfing the process. Any suggestions for improvement are welcome in the comments!
Links:
$crvUSD White Paper:
https://github.com/curvefi/curve-stablecoin/blob/master/doc/curve-stablecoin.pdfGithub Repository:
https://github.com/curvefi/vyper-tutorial/tree/main/crvusd/arbSample Etherscan Transaction:
https://etherscan.io/tx/0xd849964857657f480d039a409f4cdab728286c7cda081075237452813c2cc0b8Eigenphi Visualization:
https://eigenphi.io/mev/eigentx/multi/0xd849964857657f480d039a409f4cdab728286c7cda081075237452813c2cc0b8