Skip to content

Commit d52855d

Browse files
[SDK] Fix supportedTokens address comparison in usePaymentMethods (#7824)
1 parent a3909e1 commit d52855d

File tree

6 files changed

+116
-34
lines changed

6 files changed

+116
-34
lines changed

.changeset/violet-readers-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix supportedTokens address comparison

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,15 @@ export function usePaymentMethods(options: {
9393
type: "wallet" as const,
9494
quote: s.quote,
9595
}));
96-
const insufficientBalanceQuotes = validTokenQuotes
97-
.filter((s) => s.balance < s.quote.originAmount)
98-
.sort((a, b) => {
99-
return (
100-
Number.parseFloat(
101-
toTokens(a.quote.originAmount, a.originToken.decimals),
102-
) *
103-
(a.originToken.prices.USD || 1) -
104-
Number.parseFloat(
105-
toTokens(b.quote.originAmount, b.originToken.decimals),
106-
) *
107-
(b.originToken.prices.USD || 1)
108-
);
109-
});
96+
11097
const sufficientBalanceQuotes = validTokenQuotes
111-
.filter((s) => s.balance >= s.quote.originAmount)
98+
.filter((s) => !!s.originToken.prices.USD)
11299
.sort((a, b) => {
113100
return (
114-
Number.parseFloat(
115-
toTokens(a.quote.originAmount, a.originToken.decimals),
116-
) *
117-
(a.originToken.prices.USD || 1) -
118-
Number.parseFloat(
119-
toTokens(b.quote.originAmount, b.originToken.decimals),
120-
) *
121-
(b.originToken.prices.USD || 1)
101+
Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
102+
(b.originToken.prices.USD || 1) -
103+
Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) *
104+
(a.originToken.prices.USD || 1)
122105
);
123106
});
124107

@@ -133,15 +116,14 @@ export function usePaymentMethods(options: {
133116
)
134117
: [];
135118
const finalQuotes = supportedTokens
136-
? [...sufficientBalanceQuotes, ...insufficientBalanceQuotes].filter(
137-
(q) =>
138-
tokensToInclude.find(
139-
(t) =>
140-
t.chainId === q.originToken.chainId &&
141-
t.address === q.originToken.address,
142-
),
119+
? sufficientBalanceQuotes.filter((q) =>
120+
tokensToInclude.find(
121+
(t) =>
122+
t.chainId === q.originToken.chainId &&
123+
t.address.toLowerCase() === q.originToken.address.toLowerCase(),
124+
),
143125
)
144-
: [...sufficientBalanceQuotes, ...insufficientBalanceQuotes];
126+
: sufficientBalanceQuotes;
145127
return finalQuotes;
146128
},
147129
queryKey: [

packages/thirdweb/src/react/web/ui/Bridge/BridgeOrchestrator.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export function BridgeOrchestrator({
192192
// Handle errors
193193
const handleError = useCallback(
194194
(error: Error) => {
195+
console.error(error);
195196
onError?.(error);
196197
send({ error, type: "ERROR_OCCURRED" });
197198
},

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import { BridgeOrchestrator, type UIOptions } from "./BridgeOrchestrator.js";
3333
import { UnsupportedTokenScreen } from "./UnsupportedTokenScreen.js";
3434

3535
export type BuyWidgetProps = {
36+
/**
37+
* Customize the supported tokens that users can pay with.
38+
*/
3639
supportedTokens?: SupportedTokens;
3740
/**
3841
* A client is the entry point to the thirdweb SDK.
@@ -217,7 +220,7 @@ type UIOptionsResult =
217220
* <BuyWidget
218221
* client={client}
219222
* chain={ethereum}
220-
* amount="0.1"
223+
* amount="0.1" // in native tokens (ie. ETH)
221224
* />
222225
* ```
223226
*
@@ -229,11 +232,34 @@ type UIOptionsResult =
229232
* <BuyWidget
230233
* client={client}
231234
* chain={ethereum}
232-
* amount="100"
235+
* amount="100" // 100 USDC on mainnet
233236
* tokenAddress="0xA0b86a33E6417E4df2057B2d3C6d9F7cc11b0a70"
234237
* />
235238
* ```
236239
*
240+
* ### Customize the supported tokens
241+
*
242+
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `BuyWidget` component.
243+
*
244+
* ```tsx
245+
* <BuyWidget
246+
* client={client}
247+
* chain={ethereum}
248+
* amount="0.1"
249+
* // user will only be able to pay with these tokens
250+
* supportedTokens={{
251+
* [8453]: [
252+
* {
253+
* address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
254+
* name: "USDC",
255+
* symbol: "USDC",
256+
* },
257+
* ],
258+
* }}
259+
* />
260+
* ```
261+
*
262+
*
237263
* ### Customize the UI
238264
*
239265
* You can customize the UI of the `BuyWidget` component by passing a custom theme object to the `theme` prop.

packages/thirdweb/src/react/web/ui/Bridge/CheckoutWidget.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import { BridgeOrchestrator, type UIOptions } from "./BridgeOrchestrator.js";
2929
import { UnsupportedTokenScreen } from "./UnsupportedTokenScreen.js";
3030

3131
export type CheckoutWidgetProps = {
32+
/**
33+
* Customize the supported tokens that users can pay with.
34+
*/
3235
supportedTokens?: SupportedTokens;
3336
/**
3437
* A client is the entry point to the thirdweb SDK.
@@ -215,11 +218,43 @@ type UIOptionsResult =
215218
* @example
216219
* ### Default configuration
217220
*
218-
* By default, the `CheckoutWidget` component will allows users to fund their wallets with crypto or fiat on any of the supported chains..
221+
* The `CheckoutWidget` component allows user to pay a given wallet for any product or service. You can register webhooks to get notified for every purchase done via the widget.
222+
*
223+
* ```tsx
224+
* <CheckoutWidget
225+
* client={client}
226+
* chain={base}
227+
* amount="0.01" // in native tokens (ETH), pass tokenAddress to charge in a specific token (USDC, USDT, etc.)
228+
* seller="0x123...abc" // the wallet address that will receive the payment
229+
* name="Premium Course"
230+
* description="Complete guide to web3 development"
231+
* image="/course-thumbnail.jpg"
232+
* onSuccess={() => {
233+
* alert("Purchase successful!");
234+
* }}
235+
* />
236+
* ```
237+
*
238+
* ### Customize the supported tokens
239+
*
240+
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `CheckoutWidget` component.
219241
*
220242
* ```tsx
221243
* <CheckoutWidget
222244
* client={client}
245+
* chain={arbitrum}
246+
* amount="0.01"
247+
* seller="0x123...abc"
248+
* // user will only be able to pay with these tokens
249+
* supportedTokens={{
250+
* [8453]: [
251+
* {
252+
* address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
253+
* name: "USDC",
254+
* symbol: "USDC",
255+
* },
256+
* ],
257+
* }}
223258
* />
224259
* ```
225260
*
@@ -230,6 +265,9 @@ type UIOptionsResult =
230265
* ```tsx
231266
* <CheckoutWidget
232267
* client={client}
268+
* chain={arbitrum}
269+
* amount="0.01"
270+
* seller="0x123...abc"
233271
* theme={darkTheme({
234272
* colors: {
235273
* modalBg: "red",
@@ -258,6 +296,9 @@ type UIOptionsResult =
258296
* ```tsx
259297
* <CheckoutWidget
260298
* client={client}
299+
* chain={arbitrum}
300+
* amount="0.01"
301+
* seller="0x123...abc"
261302
* connectOptions={{
262303
* connectModal: {
263304
* size: 'compact',

packages/thirdweb/src/react/web/ui/Bridge/TransactionWidget.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import { BridgeOrchestrator, type UIOptions } from "./BridgeOrchestrator.js";
3737
import { UnsupportedTokenScreen } from "./UnsupportedTokenScreen.js";
3838

3939
export type TransactionWidgetProps = {
40+
/**
41+
* Customize the supported tokens that users can pay with.
42+
*/
4043
supportedTokens?: SupportedTokens;
4144
/**
4245
* A client is the entry point to the thirdweb SDK.
@@ -232,6 +235,30 @@ type UIOptionsResult =
232235
* />
233236
* ```
234237
*
238+
* ### Customize the supported tokens
239+
*
240+
* You can customize the supported tokens that users can pay with by passing a `supportedTokens` object to the `TransactionWidget` component.
241+
*
242+
* ```tsx
243+
* <TransactionWidget
244+
* client={client}
245+
* transaction={prepareTransaction({
246+
* to: "0x...",
247+
* chain: ethereum,
248+
* client: client,
249+
* })}
250+
* supportedTokens={{
251+
* [8453]: [
252+
* {
253+
* address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
254+
* name: "USDC",
255+
* symbol: "USDC",
256+
* },
257+
* ],
258+
* }}
259+
* />
260+
* ```
261+
*
235262
* ### Customize the UI
236263
*
237264
* You can customize the UI of the `TransactionWidget` component by passing a custom theme object to the `theme` prop.

0 commit comments

Comments
 (0)