-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher.js
More file actions
144 lines (135 loc) · 6.33 KB
/
Copy pathpublisher.js
File metadata and controls
144 lines (135 loc) · 6.33 KB
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
"use strict";
// Importing modules
const APP_SECRET = process.env.APP_SECRET.trim();
const ethers = require("ethers");
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");
// Publishing txpool data by Graphql Mutation call
const txpoolMutation = async (args) => {
const router = args["router"];
const wssUrl = args["wss"];
const layer = args["layer"];
const provider = new ethers.WebSocketProvider(wssUrl);
provider.on('pending', async (tx) => {
const txnData = await provider.getTransaction(tx);
(txnData)
? ((txnData["to"] === router && hasUniswapCommands(txnData["data"]))
? (async () => {
const decodedData = uniswapFullDecodedInput(txnData["data"]);
const fullData = {...txnData, "decodedData": decodedData, "createdAt": new Date()};
const query = createMutaionString(fullData, args["TxpoolMutation"], args["TxpoolMutationMethod"]);
await (callMutation(query))(args["graphql"]).
then(result => logger.info({result: result}, `${layer}: Txpool Data Request processed`)).
catch(error => logger.error(error, `${layer}: Txpool Data Publish Error!`));
})()
: null)
: null ;
})
};
// Publishing Transaction data by Graphql Mutation call
const txMutation = async (args) => {
const router = args["router"];
const wssUrl = args["wss"];
const layer = args["layer"];
const provider = new ethers.WebSocketProvider(wssUrl);
provider.on('block', async (tx) => {
const blockHeader = await provider.getBlock(tx);
const blockHashList = blockHeader["transactions"];
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 query = createMutaionString(fullData, args["TxnMutation"], args["TxnMutationMethod"]);
await (callMutation(query))(args["graphql"]).
then(result => logger.info({result: result}, `${layer}: Transaction Data Request Processed`)).
catch(error => logger.error(error, `${layer}: Transaction Data Publish Error!`));
})()
: null;
}));
})
};
// Publishing Transaction data **in Bulk** by Graphql Mutation call
const txBulkMutation = async (args) => {
const router = args["router"];
const wssUrl = args["wss"];
const layer = args["layer"];
const provider = new ethers.WebSocketProvider(wssUrl);
provider.on('block', async (tx) => {
const blockHeader = await provider.getBlock(tx);
const blockHashList = blockHeader["transactions"];
const bulkDataWithNull = await Promise.all(blockHashList.map(async (j) => {
const txnData = await provider.getTransaction(j);
return (txnData["to"] === router && hasUniswapCommands(txnData["data"]))
? (async () => {
const decodedData = uniswapFullDecodedInput(txnData["data"]);
const fullData = {...txnData, "decodedData": decodedData, "blockHeader": blockHeader, "createdAt": new Date()}
return fullData
})()
: null;
}));
const bulkData = bulkDataWithNull.filter((commands) => commands !== null) //filter null data
const query = createMutaionString(bulkData, args["TxnMutation"], args["TxnMutationMethod"]);
await (callMutation(query))(args["graphql"]).
then(result => logger.info({result: result}, `${layer}: Transaction Bulk Data Request Processed`)).
catch(error => logger.error(error, `${layer}: Transaction Data Publish Error!`));
})
};
const createMutaionString = (fullData, mutationName, mutationMethod) => {
// Bigint to String
const jsonData = JSON.stringify(fullData, (_, v) => typeof v === 'bigint' ? v.toString() : v);
// Removing double quotation of keys in the dictionary
const data = jsonData.replace(/"([^"]+)":/g, '$1:');
const query = JSON.stringify({
query: `mutation {
${mutationName}(
${mutationMethod}: ${data}
) {
hash
}
}`})
return query
}
const callMutation = (query) => async (graphqlUrl) => {
const response = await fetch(graphqlUrl, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json", 'Authorization': `Bearer: ${APP_SECRET}` },
body: query
});
const responseData = await response.json();
return responseData
}
const runPublish = (args) => (
(args["layer"]==="l1" && args["TxnMutation"]==="createTxnData")// Respective Data Registering
? (txMutation(args),txpoolMutation(args))
: (args["layer"]==="l2" && args["TxnMutation"]==="createl2TxnData")
? (txMutation(args))
: (args["layer"]==="l1" && args["TxnMutation"]==="createBulkTxnData") // Bulk Data Registering
? (txBulkMutation(args),txpoolMutation(args))
: (args["layer"]==="l2" && args["TxnMutation"]==="createBulkl2TxnData")
? (txBulkMutation(args))
: (() => {throw new Error('Wrong Initial Setting!')})()
)
module.exports = {
txpoolMutation,
txMutation,
txBulkMutation,
createMutaionString,
callMutation,
runPublish,
logger
};