-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcher.js
169 lines (157 loc) · 6.8 KB
/
batcher.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
"use strict";
// Importing modules
const ethers = require("ethers");
const mongoose = require("mongoose");
const pino = require('pino');
const logger = pino({
level: process.env.PINO_LOG_LEVEL ?? 'info',
formatters: {
bindings: (bindings) => ({ pid: bindings.pid, host: bindings.hostname }),
level: (label) => ({ level: label.toUpperCase()}),
},
timestamp: pino.stdTimeFunctions.isoTime,
});
const util = require("util");
const {
hasUniswapCommands,
uniswapFullDecodedInput,
} = require("../uniswap-universal-decoder/universalDecoder");
// MongoDB connection open logic
const connectMongo = async (collectionName) => {
// Database Valuabless
const db = process.env.MONGODB_DB;
const user = process.env.MONGODB_USER;
const pass = process.env.MONGODB_PASSWORD;
const mongodb_host = process.env.MONGODB_SERVER;
const database =`mongodb://${user}:${pass}@${mongodb_host}:27017/${db}`;
await mongoose.connect(database)
.then(() =>
logger.info(`Connection to the MongoDB has successfully opened`)
)
.catch(err =>
logger.error(err, `Connection to the MongoDB has error`)
);
return mongoose.connection.collection(collectionName)
}
// MongoDB connection close logic
const closeMongo = async () => {
await mongoose.connection.close()
.then(() =>
logger.info(`Connection to the MongoDB has successfully closed`)
)
.catch(err =>
logger.error(err, `Closing the Mongo DB connection has error`)
);
return mongoose.connection;
}
// Retriving Block Range Array
const indexRange = (start, end) =>
Array.from({ length: end - start + 1 }, (_, index) => start + index);
// Converting Day to block
const daysToBlock = (currentBlock, daysAgo, l1Orl2) => {
const blockFor1Day = (l1Orl2==="l1") ? 5760 : 43200;
const pastBlockNumber = currentBlock - parseInt(blockFor1Day*daysAgo);
return (pastBlockNumber < 0) ? (() => {throw new Error('Exceeding genesis block number')})() : pastBlockNumber
}
// block call separation
const callBlockSeparation = (pastBlockNumber, currentBlockNumber, rangeSize) => {
const createRangesArray = (start, end, rangeSize) =>
(start > end)
? []
: [[start, Math.min(start + rangeSize - 1, end)], ...createRangesArray(Math.min(start + rangeSize - 1, end) + 1, end, rangeSize)];
(pastBlockNumber > currentBlockNumber)
? (() => {throw new Error('pastBlockNumber is greater than currentBlockNumber')})()
: createRangesArray (pastBlockNumber, currentBlockNumber, rangeSize);
return (currentBlockNumber - pastBlockNumber < rangeSize ) ? [[pastBlockNumber, currentBlockNumber]] : createRangesArray(pastBlockNumber, currentBlockNumber, rangeSize)
}
// Designated day to Block
const daysToBlockRangeArray = async (args) => {
const wssUrl = args["wss"];
const layer = args["layer"];
const days = args["retrieveDays"]
const rangeSize = args["blockRangeSize"]
const provider = new ethers.WebSocketProvider(wssUrl);
const currentBlock = await provider.getBlockNumber();
const startBlock = daysToBlock(currentBlock, days, layer);
const blockRangeArray = callBlockSeparation(startBlock, currentBlock, rangeSize);
return blockRangeArray
}
// getBlockHeaderList
const getBlockHeaderList = async (args, startBlock, endBlock) => {
const wssUrl = args["wss"];
const provider = new ethers.WebSocketProvider(wssUrl);
// Getting Block list
const blockRange = indexRange(startBlock, endBlock);
// getting range of block hash
const blockHeaderList = await Promise.all(blockRange.map(async (i) => {
const blockHeader = await provider.getBlock(i);
return blockHeader
}));
return blockHeaderList
}
// Batch Registering Uniswap Universal Router data
const batchRegister = async (args, blockHeaderList, collection) => {
const router = args["router"];
const wssUrl = args["wss"];
const layer = args["layer"];
const provider = new ethers.WebSocketProvider(wssUrl);
await Promise.all(blockHeaderList.map(async (i) => {
const blockHeader = i;
const blockHashList = blockHeader["transactions"];
// Getting Transactions from Block
await Promise.all(blockHashList.map(async (j) => {
const txnData = await provider.getTransaction(j);
(txnData["to"] === router && hasUniswapCommands(txnData["data"]))
? (async () => {
const decodedData = uniswapFullDecodedInput(txnData["data"]);
const fullData = {...txnData, "decodedData": decodedData, "blockHeader": blockHeader, "createdAt": new Date()}
const jsonData = JSON.stringify(fullData, (_, v) => typeof v === 'bigint' ? v.toString() : v);
// Block Receipt Registering
await collection.insertOne(JSON.parse(jsonData))
.then(result => logger.info({insertedId: result["insertedId"]},`Layer: ${layer}, Block: ${txnData["blockNumber"]}, Hush: ${j}, decoded data inserted`))
.catch(err => logger.error(err, `Layer: ${layer}, Block: ${txnData["blockNumber"]}, Hush: ${j}, decoded data insert error!`));
})()
: null;
}));
}));
};
// Bulk registering logic
const registerBulk = async (args) => {
const blockRangeArray = await daysToBlockRangeArray(args);
const layer = args["layer"];
logger.info({settings: args}, `${layer} Setting arguments`);
// connecting MongoDb
const collection = await connectMongo(args["receiptCollectionName"]);
// Recursive calling with delay
const sequenceCall = async (index, fn, delay=args["waitTime"]) => {
const startBlock = blockRangeArray[index][0];
const endBlock = blockRangeArray[index][1];
fn(index);
return index > 0 ? await new Promise(() => setTimeout(async ()=> {
try {
const blockHeaderList = await getBlockHeaderList(args, startBlock, endBlock);
await batchRegister(args,blockHeaderList, collection);
await sequenceCall(index -1, fn);
}
catch(err) {
logger.fatal(err, `Layer: ${layer} Network Connection Problem Happend!`);
await sequenceCall(index -1, fn);
}
}, delay)) : index;
}
return await sequenceCall((blockRangeArray.length -1) , (index)=> {
logger.info(`${layer} Remaining Job: ${index}`);
(index === 0) ? logger.info(`${layer} Batch job successfully ended`) : null ;
});
}
module.exports = {
connectMongo,
closeMongo,
indexRange,
daysToBlock,
callBlockSeparation,
daysToBlockRangeArray,
getBlockHeaderList,
registerBulk,
logger
};