@@ -4,10 +4,10 @@ const { CHAINLINK_ABI } = require('../common/abi/chainlinkABI.js')
4
4
const { arbitrageContractABI } = require ( '../common/abi/arbitrageABI' )
5
5
const { ChainId, Token, Fetcher, Route, Trade, TradeType, TokenAmount } = require ( '@uniswap/sdk' )
6
6
const { EthersLiquity } = require ( "@liquity/lib-ethers" ) ;
7
+ const { toWei, fromWei, toBN } = require ( 'web3-utils/src/index.js' )
7
8
const abiDecoder = require ( 'abi-decoder' ) ;
8
9
const ETH_NETWORK = ChainId . KOVAN
9
10
const NETWORK = "KOVAN"
10
- const Web3 = require ( "web3" ) ;
11
11
const BN = require ( 'bn.js' ) ;
12
12
const ACCOUNT_ADDRESS = process . env . ACCOUNT_ADDRESS
13
13
const LUSD = new Token ( ETH_NETWORK , ADDRESSES [ NETWORK ] [ "LUSD" ] , 6 )
@@ -28,20 +28,21 @@ const arbitrageStatus = async (provider, web3, wallet) => {
28
28
const { uniswapPrice, chainLinkPrice, redemptionFee } = await fetchPrices ( web3 , trade , ethForSwap ) ;
29
29
30
30
const priceRatio = uniswapPrice / chainLinkPrice ;
31
- const ethUsed = parseFloat ( web3 . utils . fromWei ( ethForSwap , 'ether' ) )
31
+ const ethUsed = parseFloat ( fromWei ( ethForSwap , 'ether' ) )
32
32
const ethAfterArbitrage = ethUsed * priceRatio * ( 1 - redemptionFee ) ;
33
33
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 ( ) ) ;
37
36
console . log ( "Chainlink Price %s" , chainLinkPrice . toString ( ) ) ;
38
37
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' ) )
40
39
41
40
console . log ( "After Arbitrage(without fees): %d" , priceRatio * ethUsed )
42
41
console . log ( "After arbitrage: %d eth" , ethAfterArbitrage ) ;
43
42
console . log ( "Total profit(inc gas cost): %d" , profit )
44
-
43
+
44
+ if ( profit > 0 ) {
45
+
45
46
return { "status" : 1 , "amountIn" : ethForSwap , "populatedRedemption" : populatedRedemption , 'profit' : profit }
46
47
} else {
47
48
return { "status" : 0 , "amountIn" : ethForSwap , "populatedRedemption" : populatedRedemption , 'profit' : profit }
@@ -51,8 +52,8 @@ const arbitrageStatus = async (provider, web3, wallet) => {
51
52
const getFeasibleTrade = async ( liquity , pair , wallet ) => {
52
53
53
54
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 ( ) )
56
57
57
58
let ethTradeAmout ;
58
59
if ( ethBalanceInWallet . lt ( ethUniswapReserve ) ) {
@@ -63,16 +64,16 @@ const getFeasibleTrade = async (liquity, pair, wallet) => {
63
64
64
65
const route = new Route ( [ pair ] , WETH )
65
66
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' ) )
67
68
68
69
let populatedRedemption = await liquity . populate . redeemLUSD ( lusdObtainedFromUni . toString ( ) )
69
70
. catch ( function ( error ) {
70
71
console . log ( 'possibly wallet does not contain any LUSD. Keep a standing balance' + error )
71
72
} ) ;
72
73
73
- let lusdTradeAmount = Web3 . utils . toWei ( populatedRedemption . redeemableLUSDAmount . toString ( ) , 'ether' )
74
+ let lusdTradeAmount = toWei ( populatedRedemption . redeemableLUSDAmount . toString ( ) , 'ether' )
74
75
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' ) )
76
77
77
78
return { "trade" : trade , "ethForSwap" : ethTradeAmout , "populatedRedemption" : populatedRedemption } ;
78
79
}
@@ -83,33 +84,22 @@ const fetchPrices = async (web3, trade, ethForSwap) => {
83
84
const roundData = await priceFeed . methods . latestRoundData ( ) . call ( )
84
85
const chainLinkPrice = roundData [ 'answer' ] / ( 10 ** 8 )
85
86
86
- const outputAmount = web3 . utils . toBN ( web3 . utils . toWei ( trade . outputAmount . toFixed ( ) , "Mwei" ) )
87
+ const outputAmount = toBN ( toWei ( trade . outputAmount . toFixed ( ) , "Mwei" ) )
87
88
88
89
const troveManager = new web3 . eth . Contract ( TroveManagerABI , ADDRESSES [ NETWORK ] [ "TroveManager" ] )
89
90
const redemptionFeeInWei = await troveManager . methods . getRedemptionRate ( ) . call ( )
90
91
91
92
return {
92
93
"uniswapPrice" : outputAmount . div ( ethForSwap ) . toNumber ( ) ,
93
94
"chainLinkPrice" : chainLinkPrice ,
94
- "redemptionFee" : parseFloat ( web3 . utils . fromWei ( redemptionFeeInWei , 'ether' ) )
95
+ "redemptionFee" : parseFloat ( fromWei ( redemptionFeeInWei , 'ether' ) )
95
96
}
96
97
}
97
98
98
99
const executeArbitrage = async ( amountIn , populatedRedemption , profit , web3 ) => {
99
100
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 )
113
103
114
104
const LIQUITY_ARBITRAGE_CONTRACT = new web3 . eth . Contract ( arbitrageContractABI , ADDRESSES [ NETWORK ] [ "LIQUITY_ARBITRAGE" ] )
115
105
const tx = LIQUITY_ARBITRAGE_CONTRACT . methods . ethToLusdAndBackArbitrage (
@@ -127,7 +117,7 @
8372
@ const executeArbitrage = async (amountIn, populatedRedemption, profit, web3) =>
127
117
console . log ( 'The transaction will fail. Wait for a different opportunity' + error )
128
118
} ) ;
129
119
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' ) )
131
121
132
122
if ( profit - gasCost < 0 ) {
133
123
console . log ( 'Gas Cost will eat up the profits , not worth doing the arbitrage' )
0 commit comments