@@ -9,10 +9,7 @@ import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
99import { getAddress } from "../../../../../utils/address.js" ;
1010import { CustomThemeProvider } from "../../../../core/design-system/CustomThemeProvider.js" ;
1111import type { Theme } from "../../../../core/design-system/index.js" ;
12- import type {
13- BridgePrepareRequest ,
14- BridgePrepareResult ,
15- } from "../../../../core/hooks/useBridgePrepare.js" ;
12+ import type { BridgePrepareRequest } from "../../../../core/hooks/useBridgePrepare.js" ;
1613import type { CompletedStatusResult } from "../../../../core/hooks/useStepExecutor.js" ;
1714import { webWindowAdapter } from "../../../adapters/WindowAdapter.js" ;
1815import { EmbedContainer } from "../../ConnectWallet/Modal/ConnectEmbed.js" ;
@@ -24,7 +21,11 @@ import { StepRunner } from "../StepRunner.js";
2421import { useActiveWalletInfo } from "./hooks.js" ;
2522import { getLastUsedTokens , setLastUsedTokens } from "./storage.js" ;
2623import { SwapUI } from "./swap-ui.js" ;
27- import type { SwapWidgetConnectOptions , TokenSelection } from "./types.js" ;
24+ import type {
25+ SwapPreparedQuote ,
26+ SwapWidgetConnectOptions ,
27+ TokenSelection ,
28+ } from "./types.js" ;
2829import { useBridgeChains } from "./use-bridge-chains.js" ;
2930
3031export type SwapWidgetProps = {
@@ -44,54 +45,6 @@ export type SwapWidgetProps = {
4445 * ```
4546 */
4647 client : ThirdwebClient ;
47- /**
48- * Set the theme for the `SwapWidget` component. By default it is set to `"dark"`
49- *
50- * theme can be set to either `"dark"`, `"light"` or a custom theme object.
51- * You can also import [`lightTheme`](https://portal.thirdweb.com/references/typescript/v5/lightTheme)
52- * or [`darkTheme`](https://portal.thirdweb.com/references/typescript/v5/darkTheme)
53- * functions from `thirdweb/react` to use the default themes as base and overrides parts of it.
54- * @example
55- * ```ts
56- * import { lightTheme } from "thirdweb/react";
57- *
58- * const customTheme = lightTheme({
59- * colors: {
60- * modalBg: 'red'
61- * }
62- * })
63- *
64- * function Example() {
65- * return <SwapWidget client={client} theme={customTheme} />
66- * }
67- * ```
68- */
69- theme ?: "light" | "dark" | Theme ;
70- className ?: string ;
71- /**
72- * The currency to use for the payment.
73- * @default "USD"
74- */
75- currency ?: SupportedFiatCurrency ;
76- style ?: React . CSSProperties ;
77- /**
78- * Whether to show thirdweb branding in the widget.
79- * @default true
80- */
81- showThirdwebBranding ?: boolean ;
82- /**
83- * Callback to be called when the swap is successful.
84- */
85- onSuccess ?: ( ) => void ;
86- /**
87- * Callback to be called when user encounters an error when swapping.
88- */
89- onError ?: ( error : Error ) => void ;
90- /**
91- * Callback to be called when the user cancels the purchase.
92- */
93- onCancel ?: ( ) => void ;
94- connectOptions ?: SwapWidgetConnectOptions ;
9548 /**
9649 * The prefill Buy and/or Sell tokens for the swap widget. If `tokenAddress` is not provided, the native token will be used
9750 *
@@ -153,6 +106,54 @@ export type SwapWidgetProps = {
153106 amount ?: string ;
154107 } ;
155108 } ;
109+ /**
110+ * Set the theme for the `SwapWidget` component. By default it is set to `"dark"`
111+ *
112+ * theme can be set to either `"dark"`, `"light"` or a custom theme object.
113+ * You can also import [`lightTheme`](https://portal.thirdweb.com/references/typescript/v5/lightTheme)
114+ * or [`darkTheme`](https://portal.thirdweb.com/references/typescript/v5/darkTheme)
115+ * functions from `thirdweb/react` to use the default themes as base and overrides parts of it.
116+ * @example
117+ * ```ts
118+ * import { lightTheme } from "thirdweb/react";
119+ *
120+ * const customTheme = lightTheme({
121+ * colors: {
122+ * modalBg: 'red'
123+ * }
124+ * })
125+ *
126+ * function Example() {
127+ * return <SwapWidget client={client} theme={customTheme} />
128+ * }
129+ * ```
130+ */
131+ theme ?: "light" | "dark" | Theme ;
132+ /**
133+ * The currency to use for the payment.
134+ * @default "USD"
135+ */
136+ currency ?: SupportedFiatCurrency ;
137+ connectOptions ?: SwapWidgetConnectOptions ;
138+ /**
139+ * Whether to show thirdweb branding in the widget.
140+ * @default true
141+ */
142+ showThirdwebBranding ?: boolean ;
143+ /**
144+ * Callback to be called when the swap is successful.
145+ */
146+ onSuccess ?: ( quote : SwapPreparedQuote ) => void ;
147+ /**
148+ * Callback to be called when user encounters an error when swapping.
149+ */
150+ onError ?: ( error : Error , quote : SwapPreparedQuote ) => void ;
151+ /**
152+ * Callback to be called when the user cancels the purchase.
153+ */
154+ onCancel ?: ( quote : SwapPreparedQuote ) => void ;
155+ style ?: React . CSSProperties ;
156+ className ?: string ;
156157} ;
157158
158159/**
@@ -274,7 +275,7 @@ type SwapWidgetScreen =
274275 }
275276 | {
276277 id : "2:preview" ;
277- preparedQuote : Extract < BridgePrepareResult , { type : "buy" | "sell" } > ;
278+ preparedQuote : SwapPreparedQuote ;
278279 request : BridgePrepareRequest ;
279280 quote : Buy . quote . Result | Sell . quote . Result ;
280281 buyToken : TokenWithPrices ;
@@ -286,7 +287,7 @@ type SwapWidgetScreen =
286287 id : "3:execute" ;
287288 request : BridgePrepareRequest ;
288289 quote : Buy . quote . Result | Sell . quote . Result ;
289- preparedQuote : Extract < BridgePrepareResult , { type : "buy" | "sell" } > ;
290+ preparedQuote : SwapPreparedQuote ;
290291 buyToken : TokenWithPrices ;
291292 sellToken : TokenWithPrices ;
292293 sellTokenBalance : bigint ;
@@ -295,12 +296,13 @@ type SwapWidgetScreen =
295296 | {
296297 id : "4:success" ;
297298 completedStatuses : CompletedStatusResult [ ] ;
298- preparedQuote : Extract < BridgePrepareResult , { type : "buy" | "sell" } > ;
299+ preparedQuote : SwapPreparedQuote ;
299300 buyToken : TokenWithPrices ;
300301 sellToken : TokenWithPrices ;
301302 }
302303 | {
303304 id : "error" ;
305+ preparedQuote : SwapPreparedQuote ;
304306 error : Error ;
305307 } ;
306308
@@ -377,11 +379,12 @@ function SwapWidgetContent(props: SwapWidgetProps) {
377379 useBridgeChains ( props . client ) ;
378380
379381 const handleError = useCallback (
380- ( error : Error ) => {
382+ ( error : Error , quote : SwapPreparedQuote ) => {
381383 console . error ( error ) ;
382- props . onError ?.( error ) ;
384+ props . onError ?.( error , quote ) ;
383385 setScreen ( {
384386 id : "error" ,
387+ preparedQuote : quote ,
385388 error,
386389 } ) ;
387390 } ,
@@ -439,7 +442,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
439442 id : "3:execute" ,
440443 } ) ;
441444 } }
442- onError = { handleError }
445+ onError = { ( error ) => handleError ( error , screen . preparedQuote ) }
443446 paymentMethod = { {
444447 quote : screen . quote ,
445448 type : "wallet" ,
@@ -471,9 +474,9 @@ function SwapWidgetContent(props: SwapWidgetProps) {
471474 sellTokenBalance : screen . sellTokenBalance ,
472475 } ) ;
473476 } }
474- onCancel = { props . onCancel }
477+ onCancel = { ( ) => props . onCancel ?. ( screen . preparedQuote ) }
475478 onComplete = { ( completedStatuses ) => {
476- props . onSuccess ?.( ) ;
479+ props . onSuccess ?.( screen . preparedQuote ) ;
477480 setScreen ( {
478481 ...screen ,
479482 id : "4:success" ,
@@ -519,7 +522,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
519522 error = { screen . error }
520523 onCancel = { ( ) => {
521524 setScreen ( { id : "1:swap-ui" } ) ;
522- props . onCancel ?.( ) ;
525+ props . onCancel ?.( screen . preparedQuote ) ;
523526 } }
524527 onRetry = { ( ) => {
525528 setScreen ( { id : "1:swap-ui" } ) ;
0 commit comments