How to run an arbitrage bot?
Last updated
Was this helpful?
Last updated
Was this helpful?
Every time USDL deviates from its 1 USD peg, an almost riskless arbitrage opportunity arises. The only potential downside is having to pay gas costs for a transaction that doesn't go through. See for the high level details explaining the concepts behind the price stability mechanism.
This contract is a reference implementation for an arbitrage bot that profits off of the price deviations of USDL. The high level steps in this implementation are:
Check if USDL has deviated from its 1 USD price on the Uniswap v3 WETH<>USDL pool
If USDL is worth more than 1 USD, then in a single transaction, the following happens:
A flashloan is used to borrow WETH
The WETH is used to mint USDL
The USDL is swapped for WETH in the Uniswap pool
The loan is repaid using the the profits in WETH
If USDL is worth less than 1 USD, then in a single transaction, the following happens:
A flashloan is used to borrow WETH
The WETH is swapped for USDL in the Uniswap pool
The USDL is redeemed for 1 USD worth of WETH
The loan is repaid using the the profits in WETH
The repository with the reference contract implementation can be found here:
Function purpose: initializes the bot contract & points it to the chosen Uniswap v3 pair to arbitrage (in the example above that would be the USDL/WETH pair) and to the USDLemma contracts.
_collateral
Address for the collateral type to be used in the arbitrage.
eg. for a USDL<>WETH arbitrage, the _collateral address would be the WETH address.
_perpetualDEXIndex
Index for derivative dex to be used (right now only MCDEX is integrated, and its corresponding index is 0).
Integer
_usdLemma
_poolFee
Integer, you can pass in: 500, 3000 or 10000. Those respectively correspond to the following Uniswap fees: 0.05% 0.3% and 1%.
_swapRouter
_flashLender
Smart contract address
Function purpose: execute the arbitrage steps listed above.
amountOfCollateralToBorrow
Amount of WETH to borrow via the flashloan.
WETH, 18 decimals
amountOfUSDLToMint
Amount of USDL to be minted. If USDL needs to be redeemed then this should be 0.
USD, 18 decimals
ERC20 address, initially will be just
Address of contract.
ERC20 address, always will be:
for the USDL<>WETH uniswap V3 pool you will arbitrage.
Router for stateless execution of swaps against Uniswap V3 address, .
Smart contract address, will always be:
Source for the flashloan for the token type (_collateral, initially just WETH) that conforms to standard.
If you would like to use a Uniswap V3 pair as a flashloan source, please see how to deploy a ERC3156 wrapper.
is a reference script for how to calculate that sum.