@@ -7,7 +7,7 @@ import { GasToken } from '@common/constants/token.constant';
7
7
import { useAdenaContext } from '@hooks/use-context' ;
8
8
import { useQuery , UseQueryOptions , UseQueryResult } from '@tanstack/react-query' ;
9
9
import { NetworkFeeSettingInfo , NetworkFeeSettingType } from '@types' ;
10
- import { Document } from 'adena-module' ;
10
+ import { Document , documentToDefaultTx } from 'adena-module' ;
11
11
import BigNumber from 'bignumber.js' ;
12
12
import { useGetGasPriceTier } from './use-get-gas-price' ;
13
13
@@ -39,10 +39,27 @@ function makeGasInfoBy(
39
39
} ;
40
40
}
41
41
42
+ function modifyDocument ( document : Document , gasWanted : number , gasFee : number ) : Document {
43
+ return {
44
+ ...document ,
45
+ fee : {
46
+ ...document . fee ,
47
+ gas : gasWanted . toString ( ) ,
48
+ amount : [
49
+ {
50
+ denom : GasToken . denom ,
51
+ amount : gasFee . toString ( ) ,
52
+ } ,
53
+ ] ,
54
+ } ,
55
+ } ;
56
+ }
57
+
42
58
export const useGetEstimateGasPriceTiers = (
43
59
document : Document | null | undefined ,
44
60
gasUsed : number | undefined ,
45
61
gasAdjustment : string ,
62
+ isSuccessSimulate = true ,
46
63
options ?: UseQueryOptions < NetworkFeeSettingInfo [ ] | null , Error > ,
47
64
) : UseQueryResult < NetworkFeeSettingInfo [ ] | null > => {
48
65
const { transactionGasService } = useAdenaContext ( ) ;
@@ -80,16 +97,30 @@ export const useGetEstimateGasPriceTiers = (
80
97
const { gasWanted : resultGasWanted , gasFee : resultGasFee } = makeGasInfoBy (
81
98
gasUsed ,
82
99
adjustedGasPrice ,
83
- GAS_FEE_SAFETY_MARGIN ,
100
+ isSuccessSimulate ? GAS_FEE_SAFETY_MARGIN : 2 ,
84
101
) ;
85
102
103
+ const modifiedDocument = modifyDocument ( document , resultGasWanted , resultGasFee ) ;
104
+
105
+ const isSuccess = await transactionGasService
106
+ . simulateTx ( documentToDefaultTx ( modifiedDocument ) )
107
+ . then ( ( ) => true )
108
+ . catch ( ( e : Error ) => {
109
+ if ( e ?. message === '/std.InvalidPubKeyError' ) {
110
+ return true ;
111
+ }
112
+
113
+ return false ;
114
+ } ) ;
115
+
86
116
return {
87
117
settingType : tier ,
88
118
gasInfo : {
89
119
gasFee : resultGasFee ,
90
120
gasUsed,
91
121
gasWanted : resultGasWanted ,
92
122
gasPrice : adjustedGasPrice ,
123
+ hasError : ! isSuccess ,
93
124
} ,
94
125
} ;
95
126
} ) ,
0 commit comments