ibhi / bot

(Winning) submission for https://gitcoin.co/issue/liquity/beta/2/100025005

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This project contains code to arbitrage the ETH/LUSD rates on Uniswap and the redemption rate on Liquity. It is designed for the Kovan testnet, however this can be adapted by changing the addresses in constants.ts.

Improvements

I can now update this page as the competition has finished. Since submitting, I have realized some improvements that could be made, notably:

* Add a function to withdraw the LUSD balance of the contract (redemptions can give LUSD as a gas refund or your calculations can be incorrect and lead to some residual LUSD).
* Instead of using low level Solidity calls, implement the TroveManager and UniswapPair interface types and use those.

Design

It works by querying prices from the Uniswap pair and the Chainlink oracle every block. If LUSD is sufficiently cheaper on Uniswap, then the bot will calculate how much would get by swapping ETH for LUSD on Uniswap after slippage. It will then pass the result of that calculation into redeemLUSD. We then generate the transaction data from these actions and pass it to our arbitrager smart contract. This smart contract will transfer the specified amount of WETH to Uniswap, execute the swap for LUSD, then redeem the LUSD through the TroveManager contract. It will also revert if the balance after execution is lower than the balance before to ensure that we do not lose money (except gas) even if we are frontrun or if our transaction is included late.

Run

First, install dependencies:

$ npm install

Due to a bug (?) in @liquity/lib-ethers, you need to run:

$ cp node_modules/@liquity/lib-ethers/deployments/default/kovan.json node_modules/@liquity/lib-ethers/dist/deployments/dev.json 

before running the code.

Next, deploy contracts/Execute.sol using Ethereum Remix. Make sure to add some initial ETH to it or it will not be able to function. Replace ARBITRAGE_CONTRACT_ADDRESS in constants.ts with the deployed address.

Once you have done that, you can run (using the private key of the address that deployed the contract):

$ ETHEREUM_PRIVATE_KEY=xxxx INFURA_KEY=xxxx npm start 

Example Transactions

About

(Winning) submission for https://gitcoin.co/issue/liquity/beta/2/100025005

License:MIT License


Languages

Language:JavaScript 49.4%Language:TypeScript 46.0%Language:Solidity 4.6%