forked from dappuniversity/price-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (185 loc) · 6.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const http = require('http')
const moment = require('moment-timezone')
const numeral = require('numeral')
const _ = require('lodash')
const axios = require('axios')
// ethereum dependencies
const ethers = require('ethers');
const { parseUnits, formatUnits } = ethers.utils;
const { legos } = require('@studydefi/money-legos');
// SERVER CONFIG
const PORT = process.env.PORT || 5001
const app = express();
const server = http.createServer(app).listen(PORT, () => console.log(`Listening on ${ PORT }`))
// ETHERS CONFIG
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
// Contract UniswapV2Router02 https://etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#code
const uniswapV2 = new ethers.Contract(
'0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
[
'function getAmountsOut(uint256 amountIn, address[] calldata path) external view returns (uint256[] memory amounts)',
'function WETH() external pure returns (address)'
],
provider
);
// Contract Kyber Network V2
const kyber = new ethers.Contract(legos.kyber.network.address, legos.kyber.network.abi, provider);
async function checkPair(args) {
const { inputTokenSymbol, inputTokenAddress, outputTokenSymbol, outputTokenAddress, inputAmount } = args
// calculate uniswap amount
const path = [inputTokenAddress, outputTokenAddress];
const amounts = await uniswapV2.getAmountsOut(inputAmount, path);
const uniswapAmount = amounts[1];
// calculate kyber amount
const { expectedRate, slippageRate } = await kyber.getExpectedRate(inputTokenAddress, outputTokenAddress, inputAmount);
const kyberExpectedAmount = expectedRate;
const kyberSlippageAmount = slippageRate;
console.table([{
'Input Token': inputTokenSymbol,
'Output Token': outputTokenSymbol,
'Input Amount': formatUnits(inputAmount, 18),
'Uniswap Return': formatUnits(uniswapAmount, 18),
'Kyber Expected Rate': formatUnits(kyberExpectedAmount, 18),
'Kyber Min Return': formatUnits(kyberSlippageAmount, 18),
'Timestamp': moment().tz('America/Chicago').format(),
}])
}
let priceMonitor
let monitoringPrice = false
async function monitorPrice() {
if(monitoringPrice) {
return
}
console.log("Checking prices...")
monitoringPrice = true
try {
// ADD YOUR CUSTOM TOKEN PAIRS HERE!!!
const WETH_ADDRESS = await uniswapV2.WETH(); // Uniswap V2 uses wrapped eth
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'DAI',
outputTokenAddress: '0x6b175474e89094c44da98b954eedeac495271d0f',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'KNC',
outputTokenAddress: '0xdd974d5c2e2928dea5f71b9825b8b646686bd200',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'LINK',
outputTokenAddress: '0x514910771af9ca656af840dff83e8264ecf986ca',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'MKR',
outputTokenAddress: '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'MATIC',
outputTokenAddress: '0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'UNI',
outputTokenAddress: '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'BAT',
outputTokenAddress: legos.erc20.bat.address,
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'SNX',
outputTokenAddress: '0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'BNT',
outputTokenAddress: '0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: '1INCH',
outputTokenAddress: '0x111111111117dC0aa78b770fA6A738034120C302',
inputAmount: parseUnits('1', 18)
})
// Kyber Rates?
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'GETH',
outputTokenAddress: '0xdD69DB25F6D620A7baD3023c5d32761D353D3De9',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'GRT',
outputTokenAddress: '0xc944E90C64B2c07662A292be6244BDf05Cda44a7',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'ENS',
outputTokenAddress: '0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'EWTB',
outputTokenAddress: '0x178c820f862B14f316509ec36b13123DA19A6054',
inputAmount: parseUnits('1', 18)
})
// Uniswap Rates?
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'OCEAN',
outputTokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
inputAmount: parseUnits('1', 18)
})
await checkPair({
inputTokenSymbol: 'WETH',
inputTokenAddress: WETH_ADDRESS,
outputTokenSymbol: 'UBT',
outputTokenAddress: '0x8400D94A5cb0fa0D041a3788e395285d61c9ee5e',
inputAmount: parseUnits('1', 18)
})
} catch (error) {
console.error(error)
monitoringPrice = false
clearInterval(priceMonitor)
return
}
monitoringPrice = false
}
// Check markets every 3 seconds
const POLLING_INTERVAL = process.env.POLLING_INTERVAL || 3000 // 3 Seconds
priceMonitor = setInterval(async () => { await monitorPrice() }, POLLING_INTERVAL)