-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_subscriber.js
97 lines (85 loc) · 2.71 KB
/
event_subscriber.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
const BlockNumController = require('./controllers/blockController');
const { provider, StoreFront} = require('./contracts');
const handleBuy = () => {
const handler = async (tx) => {
console.log("*********************")
console.log(tx)
var tokenDatas = {
address: tx.args._address,
tokens: tx.args._tokens,
price: tx.args._price
};
try {
BlockNumController.control.confirmEvent(tokenDatas.tokens, tokenDatas.address)
} catch (err) {
console.log(
"buy handle error:", err.message
)
}
}
handleEvent({
id: "buy",
provider: provider,
contract: StoreFront,
event: "Buy",
times: 15,
handler: handler
});
// setTimeout(() => {
// handleBuy()
// }, 10000);
}
const handleEvent = (props) => {
const { id, provider, contract, event, times, handler} = props;
var latestblocknumber;
const handletransactions = async () => {
try {
let blockNumber = await provider.getBlockNumber();
if (blockNumber > latestblocknumber) {
blockNumber = blockNumber - latestblocknumber > 20 ? latestblocknumber + 20 : blockNumber;
console.log("currenct block: ", blockNumber, "latest block", latestblocknumber, "handle", blockNumber);
var txhistory = contract.queryFilter(event, latestblocknumber + 1, blockNumber);
await txhistory.then(async (res) => {
for (var index in res) {
handler(res[index]);
}
});
latestblocknumber = blockNumber;
await BlockNumController.control.update({
id: 'buy',
lastblock: blockNumber
});
}
} catch (err) {
console.log("handleEvent err", err)
}
setTimeout(() => {
handletransactions()
}, 3000);
}
const handleEvent = async () => {
var blockNumber = 0;
try {
blockNumber = (await BlockNumController.control.find({ id: id }));
if(blockNumber){
blockNumber = blockNumber.lastblock
}
} catch (err) {
try {
blockNumber = await provider.getBlockNumber();
await BlockNumController.control.create({
id: id,
lastblock: blockNumber
});
} catch (err) {
console.log("provider err,", err.message);
}
}
latestblocknumber = Number(blockNumber);
setTimeout(() => {
handletransactions();
}, times);
}
handleEvent();
}
module.exports = {handleBuy}