1- import { DEFAULT_GAS_PRICE_RATE , GAS_FEE_SAFETY_MARGIN } from '@common/constants/gas.constant' ;
2- import { GasToken } from '@common/constants/token.constant' ;
1+ import { DEFAULT_GAS_PRICE_RATE , DEFAULT_GAS_USED } from '@common/constants/gas.constant' ;
32import { INVALID_PUBLIC_KEY_ERROR_TYPE } from '@common/constants/tx-error.constant' ;
4- import { DEFAULT_GAS_WANTED } from '@common/constants/tx.constant' ;
5- import { useAdenaContext } from '@hooks/use-context' ;
3+ import { useAdenaContext , useWalletContext } from '@hooks/use-context' ;
64import { useQuery , UseQueryOptions , UseQueryResult } from '@tanstack/react-query' ;
75import { NetworkFeeSettingInfo , NetworkFeeSettingType } from '@types' ;
8- import { Document , documentToDefaultTx } from 'adena-module' ;
6+ import { Document } from 'adena-module' ;
97import BigNumber from 'bignumber.js' ;
8+ import { makeEstimateGasTransaction } from './use-get-estimate-gas-info' ;
109import { useGetGasPrice } from './use-get-gas-price' ;
1110
1211export const GET_ESTIMATE_GAS_PRICE_TIERS = 'transactionGas/getEstimateGasPriceTiers' ;
1312
1413const REFETCH_INTERVAL = 5_000 ;
1514
16- function makeGasInfoBy (
17- gasUsed : number | null | undefined ,
18- gasPrice : number | null | undefined ,
19- safetyMargin : number ,
20- ) : {
21- gasWanted : number ;
22- gasFee : number ;
23- } {
24- if ( ! gasUsed || ! gasPrice ) {
25- return {
26- gasWanted : 0 ,
27- gasFee : 0 ,
28- } ;
29- }
30-
31- const gasWantedBN = BigNumber ( gasUsed ) . multipliedBy ( safetyMargin ) ;
32- const gasFeeBN = BigNumber ( gasUsed ) . multipliedBy ( gasPrice ) ;
33-
34- return {
35- gasWanted : Number ( gasWantedBN . toFixed ( 0 , BigNumber . ROUND_DOWN ) ) ,
36- gasFee : Number ( gasFeeBN . toFixed ( 0 , BigNumber . ROUND_UP ) ) ,
37- } ;
38- }
39-
40- function makeDefaultGasInfoBy (
41- gasUsed : number | null | undefined ,
42- gasPrice : number | null | undefined ,
43- ) : {
44- gasWanted : number ;
45- gasFee : number ;
46- } {
47- if ( ! gasUsed || ! gasPrice ) {
48- return {
49- gasWanted : 0 ,
50- gasFee : 0 ,
51- } ;
52- }
53-
54- const gasFeeBN = BigNumber ( gasUsed ) . multipliedBy ( gasPrice ) ;
55-
56- return {
57- gasWanted : DEFAULT_GAS_WANTED ,
58- gasFee : Number ( gasFeeBN . toFixed ( 0 , BigNumber . ROUND_UP ) ) ,
59- } ;
60- }
61-
62- function modifyDocument ( document : Document , gasWanted : number , gasFee : number ) : Document {
63- return {
64- ...document ,
65- fee : {
66- ...document . fee ,
67- gas : gasWanted . toString ( ) ,
68- amount : [
69- {
70- denom : GasToken . denom ,
71- amount : gasFee . toString ( ) ,
72- } ,
73- ] ,
74- } ,
75- } ;
76- }
77-
7815export const useGetEstimateGasPriceTiers = (
7916 document : Document | null | undefined ,
8017 gasUsed : number | undefined ,
8118 gasAdjustment : string ,
82- isSuccessSimulate = true ,
8319 options ?: UseQueryOptions < NetworkFeeSettingInfo [ ] | null , Error > ,
8420) : UseQueryResult < NetworkFeeSettingInfo [ ] | null > => {
85- const { transactionGasService } = useAdenaContext ( ) ;
21+ const { transactionGasService, transactionService } = useAdenaContext ( ) ;
8622 const { data : gasPrice } = useGetGasPrice ( ) ;
23+ const { wallet } = useWalletContext ( ) ;
8724
8825 return useQuery < NetworkFeeSettingInfo [ ] | null , Error > ( {
8926 queryKey : [
@@ -96,45 +33,83 @@ export const useGetEstimateGasPriceTiers = (
9633 gasPrice || 0 ,
9734 ] ,
9835 queryFn : async ( ) : Promise < NetworkFeeSettingInfo [ ] | null > => {
99- if ( ! transactionGasService || ! document || gasUsed === undefined || ! gasPrice ) {
36+ if ( ! transactionService || ! transactionGasService || ! document || ! gasPrice ) {
10037 return null ;
10138 }
10239
10340 return Promise . all (
10441 Object . keys ( NetworkFeeSettingType ) . map ( async ( key ) => {
10542 const tier = key as NetworkFeeSettingType ;
10643
107- const adjustedGasPriceBN = BigNumber ( gasPrice )
108- . multipliedBy ( DEFAULT_GAS_PRICE_RATE [ tier ] )
109- . multipliedBy ( gasAdjustment ) ;
44+ const adjustGasUsedBN = BigNumber ( gasUsed || DEFAULT_GAS_USED ) . multipliedBy (
45+ DEFAULT_GAS_PRICE_RATE [ tier ] ,
46+ ) ;
47+ const adjustGasUsed = adjustGasUsedBN . toFixed ( 0 , BigNumber . ROUND_DOWN ) ;
48+ const adjustedGasPriceBN = BigNumber ( gasPrice ) . multipliedBy ( gasAdjustment ) ;
11049 const adjustedGasPrice = adjustedGasPriceBN . toNumber ( ) ;
111-
112- const { gasWanted : resultGasWanted , gasFee : resultGasFee } = isSuccessSimulate
113- ? makeGasInfoBy ( gasUsed , adjustedGasPrice , GAS_FEE_SAFETY_MARGIN )
114- : makeDefaultGasInfoBy ( gasUsed , adjustedGasPrice ) ;
115-
116- const modifiedDocument = modifyDocument ( document , resultGasWanted , resultGasFee ) ;
117-
118- const errorMessage = await transactionGasService
119- . simulateTx ( documentToDefaultTx ( modifiedDocument ) )
120- . then ( ( ) => null )
50+ const gasFee = adjustedGasPriceBN
51+ . multipliedBy ( adjustGasUsed )
52+ . toFixed ( 0 , BigNumber . ROUND_UP ) ;
53+
54+ const tx = await makeEstimateGasTransaction (
55+ wallet ,
56+ transactionService ,
57+ document ,
58+ Number ( adjustGasUsed ) ,
59+ adjustedGasPriceBN . toNumber ( ) ,
60+ ) ;
61+
62+ console . log ( 'tx' , tx ) ;
63+ console . log ( 'gasUsed' , gasUsed ) ;
64+ console . log ( 'gasPrice' , adjustedGasPrice ) ;
65+ console . log ( 'gasFee' , gasFee ) ;
66+ console . log ( 'document' , document ) ;
67+
68+ if ( ! tx ) {
69+ return {
70+ settingType : tier ,
71+ gasInfo : {
72+ gasFee : 0 ,
73+ gasUsed : Number ( adjustGasUsed ) ,
74+ gasWanted : Number ( adjustGasUsed ) ,
75+ gasPrice : adjustedGasPrice ,
76+ hasError : true ,
77+ simulateErrorMessage : 'Failed to simulate transaction' ,
78+ } ,
79+ } ;
80+ }
81+
82+ const result = await transactionGasService
83+ . simulateTx ( tx )
84+ . then ( ( simulateResult ) => {
85+ return {
86+ gasUsed : simulateResult . gasUsed . toNumber ( ) ,
87+ errorMessage : null ,
88+ } ;
89+ } )
12190 . catch ( ( e : Error ) => {
12291 if ( e ?. message === INVALID_PUBLIC_KEY_ERROR_TYPE ) {
123- return null ;
92+ return {
93+ gasUsed : Number ( adjustGasUsed ) ,
94+ errorMessage : null ,
95+ } ;
12496 }
12597
126- return e ?. message || '' ;
98+ return {
99+ gasUsed : Number ( adjustGasUsed ) ,
100+ errorMessage : e ?. message || '' ,
101+ } ;
127102 } ) ;
128103
129104 return {
130105 settingType : tier ,
131106 gasInfo : {
132- gasFee : resultGasFee ,
133- gasUsed,
134- gasWanted : resultGasWanted ,
107+ gasFee : Number ( gasFee ) ,
108+ gasUsed : result . gasUsed ,
109+ gasWanted : result . gasUsed ,
135110 gasPrice : adjustedGasPrice ,
136- hasError : errorMessage !== null ,
137- simulateErrorMessage : errorMessage ,
111+ hasError : result . errorMessage !== null ,
112+ simulateErrorMessage : result . errorMessage ,
138113 } ,
139114 } ;
140115 } ) ,
0 commit comments