1
- import { fromWei , toWei } from 'web3-utils' ;
1
+ import { toWei } from 'web3-utils' ;
2
2
3
3
import { createThunk } from '@suite-common/redux-utils' ;
4
4
import { notificationsActions } from '@suite-common/toast-notifications' ;
@@ -11,16 +11,10 @@ import {
11
11
import {
12
12
Account ,
13
13
AddressDisplayOptions ,
14
- ExternalOutput ,
15
14
PrecomposedLevels ,
16
- PrecomposedTransaction ,
17
15
RbfTransactionParams ,
18
16
} from '@suite-common/wallet-types' ;
19
17
import {
20
- amountToSmallestUnit ,
21
- calculateEthFee ,
22
- calculateMax ,
23
- calculateTotal ,
24
18
formatAmount ,
25
19
getAccountIdentity ,
26
20
getEthereumEstimateFeeParams ,
@@ -30,106 +24,18 @@ import {
30
24
isPending ,
31
25
prepareEthereumTransaction ,
32
26
} from '@suite-common/wallet-utils' ;
33
- import TrezorConnect , { FeeLevel , TokenInfo } from '@trezor/connect' ;
27
+ import TrezorConnect , { FeeLevel } from '@trezor/connect' ;
34
28
import { BigNumber } from '@trezor/utils/src/bigNumber' ;
35
29
36
30
import { SEND_MODULE_PREFIX } from './sendFormConstants' ;
31
+ import { calculateEvmTxWithFees } from './sendFormEthereumUtils' ;
37
32
import {
38
33
ComposeFeeLevelsError ,
39
34
ComposeTransactionThunkArguments ,
40
35
SignTransactionError ,
41
36
SignTransactionThunkArguments ,
42
37
} from './sendFormTypes' ;
43
38
import { selectTransactions } from '../transactions/transactionsReducer' ;
44
-
45
- const calculate = (
46
- availableBalance : string ,
47
- output : ExternalOutput ,
48
- feeLevel : FeeLevel ,
49
- token ?: TokenInfo ,
50
- ) : PrecomposedTransaction => {
51
- let amount : string ;
52
- let max : string | undefined ;
53
- const feeInGwei = calculateEthFee ( toWei ( feeLevel . feePerUnit , 'gwei' ) , feeLevel . feeLimit || '0' ) ;
54
-
55
- const availableTokenBalance = token
56
- ? amountToSmallestUnit ( token . balance ! , token . decimals )
57
- : undefined ;
58
- if ( output . type === 'send-max' || output . type === 'send-max-noaddress' ) {
59
- max = availableTokenBalance || calculateMax ( availableBalance , feeInGwei ) ;
60
- amount = max ;
61
- } else {
62
- amount = output . amount ;
63
- }
64
-
65
- // total ETH spent (amount + fee), in ERC20 only fee
66
- const totalSpent = new BigNumber ( calculateTotal ( token ? '0' : amount , feeInGwei ) ) ;
67
-
68
- if ( totalSpent . isGreaterThan ( availableBalance ) ) {
69
- if ( token ) {
70
- return {
71
- type : 'error' ,
72
- error : 'AMOUNT_NOT_ENOUGH_CURRENCY_FEE_WITH_ETH_AMOUNT' ,
73
- errorMessage : {
74
- id : 'AMOUNT_NOT_ENOUGH_CURRENCY_FEE_WITH_ETH_AMOUNT' ,
75
- values : {
76
- feeAmount : fromWei ( feeInGwei , 'ether' ) . toString ( ) ,
77
- } ,
78
- } ,
79
- } as const ;
80
- }
81
-
82
- return {
83
- type : 'error' ,
84
- error : 'AMOUNT_IS_NOT_ENOUGH' ,
85
- errorMessage : { id : 'AMOUNT_IS_NOT_ENOUGH' } ,
86
- } as const ;
87
- }
88
-
89
- // validate if token balance is not 0 or lower than amount
90
- if (
91
- availableTokenBalance &&
92
- ( availableTokenBalance === '0' || new BigNumber ( amount ) . gt ( availableTokenBalance ) )
93
- ) {
94
- return {
95
- type : 'error' ,
96
- error : 'AMOUNT_IS_NOT_ENOUGH' ,
97
- errorMessage : { id : 'AMOUNT_IS_NOT_ENOUGH' } ,
98
- } as const ;
99
- }
100
-
101
- const payloadData = {
102
- type : 'nonfinal' as const ,
103
- totalSpent : token ? amount : totalSpent . toString ( ) ,
104
- max,
105
- fee : feeInGwei ,
106
- feePerByte : feeLevel . feePerUnit ,
107
- feeLimit : feeLevel . feeLimit ,
108
- token,
109
- bytes : 0 , // TODO: calculate
110
- inputs : [ ] ,
111
- } ;
112
-
113
- if ( output . type === 'send-max' || output . type === 'payment' ) {
114
- return {
115
- ...payloadData ,
116
- type : 'final' ,
117
- // compatibility with BTC PrecomposedTransaction from @trezor /connect
118
- inputs : [ ] ,
119
- outputsPermutation : [ 0 ] ,
120
- outputs : [
121
- {
122
- address : output . address ,
123
- amount,
124
- script_type : 'PAYTOADDRESS' ,
125
- } ,
126
- ] ,
127
- } ;
128
- }
129
-
130
- return payloadData ;
131
- } ;
132
-
133
39
export const composeEthereumTransactionFeeLevelsThunk = createThunk <
134
40
PrecomposedLevels ,
135
41
ComposeTransactionThunkArguments ,
@@ -164,6 +70,7 @@ export const composeEthereumTransactionFeeLevelsThunk = createThunk<
164
70
coin : account . symbol ,
165
71
identity : getAccountIdentity ( account ) ,
166
72
request : {
73
+ feeLevels : 'smart' ,
167
74
blocks : [ 2 ] ,
168
75
specific : {
169
76
from : account . descriptor ,
@@ -212,18 +119,27 @@ export const composeEthereumTransactionFeeLevelsThunk = createThunk<
212
119
}
213
120
// in case when selectedFee is set to 'custom' construct this FeeLevel from values
214
121
if ( formState . selectedFee === 'custom' ) {
122
+ const { customMaxPriorityFeePerGas, effectiveGasPrice, feePerUnit, feeLimit } =
123
+ formState ;
124
+
125
+ const customMaxPriorityFeePerGasWei = customMaxPriorityFeePerGas
126
+ ? BigNumber ( toWei ( Number ( customMaxPriorityFeePerGas ) , 'gwei' ) )
127
+ : undefined ;
128
+
215
129
predefinedLevels . push ( {
216
130
label : 'custom' ,
217
- feePerUnit : formState . feePerUnit ,
218
- feeLimit : formState . feeLimit ,
131
+ feePerUnit,
132
+ feeLimit,
133
+ effectiveGasPrice,
134
+ maxPriorityFeePerGas : customMaxPriorityFeePerGasWei ?. toString ( ) ,
219
135
blocks : - 1 ,
220
136
} ) ;
221
137
}
222
138
223
139
// wrap response into PrecomposedLevels object where key is a FeeLevel label
224
140
const resultLevels : PrecomposedLevels = { } ;
225
141
const response = predefinedLevels . map ( level =>
226
- calculate ( availableBalance , output , level , tokenInfo ) ,
142
+ calculateEvmTxWithFees ( { availableBalance, output, feeLevel : level , token : tokenInfo } ) ,
227
143
) ;
228
144
response . forEach ( ( tx , index ) => {
229
145
const feeLabel = predefinedLevels [ index ] . label as FeeLevel [ 'label' ] ;
@@ -331,7 +247,12 @@ export const signEthereumSendFormTransactionThunk = createThunk<
331
247
amount : formState . outputs [ 0 ] . amount ,
332
248
data : formState . ethereumDataHex ,
333
249
gasLimit : precomposedTransaction . feeLimit || '' ,
334
- gasPrice : precomposedTransaction . feePerByte ,
250
+ maxFeePerGas : precomposedTransaction . maxFeePerGas ?? undefined ,
251
+ maxPriorityFeePerGas : precomposedTransaction . maxPriorityFeePerGas ?? undefined ,
252
+ gasPrice :
253
+ ! precomposedTransaction . maxFeePerGas && precomposedTransaction . feePerByte
254
+ ? precomposedTransaction . feePerByte
255
+ : undefined ,
335
256
nonce,
336
257
} ) ;
337
258
0 commit comments