-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
313 lines (285 loc) · 9.06 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
require('dotenv').config()
const mongoose = require('mongoose')
mongoose.connect('mongodb://mongo:27017/flashbot')
const TokenModel = mongoose.model('Token', {
network: String,
name: String,
address: String,
uniPoolAddr: String,
sushiPoolAddr: String,
uniAmount: String,
sushiAmount: String,
})
const Web3 = require("web3")
const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.INFURA_URL_ROPSTEN))
const WETH9 = require('./abi/WETH9.json')
const UniswapV3RouterAbi = require("./abi/UniswapV3Router.json")
const SushiswapV2Router02Abi = require('./abi/SushiswapV2Router02.json')
const UniswapV3FactoryAbi = require("./abi/UniswapV3Factory.json")
const SushiswapV2FactoryAbi = require('./abi/SushiswapV2Factory.json')
const UniswapV3PoolAbi = require("./abi/UniswapV3Pool.json")
const SushiswapV2PairAbi = require('./abi/IUniswapV2Pair.json')
// ropsten specific
const addresses = {
WETH: '0xc778417E063141139Fce010982780140Aa0cD5Ab',
uniFactory: '0x1F98431c8aD98523631AE4a59f267346ea31F984',
sushiFactory: '0xc35DADB65012eC5796536bD9864eD8773aBc74C4',
uniRouter: '0xE592427A0AEce92De3Edee1F18E0157C05861564',
sushiRouter: '0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506'
}
// RIP HDWallet Provider, still unable to .on websockets
const account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY)
const uniFactory = new web3.eth.Contract(
UniswapV3FactoryAbi,
addresses.uniFactory,
);
const sushiFactory = new web3.eth.Contract(
SushiswapV2FactoryAbi,
addresses.sushiFactory,
)
const uniRouter = new web3.eth.Contract(
UniswapV3RouterAbi,
addresses.uniRouter,
);
const sushiRouter = new web3.eth.Contract(
SushiswapV2Router02Abi,
addresses.sushiRouter,
);
const weth9 = new web3.eth.Contract(
WETH9,
addresses.WETH
);
(async () => {
console.log(account.address)
console.log(await web3.eth.getTransactionCount(account.address, 'pending'))
})()
uniFactory.events.PoolCreated({
fromBlock: 'latest'
}, (err, event) => console.log(event))
.on('data', async (event) => {
const {
token0,
token1,
fee,
tickSpacing,
pool
} = event.returnValues
let tokenIn, tokenOut
if(token0 === addresses.WETH) {
tokenIn = token0;
tokenOut = token1;
} else if(token1 == addresses.WETH) {
tokenIn = token1;
tokenOut = token0;
} else {
console.log('not weth')
return
}
const params = {
tokenIn,
tokenOut,
fee,
recipient: account.address,
deadline: Math.floor(Date.now() / 1000) + 900,
amountIn: web3.utils.toWei('0.000001', 'ether'),
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}
const swap = await uniRouter.methods.exactInputSingle(params)
let encodedSwap = swap.encodeABI()
let tx = {
nonce: await web3.eth.getTransactionCount(account.address, 'pending'),
gas: 500000,
data: swap.encodeABI(),
from: account.address,
// this is why inputting eth for swap needn't have an approve
value: web3.utils.toWei('0.000001', 'ether'),
to: addresses.uniRouter
}
web3.eth.accounts.signTransaction(tx, account.privateKey, (error, stx) => {
if (error) {
console.log(error)
} else {
web3.eth.sendSignedTransaction(stx.rawTransaction).on('receipt', async (receipt) => {
console.log(receipt.logs)
const record = await TokenModel.findOneAndUpdate(
{address: tokenOut},
{
uniPoolAddr: pool,
uniAmount: web3.utils.hexToNumberString(receipt.logs[0].data),
}
)
if (!record) {
const newRecord = new TokenModel({
network: 'Ropsten',
name: 'Token',
address: tokenOut ,
uniPoolAddr: pool,
uniAmount: web3.utils.hexToNumberString(receipt.logs[0].data),
})
newRecord.save()
}
})
}
})
}
)
.on('error', console.error)
sushiFactory.events.PairCreated({
fromBlock: 'latest'
}, (err, event) => console.log(event))
.on('data', async (event) => {
console.log(event)
const {
token0,
token1,
pair
} = event.returnValues
let tokenIn, tokenOut
if(token0 === addresses.WETH) {
tokenIn = token0;
tokenOut = token1;
} else if(token1 == addresses.WETH) {
tokenIn = token1;
tokenOut = token0;
} else {
console.log('not weth')
return
}
const amountIn = web3.utils.toWei('0.000001')
const amounts = await sushiRouter.methods.getAmountsOut(amountIn, [tokenIn, tokenOut]).call()
console.log(amounts)
const amountOutMin = web3.utils.toBN(Math.floor(amounts[1] - (amounts[1] / 10)))
console.log(amountOutMin)
console.log(web3.utils.toBN(amountOutMin))
const swap = await sushiRouter.methods.swapExactETHForTokens(
amountOutMin,
[tokenIn, tokenOut],
account.address,
Date.now() + 1000 * 60* 10
)
let tx = {
nonce: await web3.eth.getTransactionCount(account.address, 'pending'),
gas: 500000,
data: swap.encodeABI(),
from: account.address,
// this is why inputting eth for swap needn't have an approve
value: web3.utils.toWei('0.000001', 'ether'),
to: addresses.sushiRouter
}
web3.eth.accounts.signTransaction(tx, account.privateKey, (error, stx) => {
if (error) {
console.log(error)
} else {
web3.eth.sendSignedTransaction(stx.rawTransaction).on('receipt', async (receipt) => {
console.log(receipt.logs)
const record = await TokenModel.findOneAndUpdate(
{address: tokenOut},
{
sushiPoolAddr: pair,
sushiAmount: web3.utils.hexToNumberString(receipt.logs[2].data),
}
)
if (!record) {
const newRecord = new TokenModel({
network: 'Ropsten',
name: 'Token',
address: tokenOut,
sushiPoolAddr: pair,
sushiAmount: web3.utils.hexToNumberString(receipt.logs[2].data),
})
newRecord.save()
} else {
}
})
}
})
})
.on('error', console.error);
web3.eth.subscribe("newBlockHeaders")
.on("connected", () => console.log('listening to blocks'))
.on("data", async (blockHeader) => {
const tokens = await TokenModel.find({
uniPoolAddr: {$exists: true},
sushiPoolAddr: {$exists: true}
});
tokens.forEach(async (token) => {
const uniPool = new web3.eth.Contract(
UniswapV3PoolAbi,
token.uniPoolAddr
)
const uniToken0 = await uniPool.methods.token0().call()
const uniToken1 = await uniPool.methods.token1().call()
const slot0 = await uniPool.methods.slot0().call()
let uniPrice = ((slot0.sqrtPriceX96*slot0.sqrtPriceX96)) / (2**(2*96))
if (uniToken0 == addresses.WETH) {
uniPrice = 1/uniPrice
}
console.log('uniprice', uniPrice)
const pairAddress = await sushiFactory.methods.getPair(addresses.WETH, token.address).call()
console.log(pairAddress)
const sushiPair = new web3.eth.Contract(
SushiswapV2PairAbi,
pairAddress
)
const sushiToken0 = await sushiPair.methods.token0().call()
const sushiToken1 = await sushiPair.methods.token1().call()
const reserves = await sushiPair.methods.getReserves().call()
let sushiPrice;
if (sushiToken0 == addresses.WETH) {
sushiPrice = reserves[0]/reserves[1]
}
if (sushiToken1 == addresses.WETH) {
sushiPrice = reserves[1]/reserves[0]
}
console.log('sushi sushiPrice', sushiPrice)
})
})
.on("error", console.error);
/*
(async () => {
const tokens = await TokenModel.find({
uniPoolAddr: {$exists: true},
sushiPoolAddr: {$exists: true}
});
const uniPool = new web3.eth.Contract(
UniswapV3PoolAbi,
tokens[0].uniPoolAddr
)
const token0 = await uniPool.methods.token0().call()
const token1 = await uniPool.methods.token1().call()
const slot0 = await uniPool.methods.slot0().call()
let price = ((slot0.sqrtPriceX96*slot0.sqrtPriceX96)) / (2**(2*96))
if (token0 == addresses.WETH) {
price = 1/price
}
console.log('uniprice', price)
})();
(async () => {
const tokens = await TokenModel.find({
uniPoolAddr: {$exists: true},
sushiPoolAddr: {$exists: true}
});
console.log(tokens[0])
const pairAddress = await sushiFactory.methods.getPair(addresses.WETH, tokens[0].address).call()
console.log(pairAddress)
const sushiPair = new web3.eth.Contract(
SushiswapV2PairAbi,
pairAddress
)
const token0 = await sushiPair.methods.token0().call()
console.log(token0)
const token1 = await sushiPair.methods.token1().call()
console.log(token1)
const reserves = await sushiPair.methods.getReserves().call()
let price;
if (token0 == addresses.WETH) {
price = reserves[0]/reserves[1]
}
if (token1 == addresses.WETH) {
price = reserves[1]/reserves[0]
}
console.log('sushi price', price)
//const quote = await sushiRouter.methods.quote().call()
})();
*/