-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathBuyWidget.tsx
More file actions
941 lines (878 loc) · 27.6 KB
/
BuyWidget.tsx
File metadata and controls
941 lines (878 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { trackPayEvent } from "../../../../analytics/track/pay.js";
import type { TokenWithPrices } from "../../../../bridge/index.js";
import type { Chain } from "../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../client/client.js";
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
import type { SupportedFiatCurrency } from "../../../../pay/convert/type.js";
import type { PurchaseData } from "../../../../pay/types.js";
import type { Address } from "../../../../utils/address.js";
import type { Wallet } from "../../../../wallets/interfaces/wallet.js";
import type { SmartWalletOptions } from "../../../../wallets/smart/types.js";
import type { AppMetadata } from "../../../../wallets/types.js";
import type { WalletId } from "../../../../wallets/wallet-types.js";
import { CustomThemeProvider } from "../../../core/design-system/CustomThemeProvider.js";
import type { Theme } from "../../../core/design-system/index.js";
import type { SiweAuthOptions } from "../../../core/hooks/auth/useSiweAuth.js";
import type { ConnectButton_connectModalOptions } from "../../../core/hooks/connection/ConnectButtonProps.js";
import type {
BridgePrepareRequest,
BridgePrepareResult,
} from "../../../core/hooks/useBridgePrepare.js";
import type { CompletedStatusResult } from "../../../core/hooks/useStepExecutor.js";
import type { SupportedTokens } from "../../../core/utils/defaultTokens.js";
import { webWindowAdapter } from "../../adapters/WindowAdapter.js";
import { AutoConnect } from "../AutoConnect/AutoConnect.js";
import connectLocaleEn from "../ConnectWallet/locale/en.js";
import { EmbedContainer } from "../ConnectWallet/Modal/ConnectEmbed.js";
import { DynamicHeight } from "../components/DynamicHeight.js";
import { ErrorBanner } from "./ErrorBanner.js";
import {
type AmountSelection,
FundWallet,
type SelectedToken,
} from "./FundWallet.js";
import { PaymentDetails } from "./payment-details/PaymentDetails.js";
import { PaymentSelection } from "./payment-selection/PaymentSelection.js";
import { SuccessScreen } from "./payment-success/SuccessScreen.js";
import { QuoteLoader } from "./QuoteLoader.js";
import { StepRunner } from "./StepRunner.js";
import type { PaymentMethod, RequiredParams } from "./types.js";
export type BuyOrOnrampPrepareResult = Extract<
BridgePrepareResult,
{ type: "buy" | "onramp" }
>;
export type BuyWidgetProps = {
/**
* Customize the supported tokens that users can pay with.
*/
supportedTokens?: SupportedTokens;
/**
* A client is the entry point to the thirdweb SDK.
* It is required for all other actions.
* You can create a client using the `createThirdwebClient` function. Refer to the [Creating a Client](https://portal.thirdweb.com/typescript/v5/client) documentation for more information.
*
* You must provide a `clientId` or `secretKey` in order to initialize a client. Pass `clientId` if you want for client-side usage and `secretKey` for server-side usage.
*
* ```tsx
* import { createThirdwebClient } from "thirdweb";
*
* const client = createThirdwebClient({
* clientId: "<your_client_id>",
* })
* ```
*/
client: ThirdwebClient;
/**
* Set the theme for the `BuyWidget` component. By default it is set to `"dark"`
*
* theme can be set to either `"dark"`, `"light"` or a custom theme object.
* You can also import [`lightTheme`](https://portal.thirdweb.com/references/typescript/v5/lightTheme)
* or [`darkTheme`](https://portal.thirdweb.com/references/typescript/v5/darkTheme)
* functions from `thirdweb/react` to use the default themes as base and overrides parts of it.
* @example
* ```ts
* import { lightTheme } from "thirdweb/react";
*
* const customTheme = lightTheme({
* colors: {
* modalBg: 'red'
* }
* })
*
* function Example() {
* return <BuyWidget client={client} theme={customTheme} />
* }
* ```
*/
theme?: "light" | "dark" | Theme;
/**
* Customize the options for "Connect" Button showing in the BuyWidget UI when the user is not connected to a wallet.
*
* Refer to the [`BuyWidgetConnectOptions`](https://portal.thirdweb.com/references/typescript/v5/BuyWidgetConnectOptions) type for more details.
*/
connectOptions?: BuyWidgetConnectOptions;
/**
* All wallet IDs included in this array will be hidden from wallet selection when connected.
*/
hiddenWallets?: WalletId[];
/**
* The wallet that should be pre-selected in the BuyWidget UI.
*/
activeWallet?: Wallet;
style?: React.CSSProperties;
className?: string;
/**
* Whether to show thirdweb branding in the widget.
* @default true
*/
showThirdwebBranding?: boolean;
/**
* The chain the accepted token is on.
*/
chain?: Chain;
/**
* Address of the token to buy. Leave undefined for the native token, or use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE.
*/
tokenAddress?: Address;
/**
* The amount to buy **(as a decimal string)**, e.g. "1.5" for 1.5 tokens.
*/
amount?: string;
/**
* The title to display in the widget. If `title` is explicity set to an empty string, the title will not be displayed.
*/
title?: string;
/**
* The description to display in the widget.
*/
description?: string;
/**
* The image to display in the widget.
*/
image?: string;
/**
* Preset fiat amounts to display in the UI. Defaults to [5, 10, 20].
*/
presetOptions?: [number, number, number];
/**
* Arbitrary data to be included in the returned status and webhook events.
*/
purchaseData?: PurchaseData;
/**
* Callback triggered when the purchase is successful.
*/
onSuccess?: (data: {
quote: BuyOrOnrampPrepareResult;
statuses: CompletedStatusResult[];
}) => void;
/**
* Callback triggered when the purchase encounters an error.
*/
onError?: (error: Error, quote: BuyOrOnrampPrepareResult | undefined) => void;
/**
* Callback triggered when the user cancels the purchase.
*/
onCancel?: (quote: BuyOrOnrampPrepareResult | undefined) => void;
/**
* @hidden
*/
paymentLinkId?: string;
/**
* Allowed payment methods
* @default ["crypto", "card"]
*/
paymentMethods?: ("crypto" | "card")[];
/**
* The currency to use for the payment.
* @default "USD"
*/
currency?: SupportedFiatCurrency;
/**
* The user's ISO 3166 alpha-2 country code. This is used to determine onramp provider support.
*/
country?: string;
/**
* Custom label for the main action button.
*/
buttonLabel?: string;
/**
* The receiver address for the purchased funds.
*/
receiverAddress?: Address;
/**
* Callback to be called when the user disconnects the active wallet.
*/
onDisconnect?: () => void;
/**
* By default the token amount is editable. Set this to false to disable editing the token amount
*/
amountEditable?: boolean;
/**
* By default the token selection is editable. Set this to false to disable editing the token selection.
*/
tokenEditable?: boolean;
/**
* Array of onramp provider IDs to hide from the payment provider selection.
* @example
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1"
* hiddenOnrampProviders={["transak"]}
* />
* ```
*/
hiddenOnrampProviders?: ("coinbase" | "stripe" | "transak")[];
};
/**
* Widget is a prebuilt UI for purchasing a specific token.
*
* @param props - Props of type [`BuyWidgetProps`](https://portal.thirdweb.com/references/typescript/v5/BuyWidgetProps) to configure the BuyWidget component.
*
* @example
* ### Basic usage
*
* The `BuyWidget` component requires `client`, `chain`, and `amount` props to function.
*
* ```tsx
* import { ethereum } from "thirdweb/chains";
*
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1" // in native tokens (ie. ETH)
* />
* ```
*
* ### Buy a specific token
*
* You can specify a token to purchase by passing the `tokenAddress` prop.
*
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="100" // 100 USDC on mainnet
* tokenAddress="0xA0b86a33E6417E4df2057B2d3C6d9F7cc11b0a70"
* />
* ```
*
* ### Customize the supported tokens
*
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `BuyWidget` component.
*
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1"
* // user will only be able to pay with these tokens
* supportedTokens={{
* [8453]: [
* {
* address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
* name: "USDC",
* symbol: "USDC",
* },
* ],
* }}
* />
* ```
*
*
* ### Customize the UI
*
* You can customize the UI of the `BuyWidget` component by passing a custom theme object to the `theme` prop.
*
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1"
* theme={darkTheme({
* colors: {
* modalBg: "red",
* },
* })}
* />
* ```
*
* Refer to the [`Theme`](https://portal.thirdweb.com/references/typescript/v5/Theme) type for more details.
*
* ### Update the Title
*
* You can update the title of the widget by passing a `title` prop to the `BuyWidget` component.
*
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1"
* title="Buy ETH"
* />
* ```
*
* ### Configure the wallet connection
*
* You can customize the wallet connection flow by passing a `connectOptions` object to the `BuyWidget` component.
*
* ```tsx
* <BuyWidget
* client={client}
* chain={ethereum}
* amount="0.1"
* connectOptions={{
* connectModal: {
* size: 'compact',
* title: "Sign in",
* }
* }}
* />
* ```
*
* Refer to the [`BuyWidgetConnectOptions`](https://portal.thirdweb.com/references/typescript/v5/BuyWidgetConnectOptions) type for more details.
*
* @bridge
*/
export function BuyWidget(props: BuyWidgetProps) {
const hasFiredRenderEvent = useRef(false);
useEffect(() => {
if (hasFiredRenderEvent.current) return;
hasFiredRenderEvent.current = true;
trackPayEvent({
client: props.client,
event: "ub:ui:buy_widget:render",
toChainId: props.chain?.id,
toToken: props.tokenAddress,
});
}, [props.client, props.chain?.id, props.tokenAddress]);
// if branding is disabled for widget, disable it for connect options too
const connectOptions = useMemo(() => {
if (props.showThirdwebBranding === false) {
return {
...props.connectOptions,
connectModal: {
...props.connectOptions?.connectModal,
showThirdwebBranding: false,
},
};
}
return props.connectOptions;
}, [props.connectOptions, props.showThirdwebBranding]);
return (
<BridgeWidgetContainer
theme={props.theme}
className={props.className}
style={props.style}
>
{props.connectOptions?.autoConnect !== false && (
<AutoConnect
client={props.client}
wallets={props.connectOptions?.wallets}
timeout={
typeof props.connectOptions?.autoConnect === "object"
? props.connectOptions?.autoConnect?.timeout
: undefined
}
appMetadata={props.connectOptions?.appMetadata}
accountAbstraction={props.connectOptions?.accountAbstraction}
chain={props.connectOptions?.chain}
/>
)}
<BridgeWidgetContent
{...props}
theme={props.theme || "dark"}
currency={props.currency || "USD"}
paymentMethods={props.paymentMethods || ["crypto", "card"]}
presetOptions={props.presetOptions || [5, 10, 20]}
connectOptions={connectOptions}
showThirdwebBranding={
props.showThirdwebBranding === undefined
? true
: props.showThirdwebBranding
}
hiddenOnrampProviders={props.hiddenOnrampProviders}
/>
</BridgeWidgetContainer>
);
}
type BuyWidgetScreen =
| { id: "1:buy-ui" }
| {
id: "2:methodSelection";
destinationAmount: string;
destinationToken: TokenWithPrices;
receiverAddress: Address;
}
| {
id: "3:load-quote";
destinationAmount: string;
destinationToken: TokenWithPrices;
receiverAddress: Address;
paymentMethod: PaymentMethod;
}
| {
id: "4:preview";
preparedQuote: BridgePrepareResult;
request: BridgePrepareRequest;
destinationAmount: string;
destinationToken: TokenWithPrices;
paymentMethod: PaymentMethod;
receiverAddress: Address;
}
| {
id: "5:execute";
preparedQuote: BridgePrepareResult;
request: BridgePrepareRequest;
destinationAmount: string;
destinationToken: TokenWithPrices;
paymentMethod: PaymentMethod;
receiverAddress: Address;
}
| {
id: "6:success";
completedStatuses: CompletedStatusResult[];
preparedQuote: BridgePrepareResult;
}
| {
id: "error";
error: Error;
preparedQuote: BridgePrepareResult | undefined;
};
function BridgeWidgetContent(
props: RequiredParams<
BuyWidgetProps,
| "currency"
| "presetOptions"
| "showThirdwebBranding"
| "paymentMethods"
| "theme"
>,
) {
const [screen, setScreen] = useState<BuyWidgetScreen>({ id: "1:buy-ui" });
const handleError = useCallback(
(error: Error, quote: BridgePrepareResult | undefined) => {
console.error(error);
if (quote?.type === "buy" || quote?.type === "onramp") {
props.onError?.(error, quote);
} else {
props.onError?.(error, undefined);
}
setScreen({
id: "error",
preparedQuote: quote,
error,
});
},
[props.onError],
);
const handleCancel = useCallback(
(preparedQuote: BridgePrepareResult | undefined) => {
if (preparedQuote?.type === "buy" || preparedQuote?.type === "onramp") {
props.onCancel?.(preparedQuote);
} else {
props.onCancel?.(undefined);
}
},
[props.onCancel],
);
const [amountSelection, setAmountSelection] = useState<AmountSelection>({
type: "token",
value: props.amount ?? "",
});
const [selectedToken, setSelectedToken] = useState<SelectedToken>(() => {
if (!props.chain?.id) {
return undefined;
}
return {
chainId: props.chain.id,
tokenAddress: props.tokenAddress || NATIVE_TOKEN_ADDRESS,
};
});
const amountEditable =
props.amountEditable === undefined ? true : props.amountEditable;
const tokenEditable =
props.tokenEditable === undefined ? true : props.tokenEditable;
if (screen.id === "1:buy-ui") {
return (
<FundWallet
theme={props.theme}
onDisconnect={props.onDisconnect}
client={props.client}
connectOptions={props.connectOptions}
onContinue={(destinationAmount, destinationToken, receiverAddress) => {
trackPayEvent({
client: props.client,
event: "payment_selection",
toChainId: destinationToken.chainId,
toToken: destinationToken.address,
});
setScreen({
id: "2:methodSelection",
destinationAmount,
destinationToken,
receiverAddress,
});
}}
presetOptions={props.presetOptions}
receiverAddress={props.receiverAddress}
showThirdwebBranding={props.showThirdwebBranding}
metadata={{
title: props.title,
description: props.description,
image: props.image,
}}
buttonLabel={props.buttonLabel}
currency={props.currency}
selectedToken={selectedToken}
setSelectedToken={setSelectedToken}
amountSelection={amountSelection}
setAmountSelection={setAmountSelection}
amountEditable={amountEditable}
tokenEditable={tokenEditable}
/>
);
}
if (screen.id === "2:methodSelection") {
return (
<PaymentSelection
// from props
client={props.client}
connectLocale={connectLocaleEn}
connectOptions={props.connectOptions}
paymentMethods={props.paymentMethods}
currency={props.currency}
supportedTokens={props.supportedTokens}
country={props.country}
hiddenOnrampProviders={props.hiddenOnrampProviders}
// others
destinationToken={screen.destinationToken}
destinationAmount={screen.destinationAmount}
receiverAddress={screen.receiverAddress}
feePayer={undefined}
onBack={() => {
setScreen({ id: "1:buy-ui" });
}}
onError={(error) => {
handleError(error, undefined);
}}
onPaymentMethodSelected={(paymentMethod) => {
trackPayEvent({
chainId:
paymentMethod.type === "wallet"
? paymentMethod.originToken.chainId
: undefined,
client: props.client,
event: "ub:ui:loading_quote:fund_wallet",
fromToken:
paymentMethod.type === "wallet"
? paymentMethod.originToken.address
: undefined,
toChainId: screen.destinationToken.chainId,
toToken: screen.destinationToken.address,
});
setScreen({
...screen,
id: "3:load-quote",
paymentMethod,
});
}}
/>
);
}
if (screen.id === "3:load-quote") {
return (
<QuoteLoader
// from props
paymentLinkId={props.paymentLinkId}
purchaseData={props.purchaseData}
client={props.client}
// others
sender={undefined}
mode="fund_wallet"
feePayer={undefined}
amount={screen.destinationAmount}
destinationToken={screen.destinationToken}
onBack={() => {
setScreen({
...screen,
id: "2:methodSelection",
});
}}
onError={(error) => {
handleError(error, undefined);
}}
onQuoteReceived={(preparedQuote, request) => {
trackPayEvent({
chainId:
preparedQuote.type === "transfer"
? preparedQuote.intent.chainId
: preparedQuote.type === "onramp"
? preparedQuote.intent.chainId
: preparedQuote.intent.originChainId,
client: props.client,
event: "payment_details",
fromToken:
preparedQuote.type === "transfer"
? preparedQuote.intent.tokenAddress
: preparedQuote.type === "onramp"
? preparedQuote.intent.tokenAddress
: preparedQuote.intent.originTokenAddress,
toChainId:
preparedQuote.type === "transfer"
? preparedQuote.intent.chainId
: preparedQuote.type === "onramp"
? preparedQuote.intent.chainId
: preparedQuote.intent.destinationChainId,
toToken:
preparedQuote.type === "transfer"
? preparedQuote.intent.tokenAddress
: preparedQuote.type === "onramp"
? preparedQuote.intent.tokenAddress
: preparedQuote.intent.destinationTokenAddress,
walletAddress:
screen.paymentMethod.payerWallet?.getAccount()?.address,
walletType: screen.paymentMethod.payerWallet?.id,
});
setScreen({
...screen,
id: "4:preview",
preparedQuote,
request,
});
}}
paymentMethod={screen.paymentMethod}
receiver={screen.receiverAddress}
/>
);
}
if (screen.id === "4:preview") {
return (
<PaymentDetails
// from props
client={props.client}
currency={props.currency}
metadata={{
title: props.title,
description: props.description,
}}
// others
confirmButtonLabel={undefined}
onBack={() => {
setScreen({
...screen,
id: "2:methodSelection",
});
}}
onConfirm={() => {
setScreen({
...screen,
id: "5:execute",
});
}}
onError={(error) => {
handleError(error, screen.preparedQuote);
}}
paymentMethod={screen.paymentMethod}
preparedQuote={screen.preparedQuote}
modeInfo={{
mode: "fund_wallet",
}}
/>
);
}
if (screen.id === "5:execute") {
return (
<StepRunner
// from props
client={props.client}
// others
title={undefined}
preparedQuote={screen.preparedQuote}
autoStart={true}
onBack={() => {
setScreen({
...screen,
id: "4:preview",
});
}}
onCancel={() => {
handleCancel(screen.preparedQuote);
}}
onComplete={(completedStatuses) => {
if (
screen.preparedQuote.type === "buy" ||
screen.preparedQuote.type === "onramp"
) {
props.onSuccess?.({
quote: screen.preparedQuote,
statuses: completedStatuses,
});
}
setScreen({
id: "6:success",
preparedQuote: screen.preparedQuote,
completedStatuses,
});
}}
request={screen.request}
wallet={screen.paymentMethod.payerWallet}
windowAdapter={webWindowAdapter}
/>
);
}
if (screen.id === "6:success") {
return (
<SuccessScreen
type="payment-success"
// from props
client={props.client}
hasPaymentId={!!props.paymentLinkId}
completedStatuses={screen.completedStatuses}
// others
onDone={() => {
setScreen({ id: "1:buy-ui" });
}}
preparedQuote={screen.preparedQuote}
showContinueWithTx={false}
windowAdapter={webWindowAdapter}
/>
);
}
if (screen.id === "error") {
return (
<ErrorBanner
client={props.client}
error={screen.error}
onCancel={() => {
setScreen({ id: "1:buy-ui" });
handleCancel(screen.preparedQuote);
}}
onRetry={() => {
setScreen({ id: "1:buy-ui" });
}}
/>
);
}
return null;
}
/**
* @internal
*/
function BridgeWidgetContainer(props: {
theme: BuyWidgetProps["theme"];
className: string | undefined;
style?: React.CSSProperties | undefined;
children: React.ReactNode;
}) {
return (
<CustomThemeProvider theme={props.theme || "dark"}>
<EmbedContainer
className={props.className}
modalSize="compact"
style={props.style}
>
<DynamicHeight>{props.children}</DynamicHeight>
</EmbedContainer>
</CustomThemeProvider>
);
}
/**
* Connection options for the `BuyWidget` component
*
* @example
* ```tsx
* <BuyWidget client={client} connectOptions={{
* connectModal: {
* size: 'compact',
* title: "Sign in",
* }
* }}
* />
* ```
*/
type BuyWidgetConnectOptions = {
/**
* Configurations for the `ConnectButton`'s Modal that is shown for connecting a wallet
* Refer to the [`ConnectButton_connectModalOptions`](https://portal.thirdweb.com/references/typescript/v5/ConnectButton_connectModalOptions) type for more details
*/
connectModal?: ConnectButton_connectModalOptions;
/**
* Configure options for WalletConnect
*
* By default WalletConnect uses the thirdweb's default project id.
* Setting your own project id is recommended.
*
* You can create a project id by signing up on [walletconnect.com](https://walletconnect.com/)
*/
walletConnect?: {
projectId?: string;
};
/**
* Enable Account abstraction for all wallets. This will connect to the users's smart account based on the connected personal wallet and the given options.
*
* This allows to sponsor gas fees for your user's transaction using the thirdweb account abstraction infrastructure.
*
*/
accountAbstraction?: SmartWalletOptions;
/**
* Array of wallets to show in Connect Modal. If not provided, default wallets will be used.
*/
wallets?: Wallet[];
/**
* When the user has connected their wallet to your site, this configuration determines whether or not you want to automatically connect to the last connected wallet when user visits your site again in the future.
*
* By default it is set to `{ timeout: 15000 }` meaning that autoConnect is enabled and if the autoConnection does not succeed within 15 seconds, it will be cancelled.
*
* If you want to disable autoConnect, set this prop to `false`.
*
* If you want to customize the timeout, you can assign an object with a `timeout` key to this prop.
* ```
*/
autoConnect?:
| {
timeout: number;
}
| boolean;
/**
* Metadata of the app that will be passed to connected wallet. Setting this is highly recommended.
*/
appMetadata?: AppMetadata;
/**
* The [`Chain`](https://portal.thirdweb.com/references/typescript/v5/Chain) object of the blockchain you want the wallet to connect to
*
* If a `chain` is not specified, Wallet will be connected to whatever is the default set in the wallet.
*
* If a `chain` is specified, Wallet will be prompted to switch to given chain after connection if it is not already connected to it.
* This ensures that the wallet is connected to the correct blockchain before interacting with your app.
*
* The `ConnectButton` also shows a "Switch Network" button until the wallet is connected to the specified chain. Clicking on the "Switch Network" button triggers the wallet to switch to the specified chain.
*
* You can create a `Chain` object using the [`defineChain`](https://portal.thirdweb.com/references/typescript/v5/defineChain) function.
* At minimum, you need to pass the `id` of the blockchain to `defineChain` function to create a `Chain` object.
* ```
*/
chain?: Chain;
/**
* Array of chains that your app supports.
*
* This is only relevant if your app is a multi-chain app and works across multiple blockchains.
* If your app only works on a single blockchain, you should only specify the `chain` prop.
*
* Given list of chains will used in various ways:
* - They will be displayed in the network selector in the `ConnectButton`'s details modal post connection
* - They will be sent to wallet at the time of connection if the wallet supports requesting multiple chains ( example: WalletConnect ) so that users can switch between the chains post connection easily
*
* You can create a `Chain` object using the [`defineChain`](https://portal.thirdweb.com/references/typescript/v5/defineChain) function.
* At minimum, you need to pass the `id` of the blockchain to `defineChain` function to create a `Chain` object.
*
* ```tsx
* import { defineChain } from "thirdweb/react";
*
* const polygon = defineChain({
* id: 137,
* });
* ```
*/
chains?: Chain[];
/**
* Wallets to show as recommended in the `ConnectButton`'s Modal
*/
recommendedWallets?: Wallet[];
/**
* By default, ConnectButton modal shows a "All Wallets" button that shows a list of 500+ wallets.
*
* You can disable this button by setting `showAllWallets` prop to `false`
*/
showAllWallets?: boolean;
/**
* Enable SIWE (Sign in with Ethererum) by passing an object of type `SiweAuthOptions` to
* enforce the users to sign a message after connecting their wallet to authenticate themselves.
*
* Refer to the [`SiweAuthOptions`](https://portal.thirdweb.com/references/typescript/v5/SiweAuthOptions) for more details
*/
auth?: SiweAuthOptions;
};