-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathuse-network-fee.ts
192 lines (160 loc) · 5.74 KB
/
use-network-fee.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { DEFAULT_GAS_ADJUSTMENT } from '@common/constants/gas.constant';
import { GasToken } from '@common/constants/token.constant';
import { GasInfo, NetworkFee, NetworkFeeSettingInfo, NetworkFeeSettingType } from '@types';
import { Document } from 'adena-module';
import BigNumber from 'bignumber.js';
import { useCallback, useMemo, useState } from 'react';
import {
useGetDefaultEstimateGasInfo,
useGetEstimateGasInfo,
} from './transaction-gas/use-get-estimate-gas-info';
import { useGetEstimateGasPriceTiers } from './transaction-gas/use-get-estimate-gas-price-tiers';
import { useGetGasPriceTier } from './transaction-gas/use-get-gas-price';
export interface UseNetworkFeeReturn {
isLoading: boolean;
isFetchedPriceTiers: boolean;
isFetchedEstimateGasInfo: boolean;
isSimulateError: boolean;
currentGasInfo: GasInfo | null;
currentGasFeeRawAmount: number;
changedGasInfo: GasInfo | null;
networkFee: NetworkFee | null;
networkFeeSettingType: NetworkFeeSettingType;
networkFeeSettings: NetworkFeeSettingInfo[] | null;
gasAdjustment: string;
setGasAdjustment: (ratio: string) => void;
setNetworkFeeSetting: (settingInfo: NetworkFeeSettingInfo) => void;
save: () => void;
}
const defaultSettingType = NetworkFeeSettingType.AVERAGE;
export const useNetworkFee = (
document?: Document | null,
isDefaultGasPrice = false,
gasInfo?: GasInfo | null,
): UseNetworkFeeReturn => {
const [currentNetworkFeeSettingType, setCurrentNetworkFeeSettingType] =
useState<NetworkFeeSettingType>(defaultSettingType);
const [networkFeeSettingType, setNetworkFeeSettingType] =
useState<NetworkFeeSettingType>(defaultSettingType);
const [selectedTier, setSelectedTier] = useState(!isDefaultGasPrice);
const [gasAdjustment, setGasAdjustment] = useState<string>(DEFAULT_GAS_ADJUSTMENT.toString());
const { data: gasPriceTier } = useGetGasPriceTier(GasToken.denom);
const { data: defaultEstimatedGasInfo, isFetched: isFetchedDefaultEstimatedGasInfo } =
useGetDefaultEstimateGasInfo(document);
const { data: estimatedGasInfo, isFetched: isFetchedEstimateGasInfo } = useGetEstimateGasInfo(
document,
gasInfo?.gasUsed || defaultEstimatedGasInfo?.gasUsed || 0,
);
const { data: gasPriceTiers, isFetched: isFetchedPriceTiers } = useGetEstimateGasPriceTiers(
document,
gasInfo?.gasUsed || estimatedGasInfo?.gasUsed,
gasAdjustment,
!estimatedGasInfo?.hasError,
);
const isLoading = useMemo(() => {
if (gasPriceTier === undefined || gasPriceTiers === undefined) {
return true;
}
if (!isFetchedDefaultEstimatedGasInfo || !isFetchedEstimateGasInfo || !isFetchedPriceTiers) {
return true;
}
return false;
}, [
gasPriceTier,
gasPriceTiers,
isFetchedDefaultEstimatedGasInfo,
isFetchedEstimateGasInfo,
isFetchedPriceTiers,
]);
const currentSettingType = useMemo(() => {
if (!gasPriceTiers || gasPriceTiers.length <= 0) {
return NetworkFeeSettingType.AVERAGE;
}
return currentNetworkFeeSettingType;
}, [currentNetworkFeeSettingType, gasPriceTiers]);
const changedSettingType = useMemo(() => {
if (!gasPriceTiers || gasPriceTiers.length <= 1) {
return NetworkFeeSettingType.AVERAGE;
}
return networkFeeSettingType;
}, [networkFeeSettingType, gasPriceTiers]);
const currentGasInfo = useMemo(() => {
if (!gasPriceTiers) {
return null;
}
const current = gasPriceTiers.find((setting) => setting.settingType === currentSettingType);
if (!current?.gasInfo) {
return {
gasFee: 0,
gasUsed: 0,
gasWanted: 0,
gasPrice: 0,
hasError: true,
};
}
return current?.gasInfo || null;
}, [gasPriceTiers, currentSettingType]);
const isSimulateError = useMemo(() => {
if (currentGasInfo?.hasError) {
return true;
}
if (!isFetchedDefaultEstimatedGasInfo && !defaultEstimatedGasInfo) {
return true;
}
return false;
}, [isFetchedDefaultEstimatedGasInfo, defaultEstimatedGasInfo, currentGasInfo]);
const changedGasInfo = useMemo(() => {
if (!gasPriceTiers) {
return null;
}
const current = gasPriceTiers.find((setting) => setting.settingType === changedSettingType);
return current?.gasInfo || null;
}, [changedSettingType, gasPriceTiers]);
const currentGasFeeRawAmount = useMemo(() => {
if (!currentGasInfo) {
return BigNumber(document?.fee.amount?.[0].amount || 0).toNumber();
}
const currentGasFeeAmount = BigNumber(currentGasInfo.gasFee).toFixed(0, BigNumber.ROUND_UP);
return Number(currentGasFeeAmount);
}, [currentGasInfo?.gasFee, document, selectedTier]);
const networkFee = useMemo(() => {
if (!gasPriceTier) {
return null;
}
if (!currentGasInfo) {
return null;
}
const networkFeeAmount = BigNumber(currentGasInfo.gasFee)
.shiftedBy(-GasToken.decimals)
.toFixed(GasToken.decimals)
.replace(/(\.\d*?)0+$/, '$1')
.replace(/\.$/, '');
return {
amount: networkFeeAmount,
denom: GasToken.symbol,
};
}, [gasPriceTier, currentGasInfo]);
const setNetworkFeeSetting = useCallback((settingInfo: NetworkFeeSettingInfo): void => {
setNetworkFeeSettingType(settingInfo.settingType);
}, []);
const save = useCallback((): void => {
setCurrentNetworkFeeSettingType(changedSettingType);
setSelectedTier(true);
}, [changedSettingType]);
return {
isLoading,
isFetchedEstimateGasInfo,
isFetchedPriceTiers: isFetchedEstimateGasInfo && isFetchedPriceTiers,
isSimulateError,
currentGasInfo,
currentGasFeeRawAmount,
changedGasInfo,
networkFeeSettingType: changedSettingType,
networkFeeSettings: gasPriceTiers || null,
setNetworkFeeSetting,
gasAdjustment,
setGasAdjustment,
networkFee,
save,
};
};