Skip to content

Commit d6f16a6

Browse files
Handle different fiat currencies in payment widgets
1 parent 5c6dd87 commit d6f16a6

File tree

24 files changed

+167
-66
lines changed

24 files changed

+167
-66
lines changed

.changeset/ready-loops-search.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+
Handle different fiat currencies in payment widgets

apps/playground-web/src/components/pay/direct-payment.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function BuyMerchPreview() {
1717
seller="0xEb0effdFB4dC5b3d5d3aC6ce29F3ED213E95d675"
1818
theme="light"
1919
tokenAddress="0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
20+
currency="EUR"
2021
/>
2122
</>
2223
);

packages/thirdweb/src/bridge/types/Token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export type Token = {
77
symbol: string;
88
name: string;
99
iconUri?: string;
10-
priceUsd: number;
10+
prices: Record<string, number>;
1111
};

packages/thirdweb/src/pay/buyWithCrypto/getQuote.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,14 @@ export async function getBuyWithCryptoQuote(
300300
Number(
301301
Value.format(quote.originAmount, firstStep.originToken.decimals),
302302
) *
303-
firstStep.originToken.priceUsd *
303+
(firstStep.originToken.prices["USD"] || 0) *
304304
100,
305305
amountWei: quote.originAmount.toString(),
306306
token: {
307307
chainId: firstStep.originToken.chainId,
308308
decimals: firstStep.originToken.decimals,
309309
name: firstStep.originToken.name,
310-
priceUSDCents: firstStep.originToken.priceUsd * 100,
310+
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
311311
symbol: firstStep.originToken.symbol,
312312
tokenAddress: firstStep.originToken.address,
313313
},
@@ -323,7 +323,7 @@ export async function getBuyWithCryptoQuote(
323323
chainId: firstStep.originToken.chainId,
324324
decimals: firstStep.originToken.decimals,
325325
name: firstStep.originToken.name,
326-
priceUSDCents: firstStep.originToken.priceUsd * 100,
326+
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
327327
symbol: firstStep.originToken.symbol,
328328
tokenAddress: firstStep.originToken.address,
329329
},
@@ -337,7 +337,7 @@ export async function getBuyWithCryptoQuote(
337337
Number(
338338
Value.format(quote.originAmount, firstStep.originToken.decimals),
339339
) *
340-
firstStep.originToken.priceUsd *
340+
(firstStep.originToken.prices["USD"] || 0) *
341341
100,
342342
gasCostUSDCents: 0,
343343
slippageBPS: 0,
@@ -348,7 +348,7 @@ export async function getBuyWithCryptoQuote(
348348
firstStep.destinationToken.decimals,
349349
),
350350
) *
351-
firstStep.destinationToken.priceUsd *
351+
(firstStep.destinationToken.prices["USD"] || 0) *
352352
100,
353353
toAmountUSDCents:
354354
Number(
@@ -357,7 +357,7 @@ export async function getBuyWithCryptoQuote(
357357
firstStep.destinationToken.decimals,
358358
),
359359
) *
360-
firstStep.destinationToken.priceUsd *
360+
(firstStep.destinationToken.prices["USD"] || 0) *
361361
100,
362362
},
363363
fromAddress: quote.intent.sender,
@@ -372,7 +372,7 @@ export async function getBuyWithCryptoQuote(
372372
chainId: firstStep.originToken.chainId,
373373
decimals: firstStep.originToken.decimals,
374374
name: firstStep.originToken.name,
375-
priceUSDCents: firstStep.originToken.priceUsd * 100,
375+
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
376376
symbol: firstStep.originToken.symbol,
377377
tokenAddress: firstStep.originToken.address,
378378
},
@@ -395,7 +395,7 @@ export async function getBuyWithCryptoQuote(
395395
chainId: firstStep.destinationToken.chainId,
396396
decimals: firstStep.destinationToken.decimals,
397397
name: firstStep.destinationToken.name,
398-
priceUSDCents: firstStep.destinationToken.priceUsd * 100,
398+
priceUSDCents: (firstStep.destinationToken.prices["USD"] || 0) * 100,
399399
symbol: firstStep.destinationToken.symbol,
400400
tokenAddress: firstStep.destinationToken.address,
401401
},

