|
| 1 | +import type { ThirdwebClient } from "../client/client.js"; |
| 2 | +import { getThirdwebBaseUrl } from "../utils/domains.js"; |
| 3 | +import { getClientFetch } from "../utils/fetch.js"; |
| 4 | +import { ApiError } from "./types/Errors.js"; |
| 5 | +import type { Token } from "./types/Token.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Retrieves supported Universal Bridge tokens based on the provided filters. |
| 9 | + * |
| 10 | + * When multiple filters are specified, a token must satisfy all filters to be included (it acts as an AND operator). |
| 11 | + * |
| 12 | + * @example |
| 13 | + * ```typescript |
| 14 | + * import { Bridge } from "thirdweb"; |
| 15 | + * |
| 16 | + * const tokens = await Bridge.tokens({ |
| 17 | + * client: thirdwebClient, |
| 18 | + * }); |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * Returned tokens might look something like: |
| 22 | + * ```typescript |
| 23 | + * [ |
| 24 | + * { |
| 25 | + * chainId: 1, |
| 26 | + * address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", |
| 27 | + * decimals: 18, |
| 28 | + * symbol: "ETH", |
| 29 | + * name: "Ethereum", |
| 30 | + * iconUri: "https://assets.relay.link/icons/1/light.png", |
| 31 | + * priceUsd: 2000.50 |
| 32 | + * }, |
| 33 | + * { |
| 34 | + * chainId: 1, |
| 35 | + * address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", |
| 36 | + * decimals: 6, |
| 37 | + * symbol: "USDC", |
| 38 | + * name: "USD Coin", |
| 39 | + * iconUri: "https://assets.coingecko.com/coins/images/6319/large/USD_Coin_icon.png", |
| 40 | + * priceUsd: 1.00 |
| 41 | + * } |
| 42 | + * ] |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * You can filter for specific chains or tokens: |
| 46 | + * ```typescript |
| 47 | + * import { Bridge } from "thirdweb"; |
| 48 | + * |
| 49 | + * // Get all tokens on Ethereum mainnet |
| 50 | + * const ethTokens = await Bridge.tokens({ |
| 51 | + * chainId: 1, |
| 52 | + * client: thirdwebClient, |
| 53 | + * }); |
| 54 | + * ``` |
| 55 | + * |
| 56 | + * You can search for tokens by symbol or name: |
| 57 | + * ```typescript |
| 58 | + * import { Bridge } from "thirdweb"; |
| 59 | + * |
| 60 | + * // Search for USDC tokens |
| 61 | + * const usdcTokens = await Bridge.tokens({ |
| 62 | + * symbol: "USDC", |
| 63 | + * client: thirdwebClient, |
| 64 | + * }); |
| 65 | + * |
| 66 | + * // Search for tokens by name |
| 67 | + * const ethereumTokens = await Bridge.tokens({ |
| 68 | + * name: "Ethereum", |
| 69 | + * client: thirdwebClient, |
| 70 | + * }); |
| 71 | + * ``` |
| 72 | + * |
| 73 | + * You can filter by a specific token address: |
| 74 | + * ```typescript |
| 75 | + * import { Bridge } from "thirdweb"; |
| 76 | + * |
| 77 | + * // Get a specific token |
| 78 | + * const token = await Bridge.tokens({ |
| 79 | + * tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", |
| 80 | + * client: thirdwebClient, |
| 81 | + * }); |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * The returned tokens will be limited based on the API. You can paginate through the results using the `limit` and `offset` parameters: |
| 85 | + * ```typescript |
| 86 | + * import { Bridge } from "thirdweb"; |
| 87 | + * |
| 88 | + * // Get the first 50 tokens |
| 89 | + * const tokens = await Bridge.tokens({ |
| 90 | + * limit: 50, |
| 91 | + * offset: 0, |
| 92 | + * client: thirdwebClient, |
| 93 | + * }); |
| 94 | + * |
| 95 | + * // Get the next 50 tokens |
| 96 | + * const nextTokens = await Bridge.tokens({ |
| 97 | + * limit: 50, |
| 98 | + * offset: 50, |
| 99 | + * client: thirdwebClient, |
| 100 | + * }); |
| 101 | + * ``` |
| 102 | + * |
| 103 | + * @param options - The options for retrieving tokens. |
| 104 | + * @param options.client - Your thirdweb client. |
| 105 | + * @param options.chainId - Filter by a specific chain ID. |
| 106 | + * @param options.tokenAddress - Filter by a specific token address. |
| 107 | + * @param options.symbol - Filter by token symbol. |
| 108 | + * @param options.name - Filter by token name. |
| 109 | + * @param options.limit - Number of tokens to return (min: 1, default: 100). |
| 110 | + * @param options.offset - Number of tokens to skip (min: 0, default: 0). |
| 111 | + * |
| 112 | + * @returns A promise that resolves to an array of tokens. |
| 113 | + * |
| 114 | + * @throws Will throw an error if there is an issue fetching the tokens. |
| 115 | + * @bridge |
| 116 | + * @beta |
| 117 | + */ |
| 118 | +export async function tokens(options: tokens.Options): Promise<tokens.Result> { |
| 119 | + const { client, chainId, tokenAddress, symbol, name, limit, offset } = |
| 120 | + options; |
| 121 | + |
| 122 | + const clientFetch = getClientFetch(client); |
| 123 | + const url = new URL(`${getThirdwebBaseUrl("bridge")}/v1/tokens`); |
| 124 | + |
| 125 | + if (chainId !== null && chainId !== undefined) { |
| 126 | + url.searchParams.set("chainId", chainId.toString()); |
| 127 | + } |
| 128 | + if (tokenAddress) { |
| 129 | + url.searchParams.set("tokenAddress", tokenAddress); |
| 130 | + } |
| 131 | + if (symbol) { |
| 132 | + url.searchParams.set("symbol", symbol); |
| 133 | + } |
| 134 | + if (name) { |
| 135 | + url.searchParams.set("name", name); |
| 136 | + } |
| 137 | + if (limit !== undefined) { |
| 138 | + url.searchParams.set("limit", limit.toString()); |
| 139 | + } |
| 140 | + if (offset !== null && offset !== undefined) { |
| 141 | + url.searchParams.set("offset", offset.toString()); |
| 142 | + } |
| 143 | + |
| 144 | + const response = await clientFetch(url.toString()); |
| 145 | + if (!response.ok) { |
| 146 | + const errorJson = await response.json(); |
| 147 | + throw new ApiError({ |
| 148 | + code: errorJson.code || "UNKNOWN_ERROR", |
| 149 | + message: errorJson.message || response.statusText, |
| 150 | + correlationId: errorJson.correlationId || undefined, |
| 151 | + statusCode: response.status, |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + const { data }: { data: Token[] } = await response.json(); |
| 156 | + return data; |
| 157 | +} |
| 158 | + |
| 159 | +export declare namespace tokens { |
| 160 | + /** |
| 161 | + * Input parameters for {@link Bridge.tokens}. |
| 162 | + */ |
| 163 | + type Options = { |
| 164 | + /** Your {@link ThirdwebClient} instance. */ |
| 165 | + client: ThirdwebClient; |
| 166 | + /** Filter by a specific chain ID. */ |
| 167 | + chainId?: number | null; |
| 168 | + /** Filter by a specific token address. */ |
| 169 | + tokenAddress?: string; |
| 170 | + /** Filter by token symbol. */ |
| 171 | + symbol?: string; |
| 172 | + /** Filter by token name. */ |
| 173 | + name?: string; |
| 174 | + /** Number of tokens to return (min: 1, default: 100). */ |
| 175 | + limit?: number; |
| 176 | + /** Number of tokens to skip (min: 0, default: 0). */ |
| 177 | + offset?: number | null; |
| 178 | + }; |
| 179 | + |
| 180 | + /** |
| 181 | + * The result returned from {@link Bridge.tokens}. |
| 182 | + */ |
| 183 | + type Result = Token[]; |
| 184 | +} |
0 commit comments