-
Notifications
You must be signed in to change notification settings - Fork 637
[MNY-344] Dashboard: Add swap-widget iframe #8603
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
Merged
+262
−43
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
apps/dashboard/src/app/bridge/swap-widget/SwapWidgetEmbed.client.tsx
This file contains hidden or 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,104 @@ | ||
| "use client"; | ||
|
|
||
| import { useMemo } from "react"; | ||
| import type { Address } from "thirdweb"; | ||
| import { type SupportedFiatCurrency, SwapWidget } from "thirdweb/react"; | ||
| import { NEXT_PUBLIC_SWAP_IFRAME_CLIENT_ID } from "@/constants/public-envs"; | ||
| import { getConfiguredThirdwebClient } from "@/constants/thirdweb.server"; | ||
|
|
||
| export function SwapWidgetEmbed({ | ||
| buyChainId, | ||
| buyTokenAddress, | ||
| buyAmount, | ||
| sellChainId, | ||
| sellTokenAddress, | ||
| sellAmount, | ||
| showThirdwebBranding, | ||
| theme, | ||
| currency, | ||
| }: { | ||
| buyChainId?: number; | ||
| buyTokenAddress?: Address; | ||
| buyAmount?: string; | ||
| sellChainId?: number; | ||
| sellTokenAddress?: Address; | ||
| sellAmount?: string; | ||
| showThirdwebBranding?: boolean; | ||
| theme: "light" | "dark"; | ||
| currency?: SupportedFiatCurrency; | ||
| }) { | ||
| const client = useMemo( | ||
| () => | ||
| getConfiguredThirdwebClient({ | ||
| clientId: NEXT_PUBLIC_SWAP_IFRAME_CLIENT_ID, | ||
| secretKey: undefined, | ||
| teamId: undefined, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| const prefill = useMemo(() => { | ||
| const result: { | ||
| buyToken?: { chainId: number; tokenAddress?: string; amount?: string }; | ||
| sellToken?: { chainId: number; tokenAddress?: string; amount?: string }; | ||
| } = {}; | ||
|
|
||
| if (buyChainId) { | ||
| result.buyToken = { | ||
| chainId: buyChainId, | ||
| tokenAddress: buyTokenAddress, | ||
| amount: buyAmount, | ||
| }; | ||
| } | ||
|
|
||
| if (sellChainId) { | ||
| result.sellToken = { | ||
| chainId: sellChainId, | ||
| tokenAddress: sellTokenAddress, | ||
| amount: sellAmount, | ||
| }; | ||
| } | ||
|
|
||
| return Object.keys(result).length > 0 ? result : undefined; | ||
| }, [ | ||
| buyChainId, | ||
| buyTokenAddress, | ||
| buyAmount, | ||
| sellChainId, | ||
| sellTokenAddress, | ||
| sellAmount, | ||
| ]); | ||
|
|
||
| return ( | ||
| <SwapWidget | ||
| className="shadow-xl" | ||
| client={client} | ||
| prefill={prefill} | ||
| showThirdwebBranding={showThirdwebBranding} | ||
| theme={theme} | ||
| currency={currency} | ||
| onSuccess={() => { | ||
| sendMessageToParent({ | ||
| source: "swap-widget", | ||
| type: "success", | ||
| }); | ||
| }} | ||
| onError={(error) => { | ||
| sendMessageToParent({ | ||
| source: "swap-widget", | ||
| type: "error", | ||
| message: error.message, | ||
| }); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function sendMessageToParent(content: object) { | ||
| try { | ||
| window.parent.postMessage(content, "*"); | ||
| } catch (error) { | ||
| console.error("Failed to send post message to parent window"); | ||
| console.error(error); | ||
| } | ||
| } | ||
MananTank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,27 @@ | ||
| import { Inter } from "next/font/google"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| const fontSans = Inter({ | ||
| display: "swap", | ||
| subsets: ["latin"], | ||
| variable: "--font-sans", | ||
| }); | ||
|
|
||
| export default function SwapWidgetLayout({ | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| }) { | ||
| return ( | ||
| <html lang="en" suppressHydrationWarning> | ||
| <body | ||
| className={cn( | ||
| "min-h-dvh bg-background font-sans antialiased flex flex-col", | ||
| fontSans.variable, | ||
| )} | ||
| > | ||
| {children} | ||
| </body> | ||
| </html> | ||
| ); | ||
| } | ||
MananTank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,83 @@ | ||
| import type { Metadata } from "next"; | ||
| import "@workspace/ui/global.css"; | ||
| import type { SupportedFiatCurrency } from "thirdweb/react"; | ||
| import { isValidCurrency } from "../_common/isValidCurrency"; | ||
| import { | ||
| onlyAddress, | ||
| onlyNumber, | ||
| parseQueryParams, | ||
| } from "../_common/parseQueryParams"; | ||
| import { BridgeProvidersLite } from "../(general)/components/client/Providers.client"; | ||
| import { SwapWidgetEmbed } from "./SwapWidgetEmbed.client"; | ||
|
|
||
| const title = "thirdweb Swap: Cross-Chain Token Swaps"; | ||
| const description = | ||
| "Swap tokens across any chain with the best rates. Cross-chain swaps made simple with thirdweb."; | ||
|
|
||
| export const metadata: Metadata = { | ||
| description, | ||
| openGraph: { | ||
| description, | ||
| title, | ||
| }, | ||
| title, | ||
| }; | ||
|
|
||
| type SearchParams = { | ||
| [key: string]: string | string[] | undefined; | ||
| }; | ||
|
|
||
| export default async function Page(props: { | ||
| searchParams: Promise<SearchParams>; | ||
| }) { | ||
| const searchParams = await props.searchParams; | ||
|
|
||
| // Buy token params | ||
| const buyChainId = parseQueryParams(searchParams.buyChain, onlyNumber); | ||
| const buyTokenAddress = parseQueryParams( | ||
| searchParams.buyTokenAddress, | ||
| onlyAddress, | ||
| ); | ||
| const buyAmount = parseQueryParams(searchParams.buyAmount, (v) => v); | ||
|
|
||
| // Sell token params | ||
| const sellChainId = parseQueryParams(searchParams.sellChain, onlyNumber); | ||
| const sellTokenAddress = parseQueryParams( | ||
| searchParams.sellTokenAddress, | ||
| onlyAddress, | ||
| ); | ||
| const sellAmount = parseQueryParams(searchParams.sellAmount, (v) => v); | ||
|
|
||
| // Optional params | ||
| const showThirdwebBranding = parseQueryParams( | ||
| searchParams.showThirdwebBranding, | ||
| (v) => v !== "false", | ||
| ); | ||
|
|
||
| const theme = | ||
| parseQueryParams(searchParams.theme, (v) => | ||
| v === "light" ? "light" : "dark", | ||
| ) || "dark"; | ||
|
|
||
| const currency = parseQueryParams(searchParams.currency, (v) => | ||
| isValidCurrency(v) ? (v as SupportedFiatCurrency) : undefined, | ||
| ); | ||
|
|
||
| return ( | ||
| <BridgeProvidersLite forcedTheme={theme}> | ||
| <div className="flex min-h-screen items-center justify-center bg-background px-4 py-8"> | ||
| <SwapWidgetEmbed | ||
| buyChainId={buyChainId} | ||
| buyTokenAddress={buyTokenAddress} | ||
| buyAmount={buyAmount} | ||
| sellChainId={sellChainId} | ||
| sellTokenAddress={sellTokenAddress} | ||
| sellAmount={sellAmount} | ||
| showThirdwebBranding={showThirdwebBranding} | ||
| theme={theme} | ||
| currency={currency} | ||
| /> | ||
| </div> | ||
| </BridgeProvidersLite> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Missing validation for required environment variable.
NEXT_PUBLIC_SWAP_IFRAME_CLIENT_IDis used without validation at line 33. If this environment variable is undefined, the swap widget will fail to initialize properly, likely resulting in authentication or connection errors that are difficult to debug.🔎 Recommended validation pattern
Add validation at component initialization:
export function SwapWidgetEmbed({ buyChainId, buyTokenAddress, buyAmount, sellChainId, sellTokenAddress, sellAmount, showThirdwebBranding, theme, currency, }: { buyChainId?: number; buyTokenAddress?: Address; buyAmount?: string; sellChainId?: number; sellTokenAddress?: Address; sellAmount?: string; showThirdwebBranding?: boolean; theme: "light" | "dark"; currency?: SupportedFiatCurrency; }) { + if (!NEXT_PUBLIC_SWAP_IFRAME_CLIENT_ID) { + throw new Error( + "NEXT_PUBLIC_SWAP_IFRAME_CLIENT_ID environment variable is required" + ); + } + const client = useMemo( () => getConfiguredThirdwebClient({ clientId: NEXT_PUBLIC_SWAP_IFRAME_CLIENT_ID, secretKey: undefined, teamId: undefined, }), [], );Alternatively, render an error UI similar to the invalid configuration handling in
checkout-widget/page.tsx(lines 80-115).🤖 Prompt for AI Agents