-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathABILogs.js
75 lines (64 loc) · 1.81 KB
/
ABILogs.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
//ABILogs
//npm install abi-decoder
//npm install @ethersproject/abi
const { addABI, decodeLogs } = require("abi-decoder");
const { Interface } = require("@ethersproject/abi");
const { getToken } = require("./token/findToken");
const { getEthPrice, getPairAddress } = require("./uniswapV2SubGraph");
const { getUSDPrice } = require("./api/cryptoCompareApi");
const eventsJson = require("./ABIEvents");
const addEvents = async () => {
eventsJson.map(async e => {
let { text_signature } = e;
try {
let i = new Interface([text_signature]);
await addABI(i.fragments);
} catch (e) {
console.log(e);
}
});
};
addEvents();
const getAllLogs = async (_logs) => {
const ethPrice = await getEthPrice();
return await Promise.all(decodeLogs(_logs).map(async log => {
let { coin, logo, decimals } = getToken(log.address);
if(coin == "") {
let pair = await getPairAddress(log.address);
coin = pair.coin;
}
let ethValue = 0;
let usdValue = 0;
let value;
if((log.name == "Transfer" || log.name == "Swap") && coin !== 'WETH' && coin !== ""){
try {
usdValue = await getUSDPrice(coin);
} catch (e) {
console.log(e);
}
}
log.events.map(async e => {
if ((log.name == "Transfer" || log.name == "Swap") && e.type.match("uint")) {
value = parseFloat(e.value / 10 ** decimals).toFixed(2);
if (coin === 'WETH') {
ethValue = parseFloat(value * ethPrice).toFixed(2);
} else if(coin !== "") {
if(usdValue > 0) ethValue = parseFloat(value * usdValue).toFixed(2);
}
}
});
log.coin = {
address: log.address,
name: coin,
event: log.name,
logo,
decimals,
value,
ethValue
};
return log;
}));
}
module.exports = {
getAllLogs
}