-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrc20.currency.js
387 lines (319 loc) · 10.7 KB
/
trc20.currency.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const assert = require("node:assert");
const { Decimal } = require("decimal.js-light");
const ms = require("ms");
const { isBefore } = require("date-fns/isBefore");
const Currency = require("../../Currency");
const TronNetwork = require("../tron.network");
const TronWeb = require("tronweb");
const trc20Abi = require("./abi/trc20.json");
const trc20ExtAbi = require("./abi/trc20-impl.json");
const {
STATUSES,
InternalTransactionModel,
} = require("../../../models/InternalTransaction");
const { subMilliseconds } = require("date-fns");
/**
* A TRC20 token, potentially with batch transaction sending extensions, like ACHIVX.
*/
module.exports = class TRC20Currency extends Currency {
static typeName = "TRC20";
constructor({ name, config, network }) {
super({ name, network });
assert.ok(
network instanceof TronNetwork,
`Network for TRC20 token ${name} must be a tron network`,
);
/** @type {Number} */
this._decimals = config.decimals ?? 6;
this._trc20Contract = network.tronWeb.contract(trc20Abi, config.address);
if (config.extImplAddress) {
this._trc20ExtContract = network.tronWeb.contract(
trc20ExtAbi,
config.extImplAddress,
);
} else {
this._trc20ExtContract = null;
}
this._feeLimit = config.feeLimit;
this._stuckTransactionResendTimeoutMs = ms(
config.stuckTransactionResendTimeout ?? "1 day",
);
}
async checkConfiguration() {
let hasErrors = false;
console.log(
`- Master wallet address is ${this.network.masterWalletAddress}`,
);
try {
const contractDecimalsResponse = await this._trc20Contract
.decimals()
.call();
if (contractDecimalsResponse.toString() != this._decimals.toString()) {
hasErrors = true;
console.log(
`! Decimals number is misconfigured. It is set to ${this.decimals} but should be ${contractDecimalsResponse}`,
);
} else {
console.log(
`- Decimals number (${this._decimals}) is configured correctly`,
);
}
} catch (e) {
hasErrors = true;
console.log("! Error calling main contract:");
console.log(e);
}
if (this._trc20ExtContract) {
try {
const [extSupply, mainSupply] = await Promise.all([
this._trc20ExtContract.totalSupply().call(),
this._trc20Contract.totalSupply().call(),
]);
console.log(`- total supply is ${mainSupply}`);
if (extSupply.toString() !== mainSupply.toString()) {
hasErrors = true;
console.log(`! Extension contract is set but does not seem correct`);
} else {
console.log(`- Extension contract is set and seems to be correct`);
}
} catch (e) {
hasErrors = true;
console.log("! Error checking extension contract");
console.log(e);
}
} else {
console.log(
`- Token does not support batch transactions extension.\n\tStandard TRC20 transactions will be used to withdraw tokens.\n\tThat's inefficient in case of large numbers of transactions.`,
);
}
return hasErrors;
}
async getMasterWalletBalance() {
const balanceResponse = await this._trc20Contract
.balanceOf(TronWeb.address.toHex(this.network.masterWalletAddress))
.call();
return new Decimal(balanceResponse.balance.toString())
.div(10 ** this._decimals)
.toString();
}
async _checkTransaction(transactionId) {
const response =
await this.network.tronWeb.trx.getTransactionInfo(transactionId);
console.log(`Response for transaction ${transactionId}:`, response);
if (!Object.keys(response).length) {
return { done: false };
}
if (response.result && response.result === "FAILED") {
return {
done: true,
success: false,
error: `${JSON.stringify(response)}`,
};
}
return { done: true, success: true };
}
async _waitForTransactionResult(
transactionId,
iterations = 10,
delay = 10000,
) {
for (;;) {
const status = await this._checkTransaction(transactionId);
if (status.done) {
return status;
}
if (--iterations <= 0) {
return status;
}
await new Promise((res) => setTimeout(res, delay));
}
}
async _createSignedStandardTransaction(address, amount) {
const tronWeb = this.network.tronWeb;
const { transaction } =
await tronWeb.transactionBuilder.triggerSmartContract(
TronWeb.address.toHex(this._trc20Contract.address),
"transfer(address,uint256)",
{ feeLimit: this._feeLimit },
[
{ type: "address", value: address },
{ type: "uint256", value: amount },
],
TronWeb.address.toHex(this.network.masterWalletAddress),
);
return tronWeb.trx.sign(transaction, tronWeb.defaultPrivateKey);
}
async _sendStandardTransaction(transaction) {
const amount = new Decimal(transaction.withdrawal.amount.toString())
.mul(10 ** this._decimals)
.toInteger()
.toNumber();
// TODO: Check balance (?)
const signedTransaction = await this._createSignedStandardTransaction(
transaction.withdrawal.withdrawalAddress,
amount,
);
await transaction.updateOne({
"withdrawal.transactionId": signedTransaction.txID,
"withdrawal.transactionUrl": this.network.formatTransactionUrl(
signedTransaction.txID,
),
status: STATUSES.SENDING,
});
try {
const response =
await this.network.tronWeb.trx.sendRawTransaction(signedTransaction);
console.log(response);
if (response.code) {
throw new Error(
`Tronweb error ${response.code}: ${TronWeb.toUtf8(response.message)}`,
);
}
const { done, success, error } = await this._waitForTransactionResult(
signedTransaction.txID,
);
if (done) {
const status = success ? STATUSES.MINED : STATUSES.SEND_FAILED;
await transaction.updateOne({
$set: {
status,
"withdrawal.error": error ?? null,
},
});
}
} catch (e) {
await transaction.updateOne({
$set: {
"withdrawal.error": `${e}`,
status: STATUSES.SEND_UNKNOWN_ERROR,
},
});
throw e;
}
}
async _createSignedBatchTransaction(txs) {
const parameters = [
{ type: "address[]", value: txs.map((tx) => tx.toAddress) },
{ type: "uint256[]", value: txs.map((tx) => tx.amount) },
];
const tronWeb = this.network.tronWeb;
const { transaction } =
await tronWeb.transactionBuilder.triggerSmartContract(
TronWeb.address.toHex(this._trc20ExtContract.address),
"batchTransfer(address[],uint256[])",
{ feeLimit: this._feeLimit },
parameters,
TronWeb.address.toHex(this.network.masterWalletAddress),
);
return tronWeb.trx.sign(transaction, tronWeb.defaultPrivateKey);
}
async _sendBatchTransaction(transactions) {
const payload = transactions.map((transaction) => ({
toAddress: transaction.withdrawal.withdrawalAddress,
amount: new Decimal(transaction.withdrawal.amount.toString())
.mul(10 ** this._decimals)
.toInteger()
.toNumber(),
}));
// TODO: Check balance (?)
const signedTransaction = await this._createSignedBatchTransaction(payload);
await InternalTransactionModel.updateMany(
{ _id: { $in: transactions.map((it) => it._id) } },
{
$set: {
"withdrawal.transactionId": signedTransaction.txID,
"withdrawal.transactionUrl": this.network.formatTransactionUrl(
signedTransaction.txID,
),
status: STATUSES.SENDING,
},
},
);
try {
const response =
await this.network.tronWeb.trx.sendRawTransaction(signedTransaction);
console.log(response);
if (response.code) {
throw new Error(
`Tronweb error ${response.code}: ${TronWeb.toUtf8(response.message)}`,
);
}
const { done, success, error } = await this._waitForTransactionResult(
signedTransaction.txID,
);
if (done) {
const status = success ? STATUSES.MINED : STATUSES.SEND_FAILED;
await InternalTransactionModel.updateMany(
{ _id: { $in: transactions.map((it) => it._id) } },
{
$set: {
status,
"withdrawal.error": error ?? null,
},
},
);
}
} catch (e) {
await InternalTransactionModel.updateMany(
{ _id: { $in: transactions.map((it) => it._id) } },
{ $set: { status: STATUSES.SEND_UNKNOWN_ERROR } },
);
throw e;
}
}
async processWithdrawalTransactions(transactions) {
if (this._trc20ExtContract) {
await this._sendBatchTransaction(transactions);
} else {
for (const transaction of transactions) {
await this._sendStandardTransaction(transaction);
}
}
}
async processSentWithdrawalTransactions(transactions) {
const checkResults = new Map();
const resendTransactionsStuckSince = subMilliseconds(
new Date(),
this._stuckTransactionResendTimeoutMs,
);
for (const transaction of transactions) {
if (isBefore(transaction.updatedAt, resendTransactionsStuckSince)) {
console.log(
`Transaction ${transaction.id} is stuck in SENDING status for too long. Will try to re-send it.`,
);
await transaction.updateOne({
$set: {
status: STATUSES.SEND_FAILED,
"withdrawal.error":
"Can not confirm if transaction is succeed or failed. Assuming it was not sent. Will re-send.",
},
});
continue;
}
const txId = transaction.withdrawal.transactionId;
assert.ok(txId, "SENDING transaction must have a transaction id");
let checkResult = checkResults.get(txId);
if (!checkResult) {
checkResult = await this._checkTransaction(txId);
checkResults.set(txId, checkResult);
}
if (!checkResult.done) {
console.log(`Status of transaction ${transaction.id} not known yet.`);
continue;
}
// TODO: Check contract's returned value for standard TRC20 contracts
if (checkResult.success) {
console.log(`Transaction ${transaction.id} mined successfully.`);
await transaction.updateOne({ $set: { status: STATUSES.MINED } });
} else {
console.log(`Transaction ${transaction.id} failed`);
await transaction.updateOne({
$set: {
status: STATUSES.SEND_FAILED,
"withdrawal.error": checkResult.error ?? null,
},
});
}
}
}
};