wative 1.2.7 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,119 +0,0 @@
1
- const {
2
- BN
3
- } = require('bn.js');
4
- const {
5
- stripHexPrefix
6
- } = require('ethereumjs-util');
7
- const { BigNumber } = require('bignumber.js');
8
- import Web3 from 'web3';
9
-
10
- function hexToBn(inputHex: typeof BN) {
11
- return new BN(stripHexPrefix(inputHex.toString('hex')), 16);
12
- }
13
-
14
- const addHexPrefix = (str: string) => {
15
- if (str.match(/^-?0x/u)) {
16
- return str;
17
- }
18
-
19
- if (str.match(/^-?0X/u)) {
20
- return str.replace('0X', '0x');
21
- }
22
-
23
- if (str.startsWith('-')) {
24
- return str.replace('-', '-0x');
25
- }
26
-
27
- return `0x${str}`;
28
- };
29
-
30
- function BnMultiplyByFraction(targetBN: typeof BN, numerator: number, denominator: number) {
31
- const numBN = new BN(numerator);
32
- const denomBN = new BN(denominator);
33
- return targetBN.mul(numBN).div(denomBN);
34
- }
35
-
36
- function bnToHex(inputBn: typeof BN) {
37
- return addHexPrefix(inputBn.toString(16));
38
- }
39
-
40
- export class GasUtil {
41
- query: any;
42
- constructor(rpcUrl: string) {
43
- this.query = (new Web3(Web3.givenProvider || rpcUrl)).eth;
44
- }
45
-
46
- async analyzeGasUsage(txParams: any) {
47
- const block = await this.query.getBlock('latest', false);
48
- const blockGasLimitBN = hexToBn(block.gasLimit);
49
- const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20);
50
- let estimatedGasHex = bnToHex(saferGasLimitBN);
51
- let simulationFails;
52
- try {
53
- estimatedGasHex = await this.estimateTxGas(txParams);
54
- } catch (error: any) {
55
- simulationFails = {
56
- reason: error.message,
57
- errorKey: error.errorKey,
58
- debug: {
59
- blockNumber: block.number,
60
- blockGasLimit: block.gasLimit
61
- },
62
- };
63
- }
64
-
65
- return {
66
- blockGasLimit: block.gasLimit,
67
- estimatedGasHex,
68
- simulationFails
69
- };
70
- }
71
-
72
- async estimateTxGas(txParams: any) {
73
- let gasLimit = await this.query.estimateGas(txParams);
74
- gasLimit = (new BigNumber(gasLimit.toString())).multipliedBy(1.3).toFixed(0);
75
- return gasLimit;
76
- }
77
-
78
- addGasBuffer(initialGasLimitHex: string, blockGasLimitHex: string, multiplier = 1.3) {
79
- const initialGasLimitBn = hexToBn(initialGasLimitHex);
80
- const blockGasLimitBn = hexToBn(blockGasLimitHex);
81
- const upperGasLimitBn = blockGasLimitBn.muln(0.9);
82
- const bufferedGasLimitBn = initialGasLimitBn.muln(multiplier);
83
- if (initialGasLimitBn.gt(upperGasLimitBn)) {
84
- return bnToHex(initialGasLimitBn);
85
- }
86
- if (bufferedGasLimitBn.lt(upperGasLimitBn)) {
87
- return bnToHex(bufferedGasLimitBn);
88
- }
89
- return bnToHex(upperGasLimitBn);
90
- }
91
-
92
- async getBufferedGasLimit(txParams: any, multiplier: number) {
93
- const {
94
- blockGasLimit,
95
- estimatedGasHex,
96
- simulationFails,
97
- } = await this.analyzeGasUsage(txParams);
98
- const gasLimit = this.addGasBuffer(
99
- addHexPrefix(estimatedGasHex),
100
- blockGasLimit,
101
- multiplier,
102
- );
103
-
104
- return {
105
- gasLimit,
106
- simulationFails
107
- };
108
- }
109
-
110
- async getGasPrice() {
111
- const gasPrice = await this.query.getGasPrice();
112
- return gasPrice;
113
- }
114
-
115
- async getTransactionCount(account: string) {
116
- const nonce = await this.query.getTransactionCount(account);
117
- return nonce;
118
- }
119
- }