Skip to content

Commit

Permalink
refactor: update default gas amount and slippage settings in widget c…
Browse files Browse the repository at this point in the history
…onfiguration (#546)
  • Loading branch information
ericHgorski authored Dec 9, 2024
1 parent e666095 commit 10d5222
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-balloons-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@skip-go/client': patch
'@skip-go/widget': patch
---

update default gas amounts and improve code readability
4 changes: 2 additions & 2 deletions docs/widget/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ settings?: {
};
```

- `customGasAmount`: Gas amount for CosmosSDK chain transactions. Default: `200_000`.
- `slippage`: Default slippage percentage (0-100) for CosmosSDK chain swaps. Default: `3`.
- `customGasAmount`: Gas amount for CosmosSDK chain transactions. Default: `300_000`.
- `slippage`: Default slippage percentage (0-100) for CosmosSDK chain swaps. Default: `1`.

### `onlyTestnet`

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ export class SkipClient {
return null;
}
if (chainID === 'noble-1') {
const fee = calculateFee(200000, gasPrice);
const fee = calculateFee(200_000, gasPrice);
return fee;
}
return calculateFee(Math.ceil(parseFloat(estimatedGasAmount)), gasPrice);
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/codegen/chains.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/widget/src/constants/cosmosChains/explorers.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/widget/src/constants/cosmosChains/mainnet.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/widget/src/constants/cosmosChains/testnet.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions packages/widget/src/constants/widget.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { RoutePreference } from "@/state/swapPage";

export const SLIPPAGE_OPTIONS = [0.1, 0.5, 1, 3];
export const DEFAULT_SLIPPAGE = 1;

export const ROUTE_PREFERENCE_OPTIONS: RoutePreference[] = [
RoutePreference.FASTEST,
RoutePreference.CHEAPEST,
];
export const DEFAULT_DECIMAL_PLACES = 6;

export const DEFAULT_COSMOS_GAS_AMOUNT = 200_000;

export const SWAP_COSMOS_GAS_AMOUNT = 2_000_000;
6 changes: 3 additions & 3 deletions packages/widget/src/hooks/useCosmosFeeAssetValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { Decimal } from "@cosmjs/math";
import { GasPrice, calculateFee } from "@cosmjs/stargate";
import { useAtomValue } from "jotai";
import { useGetBalance } from "./useGetBalance";
import { sourceAssetAtom } from "@/state/swapPage";
import { CosmosGasAmount, sourceAssetAtom } from "@/state/swapPage";
import { BigNumber } from "bignumber.js";
import { DEFAULT_COSMOS_GAS_AMOUNT, SWAP_COSMOS_GAS_AMOUNT } from "@/constants/widget";
import { useMemo } from "react";
import { useMaxAmountTokenMinusFees } from "@/pages/SwapPage/useSetMaxAmount";

Expand All @@ -30,7 +29,8 @@ export const useCosmosFeeAssetsBalanceValidation = (chainId?: string) => {
})();
if (!gasPrice) return undefined;
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 gasAmount = Math.ceil(isSwapChain ? CosmosGasAmount.SWAP : CosmosGasAmount.DEFAULT);
const fee = calculateFee(gasAmount, gasPrice);
const feeAmount = fee.amount[0].amount;

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/widget/src/pages/SwapPage/SwapPageFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const SwapPageFooterItems = ({
const settingsChanged = useMemo(() => {
return (
swapSettings.slippage !== defaultSwapSettings.slippage ||
swapSettings.customGasAmount !== defaultSwapSettings.customGasAmount
swapSettings.customGasAmount !== defaultSwapSettings.customGasAmount ||
swapSettings.routePreference !== defaultSwapSettings.routePreference
);
}, [swapSettings]);

Expand Down
17 changes: 9 additions & 8 deletions packages/widget/src/state/swapExecutionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ export const skipSubmitSwapExecutionAtom = atomWithMutation((get) => {
const submitSwapExecutionCallbacks = get(submitSwapExecutionCallbacksAtom);
const swapSettings = get(swapSettingsAtom);

const getFallbackGasAmount = async (_chainID: string, chainType: ChainType) => {
if (chainType === "cosmos") {
if (_chainID === "carbon-1") {
return 1_000_000;
}
return swapSettings.customGasAmount;
}
}

return {
gcTime: Infinity,
Expand All @@ -229,14 +237,7 @@ export const skipSubmitSwapExecutionAtom = atomWithMutation((get) => {
userAddresses,
slippageTolerancePercent: swapSettings.slippage.toString(),
simulate: route.sourceAssetChainID !== "984122",
getFallbackGasAmount: async (_chainID, chainType) => {
if (chainType === "cosmos") {
if (_chainID === "carbon-1") {
return 10_000_000;
}
return swapSettings.customGasAmount;
}
},
getFallbackGasAmount,
...submitSwapExecutionCallbacks,
});
} catch (error: unknown) {
Expand Down
9 changes: 8 additions & 1 deletion packages/widget/src/state/swapPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ export enum RoutePreference {
FASTEST = "Fastest",
CHEAPEST = "Cheapest",
}

export const CosmosGasAmount = {
DEFAULT: 300_000,
SWAP: 2_800_000,
CARBON: 1_000_000,
}

export const defaultSwapSettings = {
slippage: 1,
customGasAmount: 200_000,
customGasAmount: CosmosGasAmount.DEFAULT,
routePreference: RoutePreference.FASTEST,
};

Expand Down
4 changes: 2 additions & 2 deletions packages/widget/src/widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export type WidgetProps = {
settings?: {
/**
* Default slippage percentage (0-100) for CosmosSDK chain swaps.
* @default 3
* @default 1
*/
slippage?: number;
/**
* Gas amount for CosmosSDK chain transactions.
* @default 200_000
* @default 300_000
*/
customGasAmount?: number;
};
Expand Down

0 comments on commit 10d5222

Please sign in to comment.