Skip to content

Commit

Permalink
feat: editable slippage
Browse files Browse the repository at this point in the history
  • Loading branch information
berteotti committed Oct 24, 2024
1 parent b6b2d3c commit 0bdaf42
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
9 changes: 5 additions & 4 deletions app/components/ConfirmTrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
successToast,
toast,
} from '@swapr/ui';
import { SLIPPAGE, SwapDirection, SwapState } from '.';
import { SwapDirection, SwapState } from '.';
import MarketABI from '@/abi/market.json';
import {
CONDITIONAL_TOKEN_CONTRACT_ADDRESS,
Expand All @@ -22,7 +22,7 @@ import {
import ConditionalTokensABI from '@/abi/conditionalTokens.json';
import { addFraction, removeFraction } from '@/utils/price';
import { Abi, Address, erc20Abi, formatEther, parseEther } from 'viem';
import { useTx } from '@/context';
import { useSlippage, useTx } from '@/context';
import { Outcome, Token } from '@/entities';
import { formatEtherWithFixedDecimals, shortenAddress } from '@/utils';
import {
Expand Down Expand Up @@ -61,6 +61,7 @@ export const ConfirmTrade = ({
const { isModalOpen, closeModal } = useModal();
const { submitTx } = useTx();
const [isApproving, setIsApproving] = useState(false);
const { slippage } = useSlippage();

useEffect(() => {
if (!swapState.isAllowed) {
Expand Down Expand Up @@ -199,7 +200,7 @@ export const ConfirmTrade = ({
if (!tokenAmountOut || !sellAmount) return;

const roundedAmountOut = removeFraction(tokenAmountOut, ROUNDING_PRECISON);
const maxSellAmount = addFraction(sellAmount as bigint, SLIPPAGE);
const maxSellAmount = addFraction(sellAmount as bigint, slippage);

submitTx({
abi: MarketABI as Abi,
Expand Down Expand Up @@ -263,7 +264,7 @@ export const ConfirmTrade = ({
</div>
<div className="flex items-center justify-between">
<p className="text-text-low-em">Slippage</p>
<p>{SLIPPAGE * 100}%</p>
<p>{slippage * 100}%</p>
</div>
</div>
</div>
Expand Down
46 changes: 41 additions & 5 deletions app/components/Swapbox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
'use client';

import { Button, IconButton } from '@swapr/ui';
import {
Button,
IconButton,
Popover,
PopoverContent,
PopoverTrigger,
ToggleGroup,
ToggleGroupOption,
} from '@swapr/ui';
import { SwapInput } from './ui/SwapInput';
import { useEffect, useState } from 'react';
import { parseEther, Address, formatEther } from 'viem';
Expand Down Expand Up @@ -31,8 +39,8 @@ import { gnosis } from 'viem/chains';
import { formatEtherWithFixedDecimals, formatValueWithFixedDecimals } from '@/utils';
import { useQuery } from '@tanstack/react-query';
import { getTokenUSDPrice } from '@/queries/mobula';
import { useSlippage } from '@/context';

export const SLIPPAGE = 0.01;
const ONE_UNIT = '1';

export enum SwapDirection {
Expand Down Expand Up @@ -73,6 +81,7 @@ export const Swapbox = ({ fixedProductMarketMaker }: SwapboxProps) => {
const { address, isDisconnected, chainId: connectorChainId } = useAccount();
const supportedChains = useChains();
const { openModal } = useModal();
const { slippage } = useSlippage();

const [tokenAmountIn, setTokenAmountIn] = useState('');
const [tokenAmountOut, setTokenAmountOut] = useState<bigint>();
Expand Down Expand Up @@ -137,8 +146,7 @@ export const Swapbox = ({ fixedProductMarketMaker }: SwapboxProps) => {
useEffect(() => {
if (isBuying) {
if (!buyAmount) return;

const amountOut = removeFraction(buyAmount as bigint, SLIPPAGE);
const amountOut = removeFraction(buyAmount as bigint, slippage);

setTokenAmountOut(amountOut);
} else {
Expand All @@ -162,6 +170,7 @@ export const Swapbox = ({ fixedProductMarketMaker }: SwapboxProps) => {
isBuying,
outcome.index,
tokenAmountIn,
slippage,
]);

useEffect(() => {
Expand Down Expand Up @@ -337,7 +346,10 @@ export const Swapbox = ({ fixedProductMarketMaker }: SwapboxProps) => {
</div>
<div className="flex items-center justify-between">
<p className="text-text-low-em">Slippage</p>
<p>{SLIPPAGE * 100}%</p>
<div className="flex items-center space-x-2">
<p>{slippage * 100}%</p>
<SlippageSettings />
</div>
</div>
{potentialProfit && (
<div className="flex items-center justify-between">
Expand Down Expand Up @@ -398,3 +410,27 @@ export const Swapbox = ({ fixedProductMarketMaker }: SwapboxProps) => {
</>
);
};

const SlippageSettings = () => {
const { slippage, updateSlippage } = useSlippage();

return (
<Popover>
<PopoverTrigger asChild>
<IconButton name="settings" size="xs" variant="ghost" />
</PopoverTrigger>
<PopoverContent className="px-4">
<div className="space-y-2">
<div className="flex items-center text-text-low-em">
<p className="text-xs font-bold">Slippage tolerance</p>
</div>
<ToggleGroup value={slippage} onChange={updateSlippage}>
<ToggleGroupOption value={0.0001}>0.01%</ToggleGroupOption>
<ToggleGroupOption value={0.001}>0.1%</ToggleGroupOption>
<ToggleGroupOption value={0.01}>1%</ToggleGroupOption>
</ToggleGroup>
</div>
</PopoverContent>
</Popover>
);
};
3 changes: 2 additions & 1 deletion app/components/TransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export const TransactionModal = ({
variant="pastel"
size="lg"
>
View in explorer <Icon name="arrow-top-right" />
<p>View in explorer</p>
<Icon name="arrow-top-right" />
</Button>
</a>
</DialogFooter>
Expand Down
29 changes: 29 additions & 0 deletions context/SlippageContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PropsWithChildren, createContext, useContext, useState } from 'react';

export interface SlippageContextProps {
slippage: number;
updateSlippage: (slippage: number) => void;
}

const DEFAULT_SLIPPAGE = 0.0001;

const SlippageContext = createContext<SlippageContextProps>({
slippage: DEFAULT_SLIPPAGE,
updateSlippage: () => {},
});

export const useSlippage = () => useContext(SlippageContext);

export const SlippageProvider = ({ children }: PropsWithChildren) => {
const [slippage, setSlippage] = useState(DEFAULT_SLIPPAGE);

const updateSlippage = (newSlippage: number) => {
setSlippage(newSlippage);
};

return (
<SlippageContext.Provider value={{ slippage, updateSlippage }}>
{children}
</SlippageContext.Provider>
);
};
1 change: 1 addition & 0 deletions context/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ModalContext';
export * from './TxContext';
export * from './SlippageContext';
6 changes: 3 additions & 3 deletions providers/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { config } from './chain-config';
import { ConnectKitProvider, Types } from 'connectkit';
import { ThemeProvider as NextThemesProvider, useTheme } from 'next-themes';
import { TxProvider, ModalProvider } from '@/context';
import { TxProvider, ModalProvider, SlippageProvider } from '@/context';
import { useAnalytics } from '@/hooks';
import { Toaster, TooltipProvider } from '@swapr/ui';

Expand All @@ -22,10 +22,10 @@ export const Providers = ({ children }: PropsWithChildren) => {
<NextThemesProvider>
<CustomConnectKitProvider>
<ModalProvider>
<Toaster position="top-right" toastOptions={{ duration: 7000 }} />
<TxProvider>
<TooltipProvider delayDuration={300}>
{children}{' '}
<Toaster position="top-right" toastOptions={{ duration: 7000 }} />
<SlippageProvider>{children}</SlippageProvider>
</TooltipProvider>
</TxProvider>
</ModalProvider>
Expand Down

0 comments on commit 0bdaf42

Please sign in to comment.