1
1
import { Contract } from 'web3-eth-contract' ;
2
2
import Web3 from 'web3' ;
3
- import { ethToWei , getEIP1559Prices , getGasPrice , GWeiToWei } from './helpers' ;
4
- const BN = require ( 'bn .js') ;
3
+ import { EIP1559Gas , ethToWei , getEIP1559Prices } from './helpers' ;
4
+ import { BigNumber } from 'bignumber .js';
5
5
6
6
export interface IEthMethodsInitParams {
7
7
web3 : Web3 ;
8
8
ethManagerContract : Contract ;
9
9
}
10
10
11
11
export class EthMethods {
12
- private web3 : Web3 ;
12
+ public web3 : Web3 ;
13
13
private ethManagerContract : Contract ;
14
14
15
15
constructor ( params : IEthMethodsInitParams ) {
16
16
this . web3 = params . web3 ;
17
17
this . ethManagerContract = params . ethManagerContract ;
18
18
}
19
19
20
+ getGasPrice = async ( ) : Promise < EIP1559Gas | BigNumber > => {
21
+ return await getEIP1559Prices ( ) ;
22
+ } ;
23
+
20
24
swapEth = async ( userAddr , amount , sendTxCallback ?) => {
21
25
// @ts -ignore
22
26
const accounts = await ethereum . enable ( ) ;
@@ -29,30 +33,56 @@ export class EthMethods {
29
33
from : accounts [ 0 ] ,
30
34
} ) ;
31
35
32
- const gasLimit = Math . max ( estimateGas + estimateGas * 0.3 , Number ( process . env . ETH_GAS_LIMIT ) ) ;
36
+ const gasLimit = Math . max ( estimateGas + estimateGas * 0.2 , Number ( process . env . ETH_GAS_LIMIT ) ) ;
33
37
//let eip1559gas = await getEIP1559Prices();
34
38
35
39
//console.log(`${JSON.stringify(eip1559gas)}`);
36
40
37
- this . ethManagerContract . methods
38
- . swap ( secretAddrHex )
39
- . send ( {
40
- value : ethToWei ( amount ) ,
41
- from : accounts [ 0 ] ,
42
- gas : new BN ( gasLimit ) ,
43
- // maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
44
- // maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
45
- gasPrice : await getGasPrice ( this . web3 ) ,
46
- } )
47
- . on ( 'transactionHash' , function ( hash ) {
48
- sendTxCallback ( { hash } ) ;
49
- } )
50
- . then ( function ( receipt ) {
51
- sendTxCallback ( { receipt } ) ;
52
- } )
53
- . catch ( function ( error ) {
54
- sendTxCallback ( { error } ) ;
55
- } ) ;
41
+ const gasPrices = await this . getGasPrice ( ) ;
42
+
43
+ if ( gasPrices instanceof EIP1559Gas ) {
44
+ this . ethManagerContract . methods
45
+ . swap ( secretAddrHex )
46
+ . send ( {
47
+ value : ethToWei ( amount ) ,
48
+ from : accounts [ 0 ] ,
49
+ gas : gasLimit . toString ( ) ,
50
+ //maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
51
+ //maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
52
+ // maxFeePerGas: gasPrices.maxFeePerGas.toString(),
53
+ // maxPriorityFeePerGas: gasPrices.maxPriorityFeePerGas.toString(),
54
+ //gasPrice: await getGasPrice(this.web3),
55
+ } )
56
+ . on ( 'transactionHash' , function ( hash ) {
57
+ sendTxCallback ( { hash } ) ;
58
+ } )
59
+ . then ( function ( receipt ) {
60
+ sendTxCallback ( { receipt } ) ;
61
+ } )
62
+ . catch ( function ( error ) {
63
+ sendTxCallback ( { error } ) ;
64
+ } ) ;
65
+ } else {
66
+ this . ethManagerContract . methods
67
+ . swap ( secretAddrHex )
68
+ . send ( {
69
+ value : ethToWei ( amount ) ,
70
+ from : accounts [ 0 ] ,
71
+ gas : gasLimit . toString ( ) ,
72
+ // maxFeePerGas: GWeiToWei(eip1559gas.maxFeePerGas),
73
+ // maxPriorityFeePerGas: GWeiToWei(eip1559gas.maxPriorityFeePerGas),
74
+ gasPrice : gasPrices . toString ( ) ,
75
+ } )
76
+ . on ( 'transactionHash' , function ( hash ) {
77
+ sendTxCallback ( { hash } ) ;
78
+ } )
79
+ . then ( function ( receipt ) {
80
+ sendTxCallback ( { receipt } ) ;
81
+ } )
82
+ . catch ( function ( error ) {
83
+ sendTxCallback ( { error } ) ;
84
+ } ) ;
85
+ }
56
86
} ;
57
87
58
88
checkEthBalance = async addr => {
0 commit comments