|
| 1 | +import "../../global.css"; |
| 2 | +import { getThirdwebClient } from "@/constants/thirdweb.server"; |
| 3 | +import type { Metadata } from "next"; |
| 4 | +import { createThirdwebClient, defineChain, getContract } from "thirdweb"; |
| 5 | +import { name, symbol } from "thirdweb/extensions/common"; |
| 6 | +import { decimals } from "thirdweb/extensions/erc20"; |
| 7 | +import { checksumAddress } from "thirdweb/utils"; |
| 8 | +import { CheckoutEmbed } from "./components/client/CheckoutEmbed.client"; |
| 9 | + |
| 10 | +const title = "Universal Bridge: Swap, Bridge, and On-Ramp"; |
| 11 | +const description = |
| 12 | + "Swap, bridge, and on-ramp to any EVM chain with thirdweb's Universal Bridge."; |
| 13 | + |
| 14 | +export const metadata: Metadata = { |
| 15 | + title, |
| 16 | + description, |
| 17 | + openGraph: { |
| 18 | + title, |
| 19 | + description, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +export default async function RoutesPage({ |
| 24 | + searchParams, |
| 25 | +}: { searchParams: Record<string, string | string[]> }) { |
| 26 | + const { |
| 27 | + chainId, |
| 28 | + recipientAddress, |
| 29 | + tokenAddress, |
| 30 | + amount, |
| 31 | + clientId, |
| 32 | + redirectUri, |
| 33 | + } = searchParams; |
| 34 | + |
| 35 | + if (!chainId || Array.isArray(chainId)) { |
| 36 | + throw new Error("A single chainId parameter is required."); |
| 37 | + } |
| 38 | + if (!recipientAddress || Array.isArray(recipientAddress)) { |
| 39 | + throw new Error("A single recipientAddress parameter is required."); |
| 40 | + } |
| 41 | + if (!tokenAddress || Array.isArray(tokenAddress)) { |
| 42 | + throw new Error("A single tokenAddress parameter is required."); |
| 43 | + } |
| 44 | + if (!amount || Array.isArray(amount)) { |
| 45 | + throw new Error("An single amount parameter is required."); |
| 46 | + } |
| 47 | + if (Array.isArray(clientId)) { |
| 48 | + throw new Error("A single clientId parameter is required."); |
| 49 | + } |
| 50 | + if (Array.isArray(redirectUri)) { |
| 51 | + throw new Error("A single redirectUri parameter is required."); |
| 52 | + } |
| 53 | + |
| 54 | + // Use any provided clientId or use the dashboard client |
| 55 | + const client = |
| 56 | + clientId && !Array.isArray(clientId) |
| 57 | + ? createThirdwebClient({ clientId }) |
| 58 | + : getThirdwebClient(undefined); |
| 59 | + |
| 60 | + const token = await (async () => { |
| 61 | + if ( |
| 62 | + checksumAddress(tokenAddress) === |
| 63 | + "0x0000000000000000000000000000000000000000" || |
| 64 | + checksumAddress(tokenAddress) === |
| 65 | + "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" |
| 66 | + ) { |
| 67 | + return { |
| 68 | + name: "Ether", |
| 69 | + symbol: "ETH", |
| 70 | + address: tokenAddress, |
| 71 | + decimals: 18, |
| 72 | + }; |
| 73 | + } else { |
| 74 | + const tokenContract = getContract({ |
| 75 | + client, |
| 76 | + // eslint-disable-next-line no-restricted-syntax |
| 77 | + chain: defineChain(Number(chainId)), |
| 78 | + address: tokenAddress, |
| 79 | + }); |
| 80 | + const symbolPromise = symbol({ contract: tokenContract }); |
| 81 | + const namePromise = name({ contract: tokenContract }); |
| 82 | + const decimalsPromise = decimals({ contract: tokenContract }); |
| 83 | + |
| 84 | + const [symbolResult, nameResult, decimalsResult] = await Promise.all([ |
| 85 | + symbolPromise, |
| 86 | + namePromise, |
| 87 | + decimalsPromise, |
| 88 | + ]); |
| 89 | + return { |
| 90 | + name: nameResult, |
| 91 | + symbol: symbolResult, |
| 92 | + address: tokenAddress, |
| 93 | + decimals: Number(decimalsResult), |
| 94 | + }; |
| 95 | + } |
| 96 | + })(); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className="relative mx-auto flex h-screen w-screen flex-col items-center justify-center overflow-hidden border py-10"> |
| 100 | + <main className="container z-10 flex justify-center"> |
| 101 | + <CheckoutEmbed |
| 102 | + redirectUri={redirectUri} |
| 103 | + chainId={Number(chainId)} |
| 104 | + recipientAddress={recipientAddress} |
| 105 | + amount={BigInt(amount)} |
| 106 | + token={token} |
| 107 | + clientId={client.clientId} |
| 108 | + /> |
| 109 | + </main> |
| 110 | + |
| 111 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 112 | + <img |
| 113 | + alt="" |
| 114 | + src="/assets/login/background.svg" |
| 115 | + className="-bottom-12 -right-12 pointer-events-none absolute lg:right-0 lg:bottom-0" |
| 116 | + /> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +} |
0 commit comments