Skip to content

Commit 50c6371

Browse files
authored
[SDK] Feature: add ability to exclude token prices from Bridge.tokens (#7942)
1 parent 93532ec commit 50c6371

File tree

16 files changed

+99
-45
lines changed

16 files changed

+99
-45
lines changed

.changeset/ripe-teeth-fold.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+
Adds the ability to exclude prices from Bridge.tokens

packages/thirdweb/src/bridge/Onramp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getClientFetch } from "../utils/fetch.js";
77
import { stringify } from "../utils/json.js";
88
import { ApiError } from "./types/Errors.js";
99
import type { RouteStep } from "./types/Route.js";
10-
import type { Token } from "./types/Token.js";
10+
import type { TokenWithPrices } from "./types/Token.js";
1111

1212
// export status within the Onramp module
1313
export { status } from "./OnrampStatus.js";
@@ -33,7 +33,7 @@ type OnrampPrepareQuoteResponseData = {
3333
currency: string;
3434
currencyAmount: number;
3535
destinationAmount: bigint;
36-
destinationToken: Token;
36+
destinationToken: TokenWithPrices;
3737
timestamp?: number;
3838
expiration?: number;
3939
steps: RouteStep[];

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,37 @@ describe.runIf(process.env.TW_SECRET_KEY)("tokens", () => {
2222
expect(token).toHaveProperty("decimals");
2323
expect(token).toHaveProperty("symbol");
2424
expect(token).toHaveProperty("name");
25-
expect(token).toHaveProperty("priceUsd");
25+
expect(token).toHaveProperty("prices");
2626

2727
if (token) {
2828
expect(typeof token.chainId).toBe("number");
2929
expect(typeof token.address).toBe("string");
3030
expect(typeof token.decimals).toBe("number");
3131
expect(typeof token.symbol).toBe("string");
3232
expect(typeof token.name).toBe("string");
33-
expect(typeof token.priceUsd).toBe("number");
3433
}
3534
}
3635
});
3736

