-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
214 lines (201 loc) · 7.08 KB
/
main.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
import { ethers } from 'ethers';
import { infura_http_provider, infura_ws_provider, contract_address, API_URL } from "./src/constants.js";
import chalk from 'chalk';
import abiDecoder from "abi-decoder";
import { createRequire } from "module";
import axios from "axios";
const require = createRequire(import.meta.url);
const ERC721OrderFeatureABI = require("./src/ERC721OrdersFeature.json");
// import { ERC721OrderFeatureABI } from "./src/ERC721OrdersFeature";
var infura_http = new ethers.providers.JsonRpcProvider(infura_http_provider);
export const matchAddress = (a, b, caseInsensitive = true) => {
if (a === null || a === undefined) return false;
if (Array.isArray(b)) {
if (caseInsensitive) {
return b.map((x) => x.toLowerCase()).includes(a.toLowerCase());
}
return b.includes(a);
}
if (caseInsensitive) {
return a.toLowerCase() === b.toLowerCase();
}
return a === b;
};
async function assumeTransaction(data) {
console.log(data.logs.length);
for(var i = 0 ; i < data.logs.length; ++ i) {
// console.log(data.logs[i].topics);
if(data.logs[i].topics == "0x50273fa02273cceea9cf085b42de5c8af60624140168bd71357db833535877af") { // ERC721OrderFilled : erc721orders that accepted
console.log(data.logs[i]);
let abi = ["event ERC721OrderFilled(uint8 direction, address maker, address taker, uint256 nonce, address erc20Token, uint256 erc20TokenAmount, address erc721Token, uint256 erc721TokenId, address matcher)"];
let iface = new ethers.utils.Interface(abi);
let log = iface.parseLog(data.logs[i]);
console.log(log.args.nonce.toNumber(), log.args.direction);
var currentResult;
try{
if(log.args.direction == 1) {
currentResult = await axios({
method: "PATCH",
url: `${API_URL}/buy_orders/current`,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
nonce: log.args.nonce.toNumber(),
current: 3,
acceptingHash: data.logs[i].transactionHash
}),
});
} else {
currentResult = await axios({
method: "PATCH",
url: `${API_URL}/sell_orders/current`,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
nonce: log.args.nonce.toNumber(),
current: 3,
acceptingHash: data.logs[i].transactionHash
}),
});
}
console.log("Result:", currentResult.status);
} catch (e) {
console.log("error", e.length);
}
}
if(data.logs[i].topics == "0x8c5d0c41fb16a7317a6c55ff7ba93d9d74f79e434fefa694e50d6028afbfa3f0") {
console.log(data.logs[i]);
let log = data.logs[i].data;
var numParams = (log.length - 2) / 64;
if(numParams != 18) {
console.log("Not our presigned order!");
return false;
}
var paramsForOrder = [];
for(var j = 0 ; j < numParams ; ++ j) {
let param;
if(j == 0 || j == 3 || j == 4 || j == 6 || j == 9 || j == 14){
param = parseInt(log.substring(2 + 64 * j, 2 + 64 * (j + 1)), 16);
}
else {
param = "0x" + log.substring(2 + 64 * j, 2 + 64 * (j + 1)).replace(/^0+/, '');
}
// console.log(param);
paramsForOrder[j] = param;
}
// console.log(paramsForOrder);
var order = {
direction: paramsForOrder[0],
maker: paramsForOrder[1],
taker: paramsForOrder[2],
expiry: String(paramsForOrder[3]),
nonce: paramsForOrder[4],
erc20Token: paramsForOrder[5],
erc20TokenAmount: String(paramsForOrder[6]),
fees: [{
recipient: paramsForOrder[13],
amount: paramsForOrder[14],
feeData: "0x"
}],
erc721Token: paramsForOrder[8],
erc721TokenId: paramsForOrder[9],
erc721TokenProperties: [],
};
console.log(order);
if(order.direction == 1) {
try {
var currentResult = await axios({
method: "PATCH",
url: `${API_URL}/buy_orders/current`,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({ nonce: order.nonce, current: 1, makingHash: data.logs[i].transactionHash}),
});
console.log("currentResult", currentResult.status);
} catch(e){
console.log("Error!", e.length);
}
}
else {
try {
var currentResult = await axios({
method: "PATCH",
url: `${API_URL}/sell_orders/current`,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({ nonce: order.nonce, current: 1, makingHash: data.logs[i].transactionHash}),
});
} catch(e){
console.log("Error!", e.length);
}
// console.log("currentResult", currentResult.status);
}
}
if(data.logs[i].topics == "0xa015ad2dc32f266993958a0fd9884c746b971b254206f3478bc43e2f125c7b9e") {
console.log(data.logs[i]);
let abi = ["event ERC721OrderCancelled( address maker, uint256 nonce)"];
let iface = new ethers.utils.Interface(abi);
let log = iface.parseLog(data.logs[i]);
console.log(log);
var currentResult;
try {
currentResult = await axios({
method: "PATCH",
url: `${API_URL}/orders/cancel`,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
nonce: Number(log.args.nonce),
cancelHash: data.logs[i].transactionHash,
}),
});
console.log("currentResult", currentResult.status);
} catch(e){
console.log("Error!", e.length);
}
}
}
}
async function post(block) {
abiDecoder.addABI(ERC721OrderFeatureABI);
try {
for (let tx of block.transactions) {
if(matchAddress(tx.to, contract_address))
{
let tx_receipt = await infura_http.getTransactionReceipt(tx.hash);
if(tx_receipt.status != 0)
await assumeTransaction(tx_receipt);
}
}
} catch (e) {
console.log(chalk.red("Error when analyzing"));
}
};
let main = async () => {
let eth_websocket = new ethers.providers.WebSocketProvider(infura_ws_provider);
// let tx_receipt = await infura_http.getTransactionReceipt("0xd05a3974ad8e2ecaf7d2e52d4e1ef2b8954b29fc938b385ce032d3885b261d42");
// assumeTransaction(tx_receipt);
eth_websocket.on("block", (blk) => {
eth_websocket.getBlockWithTransactions(blk).then(function (block) {
console.log(block.number);
post(block);
});
});
eth_websocket._websocket.on("error", async () => {
console.log(`Unable to connect to ${ep.subdomain} retrying in 3s...`);
setTimeout(main, 3000);
});
eth_websocket._websocket.on("close", async (code) => {
console.log(
`Connection lost with code ${code}! Attempting reconnect in 3s...`
);
eth_websocket._websocket.terminate();
setTimeout(main, 3000);
});
};
main();