import { rateLimiter, EndpointCategory } from "./rate-limiter.js";
import { getConfig } from "./config.js";
const cfg = getConfig();
const BRIDGE_BASE = cfg.bridgeApiUrl;
async function bridgeFetch<T>(
path: string,
params: Record<string, string | number | boolean | undefined> = {},
): Promise<T> {
await rateLimiter.acquire(EndpointCategory.BRIDGE_API);
const url = new URL(path, BRIDGE_BASE);
for (const [k, v] of Object.entries(params)) {
if (v !== undefined) url.searchParams.set(k, String(v));
}
const res = await fetch(url.toString());
if (!res.ok) {
throw new Error(`Bridge API error: ${res.status} ${res.statusText}`);
}
return (await res.json()) as T;
}
async function bridgePost<T>(path: string, body: unknown): Promise<T> {
await rateLimiter.acquire(EndpointCategory.BRIDGE_API);
const url = new URL(path, BRIDGE_BASE);
const res = await fetch(url.toString(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) {
throw new Error(`Bridge API error: ${res.status} ${res.statusText}`);
}
return (await res.json()) as T;
}
export async function getBridgeTransactionStatus(address: string): Promise<unknown> {
return bridgeFetch<unknown>(`/status/${address}`);
}
export async function getSupportedAssets(): Promise<unknown> {
return bridgeFetch<unknown>("/supported-assets");
}
export async function getBridgeQuote(body: {
fromAmountBaseUnit: string;
fromChainId: string;
fromTokenAddress: string;
recipientAddress: string;
toChainId: string;
toTokenAddress: string;
}): Promise<unknown> {
return bridgePost<unknown>("/quote", body);
}