fix: update payment apis
This commit is contained in:
parent
a129cef985
commit
a58030323a
|
|
@ -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>> {
|
||||
const res = await fetch(`${API}/rpc/info`);
|
||||
const res = await fetch(`${API}/info`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function getExchangeRates(): Promise<Record<string, number>> {
|
||||
const res = await fetch(`${API}/invoice/rates`);
|
||||
const res = await fetch(`${API}/rates`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
|
|
@ -20,13 +20,13 @@ export async function getAddressInfoBulk(addresses: string[]): Promise<Record<st
|
|||
const res = await fetch(`${API}/address`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ terms: addresses }),
|
||||
body: JSON.stringify({ addresses }),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function fetchQrBase64(text: string): Promise<string> {
|
||||
const url = `${API}/invoice/qr/${encodeURIComponent(text)}`;
|
||||
const url = `${API}/qr/${encodeURIComponent(text)}`;
|
||||
try {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return url;
|
||||
|
|
@ -42,6 +42,6 @@ export async function fetchQrBase64(text: string): Promise<string> {
|
|||
export async function getAvailableCoins(): Promise<string[]> {
|
||||
const info = await getChainInfo();
|
||||
return Object.entries(info)
|
||||
.filter(([_, v]) => v && v.uptime != null)
|
||||
.filter(([_, v]) => v?.node?.uptime != null)
|
||||
.map(([k]) => k);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { getAddressInfo, getAddressInfoBulk } from "./freedom";
|
|||
import { COINS, planTier } from "../../shared/plans";
|
||||
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;
|
||||
|
||||
let addressMap = new Map<string, any>(); // address → payment
|
||||
|
|
@ -95,19 +95,19 @@ async function evaluatePayment(paymentId: number) {
|
|||
}
|
||||
|
||||
async function handleTxEvent(event: any) {
|
||||
const txHash = event.data?.tx?.hash;
|
||||
const txHash = event.data?.hash;
|
||||
if (!txHash || seenTxids.has(txHash)) return;
|
||||
seenTxids.add(txHash);
|
||||
|
||||
const outputs = event.data?.out ?? [];
|
||||
const outputs = event.data?.to ?? [];
|
||||
for (const out of outputs) {
|
||||
const addr = out?.script?.address;
|
||||
const addr = out?.address;
|
||||
const payment = addr && addressMap.get(addr);
|
||||
if (!payment) continue;
|
||||
|
||||
const txValue = outputs
|
||||
.filter((o: any) => o?.script?.address === addr)
|
||||
.reduce((s: number, o: any) => s + Number(o.value ?? 0), 0);
|
||||
.filter((o: any) => o?.address === addr)
|
||||
.reduce((s: number, o: any) => s + Number(o.amount ?? 0), 0);
|
||||
if (txValue <= 0) continue;
|
||||
|
||||
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) {
|
||||
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));
|
||||
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 || info.error) continue;
|
||||
|
||||
for (const tx of info.in ?? []) {
|
||||
if (!tx.txid) continue;
|
||||
await recordTx(payment.id, payment.address, tx.txid, Number(tx.amount ?? 0), tx.block != null);
|
||||
const inbound = (info.transactions ?? []).filter((tx: any) => tx.amount > 0);
|
||||
for (const tx of inbound) {
|
||||
if (!tx.hash) continue;
|
||||
await recordTx(payment.id, payment.address, tx.hash, Number(tx.amount ?? 0), tx.block != null);
|
||||
}
|
||||
await evaluatePayment(payment.id);
|
||||
} catch (e) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue