# How to run an arbitrage bot?

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 [here](https://lemma-support.gitbook.io/lemma/concepts-overview/usdl-price-stability) for the high level details explaining the concepts behind the price stability mechanism.

## Contract: LemmaUniswapV3ArbBot

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
* &#x20;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: <https://github.com/lemma-finance/usdl-arbitrageur>

## Public Functions

### constructor

```
  constructor(
        IERC20 _collateral,
        uint256 _perpetualDEXIndex,
        IUSDLemma _usdLemma,
        uint24 _poolFee,
        ISwapRouter _swapRouter,
        IERC3156FlashLender _flashLender)
```

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.

| Parameters          | Description                                                                                                                                                                                                                                                                                                                                                                                                                              | Attribute                                                                                                                                                          |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| \_collateral        | <p>Address for the collateral type to be used in the arbitrage.</p><p>eg. for a USDL<>WETH arbitrage, the \_collateral address would be the WETH address.</p>                                                                                                                                                                                                                                                                            | ERC20 address, initially will be just [WETH - 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1](https://arbiscan.io/address/0x82aF49447D8a07e3bd95BD0d56f35241523fBab1)​ |
| \_perpetualDEXIndex | Index for derivative dex to be used (right now only MCDEX is integrated, and its corresponding index is 0).                                                                                                                                                                                                                                                                                                                              | Integer                                                                                                                                                            |
| \_usdLemma          | Address of [USDLemma](https://app.gitbook.com/s/SCzheGnJY2h51BCqMXm3/r/fnBmxe6wKixiAVKNAEzt/smart-contracts/core-contracts/usdlemma) contract.                                                                                                                                                                                                                                                                                           | ERC20 address, always will be:​[0xdb41ab644abca7f5ac579a5cf2f41e606c2d6abc](https://arbiscan.io/token/0xdb41ab644abca7f5ac579a5cf2f41e606c2d6abc)                  |
| \_poolFee           | ​[Fee tier](https://docs.uniswap.org/protocol/concepts/V3-overview/fees#pool-fees-tiers) for the USDL<>WETH uniswap V3 pool you will arbitrage.                                                                                                                                                                                                                                                                                          | Integer, you can pass in: 500, 3000 or 10000. Those respectively correspond to the following Uniswap fees: 0.05% 0.3% and 1%.                                      |
| \_swapRouter        | Router for stateless execution of swaps against Uniswap V3 address, [here](https://github.com/Uniswap/v3-periphery/blob/main/deploys.md)​.                                                                                                                                                                                                                                                                                               | Smart contract address, will always be: [0xE592427A0AEce92De3Edee1F18E0157C05861564](https://arbiscan.io/address/0xE592427A0AEce92De3Edee1F18E0157C05861564)       |
| \_flashLender       | <p>Source for the flashloan for the token type (\_collateral, initially just WETH) that conforms to <a href="https://eips.ethereum.org/EIPS/eip-3156">ERC3156</a> standard.</p><p>If you would like to use a Uniswap V3 pair as a flashloan source, please see <a href="https://github.com/lemma-finance/ERC3156-Wrappers/blob/uniswapv3_wrapper/contracts/uniswapV3/UniswapV3ERC3156.sol">here</a> how to deploy a ERC3156 wrapper.</p> | Smart contract address                                                                                                                                             |

### arb <a href="#arb" id="arb"></a>

```
function arb(uint256 amountOfCollateralToBorrow, uint256 amountOfUSDLToMint)
```

Function purpose: execute the arbitrage steps listed above.

| Argument                   | Description                                                                                                                                                                                                                    | Attribute         |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
| amountOfCollateralToBorrow | <p>Amount of WETH to borrow via the flashloan.</p><p>​<a href="https://github.com/lemma-finance/usdl-arbitrageur/blob/master/test/lemmaUniswapArbBot.js#L44">Here</a> is a reference script for how to calculate that sum.</p> | WETH, 18 decimals |
| amountOfUSDLToMint         | Amount of USDL to be minted. If USDL needs to be redeemed then this should be 0.                                                                                                                                               | USD, 18 decimals  |
