-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.js
385 lines (345 loc) · 14.6 KB
/
transaction.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { routerDecoder, factoryDecoder, pairDecoder, oneinchDecoder, readTokenBasic, readPairBasic, readCachedTokenBasic } from './contractReader.js'
import {E18, REPORT_THRESHOLD_DOLLAR, ETH_PRICE_APPROX} from './const'
import {getPrices, getCachedPrice, cutDecimals} from './price'
import { saveActionsToDatabase } from './database'
import { isTrustedCoin, USD_STABLECOINS, BTC_STABLECOINS } from './coinlist'
import logger from './common/logger'
const QUEUE_SIZE = 3
var routerQueue = []
function combine(a, b) {
return Object.assign({}, a, b);
}
function isLargeETH(amount) {
amount = parseInt(amount)
return amount > REPORT_THRESHOLD_DOLLAR * E18 / ETH_PRICE_APPROX
}
function isLargeToken(tokenInfo, amount, price) {
amount = parseInt(amount) / Math.pow(10, tokenInfo.decimals)
return amount * price > REPORT_THRESHOLD_DOLLAR
}
// convert token amount to decimals, get token symbols, calculate dollar amount
// return null if not successful or transaction not reportable
async function processRawActionData(action) {
action.token0 = action.token0.toLowerCase()
action.token1 = action.token1.toLowerCase()
var token0Info = await readTokenBasic(action.token0)
var token1Info = await readTokenBasic(action.token1)
action.token0Name = token0Info.symbol
action.token1Name = token1Info.symbol
action.token0Amount = cutDecimals(action.token0Amount, token0Info.decimals)
action.token1Amount = cutDecimals(action.token1Amount, token1Info.decimals)
var token0Sym = token0Info.symbol.toLowerCase()
var token1Sym = token1Info.symbol.toLowerCase()
// if either one of the token is trusted (according to trusted list)
// use that to decide dollarValue
var eitherTrusted = false
if (isTrustedCoin(action.token0)) {
var prices = await getPrices(token0Sym)
action.token0Price = prices[token0Sym]
action.dollarValue = action.token0Price * action.token0Amount
action.token1Price = action.dollarValue * 0.997 / action.token1Amount
eitherTrusted = true
}
if (isTrustedCoin(action.token1)) {
var prices = await getPrices(token1Sym)
action.token1Price = prices[token1Sym]
action.dollarValue = action.token1Price * action.token1Amount
action.token0Price = action.dollarValue * 1.003 / action.token0Amount
eitherTrusted = true
}
if (!eitherTrusted) {
// get both prices from coingecko
var prices = await getPrices([token0Sym, token1Sym])
action.token0Price = prices[token0Sym]
action.token1Price = prices[token1Sym]
var v0 = action.token0Amount * action.token0Price
var v1 = action.token1Amount * action.token1Price
if (v0 < REPORT_THRESHOLD_DOLLAR || v1 < REPORT_THRESHOLD_DOLLAR) {
// either side is lower than reportable limit, do not report
return null
}
action.dollarValue = (v0 + v1) / 2
}
if (!action.dollarValue || isNaN(action.dollarValue)) {
logger.log('[processRawActionData] error: invalid dollar amount', action)
return null
}
if (action.dollarValue < REPORT_THRESHOLD_DOLLAR) return null
// ignore swapping stablecoins
if (USD_STABLECOINS[action.token0Name] && USD_STABLECOINS[action.token1Name]) return null
if (BTC_STABLECOINS[action.token0Name] && BTC_STABLECOINS[action.token1Name]) return null
return action
}
function processFactoryCall(tx) {
var calldata = factoryDecoder._decodeMethod(tx.input.toLowerCase())
console.log(calldata)
}
function processRouterCall(tx) {
var calldata = routerDecoder._decodeMethod(tx.input.toLowerCase())
calldata = combine(calldata, tx)
routerQueue.push(calldata)
if (routerQueue.length >= QUEUE_SIZE) {
batchProcessRouterCalls()
routerQueue = []
}
}
async function batchProcessRouterCalls() {
// clone the queue
var txs = JSON.parse(JSON.stringify(routerQueue))
// decide if reportable
var reportable = []
var requirePricesTokens = {}
for (var tx of txs) {
if (tx.name.includes('ETH')) {
if (tx.name === 'swapTokensForExactETH') {
tx.params.forEach(item => {
if (item.name === 'amountOut' && isLargeETH(item.value)) {
reportable.push(tx)
}
})
}
else if (tx.name === 'swapExactTokensForETH') {
tx.params.forEach(item => {
if (item.name === 'amountOutMin' && isLargeETH(item.value)) {
reportable.push(tx)
}
})
}
else if (isLargeETH(tx.value)){ // swap ETH for ...
reportable.push(tx)
}
else {
tx.params.forEach(item => {
if (item.name === 'amountETHMin' && isLargeETH(item.value)) { // add or remove liquidity
reportable.push(tx)
}
})
}
}
else {
// swaping tokens to tokens
// get all the prices
// note: price can be inaccurate because of duplicate symbols
// need to further decide the actual dollar value
if (tx.name.includes('swap')) {
for (var item of tx.params) {
if (item.name === 'path') {
for (var address of item.value) {
var tokenInfo = await readTokenBasic(address)
var symbol = tokenInfo.symbol.toLowerCase()
requirePricesTokens[symbol] = true
}
}
}
}
else {
for (var item of tx.params) {
if (item.name === 'tokenA' || item.name === 'tokenB') {
var tokenInfo = await readTokenBasic(item.value)
var symbol = tokenInfo.symbol.toLowerCase()
requirePricesTokens[symbol] = true
}
}
}
}
}
// need to check both tokens in case the price of one of them is not available
var tokenPrices = await getPrices(Object.keys(requirePricesTokens))
for (var tx of txs) {
if (!tx.name.includes('ETH')) {
var txParams = {'0': {}, '1': {}}
if (tx.name.includes('Liquidity')) {
// add liquity of two tokens (if one side is ETH, already handled above)
tx.params.forEach(item => {
if (item.name === 'tokenA') {
txParams['0'].token = item.value
}
else if (item.name === 'amountAMin') {
txParams['0'].amount = item.value
}
else if (item.name === 'tokenB') {
txParams['1'].token = item.value
}
else if (item.name === 'amountBMin') {
txParams['1'].amount = item.value
}
})
}
else if (tx.name.includes('swap')) {
tx.params.forEach(item => {
if (item.name === 'amountIn' || item.name === 'amountInMax') {
txParams['0'].amount = item.value
}
else if (item.name === 'path') {
txParams['0'].token = item.value[0]
txParams['1'].token = item.value[item.value.length - 1]
}
else if (item.name === 'amountOutMin' || item.name === 'amountOut') {
txParams['1'].amount = item.value
}
})
}
var token0Info = await readTokenBasic(txParams['0'].token)
var token1Info = await readTokenBasic(txParams['1'].token)
if (!token0Info || !token1Info ) continue
var price0 = tokenPrices[token0Info.symbol.toLowerCase()]
var price1 = tokenPrices[token1Info.symbol.toLowerCase()]
if ((price0 && isLargeToken(token0Info, txParams['0'].amount, price0)) || (price1 && isLargeToken(token1Info, txParams['1'].amount, price1))) {
reportable.push(tx)
}
}
}
await reportTransactions(reportable)
}
async function reportTransactions(txs) {
var storeList = [] // to be stored in database
for (var tx of txs) {
var decodedLogs = pairDecoder._decodeLogs(tx.logs)
var decodedFactoryLogs = factoryDecoder._decodeLogs(tx.logs) // will be an empty list if no factory calls
// PairCreated Event is from Factory
decodedLogs = decodedLogs.concat(decodedFactoryLogs)
var action = {} // name, sender, from, to, fromAmount, toAmount
if (tx.name.includes('swap')) {
var swapLogNumber = 0
for (var logEvent of decodedLogs) {
if (!logEvent || !logEvent.name) continue
if (logEvent.name === 'Swap') {
// get amount
for (var event of logEvent.events) {
if (swapLogNumber === 0) {
if (event.name === 'amount0In' && parseInt(event.value) != 0) {
// token0 is source token
action.token0Amount = parseInt(event.value)
}
else if (event.name === 'amount1In' && parseInt(event.value) != 0) {
action.token0Amount = parseInt(event.value)
}
}
// eventuall these will save info of last swap
if (event.name === 'amount0Out' && parseInt(event.value) != 0) {
// token1 is destination token
action.token1Amount = parseInt(event.value)
}
else if (event.name === 'amount1Out' && parseInt(event.value) != 0) {
action.token1Amount = parseInt(event.value)
}
}
swapLogNumber++
}
}
// get first and last token on the path
// one of them could be WETH
for (var item of tx.params) {
if (item.name === 'path') {
action.token0 = item.value[0]
action.token1 = item.value[item.value.length - 1]
}
}
action.name = 'Swap'
}
else if (tx.name.includes('addLiquidity')) {
for (var logEvent of decodedLogs) {
if (!logEvent || !logEvent.name) continue
if (logEvent.name === 'Mint') { // Mint LP tokens
action.token0Amount = parseInt(logEvent.events[1].value)
action.token1Amount = parseInt(logEvent.events[2].value)
var pairInfo = await readPairBasic(logEvent.address) // pair address
action.token0 = pairInfo.token0
action.token1 = pairInfo.token1
}
else if (logEvent.name === 'PairCreated') { // new pair created
action.newPair = true
}
}
action.name = 'AddLiquidity'
}
else if (tx.name.includes('removeLiquidity')) {
for (var logEvent of decodedLogs) {
if (!logEvent || !logEvent.name) continue
if (logEvent.name === 'Burn') {
action.token0Amount = parseInt(logEvent.events[1].value)
action.token1Amount = parseInt(logEvent.events[2].value)
var pairInfo = await readPairBasic(logEvent.address) // pair address
action.token0 = pairInfo.token0
action.token1 = pairInfo.token1
}
}
action.name = 'RemoveLiquidity'
}
action = await processRawActionData(action)
if (!action) continue
action.sender = tx.from
action.hash = tx.hash
action.timestamp = tx.timestamp
action.market = tx.market
logger.log(action)
storeList.push(action)
}
if (storeList.length > 0) {
await saveActionsToDatabase(storeList)
}
}
async function process1inchCall(tx) {
var methodID = tx.input.slice(2, 10).toLowerCase()
if (methodID !== '90411a32' && methodID !== '34b0793b') return
var decodedLogs = oneinchDecoder._decodeLogs(tx.logs)
for (var logItem of decodedLogs) {
if (logItem && logItem.name === 'Swapped') {
var action = {} // name, sender, from, to, fromAmount, toAmount
action.name = 'Swap'
for (var event of logItem.events) {
switch (event.name) {
case 'sender':
action.sender = event.value
break
case 'srcToken':
action.token0 = event.value
break
case 'dstToken':
action.token1 = event.value
break
case 'spentAmount':
action.token0Amount = event.value
break
case 'returnAmount':
action.token1Amount = event.value
break
default:
break
}
}
try {
action = await processRawActionData(action)
}
catch (e) {
logger.log('[process1inchCall] error', e)
return
}
if (!action) return
action.hash = tx.hash
action.timestamp = tx.timestamp
action.market = tx.market
logger.log(action)
saveActionsToDatabase([action])
return
}
}
}
export function processUniswapTransaction(tx) {
if (tx.input && tx.input.length > 0) {
tx.market = "uniswap"
processRouterCall(tx)
}
}
export function processSushiswapTransaction(tx) {
if (tx.input && tx.input.length > 0) {
tx.market = "sushiswap"
processRouterCall(tx)
}
}
export function process1inchTransaction(tx) {
if (tx.input && tx.input.length > 0) {
tx.market = "1inch"
process1inchCall(tx)
}
}