This project will perform an arbitrage of LUSD/ETH pair. This will be done by 2 steps
- Swap ETH for LUSD at uniswap
- Redeem LUSD for ETH at liquity
The project has two main components , one is the node server and another is the smart contract.
The node server is responsible for keeping a track of the prices of ETH at chainlink and at uniswap. When we notice a price difference where there is a possiblity of a profit from arbitrage it will trigger the smart contract to perform the arbitrage between uniswap and liquity smart contracts.
We have three restrictions in determining the ETH that can be used for the arbitrage.
- The ETH present in the arbitragers wallet
- The redeemable amount at liquity
- The uniswap reserves in the LUSD/ETH pair
We will first take the minimum of
min( <ETH present in Wallet> , <12.5% of the reserve pool of uniswap pair> ) // We will not use a bigger percentage of the uniswap pool because it will impact the prices heavily
Once the minimum is determined we will then check at liquity if we can redeem LUSD the amount of ETH swapped. If not , we will use the redeemableLUSD at liquity to determine the ETH used for swap.
Note: The smaller the percent of the uniswap pool we can use the better prices we can get, I have taken 12.5% of the pool for now because the pool reserves are small.
We can determine the arbitrage opportunity by the following formula.
profit = ETH_USED * (uniswapPrice/chainLinkPrice) * (1-redemptionFeePercentage) * (1-uinswap_liquidity_provider_fee) - GasCost
If we find that the profit is positive we will then invoke the smart contract.
To run the server 24/7 we will need a poller mechanism which will continously monitor the prices. To do this , this project will subscribe to the new block headers generated in the ethereum chain. This is done considering uniswap prices are updated after every block is mined.
Run the following commands
> yarn
> cp node_modules/@liquity/lib-ethers/deployments/default/kovan.json node_modules/@liquity/lib-ethers/dist/deployments/dev.json
After this add your private keys and wallet addresses to .env file.
yarn start
Once the server is started you will need to ping localhost:3000/subscribe
to start polling for prices. Without subscribing to events , the arbitrage bot will not work.
You can unsubscribe from the ethereum events by hitting localhost:3000/unsubscribe
.
Note: We use redeemLUSD function to determine the input values for the smart contract inputs, to run the redeemLUSD we need LUSD present in our wallet. So keep a amount of LUSD equivalent to the ETH in your wallet or the amount being arbitraged for.
PRIVATE_KEY='PVT_KEY_FROM_WALLET'
WSS_PROVIDER='wss://kovan.infura.io/ws/v3/API_KEY'
HTTPS_PROVIDER='https://kovan.infura.io/v3/API_KEY'
ACCOUNT_ADDRESS='WALLET_ADDRESS_OF_THE_PRIVATE_KEY'
Tested by creating a price difference in uniswap and chainlink
- Converted 0.778 ETH to 0.833 ETH ( 0.778 was choosen based on the wallet balance )
- Converted 1.01 eth to 1.05 eth & 900(wei) LUSD (1.01 eth was choosen based on the uniswap pool reserve )
- Converted 0.771 eth to 1.08 eth & 1860(wei) LUSD
- Converted 0.863 eth to 0.94 eth & 324(wei) LUSD