Skip to content

Commit

Permalink
[FRE-1237] have more intelligent gas limit estimations per chain tx (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki authored Nov 20, 2024
1 parent 1042690 commit 06dff77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-days-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@skip-go/widget': patch
---

gas amount set by chain id
4 changes: 3 additions & 1 deletion packages/widget/src/constants/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
18 changes: 10 additions & 8 deletions packages/widget/src/hooks/useCosmosFeeAssetValidation.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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();

Expand Down

0 comments on commit 06dff77

Please sign in to comment.