From 06dff777f353230f39c20fc28abc0ac35a3cc38b Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Thu, 21 Nov 2024 03:42:43 +0700 Subject: [PATCH] [FRE-1237] have more intelligent gas limit estimations per chain tx (#486) --- .changeset/rude-days-mix.md | 5 +++++ packages/widget/src/constants/widget.ts | 4 +++- .../src/hooks/useCosmosFeeAssetValidation.ts | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .changeset/rude-days-mix.md diff --git a/.changeset/rude-days-mix.md b/.changeset/rude-days-mix.md new file mode 100644 index 00000000..d2e8f6ba --- /dev/null +++ b/.changeset/rude-days-mix.md @@ -0,0 +1,5 @@ +--- +'@skip-go/widget': patch +--- + +gas amount set by chain id diff --git a/packages/widget/src/constants/widget.ts b/packages/widget/src/constants/widget.ts index f1483725..d6ae1e44 100644 --- a/packages/widget/src/constants/widget.ts +++ b/packages/widget/src/constants/widget.ts @@ -2,4 +2,6 @@ export const SLIPPAGE_OPTIONS = [0.5, 3, 5, 10]; export const DEFAULT_DECIMAL_PLACES = 6; -export const COSMOS_GAS_FEE = 2_000_000; +export const DEFAULT_COSMOS_GAS_AMOUNT = 200_000; + +export const SWAP_COSMOS_GAS_AMOUNT = 2_000_000; diff --git a/packages/widget/src/hooks/useCosmosFeeAssetValidation.ts b/packages/widget/src/hooks/useCosmosFeeAssetValidation.ts index dfb9e110..10781dc9 100644 --- a/packages/widget/src/hooks/useCosmosFeeAssetValidation.ts +++ b/packages/widget/src/hooks/useCosmosFeeAssetValidation.ts @@ -1,19 +1,20 @@ -import { skipAssetsAtom, skipChainsAtom } from "@/state/skipClient"; +import { skipAssetsAtom, skipChainsAtom, skipSwapVenuesAtom } from "@/state/skipClient"; import { convertTokenAmountToHumanReadableAmount } from "@/utils/crypto"; import { Decimal } from "@cosmjs/math"; import { GasPrice, calculateFee } from "@cosmjs/stargate"; -import { useAtom } from "jotai"; +import { useAtomValue } from "jotai"; import { useGetBalance } from "./useGetBalance"; import { sourceAssetAtom } from "@/state/swapPage"; import { BigNumber } from "bignumber.js"; -import { COSMOS_GAS_FEE } from "@/constants/widget"; +import { DEFAULT_COSMOS_GAS_AMOUNT, SWAP_COSMOS_GAS_AMOUNT } from "@/constants/widget"; import { useMemo } from "react"; import { useMaxAmountTokenMinusFees } from "@/pages/SwapPage/useSetMaxAmount"; /** feeAmount is in crypto amount */ export const useCosmosFeeAssetsBalanceValidation = (chainId?: string) => { const getBalance = useGetBalance(); - const [{ data: chains }] = useAtom(skipChainsAtom); + const { data: chains } = useAtomValue(skipChainsAtom); + const { data: swapVenues } = useAtomValue(skipSwapVenuesAtom); const feeAssetsState = useMemo(() => { if (!chainId) return undefined; @@ -28,7 +29,8 @@ export const useCosmosFeeAssetsBalanceValidation = (chainId?: string) => { return new GasPrice(Decimal.fromUserInput(price, 18), a.denom); })(); if (!gasPrice) return undefined; - const fee = calculateFee(Math.ceil(parseFloat(String(COSMOS_GAS_FEE))), gasPrice); + const isSwapChain = swapVenues?.map(venue => venue.chainID).includes(chainId) + const fee = calculateFee(Math.ceil(parseFloat(String(isSwapChain ? SWAP_COSMOS_GAS_AMOUNT : DEFAULT_COSMOS_GAS_AMOUNT))), gasPrice); const feeAmount = fee.amount[0].amount return { @@ -38,14 +40,14 @@ export const useCosmosFeeAssetsBalanceValidation = (chainId?: string) => { isSufficient: balance ? BigNumber(balance).isGreaterThanOrEqualTo(BigNumber(feeAmount)) : false, } }).filter((asset) => asset) as { feeAmount: string, denom: string, balanceWithFees: string, isSufficient: boolean }[]; - }, [chainId, chains, getBalance]); + }, [chainId, chains, getBalance, swapVenues]); return feeAssetsState; } export const useCosmosFeeAssetSourceAmountValidation = () => { - const [sourceAsset] = useAtom(sourceAssetAtom); - const [{ data: assets }] = useAtom(skipAssetsAtom); + const sourceAsset = useAtomValue(sourceAssetAtom); + const { data: assets } = useAtomValue(skipAssetsAtom); const cosmosFees = useCosmosFeeAssetsBalanceValidation(sourceAsset?.chainID); const maxAmountTokenMinusFees = useMaxAmountTokenMinusFees();