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

fix route config prop #446

Merged
merged 1 commit into from
Nov 9, 2024
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
34 changes: 32 additions & 2 deletions packages/widget/src/state/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
debouncedDestinationAssetAmountValueInitializedAtom,
} from "./swapPage";
import { atomEffect } from "jotai-effect";
import { WidgetRouteConfig } from "@/widget/Widget";

export const initializeDebounceValuesEffect = atomEffect((get, set) => {
const sourceAsset = get(sourceAssetAtom);
Expand Down Expand Up @@ -78,7 +79,7 @@ type CaughtRouteError = {
error: unknown;
};

export const routeConfigAtom = atom<RouteConfig>({
export const routeConfigAtom = atom<WidgetRouteConfig>({
experimentalFeatures: ["hyperlane"],
allowMultiTx: true,
allowUnsafe: true,
Expand All @@ -88,6 +89,34 @@ export const routeConfigAtom = atom<RouteConfig>({
},
});

export const convertWidgetRouteConfigToClientRouteConfig = (params: WidgetRouteConfig): RouteConfig => {
return {
...params,
swapVenues: params.swapVenues?.map((venue) => ({
...venue,
chainID: venue.chainId,
})),
swapVenue: params.swapVenue && {
...params.swapVenue,
chainID: params.swapVenue.chainId,
},
};
}

export const convertClientRouteConfigToWidgetRouteConfig = (params: RouteConfig): WidgetRouteConfig => {
return {
...params,
swapVenues: params.swapVenues?.map((venue) => ({
...venue,
chainId: venue.chainID,
})),
swapVenue: params.swapVenue && {
...params.swapVenue,
chainId: params.swapVenue.chainID,
},
};
}

export const _skipRouteAtom = atomWithQuery((get) => {
const skip = get(skipClient);
const params = get(skipRouteRequestAtom);
Expand All @@ -110,10 +139,11 @@ export const _skipRouteAtom = atomWithQuery((get) => {
throw new Error("No route request provided");
}
try {
const _skipRouteConfig = convertWidgetRouteConfigToClientRouteConfig(skipRouteConfig);
const response = await skip.route({
...params,
smartRelay: true,
...skipRouteConfig,
..._skipRouteConfig,
});
return response;
} catch (error) {
Expand Down
27 changes: 13 additions & 14 deletions packages/widget/src/widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import {
import { routeConfigAtom } from "@/state/route";
import { RouteConfig } from "@skip-go/client";

export type WidgetRouteConfig =
Omit<RouteConfig, "swapVenues" | "swapVenue"> & {
swapVenues?: NewSwapVenueRequest[];
swapVenue?: NewSwapVenueRequest;
};

export type WidgetProps = {
theme?: PartialTheme | "light" | "dark";
brandColor?: string;
Expand All @@ -45,9 +51,7 @@ export type WidgetProps = {
*/
customGasAmount?: number;
};
routeConfig?: Omit<RouteConfig, "swapVenues"> & {
swapVenues?: NewSwapVenueRequest[];
};
routeConfig?: WidgetRouteConfig;
filter?: ChainFilter;
} & NewSkipClientOptions;

Expand Down Expand Up @@ -115,17 +119,12 @@ const WidgetWithoutNiceModalProvider = (props: WidgetProps) => {
});
}
if (props.routeConfig) {
// temp before clientLibrary updates swapVenues to be
// { name: string, chainId: string }[]
const tempClientRouteConfig = props.routeConfig;
if (tempClientRouteConfig.swapVenues) {
(tempClientRouteConfig as RouteConfig).swapVenues =
tempClientRouteConfig.swapVenues.map((swapVenue) => ({
name: swapVenue.name,
chainID: swapVenue.chainId,
}));
}
setRouteConfig(tempClientRouteConfig as RouteConfig);
setRouteConfig((prev) => {
return {
...prev,
...props.routeConfig,
};
});
}
if (props.filter) {
setChainFilter(props.filter);
Expand Down
Loading