Skip to content

Commit

Permalink
Merge pull request #207 from gnoswap-labs/GSW-371-manage-slippage-on-…
Browse files Browse the repository at this point in the history
…local-storage

[GSW-371] fix: Manage slippage on local storage
  • Loading branch information
jinoosss authored Oct 12, 2023
2 parents 3a5a856 + f818af7 commit feb8dfd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/web/src/constants/option.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ export const PriceRangeTooltip: {
"A passive price range of [-50% ~ +100%] for moderate risks & returns.",
Custom: undefined,
};

export const DEFAULT_SLIPPAGE = 0.5;
22 changes: 22 additions & 0 deletions packages/web/src/hooks/common/use-slippage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useAtom } from "jotai";
import { CommonState } from "@states/index";
import { useCallback } from "react";
import { DEFAULT_SLIPPAGE } from "@constants/option.constant";

export const useSlippage = () => {
const [slippage, setSlippage] = useAtom(CommonState.slippage);

const changeSlippage = useCallback(
(slippage: number) => {
const changedSlippage = Math.min(100, Math.max(0, slippage));
setSlippage(changedSlippage);
},
[setSlippage],
);

const resetSlippage = useCallback(() => {
setSlippage(DEFAULT_SLIPPAGE);
}, [setSlippage]);

return { slippage, changeSlippage, resetSlippage };
};
4 changes: 4 additions & 0 deletions packages/web/src/states/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { NetworkModel } from "@models/common/network-model";
import { DEVICE_TYPE } from "@styles/media";
import { atom } from "jotai";
import NetworkData from "@resources/chains.json";
import { atomWithStorage } from "jotai/utils";
import { DEFAULT_SLIPPAGE } from "@constants/option.constant";

interface HeaderToggleProps {
walletConnect: boolean;
Expand All @@ -20,3 +22,5 @@ export const modalContent = atom<React.ReactNode | null>(null);
export const breakpoint = atom<DEVICE_TYPE>(DEVICE_TYPE.WEB);

export const network = atom<NetworkModel>(NetworkData[0]);

export const slippage = atomWithStorage<number>("slippage", DEFAULT_SLIPPAGE);
9 changes: 9 additions & 0 deletions packages/web/src/utils/number-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ export const toUnitFormat = (
// TODO : Else Return Type
return (usd ? "$" : "") + bigNumber.decimalPlaces(2).toString();
};

export function toDecimalNumber(
value: BigNumber | string | number,
decimals?: number,
) {
const powers = 10 ** (decimals || 0);
const num = BigNumber(value).toNumber();
return Math.round(num * powers) / powers;
}

0 comments on commit feb8dfd

Please sign in to comment.