|
| 1 | +import type { Address } from "abitype"; |
| 2 | +import type { Chain } from "../../chains/types.js"; |
| 3 | +import type { ThirdwebClient } from "../../client/client.js"; |
| 4 | +import { getClientFetch } from "../../utils/fetch.js"; |
| 5 | +import { getPayConvertCryptoToFiatEndpoint } from "../utils/definitions.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Props for the `convertCryptoToFiat` function |
| 9 | + * @convertCrypto |
| 10 | + */ |
| 11 | +export type ConvertCryptoToFiatParams = { |
| 12 | + client: ThirdwebClient; |
| 13 | + /** |
| 14 | + * The contract address of the token |
| 15 | + * For native token, use NATIVE_TOKEN_ADDRESS |
| 16 | + */ |
| 17 | + fromTokenAddress: Address; |
| 18 | + /** |
| 19 | + * The amount of token to convert to fiat value |
| 20 | + */ |
| 21 | + fromAmount: number; |
| 22 | + /** |
| 23 | + * The chain that the token is deployed to |
| 24 | + */ |
| 25 | + chain: Chain; |
| 26 | + /** |
| 27 | + * The fiat symbol. e.g "usd" |
| 28 | + */ |
| 29 | + to: string; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Get a price of a token (using tokenAddress + chainId) in fiat. |
| 34 | + * Only USD is supported at the moment. |
| 35 | + * @example |
| 36 | + * ### Basic usage |
| 37 | + * For native token (non-ERC20), you should use NATIVE_TOKEN_ADDRESS as the value for `tokenAddress` |
| 38 | + * ```ts |
| 39 | + * import { convertCryptoToFiat } from "thirdweb/pay"; |
| 40 | + * |
| 41 | + * // Get Ethereum price |
| 42 | + * const result = convertCryptoToFiat({ |
| 43 | + * fromTokenAddress: NATIVE_TOKEN_ADDRESS, |
| 44 | + * // This is not case sensitive, so either "USD" or "usd" is fine |
| 45 | + * to: "USD", |
| 46 | + * chain: ethereum, |
| 47 | + * fromAmount: 1, |
| 48 | + * }); |
| 49 | + * |
| 50 | + * // Result: 3404.11 |
| 51 | + * ``` |
| 52 | + * @convertCrypto |
| 53 | + * @returns a number representing the price (in selected fiat) of "x" token, with "x" being the `fromAmount`. |
| 54 | + */ |
| 55 | +export async function convertCryptoToFiat( |
| 56 | + options: ConvertCryptoToFiatParams, |
| 57 | +): Promise<{ result: number }> { |
| 58 | + const { client, fromTokenAddress, to, chain, fromAmount } = options; |
| 59 | + if (Number(fromAmount) === 0) { |
| 60 | + return { result: 0 }; |
| 61 | + } |
| 62 | + try { |
| 63 | + // Testnets just don't work with our current provider(s) |
| 64 | + if (chain.testnet === true) { |
| 65 | + throw new Error( |
| 66 | + `Cannot fetch price for a testnet (chainId: ${chain.id})`, |
| 67 | + ); |
| 68 | + } |
| 69 | + const params = { |
| 70 | + fromTokenAddress, |
| 71 | + to, |
| 72 | + chainId: String(chain.id), |
| 73 | + fromAmount: String(fromAmount), |
| 74 | + }; |
| 75 | + const queryString = new URLSearchParams(params).toString(); |
| 76 | + const url = `${getPayConvertCryptoToFiatEndpoint()}?${queryString}`; |
| 77 | + const response = await getClientFetch(client)(url); |
| 78 | + if (!response.ok) { |
| 79 | + response.body?.cancel(); |
| 80 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 81 | + } |
| 82 | + |
| 83 | + const data: { result: number } = await response.json(); |
| 84 | + return data; |
| 85 | + } catch (error) { |
| 86 | + console.error("Fetch error:", error); |
| 87 | + throw new Error(`Fetch failed: ${error}`); |
| 88 | + } |
| 89 | +} |
0 commit comments