-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from skip-mev/switch-to-widget
Switch to widget
- Loading branch information
Showing
9 changed files
with
1,143 additions
and
5,207 deletions.
There are no files selected for viewing
Submodule chain-registry
updated
134 files
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { useAssets, useChains } from "@skip-go/widget"; | ||
import { useQueryState } from "nuqs"; | ||
import { useEffect, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
|
||
export const useURLQueryParams = () => { | ||
const { data: chains } = useChains(); | ||
const { isReady, assetsByChainID } = useAssets(); | ||
const [state, setState] = useState<{ | ||
srcChain?: string; | ||
srcAssetDenom?: string; | ||
destChain?: string; | ||
destAssetDenom?: string; | ||
amountIn?: string; | ||
amountOut?: string; | ||
}>(); | ||
|
||
const toastId = "url-params-toast"; | ||
|
||
const [srcChainQP, setSrcChainQP] = useQueryState("src_chain"); | ||
const [srcAssetQP, setSrcAssetQP] = useQueryState("src_asset"); | ||
useEffect(() => { | ||
if (!chains || !isReady) return; | ||
if (srcChainQP) { | ||
const findChain = chains.find((x) => x.chainID.toLowerCase() === decodeURI(srcChainQP).toLowerCase()); | ||
if (findChain) { | ||
if (srcAssetQP) { | ||
const assets = assetsByChainID(findChain.chainID); | ||
const findAsset = assets.find((x) => x.denom.toLowerCase() === decodeURI(srcAssetQP).toLowerCase()); | ||
if (findAsset) { | ||
setState((prev) => ({ ...prev, srcChain: findChain.chainID, srcAssetDenom: findAsset.denom })); | ||
setSrcChainQP(null); | ||
setSrcAssetQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
} | ||
setState((prev) => ({ ...prev, srcChain: findChain.chainID })); | ||
} | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
setSrcChainQP(null); | ||
setSrcAssetQP(null); | ||
} | ||
}, [assetsByChainID, chains, isReady, setSrcAssetQP, setSrcChainQP, srcAssetQP, srcChainQP]); | ||
|
||
const [destChainQP, setDestChainQP] = useQueryState("dest_chain"); | ||
const [destAssetQP, setDestAssetQP] = useQueryState("dest_asset"); | ||
useEffect(() => { | ||
if (!chains || !isReady) return; | ||
if (destChainQP) { | ||
const findChain = chains.find((x) => x.chainID.toLowerCase() === decodeURI(destChainQP).toLowerCase()); | ||
if (findChain) { | ||
if (destAssetQP) { | ||
const assets = assetsByChainID(findChain.chainID); | ||
const findAsset = assets.find((x) => x.denom.toLowerCase() === decodeURI(destAssetQP).toLowerCase()); | ||
if (findAsset) { | ||
setState((prev) => ({ ...prev, destChain: findChain.chainID, destAssetDenom: findAsset.denom })); | ||
setDestChainQP(null); | ||
setDestAssetQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
} | ||
setState((prev) => ({ ...prev, destChain: findChain.chainID })); | ||
} | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
setDestChainQP(null); | ||
setDestAssetQP(null); | ||
} | ||
}, [assetsByChainID, chains, setDestAssetQP, setDestChainQP, destAssetQP, destChainQP, isReady]); | ||
|
||
const [amountInQP, setAmountInQP] = useQueryState("amount_in"); | ||
const [amountOutQP, setAmountOutQP] = useQueryState("amount_out"); | ||
|
||
useEffect(() => { | ||
if (amountInQP) { | ||
setState((prev) => ({ ...prev, amountIn: amountInQP })); | ||
setAmountOutQP(null); | ||
setAmountInQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
if (amountOutQP) { | ||
setState((prev) => ({ ...prev, amountOut: amountOutQP })); | ||
setAmountOutQP(null); | ||
setAmountInQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
} | ||
}, [amountInQP, amountOutQP, setAmountInQP, setAmountOutQP]); | ||
|
||
useEffect(() => { | ||
// this is a loading state when we are waiting for chains and assets to load when query params are present | ||
if ( | ||
(!chains || !isReady) && | ||
(srcChainQP || srcAssetQP || destChainQP || destAssetQP || amountInQP || amountOutQP) | ||
) { | ||
toast.loading("URL parameters are being processed...", { | ||
id: toastId, | ||
duration: Infinity, | ||
}); | ||
} | ||
}, [amountInQP, amountOutQP, chains, destAssetQP, destChainQP, isReady, srcAssetQP, srcChainQP]); | ||
return state; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SwapWidgetProviderProps } from "@skip-go/widget/build/provider"; | ||
|
||
import { appUrl } from "@/constants/api"; | ||
|
||
export const endpointOptions: SwapWidgetProviderProps["endpointOptions"] = { | ||
getRpcEndpointForChain: async (chainID) => { | ||
return `${appUrl}/api/rpc/${chainID}`; | ||
}, | ||
getRestEndpointForChain: async (chainID) => { | ||
return `${appUrl}/api/rest/${chainID}`; | ||
}, | ||
}; | ||
|
||
export const apiURL = `${appUrl}/api/skip`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import "@/styles/globals.css"; | ||
|
||
import { SwapWidgetProvider } from "@skip-go/widget"; | ||
import { Analytics } from "@vercel/analytics/react"; | ||
import { AppProps } from "next/app"; | ||
|
||
import { DefaultSeo } from "@/components/DefaultSeo"; | ||
import { Provider } from "@/widget"; | ||
import { apiURL, endpointOptions } from "@/lib/skip-go-widget"; | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
return ( | ||
<> | ||
<DefaultSeo /> | ||
<Analytics /> | ||
<Provider> | ||
<SwapWidgetProvider | ||
endpointOptions={endpointOptions} | ||
apiURL={apiURL} | ||
> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
</SwapWidgetProvider> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
import { useLayoutEffect } from "react"; | ||
import { SwapWidget } from "@skip-go/widget"; | ||
|
||
import { defaultValues, useSwapWidgetStore } from "@/components/SwapWidget/useSwapWidget"; | ||
import { Widget } from "@/widget"; | ||
import { useURLQueryParams } from "@/hooks/useURLQueryParams"; | ||
|
||
export default function WidgetPage() { | ||
useLayoutEffect(() => { | ||
useSwapWidgetStore.setState(defaultValues); | ||
}, []); | ||
const defaultRoute = useURLQueryParams(); | ||
return ( | ||
<div className="relative bg-white p-6 scrollbar-hide"> | ||
<Widget /> | ||
<SwapWidget | ||
className="" | ||
defaultRoute={{ | ||
srcChainID: defaultRoute?.srcChain || "cosmoshub-4", | ||
srcAssetDenom: defaultRoute?.srcAssetDenom, | ||
destChainID: defaultRoute?.destChain, | ||
destAssetDenom: defaultRoute?.destAssetDenom, | ||
amountIn: Number(defaultRoute?.amountIn), | ||
amountOut: Number(defaultRoute?.amountOut), | ||
}} | ||
settings={{ | ||
customGasAmount: 200_000, | ||
slippage: 3, | ||
}} | ||
onlyTestnet={process.env.NEXT_PUBLIC_IS_TESTNET ? true : false} | ||
/> | ||
</div> | ||
); | ||
} |