From 0c5ceabe56e596377b40329c7083b3459f1314a2 Mon Sep 17 00:00:00 2001 From: SIhun Ahn Date: Fri, 7 Jun 2024 19:24:01 +0900 Subject: [PATCH 1/5] refactor((cosmos sign page)): edit fee coin selection logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - fee코인을 선택할 때 fee코인 내에서 선택되는게 아닌 tx의 fee.amount필드의 값이 선택되도록 수정했습니다. - 사인 페이지에서 fee코인을 변경할 때 decimals가 변경되지 않는 오류를 수정했습니다. --- .../pages/Popup/Cosmos/Sign/Amino/entry.tsx | 70 +++++++++++++++--- .../pages/Popup/Cosmos/Sign/Direct/entry.tsx | 71 ++++++++++++++++--- 2 files changed, 121 insertions(+), 20 deletions(-) diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx index 7a903fcca..49eed2da8 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx @@ -12,6 +12,8 @@ import Tooltip from '~/Popup/components/common/Tooltip'; import Fee from '~/Popup/components/Fee'; import LedgerToTab from '~/Popup/components/Loading/LedgerToTab'; import PopupHeader from '~/Popup/components/PopupHeader'; +import { useAssetsSWR } from '~/Popup/hooks/SWR/cosmos/useAssetsSWR'; +import { useBalanceSWR } from '~/Popup/hooks/SWR/cosmos/useBalanceSWR'; import { useCurrentFeesSWR } from '~/Popup/hooks/SWR/cosmos/useCurrentFeesSWR'; import { useSimulateSWR } from '~/Popup/hooks/SWR/cosmos/useSimulateSWR'; import { useCurrentAccount } from '~/Popup/hooks/useCurrent/useCurrentAccount'; @@ -27,7 +29,7 @@ import CosmosApp from '~/Popup/utils/ledger/cosmos'; import { responseToWeb } from '~/Popup/utils/message'; import { broadcast, protoTx, protoTxBytes } from '~/Popup/utils/proto'; import { isEqualsIgnoringCase } from '~/Popup/utils/string'; -import type { CosmosChain, GasRateKey } from '~/types/chain'; +import type { CosmosChain, FeeCoin, GasRateKey } from '~/types/chain'; import type { Queue } from '~/types/extensionStorage'; import type { CosSignAmino, CosSignAminoResponse } from '~/types/message/cosmos'; import type { Path } from '~/types/route'; @@ -59,7 +61,37 @@ export default function Entry({ queue, chain }: EntryProps) { const [isProgress, setIsProgress] = useState(false); - const { feeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); + const assets = useAssetsSWR(chain); + + const balance = useBalanceSWR(chain); + + const selectableFeeCoins = useMemo( + () => [ + ...assets.data.map((asset) => ({ + originBaseDenom: asset.origin_denom, + baseDenom: asset.denom, + decimals: asset.decimals, + displayDenom: asset.symbol, + coinGeckoId: asset.coinGeckoId, + availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', + })), + ], + [assets.data, balance.data?.balance], + ); + + const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain); + + const feeCoins = useMemo(() => { + const aggregatedCoins = [...supportedFeeCoins, ...selectableFeeCoins]; + + const uniqueFeeCoins = aggregatedCoins.reduce((acc: FeeCoin[], current) => { + if (!acc.find((coin) => coin.baseDenom === current.baseDenom)) { + return [...acc, current]; + } + return acc; + }, []); + return uniqueFeeCoins; + }, [selectableFeeCoins, supportedFeeCoins]); const { message, messageId, origin, channel } = queue; @@ -93,11 +125,19 @@ export default function Entry({ queue, chain }: EntryProps) { const inputFeeAmount = useMemo(() => inputFee.amount, [inputFee.amount]); - const [currentFeeBaseDenom, setCurrentFeeBaseDenom] = useState( - feeCoins.find((item) => item.baseDenom === inputFee.denom)?.baseDenom ?? feeCoins[0].baseDenom, - ); + const [currentFeeBaseDenom, setCurrentFeeBaseDenom] = useState(inputFee.denom); - const currentFeeCoin = useMemo(() => feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) ?? feeCoins[0], [currentFeeBaseDenom, feeCoins]); + const currentFeeCoin = useMemo( + () => + feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { + availableAmount: '0', + decimals: 0, + baseDenom: inputFee.denom, + originBaseDenom: inputFee.denom, + displayDenom: 'UNKNOWN', + }, + [currentFeeBaseDenom, inputFee.denom, feeCoins], + ); const memoizedProtoTx = useMemo(() => { if (isEditFee) { @@ -124,7 +164,18 @@ export default function Entry({ queue, chain }: EntryProps) { [customGas, inputGas, simulatedGas], ); - const currentFeeGasRate = useMemo(() => currentFeeCoin.gasRate || gasRate || chain.gasRate, [chain.gasRate, currentFeeCoin.gasRate, gasRate]); + const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); + + const currentFeeGasRate = useMemo( + () => + currentFeeCoin.gasRate || + gasRate || { + average: initialGasRate, + low: initialGasRate, + tiny: initialGasRate, + }, + [currentFeeCoin.gasRate, gasRate, initialGasRate], + ); const signingMemo = useMemo(() => (isEditMemo ? memo : doc.memo), [doc.memo, isEditMemo, memo]); @@ -132,8 +183,6 @@ export default function Entry({ queue, chain }: EntryProps) { const isFeeUpdateAllowed = useMemo(() => isEditFee || isFeeCustomed, [isFeeCustomed, isEditFee]); - const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); - const baseFee = useMemo( () => (isFeeUpdateAllowed ? times(currentGas, currentGasRateKey ? currentFeeGasRate[currentGasRateKey] : initialGasRate) : inputFeeAmount), [currentFeeGasRate, currentGas, currentGasRateKey, initialGasRate, inputFeeAmount, isFeeUpdateAllowed], @@ -187,12 +236,13 @@ export default function Entry({ queue, chain }: EntryProps) { { setCurrentFeeBaseDenom(selectedFeeCoin.baseDenom); + setCustomGasRateKey('low'); }} onChangeGas={(g) => setCustomGas(g)} onChangeGasRateKey={(gasRateKey) => { diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx index dfbb0b6ea..641ee4f80 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx @@ -8,6 +8,8 @@ import { Tab, TabPanel, Tabs } from '~/Popup/components/common/Tab'; import Tooltip from '~/Popup/components/common/Tooltip'; import Fee from '~/Popup/components/Fee'; import PopupHeader from '~/Popup/components/PopupHeader'; +import { useAssetsSWR } from '~/Popup/hooks/SWR/cosmos/useAssetsSWR'; +import { useBalanceSWR } from '~/Popup/hooks/SWR/cosmos/useBalanceSWR'; import { useCurrentFeesSWR } from '~/Popup/hooks/SWR/cosmos/useCurrentFeesSWR'; import { useProtoBuilderDecodeSWR } from '~/Popup/hooks/SWR/cosmos/useProtoBuilderDecodeSWR'; import { useSimulateSWR } from '~/Popup/hooks/SWR/cosmos/useSimulateSWR'; @@ -21,7 +23,7 @@ import { cosmosURL, getDefaultAV, getPublicKeyType, signDirect } from '~/Popup/u import { responseToWeb } from '~/Popup/utils/message'; import { broadcast, decodeProtobufMessage, protoTxBytes } from '~/Popup/utils/proto'; import { cosmos } from '~/proto/cosmos-v0.44.2.js'; -import type { CosmosChain, GasRateKey } from '~/types/chain'; +import type { CosmosChain, FeeCoin, GasRateKey } from '~/types/chain'; import type { Queue } from '~/types/extensionStorage'; import type { CosSignDirect, CosSignDirectResponse } from '~/types/message/cosmos'; import type { Path } from '~/types/route'; @@ -50,7 +52,37 @@ export default function Entry({ queue, chain }: EntryProps) { const [isProgress, setIsProgress] = useState(false); - const { feeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); + const assets = useAssetsSWR(chain); + + const balance = useBalanceSWR(chain); + + const selectableFeeCoins = useMemo( + () => [ + ...assets.data.map((asset) => ({ + originBaseDenom: asset.origin_denom, + baseDenom: asset.denom, + decimals: asset.decimals, + displayDenom: asset.symbol, + coinGeckoId: asset.coinGeckoId, + availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', + })), + ], + [assets.data, balance.data?.balance], + ); + + const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); + + const feeCoins = useMemo(() => { + const aggregatedCoins = [...supportedFeeCoins, ...selectableFeeCoins]; + + const uniqueFeeCoins = aggregatedCoins.reduce((acc: FeeCoin[], current) => { + if (!acc.find((coin) => coin.baseDenom === current.baseDenom)) { + return [...acc, current]; + } + return acc; + }, []); + return uniqueFeeCoins; + }, [selectableFeeCoins, supportedFeeCoins]); const { message, messageId, origin, channel } = queue; @@ -95,11 +127,19 @@ export default function Entry({ queue, chain }: EntryProps) { const inputFeeAmount = useMemo(() => inputFee.amount || '0', [inputFee.amount]); - const [currentFeeBaseDenom, setCurrentFeeBaseDenom] = useState( - feeCoins.find((item) => item.baseDenom === inputFee.denom)?.baseDenom ?? feeCoins[0].baseDenom, - ); + const [currentFeeBaseDenom, setCurrentFeeBaseDenom] = useState(inputFee.denom); - const currentFeeCoin = useMemo(() => feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) ?? feeCoins[0], [currentFeeBaseDenom, feeCoins]); + const currentFeeCoin = useMemo( + () => + feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { + availableAmount: '0', + decimals: 0, + baseDenom: inputFee.denom || '', + originBaseDenom: inputFee.denom || '', + displayDenom: 'UNKNOWN', + }, + [currentFeeBaseDenom, inputFee.denom, feeCoins], + ); const memoizedProtoTx = useMemo(() => { if (isEditFee) { @@ -123,14 +163,24 @@ export default function Entry({ queue, chain }: EntryProps) { () => customGas || (gte(simulatedGas || '0', inputGas) ? simulatedGas || inputGas : inputGas), [customGas, inputGas, simulatedGas], ); - const currentFeeGasRate = useMemo(() => currentFeeCoin.gasRate || gasRate || chain.gasRate, [chain.gasRate, currentFeeCoin.gasRate, gasRate]); + + const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); + + const currentFeeGasRate = useMemo( + () => + currentFeeCoin.gasRate || + gasRate || { + average: initialGasRate, + low: initialGasRate, + tiny: initialGasRate, + }, + [currentFeeCoin.gasRate, gasRate, initialGasRate], + ); const isFeeCustomed = useMemo(() => !!customGas || !!customGasRateKey, [customGasRateKey, customGas]); const isFeeUpdateAllowed = useMemo(() => isEditFee || isFeeCustomed, [isFeeCustomed, isEditFee]); - const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); - const baseFee = useMemo( () => (isFeeUpdateAllowed ? times(currentGas, currentGasRateKey ? currentFeeGasRate[currentGasRateKey] : initialGasRate) : inputFeeAmount), [currentFeeGasRate, currentGas, currentGasRateKey, initialGasRate, inputFeeAmount, isFeeUpdateAllowed], @@ -226,12 +276,13 @@ export default function Entry({ queue, chain }: EntryProps) { { setCurrentFeeBaseDenom(selectedFeeCoin.baseDenom); + setCustomGasRateKey('low'); }} onChangeGas={(g) => setCustomGas(g)} onChangeGasRateKey={(gasRateKey) => { From 7f9baa4cbccb8f2438198f9ff04b92679e9e9c8b Mon Sep 17 00:00:00 2001 From: SIhun Ahn Date: Mon, 10 Jun 2024 11:46:58 +0900 Subject: [PATCH 2/5] chore: Update gas rate calculation logic in Cosmos sign pages --- .../pages/Popup/Cosmos/Sign/Amino/entry.tsx | 27 ++++++++++++------- .../pages/Popup/Cosmos/Sign/Direct/entry.tsx | 25 +++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx index 49eed2da8..df89eb1f1 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx @@ -22,7 +22,7 @@ import { useCurrentQueue } from '~/Popup/hooks/useCurrent/useCurrentQueue'; import { useLedgerTransport } from '~/Popup/hooks/useLedgerTransport'; import { useLoading } from '~/Popup/hooks/useLoading'; import { useTranslation } from '~/Popup/hooks/useTranslation'; -import { ceil, divide, equal, gte, times } from '~/Popup/utils/big'; +import { ceil, divide, equal, gt, gte, times } from '~/Popup/utils/big'; import { getAddress, getKeyPair } from '~/Popup/utils/common'; import { cosmosURL, getDefaultAV, getPublicKeyType, signAmino } from '~/Popup/utils/cosmos'; import CosmosApp from '~/Popup/utils/ledger/cosmos'; @@ -79,7 +79,7 @@ export default function Entry({ queue, chain }: EntryProps) { [assets.data, balance.data?.balance], ); - const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain); + const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); const feeCoins = useMemo(() => { const aggregatedCoins = [...supportedFeeCoins, ...selectableFeeCoins]; @@ -166,16 +166,25 @@ export default function Entry({ queue, chain }: EntryProps) { const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); - const currentFeeGasRate = useMemo( - () => - currentFeeCoin.gasRate || - gasRate || { + const currentFeeGasRate = useMemo(() => { + if (currentFeeCoin.gasRate) { + return currentFeeCoin.gasRate; + } + + if (gasRate) { + return gasRate; + } + + if (gt(initialGasRate, '0')) { + return { average: initialGasRate, low: initialGasRate, tiny: initialGasRate, - }, - [currentFeeCoin.gasRate, gasRate, initialGasRate], - ); + }; + } + + return chain.gasRate; + }, [chain.gasRate, currentFeeCoin.gasRate, gasRate, initialGasRate]); const signingMemo = useMemo(() => (isEditMemo ? memo : doc.memo), [doc.memo, isEditMemo, memo]); diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx index 641ee4f80..54aff04c4 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx @@ -17,7 +17,7 @@ import { useCurrentAccount } from '~/Popup/hooks/useCurrent/useCurrentAccount'; import { useCurrentPassword } from '~/Popup/hooks/useCurrent/useCurrentPassword'; import { useCurrentQueue } from '~/Popup/hooks/useCurrent/useCurrentQueue'; import { useTranslation } from '~/Popup/hooks/useTranslation'; -import { ceil, divide, equal, gte, times } from '~/Popup/utils/big'; +import { ceil, divide, equal, gt, gte, times } from '~/Popup/utils/big'; import { getAddress, getKeyPair } from '~/Popup/utils/common'; import { cosmosURL, getDefaultAV, getPublicKeyType, signDirect } from '~/Popup/utils/cosmos'; import { responseToWeb } from '~/Popup/utils/message'; @@ -166,16 +166,25 @@ export default function Entry({ queue, chain }: EntryProps) { const initialGasRate = useMemo(() => (equal(inputGas, '0') ? '0' : divide(inputFeeAmount, inputGas)), [inputFeeAmount, inputGas]); - const currentFeeGasRate = useMemo( - () => - currentFeeCoin.gasRate || - gasRate || { + const currentFeeGasRate = useMemo(() => { + if (currentFeeCoin.gasRate) { + return currentFeeCoin.gasRate; + } + + if (gasRate) { + return gasRate; + } + + if (gt(initialGasRate, '0')) { + return { average: initialGasRate, low: initialGasRate, tiny: initialGasRate, - }, - [currentFeeCoin.gasRate, gasRate, initialGasRate], - ); + }; + } + + return chain.gasRate; + }, [chain.gasRate, currentFeeCoin.gasRate, gasRate, initialGasRate]); const isFeeCustomed = useMemo(() => !!customGas || !!customGasRateKey, [customGasRateKey, customGas]); From 34aeea4146a66a23b02403ac568fdc30bf5eba3b Mon Sep 17 00:00:00 2001 From: SIhun Ahn Date: Mon, 10 Jun 2024 11:50:24 +0900 Subject: [PATCH 3/5] refactor(cosmos sign page): delete meaningless using spread operator --- src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx | 6 +++--- src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx index df89eb1f1..1035eda4a 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx @@ -66,8 +66,8 @@ export default function Entry({ queue, chain }: EntryProps) { const balance = useBalanceSWR(chain); const selectableFeeCoins = useMemo( - () => [ - ...assets.data.map((asset) => ({ + () => + assets.data.map((asset) => ({ originBaseDenom: asset.origin_denom, baseDenom: asset.denom, decimals: asset.decimals, @@ -75,7 +75,7 @@ export default function Entry({ queue, chain }: EntryProps) { coinGeckoId: asset.coinGeckoId, availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', })), - ], + [assets.data, balance.data?.balance], ); diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx index 54aff04c4..3010cb59e 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx @@ -57,8 +57,8 @@ export default function Entry({ queue, chain }: EntryProps) { const balance = useBalanceSWR(chain); const selectableFeeCoins = useMemo( - () => [ - ...assets.data.map((asset) => ({ + () => + assets.data.map((asset) => ({ originBaseDenom: asset.origin_denom, baseDenom: asset.denom, decimals: asset.decimals, @@ -66,7 +66,6 @@ export default function Entry({ queue, chain }: EntryProps) { coinGeckoId: asset.coinGeckoId, availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', })), - ], [assets.data, balance.data?.balance], ); From 1095f3187b831a19409211e5898ca911603aa429 Mon Sep 17 00:00:00 2001 From: SIhun Ahn Date: Mon, 10 Jun 2024 12:06:25 +0900 Subject: [PATCH 4/5] refactor: Edit fee coin selection logic in Cosmos sign pages --- .../pages/Popup/Cosmos/Sign/Amino/entry.tsx | 39 ++++++++---------- .../pages/Popup/Cosmos/Sign/Direct/entry.tsx | 40 +++++++++---------- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx index 1035eda4a..884b9cd7e 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx @@ -65,33 +65,28 @@ export default function Entry({ queue, chain }: EntryProps) { const balance = useBalanceSWR(chain); - const selectableFeeCoins = useMemo( - () => - assets.data.map((asset) => ({ - originBaseDenom: asset.origin_denom, - baseDenom: asset.denom, - decimals: asset.decimals, - displayDenom: asset.symbol, - coinGeckoId: asset.coinGeckoId, - availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', - })), - - [assets.data, balance.data?.balance], - ); - const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); - const feeCoins = useMemo(() => { - const aggregatedCoins = [...supportedFeeCoins, ...selectableFeeCoins]; + const availableFeeCoins = useMemo(() => { + const availableCoins = assets.data.map((asset) => ({ + originBaseDenom: asset.origin_denom, + baseDenom: asset.denom, + decimals: asset.decimals, + displayDenom: asset.symbol, + coinGeckoId: asset.coinGeckoId, + availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', + })); + + const aggregatedFeeCoins = [...supportedFeeCoins, ...availableCoins]; - const uniqueFeeCoins = aggregatedCoins.reduce((acc: FeeCoin[], current) => { + const uniqueFeeCoins = aggregatedFeeCoins.reduce((acc: FeeCoin[], current) => { if (!acc.find((coin) => coin.baseDenom === current.baseDenom)) { return [...acc, current]; } return acc; }, []); return uniqueFeeCoins; - }, [selectableFeeCoins, supportedFeeCoins]); + }, [assets.data, balance.data?.balance, supportedFeeCoins]); const { message, messageId, origin, channel } = queue; @@ -115,12 +110,12 @@ export default function Entry({ queue, chain }: EntryProps) { const inputFee = useMemo( () => - fee.amount?.find((item) => feeCoins.map((feeCoin) => feeCoin.baseDenom).includes(item.denom)) || + fee.amount?.find((item) => availableFeeCoins.map((feeCoin) => feeCoin.baseDenom).includes(item.denom)) || fee.amount?.[0] || { denom: chain.baseDenom, amount: '0', }, - [chain.baseDenom, fee.amount, feeCoins], + [chain.baseDenom, fee.amount, availableFeeCoins], ); const inputFeeAmount = useMemo(() => inputFee.amount, [inputFee.amount]); @@ -129,14 +124,14 @@ export default function Entry({ queue, chain }: EntryProps) { const currentFeeCoin = useMemo( () => - feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { + availableFeeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { availableAmount: '0', decimals: 0, baseDenom: inputFee.denom, originBaseDenom: inputFee.denom, displayDenom: 'UNKNOWN', }, - [currentFeeBaseDenom, inputFee.denom, feeCoins], + [currentFeeBaseDenom, inputFee.denom, availableFeeCoins], ); const memoizedProtoTx = useMemo(() => { diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx index 3010cb59e..faed5ff6e 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx @@ -56,32 +56,28 @@ export default function Entry({ queue, chain }: EntryProps) { const balance = useBalanceSWR(chain); - const selectableFeeCoins = useMemo( - () => - assets.data.map((asset) => ({ - originBaseDenom: asset.origin_denom, - baseDenom: asset.denom, - decimals: asset.decimals, - displayDenom: asset.symbol, - coinGeckoId: asset.coinGeckoId, - availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', - })), - [assets.data, balance.data?.balance], - ); - const { feeCoins: supportedFeeCoins, defaultGasRateKey } = useCurrentFeesSWR(chain, { suspense: true }); - const feeCoins = useMemo(() => { - const aggregatedCoins = [...supportedFeeCoins, ...selectableFeeCoins]; + const availableFeeCoins = useMemo(() => { + const availableCoins = assets.data.map((asset) => ({ + originBaseDenom: asset.origin_denom, + baseDenom: asset.denom, + decimals: asset.decimals, + displayDenom: asset.symbol, + coinGeckoId: asset.coinGeckoId, + availableAmount: balance.data?.balance?.find((item) => item.denom === asset.denom)?.amount || '0', + })); + + const aggregatedFeeCoins = [...supportedFeeCoins, ...availableCoins]; - const uniqueFeeCoins = aggregatedCoins.reduce((acc: FeeCoin[], current) => { + const uniqueFeeCoins = aggregatedFeeCoins.reduce((acc: FeeCoin[], current) => { if (!acc.find((coin) => coin.baseDenom === current.baseDenom)) { return [...acc, current]; } return acc; }, []); return uniqueFeeCoins; - }, [selectableFeeCoins, supportedFeeCoins]); + }, [assets.data, balance.data?.balance, supportedFeeCoins]); const { message, messageId, origin, channel } = queue; @@ -110,7 +106,9 @@ export default function Entry({ queue, chain }: EntryProps) { const inputGas = useMemo(() => (fee?.gas_limit ? String(fee.gas_limit) : '0'), [fee?.gas_limit]); const inputFee = useMemo(() => { - const foundFee = fee?.amount?.find((item) => item?.denom && item?.amount && feeCoins.map((feeCoin) => feeCoin.baseDenom).includes(item?.denom || '')); + const foundFee = fee?.amount?.find( + (item) => item?.denom && item?.amount && availableFeeCoins.map((feeCoin) => feeCoin.baseDenom).includes(item?.denom || ''), + ); if (foundFee) return foundFee; @@ -122,7 +120,7 @@ export default function Entry({ queue, chain }: EntryProps) { denom: chain.baseDenom, amount: '0', }; - }, [chain.baseDenom, fee?.amount, feeCoins]); + }, [chain.baseDenom, fee?.amount, availableFeeCoins]); const inputFeeAmount = useMemo(() => inputFee.amount || '0', [inputFee.amount]); @@ -130,14 +128,14 @@ export default function Entry({ queue, chain }: EntryProps) { const currentFeeCoin = useMemo( () => - feeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { + availableFeeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { availableAmount: '0', decimals: 0, baseDenom: inputFee.denom || '', originBaseDenom: inputFee.denom || '', displayDenom: 'UNKNOWN', }, - [currentFeeBaseDenom, inputFee.denom, feeCoins], + [currentFeeBaseDenom, inputFee.denom, availableFeeCoins], ); const memoizedProtoTx = useMemo(() => { From dd35aa7521c03a8bef261d85a7bf4e3effec4f6f Mon Sep 17 00:00:00 2001 From: SIhun Ahn Date: Thu, 13 Jun 2024 12:19:34 +0900 Subject: [PATCH 5/5] fix: Checking unknown fee coin balance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 체인리스트에 등록되지 않은 코인일지라도 fee코인으로 사용될 수 있기 때문에 무조건적으로 밸런스를 0으로 설정하는 것이 아닌 'balance' 리스폰스의 밸런스 값을 참조한 값이 설정되도록 변경했습니다. --- src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx | 4 ++-- src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx index 884b9cd7e..cd13ec4a8 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Amino/entry.tsx @@ -125,13 +125,13 @@ export default function Entry({ queue, chain }: EntryProps) { const currentFeeCoin = useMemo( () => availableFeeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { - availableAmount: '0', + availableAmount: balance.data?.balance?.find((item) => item.denom === inputFee.denom)?.amount || '0', decimals: 0, baseDenom: inputFee.denom, originBaseDenom: inputFee.denom, displayDenom: 'UNKNOWN', }, - [currentFeeBaseDenom, inputFee.denom, availableFeeCoins], + [availableFeeCoins, balance.data?.balance, currentFeeBaseDenom, inputFee.denom], ); const memoizedProtoTx = useMemo(() => { diff --git a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx index faed5ff6e..20821a0f0 100644 --- a/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx +++ b/src/Popup/pages/Popup/Cosmos/Sign/Direct/entry.tsx @@ -129,13 +129,13 @@ export default function Entry({ queue, chain }: EntryProps) { const currentFeeCoin = useMemo( () => availableFeeCoins.find((item) => item.baseDenom === currentFeeBaseDenom) || { - availableAmount: '0', + availableAmount: balance.data?.balance?.find((item) => item.denom === inputFee.denom)?.amount || '0', decimals: 0, baseDenom: inputFee.denom || '', originBaseDenom: inputFee.denom || '', displayDenom: 'UNKNOWN', }, - [currentFeeBaseDenom, inputFee.denom, availableFeeCoins], + [availableFeeCoins, balance.data?.balance, currentFeeBaseDenom, inputFee.denom], ); const memoizedProtoTx = useMemo(() => {