| 8 | } from "./schemas"; |
| 9 | |
| 10 | export class CrossChainClient { |
| 11 | private readonly apiKey: string; |
| 12 | private readonly baseUrl: string; |
| 13 | |
| 14 | constructor(apiKey: string, baseUrl = "https://api.0x.org") { |
| 15 | this.apiKey = apiKey; |
| 16 | this.baseUrl = baseUrl; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Get multiple quotes for a cross-chain swap |
| 21 | */ |
| 22 | async getQuotes( |
| 23 | request: CrossChainQuotesRequest |
| 24 | ): Promise<CrossChainQuotesResponse> { |
| 25 | const url = new URL("/cross-chain/quotes", this.baseUrl); |
| 26 | |
| 27 | // Add query parameters |
| 28 | Object.entries(request).forEach(([key, value]) => { |
| 29 | if (value !== undefined) { |
| 30 | url.searchParams.append(key, value.toString()); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | const response = await fetch(url.toString(), { |
| 35 | method: "GET", |
| 36 | headers: { |
| 37 | "0x-api-key": this.apiKey, |
| 38 | "Content-Type": "application/json", |
| 39 | }, |
| 40 | }); |
| 41 | |
| 42 | if (!response.ok) { |
| 43 | const errorText = await response.text(); |
| 44 | throw new Error( |
| 45 | `Failed to fetch quotes: ${response.status} ${response.statusText}\n${errorText}` |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | const data = await response.json(); |
| 50 | return CrossChainQuotesResponseSchema.parse(data); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the status of a cross-chain transaction |
| 55 | */ |
| 56 | async getStatus( |
| 57 | request: CrossChainStatusRequest |
| 58 | ): Promise<CrossChainStatusResponse> { |
| 59 | const url = new URL("/cross-chain/status", this.baseUrl); |
| 60 | |
| 61 | // Add query parameters |
| 62 | Object.entries(request).forEach(([key, value]) => { |
| 63 | if (value !== undefined) { |
| 64 | url.searchParams.append(key, value.toString()); |
| 65 | } |
| 66 | }); |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected