Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSW-371] fix: Manage slippage on local storage #207

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}