-
Notifications
You must be signed in to change notification settings - Fork 5
/
withdrawn.js
80 lines (68 loc) · 1.82 KB
/
withdrawn.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
const debug = require("../utils/debug")("withdrawer/withdrawn");
const { redis, web3 } = require("../connections");
const { Withdrawal } = require("../models");
function listener(job, done) {
(async () => {
try {
const [withdrawal] = await Withdrawal.find({
where: { id: job.data.withdrawal.id },
selectionSet: `
{
id
completed
}
`,
});
if (!withdrawal || withdrawal?.completed === true) {
done();
return;
}
const transaction = await web3.client.eth.getTransactionReceipt(
job.data.transactionHash
);
if (!transaction || transaction?.status === false) {
done();
await addToQueue(job.data);
return;
}
await Withdrawal.update({
where: { id: withdrawal.id },
update: {
completed: true,
},
create: {
transaction: {
node: {
transactionHash: transaction.transactionHash,
transactionIndex: transaction.transactionIndex,
blockHash: transaction.blockHash,
blockNumber: transaction.blockNumber,
gasUsed: transaction.gasUsed,
cumulativeGasUsed: transaction.cumulativeGasUsed,
gasPrice: job.data.gasPrice,
},
},
},
});
done();
} catch (error) {
debug("Error");
console.error(error);
done(error);
}
})();
}
function listen() {
redis.queues.Withdrawn.process(listener);
}
async function addToQueue({ withdrawal, transactionHash, gasPrice }) {
await redis.queues.Withdrawn.add(
{
withdrawal: { id: withdrawal.id },
transactionHash,
gasPrice,
},
{ attempts: 10, backoff: 60000 }
);
}
module.exports = { listen, addToQueue };