-
Notifications
You must be signed in to change notification settings - Fork 5
/
deployed.js
97 lines (81 loc) · 2.47 KB
/
deployed.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 { redis, web3 } = require("../connections");
const { User, Bank } = require("../models");
const config = require("../config");
function listener(job, done) {
(async function () {
try {
const transaction = await web3.client.eth.getTransactionReceipt(
job.data.transactionHash
);
if (!transaction || transaction.status === false) {
done();
await addToQueue(job.data);
return;
}
const [user] = await User.find({ where: { id: job.data.user.id } });
const [bank] = await Bank.find({ where: { id: config.BANK_ID } });
const bankContract = web3.getBankContract(bank.address);
const events = await bankContract.getPastEvents("ReceiverCreated", {
fromBlock: transaction.blockNumber,
toBlock: transaction.blockNumber,
});
const validEvent = events.find(
(e) => e.returnValues.userId === web3.utils.soliditySha3(user.id)
);
if (!validEvent) {
done();
await addToQueue(job.data);
return;
}
const receiverAddress = validEvent.returnValues.receiverAddress;
await User.update({
where: { id: user.id },
create: {
receiver: {
node: {
address: receiverAddress,
transaction: {
create: {
node: {
transactionHash: transaction.transactionHash,
transactionIndex: transaction.transactionIndex,
blockHash: transaction.blockHash,
blockNumber: transaction.blockNumber,
gasUsed: transaction.gasUsed,
cumulativeGasUsed: transaction.cumulativeGasUsed,
gasPrice: job.data.gasPrice,
},
},
},
bank: {
connect: {
where: {
node: { address: bankContract.options.address },
},
},
},
},
},
},
});
done();
} catch (error) {
console.error(error);
done(error);
}
})();
}
function listen() {
redis.queues.Deployed.process(listener);
}
async function addToQueue({ transactionHash, user, gasPrice }) {
await redis.queues.Deployed.add(
{
transactionHash,
user: { id: user.id },
gasPrice,
},
{ attempts: 10, backoff: 60000 }
);
}
module.exports = { listen, addToQueue };