fix: update payment apis

This commit is contained in:
nate 2026-04-09 19:08:31 +04:00
parent a129cef985
commit a58030323a
2 changed files with 17 additions and 16 deletions

View File

@ -1,12 +1,12 @@
const API = process.env.FREEDOM_API ?? "https://api-v1.freedom.st"; const API = process.env.FREEDOM_API ?? "https://api.freedom.st";
export async function getChainInfo(): Promise<Record<string, any>> { export async function getChainInfo(): Promise<Record<string, any>> {
const res = await fetch(`${API}/rpc/info`); const res = await fetch(`${API}/info`);
return res.json(); return res.json();
} }
export async function getExchangeRates(): Promise<Record<string, number>> { export async function getExchangeRates(): Promise<Record<string, number>> {
const res = await fetch(`${API}/invoice/rates`); const res = await fetch(`${API}/rates`);
return res.json(); return res.json();
} }
@ -20,13 +20,13 @@ export async function getAddressInfoBulk(addresses: string[]): Promise<Record<st
const res = await fetch(`${API}/address`, { const res = await fetch(`${API}/address`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ terms: addresses }), body: JSON.stringify({ addresses }),
}); });
return res.json(); return res.json();
} }
export async function fetchQrBase64(text: string): Promise<string> { export async function fetchQrBase64(text: string): Promise<string> {
const url = `${API}/invoice/qr/${encodeURIComponent(text)}`; const url = `${API}/qr/${encodeURIComponent(text)}`;
try { try {
const res = await fetch(url); const res = await fetch(url);
if (!res.ok) return url; if (!res.ok) return url;
@ -42,6 +42,6 @@ export async function fetchQrBase64(text: string): Promise<string> {
export async function getAvailableCoins(): Promise<string[]> { export async function getAvailableCoins(): Promise<string[]> {
const info = await getChainInfo(); const info = await getChainInfo();
return Object.entries(info) return Object.entries(info)
.filter(([_, v]) => v && v.uptime != null) .filter(([_, v]) => v?.node?.uptime != null)
.map(([k]) => k); .map(([k]) => k);
} }

View File

@ -3,7 +3,7 @@ import { getAddressInfo, getAddressInfoBulk } from "./freedom";
import { COINS, planTier } from "../../shared/plans"; import { COINS, planTier } from "../../shared/plans";
import { generateReceipt } from "./receipt"; import { generateReceipt } from "./receipt";
const SOCK_API = process.env.FREEDOM_SOCK ?? "https://sock-v1.freedom.st"; const SOCK_API = process.env.FREEDOM_SOCK ?? "https://sock.freedom.st";
const THRESHOLD = 0.95; const THRESHOLD = 0.95;
let addressMap = new Map<string, any>(); // address → payment let addressMap = new Map<string, any>(); // address → payment
@ -95,19 +95,19 @@ async function evaluatePayment(paymentId: number) {
} }
async function handleTxEvent(event: any) { async function handleTxEvent(event: any) {
const txHash = event.data?.tx?.hash; const txHash = event.data?.hash;
if (!txHash || seenTxids.has(txHash)) return; if (!txHash || seenTxids.has(txHash)) return;
seenTxids.add(txHash); seenTxids.add(txHash);
const outputs = event.data?.out ?? []; const outputs = event.data?.to ?? [];
for (const out of outputs) { for (const out of outputs) {
const addr = out?.script?.address; const addr = out?.address;
const payment = addr && addressMap.get(addr); const payment = addr && addressMap.get(addr);
if (!payment) continue; if (!payment) continue;
const txValue = outputs const txValue = outputs
.filter((o: any) => o?.script?.address === addr) .filter((o: any) => o?.address === addr)
.reduce((s: number, o: any) => s + Number(o.value ?? 0), 0); .reduce((s: number, o: any) => s + Number(o.amount ?? 0), 0);
if (txValue <= 0) continue; if (txValue <= 0) continue;
console.log(`SSE: tx ${txHash} for payment ${payment.id}: +${txValue} ${payment.coin}`); console.log(`SSE: tx ${txHash} for payment ${payment.id}: +${txValue} ${payment.coin}`);
@ -119,7 +119,7 @@ async function handleTxEvent(event: any) {
} }
async function handleBlockEvent(event: any) { async function handleBlockEvent(event: any) {
const blockTxs: string[] = event.data?.tx ?? []; const blockTxs: string[] = (event.data?.transactions ?? []).map((t: any) => typeof t === 'string' ? t : t.hash);
const matched = blockTxs.filter(t => txidToPayment.has(t)); const matched = blockTxs.filter(t => txidToPayment.has(t));
if (matched.length === 0) return; if (matched.length === 0) return;
@ -192,9 +192,10 @@ export async function checkPayments() {
if (!info) try { info = await getAddressInfo(payment.address); } catch { continue; } if (!info) try { info = await getAddressInfo(payment.address); } catch { continue; }
if (!info || info.error) continue; if (!info || info.error) continue;
for (const tx of info.in ?? []) { const inbound = (info.transactions ?? []).filter((tx: any) => tx.amount > 0);
if (!tx.txid) continue; for (const tx of inbound) {
await recordTx(payment.id, payment.address, tx.txid, Number(tx.amount ?? 0), tx.block != null); if (!tx.hash) continue;
await recordTx(payment.id, payment.address, tx.hash, Number(tx.amount ?? 0), tx.block != null);
} }
await evaluatePayment(payment.id); await evaluatePayment(payment.id);
} catch (e) { } catch (e) {