Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-orbs committed Dec 16, 2024
1 parent 50ee56c commit 31d5d38
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import { persist } from 'zustand/middleware';
interface AppStore {
slippage: number;
setSlippage: (slippage: number) => void;
forceLiquidityHub: boolean;
setForceLiquidityHub: (forceLiquidityHub: boolean) => void;
}
export const useAppState = create(
persist<AppStore>(
(set) => ({
slippage: 0.5,
setSlippage: (slippage: number) => set({ slippage }),
forceLiquidityHub: false,
setForceLiquidityHub: (forceLiquidityHub: boolean) => set({ forceLiquidityHub }),
}),
{
name: 'main-store',
Expand Down
9 changes: 0 additions & 9 deletions src/trade/liquidity-hub/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const initialState: State = {
acceptedQuote: undefined,
acceptedOptimalRate: undefined,
liquidityHubDisabled: false,
forceLiquidityHub: false,
confirmationModalOpen: false,
proceedWithLiquidityHub: false,
};
Expand All @@ -24,7 +23,6 @@ interface State {
inputAmount: string;
acceptedQuote: Quote | undefined;
liquidityHubDisabled: boolean;
forceLiquidityHub: boolean;
signature?: string;
confirmationModalOpen: boolean;
proceedWithLiquidityHub: boolean;
Expand Down Expand Up @@ -88,13 +86,6 @@ export const LiquidityHubSwapProvider = ({ children }: { children: ReactNode })

const sdk = useMemo(() => constructSDK({ partner: 'widget', chainId }), [chainId]);

useEffect(() => {
const params = new URLSearchParams(window.location.search);
const lh = params.get('lh');
if (lh) {
updateState({ forceLiquidityHub: true });
}
}, [updateState]);

return (
<LiquidityHubContext.Provider
Expand Down
4 changes: 3 additions & 1 deletion src/trade/liquidity-hub/useIsLiquidityHubTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { useLiquidityHubSwapContext } from './useLiquidityHubSwapContext';
import { useParaswapMinAmountOut } from './hooks';
import { useLiquidityHubQuote } from './useLiquidityHubQuote';
import BN from 'bignumber.js';
import { useAppState } from '@/store';

export const useIsLiquidityHubTrade = () => {
const {
state: { liquidityHubDisabled, proceedWithLiquidityHub, forceLiquidityHub },
state: { liquidityHubDisabled, proceedWithLiquidityHub },
} = useLiquidityHubSwapContext();
const {forceLiquidityHub} = useAppState()
const liquidityHubQuote = useLiquidityHubQuote().data;
const paraswapMinAmountOut = useParaswapMinAmountOut();

Expand Down
15 changes: 13 additions & 2 deletions src/trade/liquidity-hub/useLiquidityHubSwapCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { useLiquidityHubSwapContext } from "./useLiquidityHubSwapContext";
import { useOptimalRate, useLiquidityHubApproval } from "./hooks";
import { useLiquidityHubQuote } from "./useLiquidityHubQuote";

export const isRejectedError = (error: any) => {
const message = error.message?.toLowerCase();
return message?.includes('rejected') || message?.includes('denied');
};


export function useLiquidityHubSwapCallback(
updateSwapProgressState: (values: Partial<SwapProgressState>) => void
) {
Expand Down Expand Up @@ -118,6 +124,7 @@ export function useLiquidityHubSwapCallback(
updateState({ signature });
} catch (error) {
liquidityHub.analytics.onSignatureFailed((error as Error).message);
throw error;
}

// Pass the liquidity provider txData if possible
Expand Down Expand Up @@ -150,9 +157,13 @@ export function useLiquidityHubSwapCallback(
console.log("Swapped");
updateSwapProgressState({ swapStatus: SwapStatus.SUCCESS });
} catch (error) {
updateSwapProgressState({ swapStatus: SwapStatus.FAILED });
if(isRejectedError(error)) {
updateSwapProgressState({ swapStatus: undefined });
}else{
updateSwapProgressState({ swapStatus: SwapStatus.FAILED });
throw error;
}

throw error;
}
},
});
Expand Down
28 changes: 17 additions & 11 deletions src/trade/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { useAppState } from "@/store";
import { SettingsIcon } from "lucide-react";
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Switch } from '@/components/ui/switch';
import { useAppState } from '@/store';
import { SettingsIcon } from 'lucide-react';

export const Settings = () => {
const { slippage, setSlippage } = useAppState();
const { slippage, setSlippage, forceLiquidityHub, setForceLiquidityHub } = useAppState();
return (
<div className="flex justify-end">
<Popover>
Expand All @@ -35,6 +32,15 @@ export const Settings = () => {
<div>%</div>
</div>
</div>
<div className="flex gap-4 items-center justify-between">
<Label htmlFor="slippage">Liquidity Hub only</Label>
<div className="flex items-center gap-2">
<Switch
checked={forceLiquidityHub}
onCheckedChange={() => setForceLiquidityHub(!forceLiquidityHub)}
/>
</div>
</div>
</div>
</PopoverContent>
</Popover>
Expand Down

0 comments on commit 31d5d38

Please sign in to comment.