packages/thirdweb/src/pay/buyWithCrypto/getTransfer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ export async function getBuyWithCryptoTransfer(
198198
Number(
199199
Value.format(quote.originAmount, firstStep.originToken.decimals),
200200
) *
201-
firstStep.originToken.priceUsd *
201+
(firstStep.originToken.prices["USD"] || 0) *
202202
100,
203203
amountWei: quote.originAmount.toString(),
204204
token: {
205205
chainId: firstStep.originToken.chainId,
206206
decimals: firstStep.originToken.decimals,
207207
name: firstStep.originToken.name,
208-
priceUSDCents: firstStep.originToken.priceUsd * 100,
208+
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
209209
symbol: firstStep.originToken.symbol,
210210
tokenAddress: firstStep.originToken.address,
211211
},
@@ -226,7 +226,7 @@ export async function getBuyWithCryptoTransfer(
226226
firstStep.originToken.decimals,
227227
),
228228
) *
229-
firstStep.originToken.priceUsd *
229+
(firstStep.originToken.prices["USD"] || 0) *
230230
100
231231
: 0,
232232
amountWei:
@@ -237,7 +237,7 @@ export async function getBuyWithCryptoTransfer(
237237
chainId: firstStep.originToken.chainId,
238238
decimals: firstStep.originToken.decimals,
239239
name: firstStep.originToken.name,
240-
priceUSDCents: firstStep.originToken.priceUsd * 100,
240+
priceUSDCents: (firstStep.originToken.prices["USD"] || 0) * 100,
241241
symbol: firstStep.originToken.symbol,
242242
tokenAddress: firstStep.originToken.address,
243243
},

