diff --git a/.changeset/bright-colts-drive.md b/.changeset/bright-colts-drive.md new file mode 100644 index 0000000..28d8096 --- /dev/null +++ b/.changeset/bright-colts-drive.md @@ -0,0 +1,5 @@ +--- +"@across-protocol/app-sdk": patch +--- + +add getSwapTokens action diff --git a/packages/sdk/src/actions/getSwapTokens.ts b/packages/sdk/src/actions/getSwapTokens.ts new file mode 100644 index 0000000..1ad924f --- /dev/null +++ b/packages/sdk/src/actions/getSwapTokens.ts @@ -0,0 +1,60 @@ +import { Address } from "viem"; +import { LoggerT, fetchAcrossApi } from "../utils/index.js"; +import { MAINNET_API_URL } from "../constants/index.js"; + +export type SwapApiToken = { + chainId: number; + address: Address; + name: string; + symbol: string; + decimals: number; + logoUrl: string; + priceUsd: string | null; +}; + +type SwapApiTokensResponse = SwapApiToken[]; + +/** + * Params for {@link getSwapTokens}. + */ +export type GetSwapTokensParams = Partial<{ + /** + * [Optional] Filter tokens by chain ID. If provided, only tokens from this chain will be returned. + */ + chainId: number; + /** + * [Optional] The Across API URL to use. Defaults to the mainnet API URL. + */ + apiUrl: string; + /** + * [Optional] The logger to use. + */ + logger: LoggerT; +}>; + +export type GetSwapTokensReturnType = SwapApiToken[]; + +/** + * Get available tokens across all supported chains or filtered by chain ID. + * @param params - See {@link GetSwapTokensParams}. + * @returns See {@link GetSwapTokensReturnType}. + * @public + */ +export async function getSwapTokens({ + chainId, + apiUrl = MAINNET_API_URL, + logger, +}: GetSwapTokensParams = {}): Promise { + const tokens = await fetchAcrossApi( + `${apiUrl}/swap/tokens`, + {}, + logger, + ); + + // Apply client-side chainId filtering if specified + if (chainId) { + return tokens.filter((token) => token.chainId === chainId); + } + + return tokens; +} \ No newline at end of file diff --git a/packages/sdk/src/actions/index.ts b/packages/sdk/src/actions/index.ts index 747b1c9..d46eb7d 100644 --- a/packages/sdk/src/actions/index.ts +++ b/packages/sdk/src/actions/index.ts @@ -2,6 +2,7 @@ export * from "./getSuggestedFees.js"; export * from "./getAvailableRoutes.js"; export * from "./getLimits.js"; export * from "./getQuote.js"; +export * from "./getSwapTokens.js"; export * from "./simulateDepositTx.js"; export * from "./waitForDepositTx.js"; export * from "./getFillByDepositTx.js"; diff --git a/packages/sdk/src/client.ts b/packages/sdk/src/client.ts index f450505..ffc886e 100644 --- a/packages/sdk/src/client.ts +++ b/packages/sdk/src/client.ts @@ -10,6 +10,7 @@ import { getSuggestedFees, getLimits, getQuote, + getSwapTokens, waitForDepositTx, getDeposit, getFillByDepositTx, @@ -20,6 +21,8 @@ import { GetSuggestedFeesReturnType, GetLimitsParams, GetLimitsReturnType, + GetSwapTokensParams, + GetSwapTokensReturnType, simulateDepositTx, SimulateDepositTxParams, waitForFillTx, @@ -375,6 +378,21 @@ export class AcrossClient { }); } + /** + * Get available tokens from the swap API. See {@link getSwapTokens}. + * @param params - See {@link GetSwapTokensParams}. + * @returns See {@link GetSwapTokensReturnType}. + */ + async getSwapTokens( + params: MakeOptional = {}, + ): Promise { + return getSwapTokens({ + ...params, + apiUrl: params?.apiUrl || this.apiUrl, + logger: params?.logger ?? this.logger, + }); + } + /** * Get the suggested fees for a given route. See {@link getSuggestedFees}. * @param params - See {@link GetSuggestedFeesParams}.