From 9a3f0bee717f44959c798b8edf0feca1aaaeb5a3 Mon Sep 17 00:00:00 2001 From: Eugene Chybisov Date: Wed, 22 Mar 2023 16:58:16 +0800 Subject: [PATCH] feat: add insurance config option --- packages/widget-playground/src/config.ts | 3 ++- packages/widget/src/hooks/useSwapRoutes.ts | 9 ++++----- packages/widget/src/pages/SwapPage/SwapPage.tsx | 5 +++-- packages/widget/src/types/widget.ts | 7 +++++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/widget-playground/src/config.ts b/packages/widget-playground/src/config.ts index 4a5d8599d..4841ab11e 100644 --- a/packages/widget-playground/src/config.ts +++ b/packages/widget-playground/src/config.ts @@ -29,10 +29,11 @@ export const widgetBaseConfig: WidgetConfig = { // disabledUI: ['toAddress', 'fromAmount', 'toToken', 'fromToken'], // requiredUI: ['toAddress'], // slippage: 0.003, - maxPriceImpact: 1, + insurance: true, sdkConfig: { apiUrl: 'https://develop.li.quest/v1', defaultRouteOptions: { + maxPriceImpact: 1, // slippage: 0.03, // order: 'SAFEST', // allowSwitchChain: false, diff --git a/packages/widget/src/hooks/useSwapRoutes.ts b/packages/widget/src/hooks/useSwapRoutes.ts index a071a00a2..cf1856ef7 100644 --- a/packages/widget/src/hooks/useSwapRoutes.ts +++ b/packages/widget/src/hooks/useSwapRoutes.ts @@ -21,7 +21,7 @@ export const useSwapRoutes = ({ insurableRoute, }: SwapRoutesProps = {}) => { const lifi = useLiFi(); - const { variant, sdkConfig, maxPriceImpact } = useWidgetConfig(); + const { variant, sdkConfig, insurance } = useWidgetConfig(); const { account, provider } = useWallet(); const queryClient = useQueryClient(); const { @@ -102,9 +102,9 @@ export const useSwapRoutes = ({ routePriority, variant, sdkConfig?.defaultRouteOptions?.allowSwitchChain, - maxPriceImpact, enabledRefuel && enabledAutoRefuel, gasRecommendation?.fromAmount, + insurance, insurableRoute?.id, ]; @@ -131,9 +131,9 @@ export const useSwapRoutes = ({ routePriority, variant, allowSwitchChain, - maxPriceImpact, enabledRefuel, gasRecommendationFromAmount, + insurance, insurableRouteId, ], signal, @@ -233,8 +233,7 @@ export const useSwapRoutes = ({ }, order: routePriority, allowSwitchChain: variant === 'refuel' ? false : allowSwitchChain, - maxPriceImpact, - insurance: insurableRoute ? true : false, + insurance: insurance ? Boolean(insurableRoute) : undefined, }, }, { signal }, diff --git a/packages/widget/src/pages/SwapPage/SwapPage.tsx b/packages/widget/src/pages/SwapPage/SwapPage.tsx index 338c58253..bcd72add8 100644 --- a/packages/widget/src/pages/SwapPage/SwapPage.tsx +++ b/packages/widget/src/pages/SwapPage/SwapPage.tsx @@ -25,7 +25,7 @@ export const SwapPage: React.FC = () => { const { t } = useTranslation(); const { setValue } = useFormContext(); const { navigateBack } = useNavigateBack(); - const { variant } = useWidgetConfig(); + const { variant, insurance } = useWidgetConfig(); const { state }: any = useLocation(); const stateRouteId = state?.routeId; const [routeId, setRouteId] = useState(stateRouteId); @@ -80,11 +80,12 @@ export const SwapPage: React.FC = () => { }; const SwapButton = - status === RouteExecutionStatus.Idle + insurance && status === RouteExecutionStatus.Idle ? StartIdleSwapButton : StartSwapButton; const insuranceAvailable = + insurance && variant !== 'refuel' && (route?.insurance?.state === 'INSURED' || (status === RouteExecutionStatus.Idle && diff --git a/packages/widget/src/types/widget.ts b/packages/widget/src/types/widget.ts index 08b447716..22513975e 100644 --- a/packages/widget/src/types/widget.ts +++ b/packages/widget/src/types/widget.ts @@ -75,7 +75,10 @@ export interface SDKConfig | 'disableVersionCheck' | 'integrator' > { - defaultRouteOptions?: Omit; + defaultRouteOptions?: Omit< + RouteOptions, + 'bridges' | 'exchanges' | 'insurance' + >; } export interface WidgetContractTool { @@ -112,7 +115,7 @@ export interface WidgetConfig { routePriority?: Order; slippage?: number; - maxPriceImpact?: number; + insurance?: boolean; variant?: WidgetVariant;