packages/thirdweb/src/pay/buyWithFiat/getQuote.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ export async function getBuyWithFiatQuote(
367367
decimals: number;
368368
symbol: string;
369369
name: string;
370-
priceUsd: number;
370+
prices: Record<string, number>;
371371
}): PayTokenInfo => ({
372372
chainId: token.chainId,
373373
decimals: token.decimals,
374374
name: token.name,
375-
priceUSDCents: Math.round(token.priceUsd * 100),
375+
priceUSDCents: Math.round((token.prices["USD"] || 0) * 100),
376376
symbol: token.symbol,
377377
tokenAddress: token.address,
378378
});
@@ -408,7 +408,7 @@ export async function getBuyWithFiatQuote(
408408
const onRampTokenObject = {
409409
amount: onRampTokenAmount,
410410
amountUSDCents: Math.round(
411-
Number(onRampTokenAmount) * onRampTokenRaw.priceUsd * 100,
411+
Number(onRampTokenAmount) * (onRampTokenRaw.prices["USD"] || 0) * 100,
412412
),
413413
amountWei: onRampTokenAmountWei.toString(),
414414
token: tokenToPayTokenInfo(onRampTokenRaw),
@@ -434,7 +434,7 @@ export async function getBuyWithFiatQuote(
434434
routingTokenObject = {
435435
amount: routingAmount,
436436
amountUSDCents: Math.round(
437-
Number(routingAmount) * routingTokenRaw.priceUsd * 100,
437+
Number(routingAmount) * (routingTokenRaw.prices["USD"] || 0) * 100,
438438
),
439439
amountWei: routingAmountWei.toString(),
440440
token: tokenToPayTokenInfo(routingTokenRaw),

packages/thirdweb/src/pay/convert/cryptoToFiat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export type ConvertCryptoToFiatParams = {
2626
chain: Chain;
2727
/**
2828
* The fiat symbol. e.g "USD"
29-
* Only USD is supported at the moment.
3029
*/
3130
to: SupportedFiatCurrency;
3231
};
@@ -56,7 +55,7 @@ export type ConvertCryptoToFiatParams = {
5655
export async function convertCryptoToFiat(
5756
options: ConvertCryptoToFiatParams,
5857
): Promise<{ result: number }> {
59-
const { client, fromTokenAddress, chain, fromAmount } = options;
58+
const { client, fromTokenAddress, chain, fromAmount, to } = options;
6059
if (Number(fromAmount) === 0) {
6160
return { result: 0 };
6261
}
@@ -74,10 +73,11 @@ export async function convertCryptoToFiat(
7473
);
7574
}
7675
const token = await getToken(client, fromTokenAddress, chain.id);
77-
if (token.priceUsd === 0) {
76+
const price = token?.prices[to] || 0;
77+
if (!token || price === 0) {
7878
throw new Error(
7979
`Error: Failed to fetch price for token ${fromTokenAddress} on chainId: ${chain.id}`,
8080
);
8181
}
82-
return { result: token.priceUsd * fromAmount };
82+
return { result: price * fromAmount };
8383
}

packages/thirdweb/src/pay/convert/fiatToCrypto.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type ConvertFiatToCryptoParams = {
1313
client: ThirdwebClient;
1414
/**
1515
* The fiat symbol. e.g: "USD"
16-
* Currently only USD is supported.
1716
*/
1817
from: SupportedFiatCurrency;
1918
/**
@@ -57,7 +56,7 @@ export type ConvertFiatToCryptoParams = {
5756
export async function convertFiatToCrypto(
5857
options: ConvertFiatToCryptoParams,
5958
): Promise<{ result: number }> {
60-
const { client, to, chain, fromAmount } = options;
59+
const { client, to, chain, fromAmount, from } = options;
6160
if (Number(fromAmount) === 0) {
6261
return { result: 0 };
6362
}
@@ -73,10 +72,11 @@ export async function convertFiatToCrypto(
7372
throw new Error("Invalid `to`. Expected a valid EVM contract address");
7473
}
7574
const token = await getToken(client, to, chain.id);
76-
if (!token || token.priceUsd === 0) {
75+
const price = token?.prices[from] || 0;
76+
if (!token || price === 0) {
7777
throw new Error(
7878
`Error: Failed to fetch price for token ${to} on chainId: ${chain.id}`,
7979
);
8080
}
81-
return { result: fromAmount / token.priceUsd };
81+
return { result: fromAmount / price };
8282
}

packages/thirdweb/src/pay/convert/type.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
const SUPPORTED_FIAT_CURRENCIES = [
1+
const CURRENCIES = [
22
"USD",
3-
"CAD",
4-
"GBP",
53
"EUR",
4+
"GBP",
65
"JPY",
6+
"KRW",
7+
"CNY",
8+
"INR",
9+
"NOK",
10+
"SEK",
11+
"CHF",
712
"AUD",
13+
"CAD",
814
"NZD",
15+
"MXN",
16+
"BRL",
17+
"CLP",
18+
"CZK",
19+
"DKK",
20+
"HKD",
21+
"HUF",
22+
"IDR",
23+
"ILS",
24+
"ISK",
925
] as const;
10-
/**
11-
* @internal
12-
*/
13-
export type SupportedFiatCurrency = (typeof SUPPORTED_FIAT_CURRENCIES)[number];
26+
27+
export type SupportedFiatCurrency = (typeof CURRENCIES)[number] | (string & {});
1428

1529
export function getFiatSymbol(showBalanceInFiat: SupportedFiatCurrency) {
1630
switch (showBalanceInFiat) {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export function usePaymentMethods(options: {
106106
decimals: b.decimals,
107107
iconUri: "",
108108
name: b.name,
109-
priceUsd: 0,
109+
prices: {
110+
USD: 0,
111+
},
110112
symbol: b.symbol,
111113
} as Token,
112114
}));
@@ -183,10 +185,10 @@ export function usePaymentMethods(options: {
183185
validOwnedTokens.sort((a, b) => {
184186
const aDollarBalance =
185187
Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) *
186-
a.originToken.priceUsd;
188+
(a.originToken.prices["USD"] || 0);
187189
const bDollarBalance =
188190
Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
189-
b.originToken.priceUsd;
191+
(b.originToken.prices["USD"] || 0);
190192
return bDollarBalance - aDollarBalance;
191193
});
192194

0 commit comments

Comments
 (0)