pingql/apps/pay/src/freedom.ts

48 lines
1.4 KiB
TypeScript

const API = process.env.FREEDOM_API ?? "https://api.freedom.st";
export async function getChainInfo(): Promise<Record<string, any>> {
const res = await fetch(`${API}/info`);
return res.json();
}
export async function getExchangeRates(): Promise<Record<string, number>> {
const res = await fetch(`${API}/rates`);
return res.json();
}
export async function getAddressInfo(address: string): Promise<any> {
const res = await fetch(`${API}/address/${address}`);
return res.json();
}
export async function getAddressInfoBulk(addresses: string[]): Promise<Record<string, any>> {
if (addresses.length === 0) return {};
const res = await fetch(`${API}/address`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ addresses }),
});
return res.json();
}
export async function fetchQrBase64(text: string): Promise<string> {
const url = `${API}/qr/${encodeURIComponent(text)}`;
try {
const res = await fetch(url);
if (!res.ok) return url;
const buf = await res.arrayBuffer();
const type = res.headers.get("content-type") || "image/png";
const b64 = Buffer.from(buf).toString("base64");
return `data:${type};base64,${b64}`;
} catch {
return url;
}
}
export async function getAvailableCoins(): Promise<string[]> {
const info = await getChainInfo();
return Object.entries(info)
.filter(([_, v]) => v != null)
.map(([k]) => k);
}