|
| 1 | +import type { Hex as ox__Hex, Address as ox__Address } from "ox"; |
| 2 | +import type { ThirdwebClient } from "../client/client.js"; |
| 3 | +import { getClientFetch } from "../utils/fetch.js"; |
| 4 | +import { UNIVERSAL_BRIDGE_URL } from "./constants.js"; |
| 5 | +import type { Route } from "./types/Route.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Retrieves supported Universal Bridge routes based on the provided filters. |
| 9 | + * |
| 10 | + * When multiple filters are specified, a route must satisfy all filters to be included (it acts as an AND operator). |
| 11 | + * |
| 12 | + * @example |
| 13 | + * ```typescript |
| 14 | + * import { Universal } from "thirdweb"; |
| 15 | + * |
| 16 | + * const routes = await Universal.routes({ |
| 17 | + * client: thirdwebClient, |
| 18 | + * }); |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * Returned routes might look something like: |
| 22 | + * ```typescript |
| 23 | + * [ |
| 24 | + * { |
| 25 | + * destinationToken: { |
| 26 | + * address: "0x12c88a3C30A7AaBC1dd7f2c08a97145F5DCcD830", |
| 27 | + * chainId: 1, |
| 28 | + * decimals: 18, |
| 29 | + * iconUri: "https://assets.coingecko.com/coins/images/37207/standard/G.jpg", |
| 30 | + * name: "G7", |
| 31 | + * symbol: "G7", |
| 32 | + * }, |
| 33 | + * originToken: { |
| 34 | + * address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", |
| 35 | + * chainId: 480, |
| 36 | + * decimals: 18, |
| 37 | + * iconUri: "https://assets.relay.link/icons/1/light.png", |
| 38 | + * name: "Ether", |
| 39 | + * symbol: "ETH", |
| 40 | + * } |
| 41 | + * }, |
| 42 | + * { |
| 43 | + * destinationToken: { |
| 44 | + * address: "0x4d224452801ACEd8B2F0aebE155379bb5D594381", |
| 45 | + * chainId: 1, |
| 46 | + * decimals: 18, |
| 47 | + * iconUri: "https://coin-images.coingecko.com/coins/images/24383/large/apecoin.jpg?1696523566", |
| 48 | + * name: "ApeCoin", |
| 49 | + * symbol: "APE", |
| 50 | + * }, |
| 51 | + * originToken: { |
| 52 | + * address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", |
| 53 | + * chainId: 480, |
| 54 | + * decimals: 18, |
| 55 | + * iconUri: "https://assets.relay.link/icons/1/light.png", |
| 56 | + * name: "Ether", |
| 57 | + * symbol: "ETH", |
| 58 | + * } |
| 59 | + * } |
| 60 | + * ] |
| 61 | + * ``` |
| 62 | + * |
| 63 | + * You can filter for specific chains or tokens: |
| 64 | + * ```typescript |
| 65 | + * import { Universal } from "thirdweb"; |
| 66 | + * |
| 67 | + * // Get all routes starting from mainnet ETH |
| 68 | + * const routes = await Universal.routes({ |
| 69 | + * originChainId: 1, |
| 70 | + * originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", |
| 71 | + * client: thirdwebClient, |
| 72 | + * }); |
| 73 | + * ``` |
| 74 | + * |
| 75 | + * The returned routes will be limited based on the API. You can paginate through the results using the `limit` and `offset` parameters: |
| 76 | + * ```typescript |
| 77 | + * import { Universal } from "thirdweb"; |
| 78 | + * |
| 79 | + * // Get the first 10 routes starting from mainnet ETH |
| 80 | + * const routes = await Universal.routes({ |
| 81 | + * originChainId: 1, |
| 82 | + * originTokenAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", |
| 83 | + * limit: 10, |
| 84 | + * offset: 0, |
| 85 | + * client: thirdwebClient, |
| 86 | + * }); |
| 87 | + * ``` |
| 88 | + * |
| 89 | + * @param options - The options for the quote. |
| 90 | + * @param options.client - Your thirdweb client. |
| 91 | + * @param options.originChainId - Filter by a specific origin chain ID. |
| 92 | + * @param options.originTokenAddress - Filter by a specific origin token address. |
| 93 | + * @param options.destinationChainId - Filter by a specific destination chain ID. |
| 94 | + * @param options.destinationTokenAddress - Filter by a specific destination token address. |
| 95 | + * @param options.transactionHash - Filter by a specific transaction hash. |
| 96 | + * @param options.limit - Limit the number of routes returned. |
| 97 | + * @param options.offset - Offset the number of routes returned. |
| 98 | + * |
| 99 | + * @returns A promise that resolves to an array of routes. |
| 100 | + * |
| 101 | + * @throws Will throw an error if there is an issue fetching the routes. |
| 102 | + */ |
| 103 | +export async function routes(options: routes.Options): Promise<routes.Result> { |
| 104 | + const { |
| 105 | + client, |
| 106 | + originChainId, |
| 107 | + originTokenAddress, |
| 108 | + destinationChainId, |
| 109 | + destinationTokenAddress, |
| 110 | + limit, |
| 111 | + offset, |
| 112 | + } = options; |
| 113 | + |
| 114 | + const clientFetch = getClientFetch(client); |
| 115 | + const url = new URL(`${UNIVERSAL_BRIDGE_URL}/routes`); |
| 116 | + if (originChainId) { |
| 117 | + url.searchParams.set("originChainId", originChainId.toString()); |
| 118 | + } |
| 119 | + if (originTokenAddress) { |
| 120 | + url.searchParams.set("originTokenAddress", originTokenAddress); |
| 121 | + } |
| 122 | + if (destinationChainId) { |
| 123 | + url.searchParams.set("destinationChainId", destinationChainId.toString()); |
| 124 | + } |
| 125 | + if (destinationTokenAddress) { |
| 126 | + url.searchParams.set("destinationTokenAddress", destinationTokenAddress); |
| 127 | + } |
| 128 | + if (limit) { |
| 129 | + url.searchParams.set("limit", limit.toString()); |
| 130 | + } |
| 131 | + if (offset) { |
| 132 | + url.searchParams.set("offset", offset.toString()); |
| 133 | + } |
| 134 | + |
| 135 | + const response = await clientFetch(url.toString()); |
| 136 | + if (!response.ok) { |
| 137 | + const errorJson = await response.json(); |
| 138 | + throw new Error(`${errorJson.code}: ${errorJson.message}`); |
| 139 | + } |
| 140 | + |
| 141 | + const { data }: { data: Route[] } = await response.json(); |
| 142 | + return data; |
| 143 | +} |
| 144 | + |
| 145 | +export declare namespace routes { |
| 146 | + type Options = { |
| 147 | + client: ThirdwebClient; |
| 148 | + originChainId?: number; |
| 149 | + originTokenAddress?: ox__Address.Address; |
| 150 | + destinationChainId?: number; |
| 151 | + destinationTokenAddress?: ox__Address.Address; |
| 152 | + transactionHash?: ox__Hex.Hex; |
| 153 | + limit?: number; |
| 154 | + offset?: number; |
| 155 | + }; |
| 156 | + |
| 157 | + type Result = Route[]; |
| 158 | +} |
0 commit comments