37+
it("should exclude prices if includePrices is false", async () => {
38+
// Setup
39+
const client = TEST_CLIENT;
40+
41+
// Test
42+
const result = await tokens({
43+
client,
44+
includePrices: false,
45+
});
46+
47+
// Verify
48+
expect(result).toBeInstanceOf(Array);
49+
50+
// All tokens should not have prices
51+
for (const token of result) {
52+
expect(token.prices).toBeUndefined();
53+
}
54+
});
55+
3856
it("should filter tokens by chainId", async () => {
3957
// Setup
4058
const client = TEST_CLIENT;

packages/thirdweb/src/bridge/Token.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ThirdwebClient } from "../client/client.js";
22
import { getThirdwebBaseUrl } from "../utils/domains.js";
33
import { getClientFetch } from "../utils/fetch.js";
44
import { ApiError } from "./types/Errors.js";
5-
import type { Token } from "./types/Token.js";
5+
import type { Token, TokenWithPrices } from "./types/Token.js";
66

77
/**
88
* Retrieves supported Universal Bridge tokens based on the provided filters.
@@ -128,9 +128,20 @@ import type { Token } from "./types/Token.js";
128128
* @bridge
129129
* @beta
130130
*/
131-
export async function tokens(options: tokens.Options): Promise<tokens.Result> {
132-
const { client, chainId, tokenAddress, symbol, name, limit, offset } =
133-
options;
131+
export async function tokens<
132+
IncludePrices extends boolean = true,
133+
R extends Token | TokenWithPrices = TokenWithPrices,
134+
>(options: tokens.Options<IncludePrices>): Promise<R[]> {
135+
const {
136+
client,
137+
chainId,
138+
tokenAddress,
139+
symbol,
140+
name,
141+
limit,
142+
offset,
143+
includePrices,
144+
} = options;
134145

135146
const clientFetch = getClientFetch(client);
136147
const url = new URL(`${getThirdwebBaseUrl("bridge")}/v1/tokens`);
@@ -153,6 +164,9 @@ export async function tokens(options: tokens.Options): Promise<tokens.Result> {
153164
if (offset !== null && offset !== undefined) {
154165
url.searchParams.set("offset", offset.toString());
155166
}
167+
if (includePrices !== undefined) {
168+
url.searchParams.set("includePrices", includePrices.toString());
169+
}
156170

157171
const response = await clientFetch(url.toString());
158172
if (!response.ok) {
@@ -165,15 +179,15 @@ export async function tokens(options: tokens.Options): Promise<tokens.Result> {
165179
});
166180
}
167181

168-
const { data }: { data: Token[] } = await response.json();
182+
const { data }: { data: R[] } = await response.json();
169183
return data;
170184
}
171185

172186
export declare namespace tokens {
173187
/**
174188
* Input parameters for {@link tokens}.
175189
*/
176-
type Options = {
190+
type Options<IncludePrices extends boolean> = {
177191
/** Your {@link ThirdwebClient} instance. */
178192
client: ThirdwebClient;
179193
/** Filter by a specific chain ID. */
@@ -188,12 +202,14 @@ export declare namespace tokens {
188202
limit?: number;
189203
/** Number of tokens to skip (min: 0, default: 0). */
190204
offset?: number | null;
205+
/** Whether or not to include prices for the tokens. Setting this to false will speed up the request. */
206+
includePrices?: IncludePrices;
191207
};
192208

193209
/**
194210
* The result returned from {@link Bridge.tokens}.
195211
*/
196-
type Result = Token[];
212+
type Result<T extends Token | TokenWithPrices> = T[];
197213
}
198214

199215
/**
@@ -254,7 +270,7 @@ export async function add(options: add.Options): Promise<add.Result> {
254270
});
255271
}
256272

257-
const { data }: { data: Token } = await response.json();
273+
const { data }: { data: TokenWithPrices } = await response.json();
258274
return data;
259275
}
260276

@@ -274,5 +290,5 @@ export declare namespace add {
274290
/**
275291
* The result returned from {@link Bridge.add}.
276292
*/
277-
type Result = Token;
293+
type Result = TokenWithPrices;
278294
}

packages/thirdweb/src/bridge/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type {
1717
RouteTransaction,
1818
} from "./types/Route.js";
1919
export type { Status } from "./types/Status.js";
20-
export type { Token } from "./types/Token.js";
20+
export type { Token, TokenWithPrices } from "./types/Token.js";
2121
export type { WebhookPayload } from "./Webhook.js";
2222
export * as Webhook from "./Webhook.js";
2323
export { parse } from "./Webhook.js";

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import type { Hex as ox__Hex } from "ox";
22
import type { Chain } from "../../chains/types.js";
33
import type { ThirdwebClient } from "../../client/client.js";
44
import type { Action } from "./BridgeAction.js";
5-
import type { Token } from "./Token.js";
5+
import type { TokenWithPrices } from "./Token.js";
66

77
export type Route = {
8-
originToken: Token;
9-
destinationToken: Token;
8+
originToken: TokenWithPrices;
9+
destinationToken: TokenWithPrices;
1010
};
1111

1212
export type RouteQuoteStep = {
13-
originToken: Token;
14-
destinationToken: Token;
13+
originToken: TokenWithPrices;
14+
destinationToken: TokenWithPrices;
1515
originAmount: bigint;
1616
destinationAmount: bigint;
1717
estimatedExecutionTimeMs: number;
1818
};
1919

2020
export type RouteStep = {
21-
originToken: Token;
22-
destinationToken: Token;
21+
originToken: TokenWithPrices;
22+
destinationToken: TokenWithPrices;
2323
originAmount: bigint;
2424
destinationAmount: bigint;
2525
estimatedExecutionTimeMs: number;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ export type Token = {
77
symbol: string;
88
name: string;
99
iconUri?: string;
10+
};
11+
12+
export type TokenWithPrices = Token & {
1013
prices: Record<string, number>;
1114
};

packages/thirdweb/src/pay/convert/get-token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { add, tokens } from "../../bridge/Token.js";
2-
import type { Token } from "../../bridge/types/Token.js";
2+
import type { TokenWithPrices } from "../../bridge/types/Token.js";
33
import type { ThirdwebClient } from "../../client/client.js";
44
import { withCache } from "../../utils/promise/withCache.js";
55

66
export async function getToken(
77
client: ThirdwebClient,
88
tokenAddress: string,
99
chainId: number,
10-
): Promise<Token> {
10+
): Promise<TokenWithPrices> {
1111
return withCache(
1212
async () => {
1313
const result = await tokens({

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useQuery } from "@tanstack/react-query";
22
import type { Quote } from "../../../bridge/index.js";
33
import { ApiError } from "../../../bridge/types/Errors.js";
4-
import type { Token } from "../../../bridge/types/Token.js";
4+
import type { Token, TokenWithPrices } from "../../../bridge/types/Token.js";
55
import type { ThirdwebClient } from "../../../client/client.js";
66
import { getThirdwebBaseUrl } from "../../../utils/domains.js";
77
import { getClientFetch } from "../../../utils/fetch.js";
@@ -82,7 +82,7 @@ export function usePaymentMethods(options: {
8282

8383
const {
8484
data: allValidOriginTokens,
85-
}: { data: { quote: Quote; balance: string; token: Token }[] } =
85+
}: { data: { quote: Quote; balance: string; token: TokenWithPrices }[] } =
8686
await response.json();
8787

8888
// Sort by enough balance to pay THEN gross balance

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useQuery } from "@tanstack/react-query";
22
import type { AbiFunction } from "abitype";
33
import { toFunctionSelector } from "viem";
4-
import type { Token } from "../../../bridge/index.js";
4+
import type { TokenWithPrices } from "../../../bridge/index.js";
55
import type { ThirdwebClient } from "../../../client/client.js";
66
import { NATIVE_TOKEN_ADDRESS } from "../../../constants/addresses.js";
77
import type { CompilerMetadata } from "../../../contract/actions/compiler-metadata.js";
@@ -34,7 +34,7 @@ interface TransactionDetails {
3434
usdValueDisplay: string | null;
3535
txCostDisplay: string;
3636
gasCostDisplay: string | null;
37-
tokenInfo: Token | null;
37+
tokenInfo: TokenWithPrices | null;
3838
costWei: bigint;
3939
gasCostWei: bigint | null;
4040
totalCost: string;

0 commit comments

Comments
 (0)