-
Notifications
You must be signed in to change notification settings - Fork 67
feat: cross chain swaps gateway client #892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 19 commits
25630c2
2086ef6
9793cbd
680b54f
f43b22b
c839017
88e4bdc
aa828c8
5b2d178
2d4213f
e92d0a1
bebd700
02747af
94f04ea
0868fa4
e32e098
3698746
cc66325
f958e79
d2f2670
dcd4f72
d88e0e9
7f76a95
7ede063
bbfc1f4
b7fac7c
d744ee1
75c0500
be6007b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { Chain } from 'viem'; | ||
| import { | ||
| ActionsParams, | ||
| ActionsResponse, | ||
| PathsParams, | ||
| PathsResponse, | ||
| StatusParams, | ||
| StatusResponse, | ||
| TransactionParams, | ||
| TransactionResponse, | ||
| } from './types'; | ||
|
|
||
| export class SwapsClient { | ||
| private basePath: string; | ||
|
|
||
| constructor() { | ||
| this.basePath = 'https://box-api-git-zev-swaps-v2chains-decent-webapp.vercel.app/api/'; | ||
| } | ||
|
|
||
| async getAction(params: ActionsParams): Promise<ActionsResponse> { | ||
| const url = new URL('getAction', this.basePath); | ||
|
|
||
| // Convert params to URLSearchParams-compatible format | ||
| const searchParams = new URLSearchParams(); | ||
| Object.entries(params).forEach(([key, value]) => { | ||
| if (value === undefined || value === null) { | ||
| return; // Skip undefined/null values | ||
| } | ||
| if (Array.isArray(value)) { | ||
| // Handle arrays (e.g., bridgeIds) | ||
| value.forEach((item) => searchParams.append(key, String(item))); | ||
| } else { | ||
| // Convert all values to strings | ||
| searchParams.append(key, String(value)); | ||
| } | ||
| }); | ||
|
|
||
| url.search = searchParams.toString(); | ||
| return this.getJson(url.toString()); | ||
| } | ||
|
|
||
| async getStatus(params: StatusParams): Promise<StatusResponse> { | ||
| const url = new URL('getStatus', this.basePath); | ||
| url.search = new URLSearchParams(params as unknown as Record<string, string>).toString(); | ||
| return this.getJson(url.toString()); | ||
| } | ||
|
|
||
| async getTransactions(params: TransactionParams): Promise<TransactionResponse> { | ||
| const url = new URL('getTransactions', this.basePath); | ||
| url.search = new URLSearchParams(params as unknown as Record<string, string>).toString(); | ||
| return this.getJson(url.toString()); | ||
| } | ||
|
|
||
| async getChainList(): Promise<Chain> { | ||
| const url = new URL('getChainList', this.basePath); | ||
| return this.getJson(url.toString()); | ||
| } | ||
|
|
||
| async getPaths(params: PathsParams): Promise<PathsResponse> { | ||
| const url = new URL('getPaths', this.basePath); | ||
| url.search = new URLSearchParams(params as unknown as Record<string, string>).toString(); | ||
| return this.getJson(url.toString()); | ||
| } | ||
slavastartsev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private async getJson<T>(url: string): Promise<T> { | ||
| if (!process.env.NEXT_PUBLIC_SWAPS_API_KEY && !process.env.SWAPS_API_KEY) | ||
| throw new Error('process.env.NEXT_PUBLIC_SWAPS_API_KEY or process.env.SWAPS_API_KEY is missing'); | ||
|
Check failure on line 67 in sdk/src/gateway/swaps.ts
|
||
|
|
||
| const response = await fetch(url, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json', | ||
| 'x-api-key': process.env.NEXT_PUBLIC_SWAPS_API_KEY || (process.env.SWAPS_API_KEY as string), | ||
| }, | ||
| }); | ||
| if (!response.ok) { | ||
| let errorMessage = response.statusText; | ||
| try { | ||
| const errorBody = await response.text(); | ||
| if (errorBody) { | ||
| errorMessage = `${response.statusText}: ${errorBody}`; | ||
| } | ||
| } catch { | ||
| // If we can't parse the error body, use statusText | ||
| } | ||
| throw new Error(`Swaps API error (${response.status}): ${errorMessage}. URL: ${url}`); | ||
| } | ||
| return response.json() as Promise<T>; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.