-
Notifications
You must be signed in to change notification settings - Fork 4
/
poll.js
217 lines (202 loc) · 5.94 KB
/
poll.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
215
216
217
/* eslint no-await-in-loop: off */
const BigNumber = require('bignumber.js');
const publisher = require('./util/gcloudPub');
const config = require('./config/config');
const {
getBlockTime: getWeb3Block,
STATUS,
BLOCK_TIME: ETH_BLOCK_TIME,
getTransactionStatus: getWeb3TxStatus,
getTransfersFromReceipt: getWeb3TransferFromReceipt,
} = require('./util/web3');
const {
getBlockTime: getCosmosBlock,
BLOCK_TIME: COSMOS_BLOCK_TIME,
getTransactionStatus: getCosmosTxStatus,
} = require('./util/cosmos');
const { db } = require('./util/db');
const { getTxAmountForLog } = require('./util/misc');
const PUBSUB_TOPIC_MISC = 'misc';
const TIME_LIMIT = config.TIME_LIMIT || 60 * 60 * 1000 * 24; // fallback: 1 day
const TIME_BEFORE_FIRST_ENQUEUE = config.TIME_BEFORE_FIRST_ENQUEUE || 0;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
class PollTxMonitor {
constructor(doc, rateLimiter) {
this.txHash = doc.id;
this.data = doc.data();
this.ts = Number.parseInt(this.data.ts, 10) || Date.now();
this.rateLimiter = rateLimiter;
this.shouldStop = false;
}
async writeTxStatus(receipt, networkTx) {
const statusUpdate = { status: this.status };
let blockNumber = 0;
let blockTime = 0;
const {
nonce,
type,
delegatorAddress,
fromId,
toId,
feeAmount,
gas,
memo,
accountNumber,
sequence,
amount,
} = this.data;
let {
value,
from,
to,
} = this.data;
if (networkTx && type === 'transferETH') {
({ from, to, value } = networkTx);
statusUpdate.from = from;
statusUpdate.to = to;
statusUpdate.value = value;
} else if (type.includes('cosmos')) {
// TODO: handle cosmos transfer value if needed
} else if (receipt && (type === 'transfer' || type === 'transferDelegated')) {
// replace ETH LIKE transfer value
const transfers = getWeb3TransferFromReceipt(receipt);
if (!transfers || !transfers.length) {
statusUpdate.status = STATUS.FAIL;
} else {
let tx = transfers.find(
transfer => transfer.to.toLowerCase() !== networkTx.from.toLowerCase(),
);
if (!tx) {
const txs = transfers.filter(transfer => (new BigNumber(transfer.value)).gt(0));
if (txs.length) {
if (txs.length === 1) [tx] = txs;
else [tx] = txs.slice(-1); // assume last entry is tranfser
}
}
if (tx) {
statusUpdate.from = tx.from;
statusUpdate.to = tx.to;
statusUpdate.value = tx.value;
}
}
}
try {
if (receipt) {
({ blockNumber } = receipt);
statusUpdate.completeBlockNumber = blockNumber;
if (type.includes('cosmos')) {
blockTime = await getCosmosBlock(blockNumber);
} else {
blockTime = await getWeb3Block(blockNumber);
}
statusUpdate.completeTs = blockTime;
}
db.collection(config.FIRESTORE_TX_ROOT).doc(this.txHash).update(statusUpdate);
} catch (err) {
console.error(err);
}
const {
likeAmount,
likeAmountUnitStr,
ETHAmount,
ETHAmountUnitStr,
} = getTxAmountForLog({
value,
amount,
type,
});
publisher.publish(PUBSUB_TOPIC_MISC, {
logType: 'eventStatus',
txHash: this.txHash,
txStatus: this.status,
txBlock: receipt ? receipt.blockHash : '',
txBlockNumber: blockNumber,
txBlockTime: blockTime,
txGasUsed: receipt ? receipt.gasUsed : 0,
txNonce: nonce,
txType: type,
fromUser: fromId,
fromWallet: from,
toUser: toId,
toWallet: to,
likeAmount,
likeAmountUnitStr,
ETHAmount,
ETHAmountUnitStr,
delegatorAddress,
feeAmount,
gas,
memo,
accountNumber,
sequence,
txAmount: amount,
});
}
async getTransactionStatus() {
if (this.data.type.includes('cosmos')) {
return getCosmosTxStatus(this.txHash);
}
return getWeb3TxStatus(this.txHash, { requireReceipt: true });
}
async startLoop() {
const blockTime = this.data.type === 'cosmosTransfer' ? COSMOS_BLOCK_TIME : ETH_BLOCK_TIME;
const delayTime = Math.max(TIME_BEFORE_FIRST_ENQUEUE, blockTime);
const startDelay = (this.ts + delayTime) - Date.now();
if (startDelay > 0) {
await sleep(startDelay);
}
let finished = false;
while (!this.shouldStop) {
try {
const { status, receipt, networkTx } = await this.rateLimiter.schedule(
() => this.getTransactionStatus(),
);
this.status = status;
switch (status) {
case STATUS.SUCCESS:
case STATUS.FAIL:
try {
await this.writeTxStatus(receipt, networkTx);
finished = true;
} catch (err) {
console.error(this.txHash, `Error when writing tx status (${this.status}):`, err); // eslint-disable-line no-console
}
break;
case STATUS.MINED:
this.ts = Date.now();
break;
case STATUS.PENDING:
break;
case STATUS.NOT_FOUND:
if (Date.now() - this.ts > TIME_LIMIT) {
// timeout
this.status = STATUS.TIMEOUT;
try {
await this.writeTxStatus();
finished = true;
} catch (err) {
console.error(this.txHash, `Error when writing tx status (${this.status}):`, err); // eslint-disable-line no-console
}
}
break;
default:
}
if (finished) {
break;
}
await sleep(blockTime);
} catch (err) {
console.error(this.txHash, 'Error in PollTxMonitor loop:', err); // eslint-disable-line no-console
}
}
if (this.onFinish) {
this.onFinish(this);
}
}
stop() {
this.shouldStop = true;
}
}
module.exports = PollTxMonitor;