-
Notifications
You must be signed in to change notification settings - Fork 22
/
web3.js
545 lines (492 loc) · 16.6 KB
/
web3.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import Big from '~/assets/big.js';
import Eth from 'web3-eth';
import Utils from 'web3-utils';
import {TinyEmitter as Emitter} from 'tiny-emitter';
import {ETHEREUM_API_URL, BSC_API_URL, ETHEREUM_CHAIN_ID, BSC_CHAIN_ID, HUB_DEPOSIT_TX_PURPOSE, HUB_CHAIN_ID, HUB_CHAIN_DATA, HUB_CHAIN_BY_ID} from '~/assets/variables.js';
import erc20ABI from '~/assets/abi-erc20.js';
export const CONFIRMATION_COUNT = 5;
export const web3Utils = Utils;
export const web3Eth = new Eth(BSC_API_URL);
export const web3EthEth = new Eth(ETHEREUM_API_URL);
export const web3EthBsc = new Eth(BSC_API_URL);
export default {
eth: web3Eth,
utils: Utils,
};
const transactionPollingInterval = 5000;
[web3Eth, web3EthEth, web3EthBsc]
.forEach((eth) => eth.transactionPollingInterval = transactionPollingInterval);
const WEI_DECIMALS = 18;
/**
* @param {number|string} balance - balance in erc20 decimals
* @param {number} [ercDecimals=18]
* @return {string}
*/
export function fromErcDecimals(balance, ercDecimals = 18) {
const decimalsDelta = Math.max(WEI_DECIMALS - ercDecimals, 0);
balance = new Big(10).pow(decimalsDelta).times(balance).toFixed(0);
return Utils.fromWei(balance, "ether");
}
/**
* @param {number|string} balance
* @param {number} [ercDecimals=18]
* @return {string}
*/
export function toErcDecimals(balance, ercDecimals = 18) {
balance = new Big(balance).toFixed(Number(ercDecimals));
balance = Utils.toWei(balance, "ether");
const decimalsDelta = Math.max(WEI_DECIMALS - ercDecimals, 0);
const tens = new Big(10).pow(decimalsDelta);
return new Big(balance).div(tens).toFixed(0);
}
/**
* @typedef {import('web3-core/types/index.d.ts').Transaction & import('web3-core/types/index.d.ts').TransactionReceipt & {confirmations: number, timestamp: number}} Web3Tx
*/
/**
* @typedef {Promise} PromiseWithEmitter
* @property {function} on
* @property {function} once
* @property {function} unsubscribe
*/
/**
* @param {string} hash
* @param {object} options
* @param {number} [options.confirmationCount = CONFIRMATION_COUNT]
* @param {number} [options.chainId]
* @param {boolean} [options.needReceipt=true]
* @param {boolean} [options.needExactConfirmationCount]
* @param {boolean} [options.needExactTimestamp=true]
* @return {PromiseWithEmitter<Web3Tx>}
*/
export function subscribeTransaction(hash, {
confirmationCount = CONFIRMATION_COUNT,
chainId,
needReceipt = true,
needExactConfirmationCount = CONFIRMATION_COUNT > 1,
needExactTimestamp = true,
} = {}) {
let isUnsubscribed = false;
const emitter = new Emitter();
let txPromise;
try {
const providerHost = getProviderHostByChain(chainId);
if (providerHost) {
// keep provider for this tx, because later it can be changed
const ethSaved = new Eth(providerHost);
txPromise = _subscribeTransaction(hash, {
confirmationCount,
ethProvider: ethSaved,
emitter,
needReceipt,
needExactConfirmationCount,
needExactTimestamp,
});
} else {
txPromise = Promise.reject(new Error(`Can't subscribe to tx, chainId ${chainId} is not supported`));
}
} catch (error) {
txPromise = Promise.reject(error);
}
// proxy `.on` and `.once`
proxyEmitter(txPromise, emitter);
// unsubscribe from all events and disable polling
txPromise.unsubscribe = function() {
isUnsubscribed = true;
emitter.off('tx');
emitter.off('confirmation');
emitter.off('confirmed');
};
return txPromise;
/**
*
* @param {Promise<T>} target
* @param {PromiseWithEmitter<T>} emitter
*/
function proxyEmitter(target, emitter) {
target.on = function() {
emitter.on(...arguments);
return target;
};
target.once = function() {
emitter.once(...arguments);
return target;
};
// target.off = function () {
// emitter.off(...arguments);
// return target;
// }
}
}
/**
*
* @param {string} hash
* @param {object} options
* @param {number} options.confirmationCount
* @param {Eth} options.ethProvider
* @param {Emitter} options.emitter
* @param {boolean} [options.needReceipt]
* @param {boolean} [options.needExactConfirmationCount]
* @param {boolean} [options.needExactTimestamp]
* @return {Promise<Web3Tx>}
* @private
*/
function _subscribeTransaction(hash, {confirmationCount, ethProvider, emitter, needReceipt, needExactConfirmationCount, needExactTimestamp}) {
let isUnsubscribed = false;
return waitTxInBlock(hash)
.then((tx) => {
return Promise.all([
needReceipt ? ethProvider.getTransactionReceipt(hash) : Promise.resolve(),
needExactTimestamp ? ethProvider.getBlock(tx.blockNumber) : Promise.resolve(),
needExactConfirmationCount ? getConfirmations(tx) : Promise.resolve(1),
Promise.resolve(tx),
]);
})
.then(([receipt, block, confirmations, txData]) => {
// console.debug({receipt, block, confirmations, txData});
const tx = {
// input, hash from tx
...txData,
// logs, status, gasUsed from receipt
...receipt,
confirmations,
timestamp: needExactTimestamp ? block.timestamp * 1000 : Date.now(),
};
emitter.emit('confirmation', tx);
// status only available of receipt was requested
if (needReceipt && !tx.status) {
throw new Error('Transaction failed');
}
if (confirmations >= confirmationCount || !needExactConfirmationCount) {
return tx;
} else {
return waitConfirmations(tx);
}
})
.then((tx) => {
emitter.emit('confirmed', tx);
return tx;
});
function waitTxInBlock(hash) {
return ethProvider.getTransaction(hash)
.then((tx) => {
// reject
if (isUnsubscribed) {
throw new Error('unsubscribed');
}
if (tx) {
emitter.emit('tx', tx);
}
if (tx && tx.blockHash) {
return tx;
} else {
return wait(10000).then(() => waitTxInBlock(hash));
}
});
}
function waitConfirmations(tx) {
return wait(10000)
.then(() => getConfirmations(tx))
.then((confirmations) => {
// reject
if (isUnsubscribed) {
throw new Error('unsubscribed');
}
tx = {
...tx,
confirmations,
};
emitter.emit('confirmation', tx);
if (confirmations >= confirmationCount) {
return tx;
} else {
return waitConfirmations(tx);
}
});
}
function getConfirmations(tx) {
return getBlockNumber(ethProvider)
.then((currentBlock) => {
return currentBlock - tx.blockNumber + 1;
});
}
}
function wait(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
let cachedBlock = {
isLoading: false,
timestamp: 0,
providerHost: '',
promise: null,
};
/**
* @param {Eth} [web3Eth]
* @return {Promise<number>}
*/
export function getBlockNumber(web3Eth = web3Eth) {
const savedProviderHost = web3Eth.currentProvider.host;
const isSameProviderHost = savedProviderHost === cachedBlock.providerHost;
if (cachedBlock.isLoading && isSameProviderHost) {
return cachedBlock.promise;
}
if (Date.now() - cachedBlock.timestamp < 5000 && isSameProviderHost) {
return cachedBlock.promise;
}
const blockPromise = web3Eth.getBlockNumber();
cachedBlock.isLoading = true;
cachedBlock.providerHost = savedProviderHost;
cachedBlock.promise = blockPromise;
blockPromise
.then(() => {
// make sure response correspond to cache (in case of two parallel requests for different hosts)
if (savedProviderHost === cachedBlock.providerHost) {
cachedBlock.isLoading = false;
cachedBlock.timestamp = Date.now();
}
})
.catch((error) => {
if (savedProviderHost === cachedBlock.providerHost) {
cachedBlock.isLoading = false;
}
throw error;
});
return blockPromise;
}
// save promises forever if no error
const decimalsPromiseCache = {};
/**
* @param {string} tokenContractAddress
* @param {number} chainId
* @param {Array<HubCoinItem>} [hubCoinList]
* @return {Promise<number>}
*/
export function getTokenDecimals(tokenContractAddress, chainId, hubCoinList = []) {
if (!chainId) {
return Promise.reject(new Error('chainId not specified'));
}
// search from cache
if (decimalsPromiseCache[chainId]?.[tokenContractAddress]) {
return decimalsPromiseCache[chainId][tokenContractAddress];
}
// search from hubCoinList
const coinItem = getExternalCoinList(hubCoinList, chainId)
.find((item) => item.externalTokenId === tokenContractAddress);
if (coinItem) {
return Promise.resolve(Number(coinItem.externalDecimals));
}
const currentEth = getProviderByChain(chainId);
const contract = new currentEth.Contract(erc20ABI, tokenContractAddress);
const decimalsPromise = contract.methods.decimals().call()
.then((decimals) => {
return Number(decimals);
})
.catch((error) => {
console.log(error);
delete decimalsPromiseCache[chainId][tokenContractAddress];
return WEI_DECIMALS;
});
if (!decimalsPromiseCache[chainId]) {
decimalsPromiseCache[chainId] = {};
}
decimalsPromiseCache[chainId][tokenContractAddress] = decimalsPromise;
return decimalsPromise;
}
/**
* May be no transactions depending on the eth node settings
* @param {string} address
* @param {number} chainId
* @return {Promise<Transaction[]>}
*/
export function getAddressPendingTransactions(address, chainId) {
return getProviderByChain(chainId).getPendingTransactions()
.then((txList) => {
return txList.filter((tx) => tx.from === address);
})
.catch((error) => {
// The method eth_pendingTransactions may be not available
console.log(error);
return [];
});
}
/**
* @TODO refactor to find by method id https://stackoverflow.com/a/55258775/4936667
* @param {HubDeposit} tx
* @param {number} chainId
* @param {Array<HubCoinItem>} [hubCoinList]
* @param {boolean} [skipAmount]
* @return {Promise<HubDepositTxInfo>}
*/
export async function getDepositTxInfo(tx, chainId, hubCoinList, skipAmount) {
chainId = Number(tx.chainId || chainId);
// remove 0x and function selector
const input = tx.input.slice(2 + 8);
const itemCount = input.length / 64;
const hubContractAddress = HUB_CHAIN_BY_ID[chainId]?.hubContractAddress;
const wrappedNativeContractAddress = HUB_CHAIN_BY_ID[chainId]?.wrappedNativeContractAddress;
let type;
// first item
let tokenContract;
// 2nd for `unlock`, 4th for `transferToChain`, 'tx.to' in `wrap` and `sendETHToChain`
let amount;
if (itemCount === 2) {
// unlock
const beneficiaryHex = '0x' + input.slice(0, 64);
const beneficiaryAddress = web3Eth.abi.decodeParameter('address', beneficiaryHex);
const isUnlockedForBridge = beneficiaryAddress.toLowerCase() === hubContractAddress;
if (isUnlockedForBridge) {
type = HUB_DEPOSIT_TX_PURPOSE.UNLOCK;
tokenContract = tx.to;
amount = skipAmount ? 0 : await getAmountFromInputValue(input.slice((itemCount - 1) * 64), tokenContract, chainId, hubCoinList);
} else {
return {
type: HUB_DEPOSIT_TX_PURPOSE.OTHER,
};
}
} else if (tx.to.toLowerCase() === hubContractAddress && itemCount === 5) {
// transferToChain
type = HUB_DEPOSIT_TX_PURPOSE.SEND;
const tokenContractHex = '0x' + input.slice(0, 64);
tokenContract = web3Eth.abi.decodeParameter('address', tokenContractHex);
amount = skipAmount ? 0 : await getAmountFromInputValue(input.slice((itemCount - 2) * 64), tokenContract, chainId, hubCoinList);
} else if (tx.to.toLowerCase() === hubContractAddress && itemCount === 3) {
// transferETHToChain
type = HUB_DEPOSIT_TX_PURPOSE.SEND;
tokenContract = wrappedNativeContractAddress;
amount = Utils.fromWei(tx.value);
} else if (tx.to.toLowerCase() === wrappedNativeContractAddress && itemCount === 1) {
// unwrap
type = HUB_DEPOSIT_TX_PURPOSE.UNWRAP;
tokenContract = tx.to;
amount = skipAmount ? 0 : await getAmountFromInputValue(input, tokenContract, chainId, hubCoinList);
} else if (tx.to.toLowerCase() === wrappedNativeContractAddress && itemCount === 0) {
// wrap
type = HUB_DEPOSIT_TX_PURPOSE.WRAP;
tokenContract = tx.to;
amount = Utils.fromWei(tx.value);
} else {
return {
type: HUB_DEPOSIT_TX_PURPOSE.OTHER,
};
}
tokenContract = tokenContract?.toLowerCase();
const coinItem = getExternalCoinList(hubCoinList, chainId)
.find((item) => item.externalTokenId === tokenContract);
const tokenName = coinItem?.denom.toUpperCase();
return {
type,
tokenContract,
tokenName,
amount,
};
}
/**
*
* @param {strong} hex
* @param {string} tokenContract
* @param {number} chainId
* @param {Array<HubCoinItem>} [hubCoinList]
* @return {Promise<string>}
*/
async function getAmountFromInputValue(hex, tokenContract, chainId, hubCoinList) {
const amountHex = '0x' + hex;
const decimals = await getTokenDecimals(tokenContract, chainId, hubCoinList);
const amount = fromErcDecimals(web3Eth.abi.decodeParameter('uint256', amountHex), decimals);
return amount;
}
/**
*
* @param {Array<HubCoinItem>} hubCoinList
* @param {number} chainId
* @return {Array<TokenInfo.AsObject>}
*/
export function getExternalCoinList(hubCoinList, chainId) {
let externalNetworks = Object.values(HUB_CHAIN_ID);
if (chainId) {
externalNetworks = externalNetworks.filter((network) => network === getHubNetworkByChain(chainId));
}
return hubCoinList
.map((item) => {
// extract external token infos by network key
/** @type {Array<TokenInfo.AsObject>}*/
const externalTokens = externalNetworks.map((network) => item[network]);
return externalTokens;
})
.flat()
.filter((item) => !!item);
}
/**
* @param {number} chainId
* @return {Eth}
*/
export function getProviderByChain(chainId) {
validateChainId(chainId);
if (!chainId) {
return web3Eth;
}
if (chainId === ETHEREUM_CHAIN_ID) {
return web3EthEth;
}
if (chainId === BSC_CHAIN_ID) {
return web3EthBsc;
}
}
/**
* @param {number} chainId
* @return {string}
*/
function getProviderHostByChain(chainId) {
validateChainId(chainId);
if (!chainId) {
return web3Eth.currentProvider.host;
}
return HUB_CHAIN_DATA[getHubNetworkByChain(chainId)]?.apiUrl;
}
/**
* @param {number} chainId
* @return {HUB_CHAIN_ID}
*/
export function getHubNetworkByChain(chainId) {
validateChainId(chainId);
return HUB_CHAIN_BY_ID[chainId]?.hubChainId;
}
/**
* @param {HUB_CHAIN_ID} network
* @return {number}
*/
export function getChainIdByHubNetwork(network) {
return HUB_CHAIN_DATA[network].chainId;
}
/**
* @param {number} chainId
* @return {string}
*/
export function getEvmNetworkName(chainId) {
chainId = Number(chainId);
switch (chainId) {
case 1:
return 'Ethereum';
case 3:
return 'Ropsten';
case 4:
return 'Rinkeby';
case 42:
return 'Kovan';
case 11155111:
return 'Sepolia';
case 56:
return 'BSC';
case 97:
return 'BSC Testnet';
case 3333:
return 'Megachain Testnet';
default:
return chainId.toString();
}
}
function validateChainId(chainId) {
if (chainId && typeof chainId !== 'number') {
throw new Error(`chainId should be a number`);
}
}