Skip to content

Commit

Permalink
feat: move slippage to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Oct 1, 2023
1 parent c41d592 commit f8c6418
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 63 deletions.
19 changes: 3 additions & 16 deletions libs/oeth/redeem/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { contracts } from '@origin/shared/contracts';
import {
BlockExplorerLink,
usePushNotification,
useSlippage,
} from '@origin/shared/providers';
import { isNilOrEmpty } from '@origin/shared/utils';
import {
Expand Down Expand Up @@ -35,26 +36,12 @@ export const useHandleAmountInChange = () => {
);
};

export const useHandleSlippageChange = () => {
const [, setRedeemState] = useRedeemState();

return useCallback(
(value: number) => {
setRedeemState(
produce((state) => {
state.slippage = value;
}),
);
},
[setRedeemState],
);
};

export const useHandleRedeem = () => {
const intl = useIntl();
const { value: slippage } = useSlippage();
const pushNotification = usePushNotification();
const { address } = useAccount();
const [{ amountIn, amountOut, slippage }, setRedeemState] = useRedeemState();
const [{ amountIn, amountOut }, setRedeemState] = useRedeemState();
const wagmiClient = useQueryClient();

return useCallback(async () => {
Expand Down
1 change: 0 additions & 1 deletion libs/oeth/redeem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export type RedeemState = {
split: RedeemEstimate[];
gas: bigint;
rate: number;
slippage: number;
isEstimateLoading: boolean;
isRedeemLoading: boolean;
};
22 changes: 13 additions & 9 deletions libs/oeth/redeem/src/views/RedeemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import {
import { GasPopover } from '@origin/oeth/shared';
import { TokenInput } from '@origin/shared/components';
import { tokens } from '@origin/shared/contracts';
import { ConnectedButton, usePrices } from '@origin/shared/providers';
import {
ConnectedButton,
usePrices,
useSlippage,
} from '@origin/shared/providers';
import { composeContexts } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';

import { RedeemRoute } from '../components/RedeemRoute';
import {
useHandleAmountInChange,
useHandleRedeem,
useHandleSlippageChange,
} from '../hooks';
import { useHandleAmountInChange, useHandleRedeem } from '../hooks';
import { RedeemProvider, useRedeemState } from '../state';

import type { BoxProps } from '@mui/material';
Expand Down Expand Up @@ -55,20 +55,24 @@ export const RedeemView = () =>

function RedeemViewWrapped() {
const intl = useIntl();
const { value: slippage, set: setSlippage } = useSlippage();
const { address, isConnected } = useAccount();
const [{ amountIn, slippage, isRedeemLoading, isEstimateLoading }] =
useRedeemState();
const [{ amountIn, isRedeemLoading, isEstimateLoading }] = useRedeemState();
const { data: prices, isLoading: isPricesLoading } = usePrices();
const { data: balOeth, isLoading: isBalOethLoading } = useBalance({
address,
token: tokens.mainnet.OETH.address,
watch: true,
scopeKey: 'redeem_balance',
});
const handleSlippageChange = useHandleSlippageChange();

const handleAmountInChange = useHandleAmountInChange();
const handleRedeem = useHandleRedeem();

const handleSlippageChange = (val: number) => {
setSlippage(val);
};

const redeemButtonLabel =
amountIn === 0n
? intl.formatMessage({ defaultMessage: 'Enter an amount' })
Expand Down
44 changes: 12 additions & 32 deletions libs/oeth/swap/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useCallback, useMemo } from 'react';

import { useCurve, usePushNotification } from '@origin/shared/providers';
import {
useCurve,
usePushNotification,
useSlippage,
} from '@origin/shared/providers';
import { isNilOrEmpty } from '@origin/shared/utils';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { produce } from 'immer';
Expand Down Expand Up @@ -168,21 +172,6 @@ export const useSwapRouteAllowance = (route: SwapRoute) => {
});
};

export const useHandleSlippageChange = () => {
const [, setSwapState] = useSwapState();

return useCallback(
(value: number) => {
setSwapState(
produce((state) => {
state.slippage = value;
}),
);
},
[setSwapState],
);
};

export const useHandleApprove = () => {
const intl = useIntl();
const { address } = useAccount();
Expand Down Expand Up @@ -265,13 +254,14 @@ export const useHandleApprove = () => {

export const useHandleSwap = () => {
const intl = useIntl();
const { value: slippage } = useSlippage();
const { address } = useAccount();
const curve = useCurve();
const queryClient = useQueryClient();
const wagmiClient = useWagmiClient();
const pushNotification = usePushNotification();
const [
{ amountIn, amountOut, selectedSwapRoute, slippage, tokenIn, tokenOut },
{ amountIn, amountOut, selectedSwapRoute, tokenIn, tokenOut },
setSwapState,
] = useSwapState();

Expand Down Expand Up @@ -304,35 +294,25 @@ export const useHandleSwap = () => {
title: intl.formatMessage({ defaultMessage: 'Swap complete' }),
severity: 'success',
});
setSwapState(
produce((draft) => {
draft.isSwapLoading = false;
}),
);
},
onError: () => {
pushNotification({
title: intl.formatMessage({ defaultMessage: 'Swap failed' }),
severity: 'error',
});
setSwapState(
produce((draft) => {
draft.isSwapLoading = false;
}),
);
},
onReject: () => {
pushNotification({
title: intl.formatMessage({ defaultMessage: 'Swap cancelled' }),
severity: 'info',
});
setSwapState(
produce((draft) => {
draft.isSwapLoading = false;
}),
);
},
});
setSwapState(
produce((draft) => {
draft.isSwapLoading = false;
}),
);
}, [
address,
amountIn,
Expand Down
1 change: 0 additions & 1 deletion libs/oeth/swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export type SwapState = {
tokenOut: Token;
swapRoutes: EstimatedSwapRoute[];
selectedSwapRoute: EstimatedSwapRoute | null;
slippage: number;
isSwapRoutesLoading: boolean;
isApproved: boolean;
isApprovalLoading: boolean;
Expand Down
14 changes: 10 additions & 4 deletions libs/oeth/swap/src/views/SwapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
} from '@mui/material';
import { GasPopover } from '@origin/oeth/shared';
import { TokenInput } from '@origin/shared/components';
import { ConnectedButton, usePrices } from '@origin/shared/providers';
import {
ConnectedButton,
usePrices,
useSlippage,
} from '@origin/shared/providers';
import { composeContexts, isNilOrEmpty } from '@origin/shared/utils';
import { useIntl } from 'react-intl';
import { useAccount, useBalance } from 'wagmi';
Expand All @@ -27,7 +31,6 @@ import { routeActionLabel } from '../constants';
import {
useHandleAmountInChange,
useHandleApprove,
useHandleSlippageChange,
useHandleSwap,
useHandleTokenChange,
useHandleTokenFlip,
Expand Down Expand Up @@ -69,6 +72,7 @@ export const SwapView = () =>

function SwapViewWrapped() {
const intl = useIntl();
const { value: slippage, set: setSlippage } = useSlippage();
const { address, isConnected } = useAccount();
const [tokenSource, setTokenSource] = useState<TokenSource | null>(null);
const [
Expand All @@ -78,7 +82,6 @@ function SwapViewWrapped() {
tokenIn,
tokenOut,
selectedSwapRoute,
slippage,
isSwapLoading,
isSwapRoutesLoading,
isApprovalLoading,
Expand All @@ -99,7 +102,6 @@ function SwapViewWrapped() {
watch: true,
scopeKey: 'swap_balance',
});
const handleSlippageChange = useHandleSlippageChange();
const handleAmountInChange = useHandleAmountInChange();
const handleTokenChange = useHandleTokenChange();
const handleTokenFlip = useHandleTokenFlip();
Expand All @@ -114,6 +116,10 @@ function SwapViewWrapped() {
handleTokenChange(tokenSource, value);
};

const handleSlippageChange = (val: number) => {
setSlippage(val);
};

const needsApproval =
isConnected &&
amountIn > 0n &&
Expand Down
1 change: 1 addition & 0 deletions libs/shared/providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './gas';
export * from './geoFence';
export * from './notifications';
export * from './prices';
export * from './slippage';
export * from './wagmi';
7 changes: 7 additions & 0 deletions libs/shared/providers/src/slippage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useLocalStorageValue } from '@react-hookz/web';

export const useSlippage = () => {
return useLocalStorageValue('@originprotocol/oeth-slippage', {
defaultValue: 0.001,
});
};

0 comments on commit f8c6418

Please sign in to comment.