11import { Suspense , lazy } from "react" ;
22import { CodeLoading } from "../../../../components/code/code.client" ;
3- import type { PayEmbedPlaygroundOptions } from "./types" ;
3+ import type { BridgeComponentsPlaygroundOptions } from "./types" ;
4+ import { useQuery } from "@tanstack/react-query" ;
5+ import { getCurrencyMetadata } from "thirdweb/extensions/erc20" ;
6+ import { defineChain , getContract , toUnits } from "thirdweb" ;
7+ import { THIRDWEB_CLIENT } from "@/lib/client" ;
48
59const CodeClient = lazy (
610 ( ) => import ( "../../../../components/code/code.client" ) ,
711) ;
812
913export function CodeGen ( props : {
10- options : PayEmbedPlaygroundOptions ;
14+ options : BridgeComponentsPlaygroundOptions ;
1115} ) {
1216 return (
1317 < div className = "flex w-full grow flex-col" >
@@ -23,8 +27,7 @@ export function CodeGen(props: {
2327 ) ;
2428}
2529
26- function getCode ( options : PayEmbedPlaygroundOptions ) {
27- const walletCodes : string [ ] = [ ] ;
30+ function getCode ( options : BridgeComponentsPlaygroundOptions ) {
2831 const imports = {
2932 react : [ "PayEmbed" ] as string [ ] ,
3033 thirdweb : [ ] as string [ ] ,
@@ -45,134 +48,71 @@ function getCode(options: PayEmbedPlaygroundOptions) {
4548 imports . chains . push ( "base" ) ;
4649 }
4750
48- // Generate chain reference code
49- let chainCode : string ;
50- if ( isCustomChain && options . payOptions . buyTokenChain ?. id ) {
51- chainCode = `defineChain(${ options . payOptions . buyTokenChain . id } )` ;
52- } else {
53- chainCode = "base" ;
54- }
55-
56- for ( const wallet of options . connectOptions . walletIds ) {
57- walletCodes . push ( `createWallet("${ wallet } ")` ) ;
58- }
59-
60- if ( options . connectOptions . walletIds . length > 0 ) {
61- imports . wallets . push ( "createWallet" ) ;
62- }
63-
64- let themeProp : string | undefined ;
65- if (
66- options . theme . type === "dark" &&
67- Object . keys ( options . theme . darkColorOverrides || { } ) . length > 0
68- ) {
69- themeProp = `darkTheme({
70- colors: ${ JSON . stringify ( options . theme . darkColorOverrides ) } ,
71- })` ;
72- imports . react . push ( "darkTheme" ) ;
73- }
74-
75- if ( options . theme . type === "light" ) {
76- if ( Object . keys ( options . theme . lightColorOverrides || { } ) . length > 0 ) {
77- themeProp = `lightTheme({
78- colors: ${ JSON . stringify ( options . theme . lightColorOverrides ) } ,
79- })` ;
80- imports . react . push ( "lightTheme" ) ;
81- } else {
82- themeProp = quotes ( "light" ) ;
51+ const { data : amount } = useQuery ( {
52+ queryKey : [
53+ "amount" ,
54+ options . payOptions . buyTokenAmount ,
55+ options . payOptions . buyTokenChain ,
56+ options . payOptions . buyTokenAddress ,
57+ ] ,
58+ queryFn : async ( ) => {
59+ if ( ! options . payOptions . buyTokenAmount ) {
60+ return ;
61+ }
62+ const contract = getContract ( {
63+ chain : defineChain ( options . payOptions . buyTokenChain . id ) ,
64+ address : options . payOptions . buyTokenAddress ,
65+ client : THIRDWEB_CLIENT ,
66+ } ) ;
67+ const token = await getCurrencyMetadata ( {
68+ contract,
69+ } ) ;
70+
71+ return toUnits ( options . payOptions . buyTokenAmount , token . decimals ) ;
72+ } ,
73+ } ) ;
74+
75+ imports . wallets . push ( "createWallet" ) ;
76+
77+ const componentName = ( ( ) => {
78+ switch ( options . payOptions . widget ) {
79+ case "buy" :
80+ return "BuyWidget" ;
81+ case "checkout" :
82+ return "CheckoutWidget" ;
83+ case "transaction" :
84+ return "TransactionWidget" ;
85+ default :
86+ return "PayEmbed" ;
8387 }
84- }
85-
86- if ( options . connectOptions . enableAccountAbstraction ) {
87- imports . chains . push ( "sepolia" ) ;
88- }
89-
90- // Generate payOptions based on the mode
91- let payOptionsCode = "{" ;
92-
93- if ( options . payOptions . title || options . payOptions . image ) {
94- payOptionsCode += `
95- metadata: {
96- ${ options . payOptions . title ? `name: ${ quotes ( options . payOptions . title ) } ,` : "" }
97- ${ options . payOptions . image ? `image: ${ quotes ( options . payOptions . image ) } ,` : "" }
98- },` ;
99- }
100-
101- // Add mode-specific options
102- if ( options . payOptions . mode ) {
103- payOptionsCode += `
104- mode: "${ options . payOptions . mode } ",` ;
105-
106- // Add buyWithCrypto and buyWithFiat if they're set to false
107- if ( options . payOptions . buyWithCrypto === false ) {
108- payOptionsCode += `
109- buyWithCrypto: false,` ;
110- }
111-
112- if ( options . payOptions . buyWithFiat === false ) {
113- payOptionsCode += `
114- buyWithFiat: false,` ;
115- }
116-
117- if ( options . payOptions . mode === "fund_wallet" || ! options . payOptions . mode ) {
118- payOptionsCode += `
119- prefillBuy: {
120- chain: ${ chainCode } ,
121- amount: ${ options . payOptions . buyTokenAmount ? quotes ( options . payOptions . buyTokenAmount ) : '"0.01"' } ,
122- ${ options . payOptions . buyTokenInfo ? `token: ${ JSON . stringify ( options . payOptions . buyTokenInfo ) } ,` : "" }
123- },` ;
124- } else if ( options . payOptions . mode === "direct_payment" ) {
125- payOptionsCode += `
126- paymentInfo: {
127- chain: ${ chainCode } ,
128- sellerAddress: ${ options . payOptions . sellerAddress ? quotes ( options . payOptions . sellerAddress ) : '"0x0000000000000000000000000000000000000000"' } ,
129- amount: ${ options . payOptions . buyTokenAmount ? quotes ( options . payOptions . buyTokenAmount ) : '"0.01"' } ,
130- ${ options . payOptions . buyTokenInfo ? `token: ${ JSON . stringify ( options . payOptions . buyTokenInfo ) } ,` : "" }
131- },` ;
132- } else if ( options . payOptions . mode === "transaction" ) {
133- payOptionsCode += `
134- transaction: claimTo({
135- contract: myNftContract,
136- quantity: 1n,
137- tokenId: 0n,
138- to: "0x...",
139- }),` ;
140- }
141- }
142-
143- payOptionsCode += `
144- }` ;
145-
146- const accountAbstractionCode = options . connectOptions . enableAccountAbstraction
147- ? `\n accountAbstraction: {
148- chain: ${ isCustomChain ? `defineChain(${ options . payOptions . buyTokenChain ?. id } )` : "base" } ,
149- sponsorGas: true,
150- }`
151- : "" ;
152-
153- const connectOptionsCode = `${ accountAbstractionCode ? `{${ accountAbstractionCode } \n }` : "" } ` ;
88+ } ) ( ) ;
89+ imports . react . push ( componentName ) ;
90+ imports . chains . push ( "defineChain" ) ;
15491
15592 return `\
15693import { createThirdwebClient } from "thirdweb";
15794${ imports . react . length > 0 ? `import { ${ imports . react . join ( ", " ) } } from "thirdweb/react";` : "" }
15895${ imports . thirdweb . length > 0 ? `import { ${ imports . thirdweb . join ( ", " ) } } from "thirdweb";` : "" }
159- ${ imports . wallets . length > 0 ? `import { ${ imports . wallets . join ( ", " ) } } from "thirdweb/wallets";` : "" }
160- ${ imports . chains . length > 0 ? `import { ${ imports . chains . join ( ", " ) } } from "thirdweb/chains";` : "" }
16196
16297const client = createThirdwebClient({
16398 clientId: "....",
16499});
165100
166101function Example() {
167102 return (
168- <PayEmbed
103+ <${ componentName }
169104 client={client}
170- payOptions={${ payOptionsCode } }${ connectOptionsCode ? `\n connectOptions={${ connectOptionsCode } }` : "" } ${ themeProp ? `\n theme={${ themeProp } }` : "" }
105+ chain={defineChain(${ options . payOptions . buyTokenChain . id } )}
106+ amount={${ amount } n}${ options . payOptions . buyTokenAddress ? `\n\t token="${ options . payOptions . buyTokenAddress } "` : "" } ${ options . payOptions . sellerAddress ? `\n\t seller="${ options . payOptions . sellerAddress } "` : "" } ${ options . payOptions . title ? `\n\t ${ options . payOptions . widget === "checkout" ? "name" : "title" } ="${ options . payOptions . title } "` : "" } ${ options . payOptions . image ? `\n\t image="${ options . payOptions . image } "` : "" } ${ options . payOptions . description ? `\n\t description="${ options . payOptions . description } "` : "" } ${ options . payOptions . widget === "transaction"
107+ ? `\n\t transaction={claimTo({
108+ contract: nftContract,
109+ quantity: 1n,
110+ tokenId: 2n,
111+ to: account?.address || "",
112+ })}`
113+ : ""
114+ }
171115 />
172116 );
173117}` ;
174118}
175-
176- function quotes ( value : string ) {
177- return `"${ value } "` ;
178- }
0 commit comments