@@ -7,11 +7,17 @@ import type { TokenWithPrices } from "../../../../bridge/index.js";
77import type { Chain } from "../../../../chains/types.js" ;
88import type { ThirdwebClient } from "../../../../client/client.js" ;
99import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js" ;
10+ import { getToken } from "../../../../pay/convert/get-token.js" ;
1011import type { SupportedFiatCurrency } from "../../../../pay/convert/type.js" ;
1112import type { PurchaseData } from "../../../../pay/types.js" ;
1213import type { WaitForReceiptOptions } from "../../../../transaction/actions/wait-for-tx-receipt.js" ;
13- import type { PreparedTransaction } from "../../../../transaction/prepare-transaction.js" ;
14- import type { Address } from "../../../../utils/address.js" ;
14+ import {
15+ type PreparedTransaction ,
16+ prepareTransaction ,
17+ } from "../../../../transaction/prepare-transaction.js" ;
18+ import { type Address , checksumAddress } from "../../../../utils/address.js" ;
19+ import { stringify } from "../../../../utils/json.js" ;
20+ import { toUnits } from "../../../../utils/units.js" ;
1521import type { Wallet } from "../../../../wallets/interfaces/wallet.js" ;
1622import type { SmartWalletOptions } from "../../../../wallets/smart/types.js" ;
1723import type { AppMetadata } from "../../../../wallets/types.js" ;
@@ -38,7 +44,6 @@ import { Spinner } from "../components/Spinner.js";
3844import { Text } from "../components/text.js" ;
3945import { ExecutingTxScreen } from "../TransactionButton/ExecutingScreen.js" ;
4046import type { LocaleId } from "../types.js" ;
41- import { useTokenQuery } from "./common/token-query.js" ;
4247import { ErrorBanner } from "./ErrorBanner.js" ;
4348import { PaymentDetails } from "./payment-details/PaymentDetails.js" ;
4449import { PaymentSelection } from "./payment-selection/PaymentSelection.js" ;
@@ -334,6 +339,15 @@ export function TransactionWidget(props: TransactionWidgetProps) {
334339 ) ;
335340}
336341
342+ type TransactionQueryResult =
343+ | {
344+ transaction : PreparedTransaction ;
345+ type : "success" ;
346+ }
347+ | {
348+ type : "unsupported_token" ;
349+ } ;
350+
337351export function TransactionWidgetContentWrapper ( props : TransactionWidgetProps ) {
338352 useQuery ( {
339353 queryFn : ( ) => {
@@ -349,10 +363,49 @@ export function TransactionWidgetContentWrapper(props: TransactionWidgetProps) {
349363 } ) ;
350364
351365 const localQuery = useConnectLocale ( props . locale || "en_US" ) ;
352- const tokenQuery = useTokenQuery ( {
353- tokenAddress : props . tokenAddress ,
354- chainId : props . transaction . chain . id ,
355- client : props . client ,
366+
367+ const txQuery = useQuery ( {
368+ queryFn : async ( ) : Promise < TransactionQueryResult > => {
369+ let erc20Value = props . transaction . erc20Value ;
370+
371+ if ( props . amount ) {
372+ // Get token decimals for conversion
373+ const tokenAddress = props . tokenAddress || NATIVE_TOKEN_ADDRESS ;
374+ const token = await getToken (
375+ props . client ,
376+ checksumAddress ( tokenAddress ) ,
377+ props . transaction . chain . id ,
378+ ) . catch ( ( e ) => {
379+ if ( e instanceof Error && e . message . includes ( "not supported" ) ) {
380+ return null ;
381+ }
382+ throw e ;
383+ } ) ;
384+ if ( ! token ) {
385+ return {
386+ type : "unsupported_token" ,
387+ } ;
388+ }
389+
390+ erc20Value = {
391+ amountWei : toUnits ( props . amount , token . decimals ) ,
392+ tokenAddress : checksumAddress ( tokenAddress ) ,
393+ } ;
394+ }
395+
396+ const transaction = prepareTransaction ( {
397+ ...props . transaction ,
398+ erc20Value,
399+ } ) ;
400+
401+ return {
402+ transaction,
403+ type : "success" ,
404+ } ;
405+ } ,
406+
407+ queryKey : [ "transaction-query" , stringify ( props ) ] ,
408+ retry : 1 ,
356409 } ) ;
357410
358411 // if branding is disabled for widget, disable it for connect options too
@@ -369,7 +422,7 @@ export function TransactionWidgetContentWrapper(props: TransactionWidgetProps) {
369422 return props . connectOptions ;
370423 } , [ props . connectOptions , props . showThirdwebBranding ] ) ;
371424
372- if ( tokenQuery . isPending || ! localQuery . data ) {
425+ if ( txQuery . isPending || ! localQuery . data ) {
373426 return (
374427 < div
375428 style = { {
@@ -382,7 +435,7 @@ export function TransactionWidgetContentWrapper(props: TransactionWidgetProps) {
382435 < Spinner color = "secondaryText" size = "xl" />
383436 </ div >
384437 ) ;
385- } else if ( tokenQuery . error ) {
438+ } else if ( txQuery . error ) {
386439 return (
387440 < div
388441 style = { {
@@ -396,25 +449,25 @@ export function TransactionWidgetContentWrapper(props: TransactionWidgetProps) {
396449 < AccentFailIcon size = { iconSize [ "3xl" ] } />
397450 < Spacer y = "lg" />
398451 < Text color = "secondaryText" size = "md" >
399- { tokenQuery . error . message }
452+ { txQuery . error . message }
400453 </ Text >
401454 </ div >
402455 ) ;
403- } else if ( tokenQuery . data ?. type === "unsupported_token" ) {
456+ } else if ( txQuery . data ?. type === "unsupported_token" ) {
404457 return (
405458 < UnsupportedTokenScreen
406459 chain = { props . transaction . chain }
407460 client = { props . client }
408461 tokenAddress = { props . tokenAddress || NATIVE_TOKEN_ADDRESS }
409462 />
410463 ) ;
411- } else if ( tokenQuery . data ?. type === "success" ) {
464+ } else if ( txQuery . data ?. type === "success" ) {
412465 return (
413466 < TransactionWidgetContent
414467 { ...props }
415468 connectOptions = { connectOptions }
416469 connectLocale = { localQuery . data }
417- destinationToken = { tokenQuery . data . token }
470+ transaction = { txQuery . data . transaction }
418471 currency = { props . currency || "USD" }
419472 paymentMethods = { props . paymentMethods || [ "crypto" , "card" ] }
420473 showThirdwebBranding = {
@@ -486,7 +539,6 @@ function TransactionWidgetContent(
486539 "currency" | "showThirdwebBranding" | "paymentMethods"
487540 > & {
488541 connectLocale : ConnectLocale ;
489- destinationToken : TokenWithPrices ;
490542 } ,
491543) {
492544 const [ screen , setScreen ] = useState < TransactionWidgetScreen > ( {
0 commit comments