Skip to content

Commit

Permalink
fix: adjust default gas price
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Jan 13, 2025
1 parent 69f7263 commit dab579b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const NetworkFee: React.FC<NetworkFeeProps> = ({
}) => {
const hasSetting = !!onClickSetting;

const isEmptyValue = value === '' || denom === '';

const hasNetworkFee = !!Number(value) && !!denom;

const hasError = isError || !hasNetworkFee;

return (
<NetworkFeeContainer>
<NetworkFeeWrapper isError={hasError}>
<NetworkFeeWrapper isError={hasError && !isEmptyValue}>
<span className='key'>{'Network Fee'}</span>

<div className='network-fee-amount-wrapper'>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { GasToken } from '@common/constants/token.constant';
import { DEFAULT_GAS_FEE } from '@common/constants/tx.constant';
import { useAdenaContext, useWalletContext } from '@hooks/use-context';
import { useQuery, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
import { NetworkFee } from '@types';
import { Document, documentToDefaultTx } from 'adena-module';
import BigNumber from 'bignumber.js';

export const GET_ESTIMATE_GAS_KEY = 'transactionGas/useGetSingleEstimateGas';

const REFETCH_INTERVAL = 5_000;

function makeTokenAmountWithDecimals(amount: string | number, decimals: number): string {
return BigNumber(amount)
.shiftedBy(decimals * -1)
.toFixed(decimals, BigNumber.ROUND_DOWN)
.replace(/\.?0+$/, '');
}

export const useGetSingleEstimateGas = (
document: Document | null | undefined,
options?: UseQueryOptions<NetworkFee | null, Error>,
): UseQueryResult<NetworkFee | null> => {
const { wallet, gnoProvider } = useWalletContext();
const { transactionGasService } = useAdenaContext();

return useQuery<NetworkFee | null, Error>({
queryKey: [GET_ESTIMATE_GAS_KEY, document?.msgs, document?.memo],
queryFn: async () => {
if (!document || !wallet || !gnoProvider) {
return null;
}

const result = await transactionGasService
.estimateGas(documentToDefaultTx(document))
.catch((e: Error) => {
if (e.message === '/std.InvalidPubKeyError') {
return DEFAULT_GAS_FEE;
}

return null;
});

if (!result) {
return null;
}

return {
denom: GasToken.symbol,
amount: makeTokenAmountWithDecimals(result, GasToken.decimals),
estimatedAmount: makeTokenAmountWithDecimals(result, GasToken.decimals),
};
},
refetchInterval: REFETCH_INTERVAL,
keepPreviousData: true,
...options,
});
};
24 changes: 21 additions & 3 deletions packages/adena-extension/src/hooks/wallet/use-network-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BigNumber from 'bignumber.js';
import { useCallback, useMemo, useState } from 'react';
import { useGetEstimateGas } from './transaction-gas/use-get-estimate-gas';
import { useGetGasPriceTires } from './transaction-gas/use-get-gas-price';
import { useGetSingleEstimateGas } from './transaction-gas/use-get-single-estimate-gas';

export interface UseNetworkFeeReturn {
currentGasPrice: GasPrice | null;
Expand All @@ -22,17 +23,25 @@ export interface UseNetworkFeeReturn {
const defaultSettingType = NetworkFeeSettingType.AVERAGE;
const defaultGasPriceRatio = '1';

export const useNetworkFee = (document?: Document | null): UseNetworkFeeReturn => {
export const useNetworkFee = (
document?: Document | null,
isDefaultGasPrice = false,
): UseNetworkFeeReturn => {
const [currentNetworkFeeSettingType, setCurrentNetworkFeeSettingType] =
useState<NetworkFeeSettingType>(defaultSettingType);
const [networkFeeSettingType, setNetworkFeeSettingType] =
useState<NetworkFeeSettingType>(defaultSettingType);
const [selectedTier, setSelectedTier] = useState(!isDefaultGasPrice);
const [gasPriceRatio, setGasPriceRatio] = useState<string>(defaultGasPriceRatio);

const { data: gasPriceTiers } = useGetGasPriceTires(gasPriceRatio);

const { data: estimatedGasPriceTiers } = useGetEstimateGas(document, gasPriceTiers);

const { data: estimatedDefaultGasPrice = null } = useGetSingleEstimateGas(document, {
enabled: !selectedTier,
});

const currentSettingType = useMemo(() => {
if (!gasPriceTiers || gasPriceTiers.length <= 0) {
return NetworkFeeSettingType.AVERAGE;
Expand Down Expand Up @@ -74,14 +83,18 @@ export const useNetworkFee = (document?: Document | null): UseNetworkFeeReturn =
}, [changedSettingType, networkFeeSettings]);

const currentGasPriceRawAmount = useMemo(() => {
if (!selectedTier) {
return BigNumber(document?.fee.amount?.[0].amount || 0).toNumber();
}

if (!currentGasPrice) {
return 0;
}

const currentGasPriceAmount = currentGasPrice?.amount;

return BigNumber(currentGasPriceAmount).shiftedBy(GasToken.decimals).toNumber();
}, [currentGasPrice]);
}, [currentGasPrice, document, selectedTier]);

const currentEstimateGas = useMemo(() => {
if (!estimatedGasPriceTiers) {
Expand All @@ -96,6 +109,10 @@ export const useNetworkFee = (document?: Document | null): UseNetworkFeeReturn =
}, [currentSettingType, estimatedGasPriceTiers]);

const networkFee = useMemo(() => {
if (!selectedTier) {
return estimatedDefaultGasPrice;
}

if (!currentEstimateGas) {
return null;
}
Expand All @@ -104,14 +121,15 @@ export const useNetworkFee = (document?: Document | null): UseNetworkFeeReturn =
amount: currentEstimateGas.estimatedAmount,
denom: currentEstimateGas.denom,
};
}, [currentEstimateGas]);
}, [currentEstimateGas, estimatedDefaultGasPrice, selectedTier]);

const setNetworkFeeSetting = useCallback((settingInfo: NetworkFeeSettingInfo): void => {
setNetworkFeeSettingType(settingInfo.settingType);
}, []);

const save = useCallback((): void => {
setCurrentNetworkFeeSettingType(changedSettingType);
setSelectedTier(true);
}, [changedSettingType]);

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Account, Document, isAirgapAccount, isLedgerAccount } from 'adena-module';
import BigNumber from 'bignumber.js';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';

Expand Down Expand Up @@ -68,7 +67,7 @@ const ApproveSignTransactionContainer: React.FC = () => {
const [processType, setProcessType] = useState<'INIT' | 'PROCESSING' | 'DONE'>('INIT');
const [response, setResponse] = useState<InjectionMessage | null>(null);
const [memo, setMemo] = useState('');
const useNetworkFeeReturn = useNetworkFee(document);
const useNetworkFeeReturn = useNetworkFee(document, true);

const processing = useMemo(() => processType !== 'INIT', [processType]);

Expand All @@ -81,23 +80,6 @@ const ApproveSignTransactionContainer: React.FC = () => {
return true;
}, [requestData?.data?.memo]);

const networkFee = useMemo(() => {
if (!document || document.fee.amount.length === 0) {
return {
amount: '1',
denom: GasToken.symbol,
};
}
const networkFeeAmount = document.fee.amount[0].amount;
const networkFeeAmountOfGnot = BigNumber(networkFeeAmount)
.shiftedBy(GasToken.decimals)
.toString();
return {
amount: networkFeeAmountOfGnot,
denom: GasToken.symbol,
};
}, [document]);

useEffect(() => {
checkLockWallet();
}, [walletService]);
Expand Down Expand Up @@ -339,7 +321,7 @@ const ApproveSignTransactionContainer: React.FC = () => {
processing={processing}
done={done}
logo={favicon}
networkFee={networkFee}
networkFee={useNetworkFeeReturn.networkFee}
useNetworkFeeReturn={useNetworkFeeReturn}
changeMemo={changeMemo}
onClickConfirm={onClickConfirm}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Account, Document, isAirgapAccount, isLedgerAccount } from 'adena-module';
import BigNumber from 'bignumber.js';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';

Expand Down Expand Up @@ -50,8 +49,6 @@ function mappedTransactionData(document: Document): TransactionData {
};
}

const DEFAULT_DENOM = 'GNOT';

const ApproveSignContainer: React.FC = () => {
const normalNavigate = useNavigate();
const { navigate } = useAppNavigate();
Expand All @@ -69,7 +66,9 @@ const ApproveSignContainer: React.FC = () => {
const [processType, setProcessType] = useState<'INIT' | 'PROCESSING' | 'DONE'>('INIT');
const [response, setResponse] = useState<InjectionMessage | null>(null);
const [memo, setMemo] = useState('');
const useNetworkFeeReturn = useNetworkFee(document);
const useNetworkFeeReturn = useNetworkFee(document, true);

const networkFee = useNetworkFeeReturn.networkFee;

const processing = useMemo(() => processType !== 'INIT', [processType]);

Expand All @@ -82,21 +81,6 @@ const ApproveSignContainer: React.FC = () => {
return true;
}, [requestData?.data?.memo]);

const networkFee = useMemo(() => {
if (!document || document.fee.amount.length === 0) {
return {
amount: '1',
denom: DEFAULT_DENOM,
};
}
const networkFeeAmount = document.fee.amount[0].amount;
const networkFeeAmountOfGnot = BigNumber(networkFeeAmount).shiftedBy(-6).toString();
return {
amount: networkFeeAmountOfGnot,
denom: DEFAULT_DENOM,
};
}, [document]);

useEffect(() => {
checkLockWallet();
}, [walletService]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const ApproveTransactionContainer: React.FC = () => {
const [processType, setProcessType] = useState<'INIT' | 'PROCESSING' | 'DONE'>('INIT');
const [response, setResponse] = useState<InjectionMessage | null>(null);
const [memo, setMemo] = useState('');
const useNetworkFeeReturn = useNetworkFee(document);
const useNetworkFeeReturn = useNetworkFee(document, true);
const networkFee = useNetworkFeeReturn.networkFee;

const processing = useMemo(() => processType !== 'INIT', [processType]);
Expand Down

0 comments on commit dab579b

Please sign in to comment.