8000 minor cleaning up · lazycoder1/liquity-arbitrage@fa22dd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa22dd6

Browse files
committed
minor cleaning up
1 parent 79973a7 commit fa22dd6

File tree

6 files changed

+20
-131
lines changed

6 files changed

+20
-131
lines changed

app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ let subscription;
2222
let status;
2323

2424
app.get('/', async (req, res) => {
25-
console.log(process.env.FOO)
2625
res.send('Start arbitrage server by hitting url: <ip>:3000/subscribe' +
2726
'<br/>Unsubscribe by hitting url: <ip>:3000/unsubscribe')
2827
})

localstore.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"tx_ongoing": false,
3-
"TX_ONGOING": true
3+
"TX_ONGOING": false
44
}

migrations/1_initial_migration.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/contracts/arbitrage.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ contract arbitrage {
6969
}
7070

7171
function getEthBack() external payable {
72+
// Fail safe method incase the contract was used wrong.
7273
msg.sender.transfer(address(this).balance);
7374
}
7475
}

src/utils/prices.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const { CHAINLINK_ABI } = require('../common/abi/chainlinkABI.js')
44
const { arbitrageContractABI } = require('../common/abi/arbitrageABI')
55
const { ChainId, Token, Fetcher, Route, Trade, TradeType, TokenAmount } = require('@uniswap/sdk')
66
const { EthersLiquity } = require("@liquity/lib-ethers");
7+
const { toWei, fromWei, toBN } = require('web3-utils/src/index.js')
78
const abiDecoder = require('abi-decoder');
89
const ETH_NETWORK = ChainId.KOVAN
910
const NETWORK = "KOVAN"
10-
const Web3 = require("web3");
1111
const BN = require('bn.js');
1212
const ACCOUNT_ADDRESS = process.env.ACCOUNT_ADDRESS
1313
const LUSD = new Token(ETH_NETWORK, ADDRESSES[NETWORK]["LUSD"], 6)
@@ -28,20 +28,21 @@ const arbitrageStatus = async (provider, web3, wallet) => {
2828
const { uniswapPrice, chainLinkPrice, redemptionFee } = await fetchPrices(web3, trade, ethForSwap);
2929

3030
const priceRatio = uniswapPrice/chainLinkPrice;
31-
const ethUsed = parseFloat(web3.utils.fromWei(ethForSwap, 'ether'))
31+
const ethUsed = parseFloat(fromWei(ethForSwap, 'ether'))
3232
const ethAfterArbitrage = ethUsed * priceRatio * (1- redemptionFee);
3333
const profit = ethAfterArbitrage - ethUsed ;
34-
35-
if (profit > 0) {
36-
console.log("UNISWAP Price:%s", uniswapPrice.toString());
34+
35+
console.log("UNISWAP Price:%s", uniswapPrice.toString());
3736
console.log("Chainlink Price %s", chainLinkPrice.toString());
3837
console.log("Redemption fee: %s", redemptionFee.toString());
39-
console.log("Eth used: %s", web3.utils.fromWei(ethForSwap.toString(), 'ether'))
38+
console.log("Eth used: %s", fromWei(ethForSwap.toString(), 'ether'))
4039

4140
console.log("After Arbitrage(without fees): %d", priceRatio * ethUsed)
4241
console.log("After arbitrage: %d eth", ethAfterArbitrage);
4342
console.log("Total profit(inc gas cost): %d", profit)
44-
43+
44+
if (profit > 0) {
45+
4546
return { "status": 1, "amountIn": ethForSwap,"populatedRedemption": populatedRedemption, 'profit': profit }
4647
} else {
4748
return { "status": 0, "amountIn": ethForSwap,"populatedRedemption": populatedRedemption, 'profit': profit }
@@ -51,8 +52,8 @@ const arbitrageStatus = async (provider, web3, wallet) => {
5152
const getFeasibleTrade = async (liquity, pair, wallet) => {
5253

5354
const ethUniswapReserve =
54-
Web3.utils.toBN(Web3.utils.toWei(pair.reserve0.toSignificant(8), 'Mwei')).div(PART_OF_LIQUIDITY_POOL_TO_USE)
55-
const ethBalanceInWallet = Web3.utils.toBN((await wallet.getBalance()).toString())
55+
toBN(toWei(pair.reserve0.toSignificant(8), 'Mwei')).div(PART_OF_LIQUIDITY_POOL_TO_USE)
56+
const ethBalanceInWallet = toBN((await wallet.getBalance()).toString())
5657

5758
let ethTradeAmout;
5859
if (ethBalanceInWallet.lt(ethUniswapReserve)) {
@@ -63,16 +64,16 @@ const getFeasibleTrade = async (liquity, pair, wallet) => {
6364

6465
const route = new Route([pair], WETH)
6566
let trade = new Trade(route, new TokenAmount(WETH, ethTradeAmout), TradeType.EXACT_INPUT)
66-
let lusdObtainedFromUni = parseFloat(Web3.utils.fromWei(trade.outputAmount.toSignificant(6), 'micro'))
67+
let lusdObtainedFromUni = parseFloat(fromWei(trade.outputAmount.toSignificant(6), 'micro'))
6768

6869
let populatedRedemption = await liquity.populate.redeemLUSD(lusdObtainedFromUni.toString())
6970
.catch(function(error){
7071
console.log('possibly wallet does not contain any LUSD. Keep a standing balance' + error)
7172
});
7273

73-
let lusdTradeAmount = Web3.utils.toWei(populatedRedemption.redeemableLUSDAmount.toString(), 'ether')
74+
let lusdTradeAmount = toWei(populatedRedemption.redeemableLUSDAmount.toString(), 'ether')
7475
trade = new Trade(route, new TokenAmount(LUSD, lusdTradeAmount), TradeType.EXACT_OUTPUT)
75-
ethTradeAmout = Web3.utils.toBN(Web3.utils.toWei(trade.inputAmount.toFixed(), 'Mwei'))
76+
ethTradeAmout = toBN(toWei(trade.inputAmount.toFixed(), 'Mwei'))
7677

7778
return { "trade": trade, "ethForSwap": ethTradeAmout, "populatedRedemption": populatedRedemption };
7879
}
@@ -83,33 +84,22 @@ const fetchPrices = async (web3, trade, ethForSwap) => {
8384
const roundData = await priceFeed.methods.latestRoundData().call()
8485
const chainLinkPrice = roundData['answer'] / (10 ** 8)
8586

86-
const outputAmount = web3.utils.toBN(web3.utils.toWei(trade.outputAmount.toFixed(), "Mwei"))
87+
const outputAmount = toBN(toWei(trade.outputAmount.toFixed(), "Mwei"))
8788

8889
const troveManager = new web3.eth.Contract(TroveManagerABI, ADDRESSES[NETWORK]["TroveManager"])
8990
const redemptionFeeInWei = await troveManager.methods.getRedemptionRate().call()
9091

9192
return {
9293
"uniswapPrice": outputAmount.div(ethForSwap).toNumber(),
9394
"chainLinkPrice": chainLinkPrice,
94-
"redemptionFee": parseFloat(web3.utils.fromWei(redemptionFeeInWei, 'ether'))
95+
"redemptionFee": parseFloat(fromWei(redemptionFeeInWei, 'ether'))
9596
}
9697
}
9798

9899
const executeArbitrage = async (amountIn, populatedRedemption, profit, web3) => {
99100
const decodedRedemptionInput = abiDecoder.decodeMethod(populatedRedemption.rawPopulatedTransaction.data)
100-
console.log('----------------------------------------------------------')
101-
console.log('Contract inputs - '+
102-
'\nEth sent - ' + amountIn.toString() +
103-
'\ninput 1 - ' + decodedRedemptionInput['params'][0]['value'] +
104-
'\ninput 2 - ' + decodedRedemptionInput['params'][1]['value'] +
105-
'\ninput 3 - ' + decodedRedemptionInput['params'][2]['value'] +
106-
'\ninput 4 - ' + decodedRedemptionInput['params'][3]['value'] +
107-
'\ninput 5 - ' + decodedRedemptionInput['params'][4]['value'] +
108-
'\ninput 6 - ' + decodedRedemptionInput['params'][5]['value'] +
109-
'\ninput 7 - ' + decodedRedemptionInput['params'][6]['value'] +
110-
'address - ' + ACCOUNT_ADDRESS
111-
)
112-
console.log('----------------------------------------------------------')
101+
console.log('Contract inputs \n'+ amountIn.toString())
102+
console.log(decodedRedemptionInput)
113103

114104
const LIQUITY_ARBITRAGE_CONTRACT = new web3.eth.Contract(arbitrageContractABI, ADDRESSES[NETWORK]["LIQUITY_ARBITRAGE"])
115105
const tx = LIQUITY_ARBITRAGE_CONTRACT.methods.ethToLusdAndBackArbitrage(
@@ -127,7 +117,7 @ 8372 @ const executeArbitrage = async (amountIn, populatedRedemption, profit, web3) =>
127117
console.log('The transaction will fail. Wait for a different opportunity' + error)
128118
});
129119
const gasPrice = await web3.eth.getGasPrice();
130-
const gasCost = parseFloat(web3.utils.fromWei(new BN(gas).mul(new BN(gasPrice)), 'ether'))
120+
const gasCost = parseFloat(fromWei(new BN(gas).mul(new BN(gasPrice)), 'ether'))
131121

132122
if ( profit - gasCost < 0) {
133123
console.log('Gas Cost will eat up the profits , not worth doing the arbitrage')

truffle-config.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0