Skip to content

Commit 4d33487

Browse files
feat: add getSwapTokens action (#234)
* feat: add getSwapApiTokens action * rename to getSwapTokens * changeset
1 parent 6689aee commit 4d33487

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

.changeset/bright-colts-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@across-protocol/app-sdk": patch
3+
---
4+
5+
add getSwapTokens action
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Address } from "viem";
2+
import { LoggerT, fetchAcrossApi } from "../utils/index.js";
3+
import { MAINNET_API_URL } from "../constants/index.js";
4+
5+
export type SwapApiToken = {
6+
chainId: number;
7+
address: Address;
8+
name: string;
9+
symbol: string;
10+
decimals: number;
11+
logoUrl: string;
12+
priceUsd: string | null;
13+
};
14+
15+
type SwapApiTokensResponse = SwapApiToken[];
16+
17+
/**
18+
* Params for {@link getSwapTokens}.
19+
*/
20+
export type GetSwapTokensParams = Partial<{
21+
/**
22+
* [Optional] Filter tokens by chain ID. If provided, only tokens from this chain will be returned.
23+
*/
24+
chainId: number;
25+
/**
26+
* [Optional] The Across API URL to use. Defaults to the mainnet API URL.
27+
*/
28+
apiUrl: string;
29+
/**
30+
* [Optional] The logger to use.
31+
*/
32+
logger: LoggerT;
33+
}>;
34+
35+
export type GetSwapTokensReturnType = SwapApiToken[];
36+
37+
/**
38+
* Get available tokens across all supported chains or filtered by chain ID.
39+
* @param params - See {@link GetSwapTokensParams}.
40+
* @returns See {@link GetSwapTokensReturnType}.
41+
* @public
42+
*/
43+
export async function getSwapTokens({
44+
chainId,
45+
apiUrl = MAINNET_API_URL,
46+
logger,
47+
}: GetSwapTokensParams = {}): Promise<GetSwapTokensReturnType> {
48+
const tokens = await fetchAcrossApi<SwapApiTokensResponse>(
49+
`${apiUrl}/swap/tokens`,
50+
{},
51+
logger,
52+
);
53+
54+
// Apply client-side chainId filtering if specified
55+
if (chainId) {
56+
return tokens.filter((token) => token.chainId === chainId);
57+
}
58+
59+
return tokens;
60+
}

packages/sdk/src/actions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./getSuggestedFees.js";
22
export * from "./getAvailableRoutes.js";
33
export * from "./getLimits.js";
44
export * from "./getQuote.js";
5+
export * from "./getSwapTokens.js";
56
export * from "./simulateDepositTx.js";
67
export * from "./waitForDepositTx.js";
78
export * from "./getFillByDepositTx.js";

packages/sdk/src/client.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getSuggestedFees,
1111
getLimits,
1212
getQuote,
13+
getSwapTokens,
1314
waitForDepositTx,
1415
getDeposit,
1516
getFillByDepositTx,
@@ -20,6 +21,8 @@ import {
2021
GetSuggestedFeesReturnType,
2122
GetLimitsParams,
2223
GetLimitsReturnType,
24+
GetSwapTokensParams,
25+
GetSwapTokensReturnType,
2326
simulateDepositTx,
2427
SimulateDepositTxParams,
2528
waitForFillTx,
@@ -375,6 +378,21 @@ export class AcrossClient {
375378
});
376379
}
377380

381+
/**
382+
* Get available tokens from the swap API. See {@link getSwapTokens}.
383+
* @param params - See {@link GetSwapTokensParams}.
384+
* @returns See {@link GetSwapTokensReturnType}.
385+
*/
386+
async getSwapTokens(
387+
params: MakeOptional<GetSwapTokensParams, "apiUrl" | "logger"> = {},
388+
): Promise<GetSwapTokensReturnType> {
389+
return getSwapTokens({
390+
...params,
391+
apiUrl: params?.apiUrl || this.apiUrl,
392+
logger: params?.logger ?? this.logger,
393+
});
394+
}
395+
378396
/**
379397
* Get the suggested fees for a given route. See {@link getSuggestedFees}.
380398
* @param params - See {@link GetSuggestedFeesParams}.

0 commit comments

Comments
 (0)