-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathToken.ts
More file actions
307 lines (291 loc) · 8.3 KB
/
Copy pathToken.ts
File metadata and controls
307 lines (291 loc) · 8.3 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
import type { ThirdwebClient } from "../client/client.js";
import { getThirdwebBaseUrl } from "../utils/domains.js";
import { getClientFetch } from "../utils/fetch.js";
import { ApiError } from "./types/Errors.js";
import type { Token, TokenWithPrices } from "./types/Token.js";
/**
* Retrieves supported Bridge tokens based on the provided filters.
*
* When multiple filters are specified, a token must satisfy all filters to be included (it acts as an AND operator).
*
* @example
* ```typescript
* import { Bridge } from "thirdweb";
*
* const tokens = await Bridge.tokens({
* client: thirdwebClient,
* chainId: 1,
* });
* ```
*
* Returned tokens might look something like:
* ```typescript
* [
* {
* chainId: 1,
* address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
* decimals: 18,
* symbol: "ETH",
* name: "Ethereum",
* iconUri: "https://assets.relay.link/icons/1/light.png",
* priceUsd: 2000.50,
* prices: {
* USD: 2000.50,
* EUR: 1800.00,
* GBP: 1500.00,
* JPY: 10000.00
* }
* },
* {
* chainId: 1,
* address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
* decimals: 6,
* symbol: "USDC",
* name: "USD Coin",
* iconUri: "https://assets.coingecko.com/coins/images/6319/large/USD_Coin_icon.png",
* priceUsd: 1.00,
* prices: {
* USD: 1.00,
* EUR: 0.84,
* GBP: 0.73,
* JPY: 120.00
* }
* }
* ]
* ```
*
* You can filter for specific chains or tokens:
* ```typescript
* import { Bridge } from "thirdweb";
*
* // Get all tokens on Ethereum mainnet
* const ethTokens = await Bridge.tokens({
* chainId: 1,
* client: thirdwebClient,
* });
* ```
*
* You can search for tokens by symbol or name:
* ```typescript
* import { Bridge } from "thirdweb";
*
* // Search for USDC tokens
* const usdcTokens = await Bridge.tokens({
* symbol: "USDC",
* client: thirdwebClient,
* });
*
* // Search for tokens by name
* const ethereumTokens = await Bridge.tokens({
* name: "Ethereum",
* client: thirdwebClient,
* });
* ```
*
* You can filter by a specific token address:
* ```typescript
* import { Bridge } from "thirdweb";
*
* // Get a specific token
* const token = await Bridge.tokens({
* tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
* client: thirdwebClient,
* });
* ```
*
* The returned tokens will be limited based on the API. You can paginate through the results using the `limit` and `offset` parameters:
* ```typescript
* import { Bridge } from "thirdweb";
*
* // Get the first 50 tokens
* const tokens = await Bridge.tokens({
* limit: 50,
* offset: 0,
* client: thirdwebClient,
* });
*
* // Get the next 50 tokens
* const nextTokens = await Bridge.tokens({
* limit: 50,
* offset: 50,
* client: thirdwebClient,
* });
* ```
*
* @param options - The options for retrieving tokens.
* @param options.client - Your thirdweb client.
* @param options.chainId - Filter by a specific chain ID.
* @param options.tokenAddress - Filter by a specific token address.
* @param options.symbol - Filter by token symbol.
* @param options.name - Filter by token name.
* @param options.limit - Number of tokens to return (min: 1, default: 100).
* @param options.offset - Number of tokens to skip (min: 0, default: 0).
*
* @returns A promise that resolves to an array of tokens.
*
* @throws Will throw an error if there is an issue fetching the tokens.
* @bridge
* @beta
*/
export async function tokens<
IncludePrices extends boolean = true,
R extends Token | TokenWithPrices = TokenWithPrices,
>(options: tokens.Options<IncludePrices>): Promise<R[]> {
const {
client,
chainId,
tokenAddress,
symbol,
name,
limit,
offset,
includePrices,
sortBy,
query,
} = options;
const clientFetch = getClientFetch(client);
const url = new URL(`${getThirdwebBaseUrl("bridge")}/v1/tokens`);
if (chainId !== null && chainId !== undefined) {
url.searchParams.set("chainId", chainId.toString());
}
if (tokenAddress) {
url.searchParams.set("tokenAddress", tokenAddress);
}
if (symbol) {
url.searchParams.set("symbol", symbol);
}
if (name) {
url.searchParams.set("name", name);
}
if (limit !== undefined) {
url.searchParams.set("limit", limit.toString());
}
if (offset !== null && offset !== undefined) {
url.searchParams.set("offset", offset.toString());
}
if (includePrices !== undefined) {
url.searchParams.set("includePrices", includePrices.toString());
}
if (sortBy !== undefined) {
url.searchParams.set("sortBy", sortBy);
}
if (query !== undefined) {
url.searchParams.set("query", query);
}
const response = await clientFetch(url.toString());
if (!response.ok) {
const errorJson = await response.json();
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
correlationId: errorJson.correlationId || undefined,
message: errorJson.message || response.statusText,
statusCode: response.status,
});
}
const { data }: { data: R[] } = await response.json();
return data;
}
export declare namespace tokens {
/**
* Input parameters for {@link tokens}.
*/
type Options<IncludePrices extends boolean> = {
/** Your {@link ThirdwebClient} instance. */
client: ThirdwebClient;
/** Filter by a specific chain ID. */
chainId?: number | null;
/** Filter by a specific token address. */
tokenAddress?: string;
/** Filter by token symbol. */
symbol?: string;
/** Filter by token name. */
name?: string;
/** Number of tokens to return (min: 1, default: 100). */
limit?: number;
/** Number of tokens to skip (min: 0, default: 0). */
offset?: number | null;
/** Whether or not to include prices for the tokens. Setting this to false will speed up the request. */
includePrices?: IncludePrices;
/** Sort by a specific field. */
sortBy?: "newest" | "oldest" | "volume" | "market_cap";
/** search for tokens by token name or symbol */
query?: string;
};
/**
* The result returned from {@link Bridge.tokens}.
*/
type Result<T extends Token | TokenWithPrices> = T[];
}
/**
* Adds a token to the Bridge for indexing.
*
* This function requests the Bridge to index a specific token on a given chain.
* Once indexed, the token will be available for cross-chain operations.
*
* @example
* ```typescript
* import { Bridge } from "thirdweb";
*
* // Add a token for indexing
* const result = await Bridge.add({
* client: thirdwebClient,
* chainId: 1,
* tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
* });
* ```
*
* @param options - The options for adding a token.
* @param options.client - Your thirdweb client.
* @param options.chainId - The chain ID where the token is deployed.
* @param options.tokenAddress - The contract address of the token to add.
*
* @returns A promise that resolves when the token has been successfully submitted for indexing.
*
* @throws Will throw an error if there is an issue adding the token.
* @bridge
* @beta
*/
export async function add(options: add.Options): Promise<add.Result> {
const { client, chainId, tokenAddress } = options;
const clientFetch = getClientFetch(client);
const url = `${getThirdwebBaseUrl("bridge")}/v1/tokens`;
const requestBody = {
chainId,
tokenAddress,
};
const response = await clientFetch(url, {
body: JSON.stringify(requestBody),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
if (!response.ok) {
const errorJson = await response.json();
throw new ApiError({
code: errorJson.code || "UNKNOWN_ERROR",
correlationId: errorJson.correlationId || undefined,
message: errorJson.message || response.statusText,
statusCode: response.status,
});
}
const { data }: { data: TokenWithPrices } = await response.json();
return data;
}
export declare namespace add {
/**
* Input parameters for {@link add}.
*/
type Options = {
/** Your {@link ThirdwebClient} instance. */
client: ThirdwebClient;
/** The chain ID where the token is deployed. */
chainId: number;
/** The contract address of the token to add. */
tokenAddress: string;
};
/**
* The result returned from {@link Bridge.add}.
*/
type Result = TokenWithPrices;
}