// liquidfun.io SDK — zero dependencies. Copy this file into your project.
// Reads the public API and verifies payouts directly on Solana.
export type Payout = {
  id: string; created_at: string; destination_ticker: string; destination_mint: string;
  lamports: number; sol: number; signature: string; status: string; source: string;
  burn_signature: string | null; burn_status: string | null; verify: string; burn_verify: string | null;
  operation_kind: "buy" | "paired_deposit"; deposit_signature: string | null;
  deposit_verify: string | null; lp_burn_verify: string | null;
  deposited_base_amount: string | null; deposited_quote_amount: string | null;
};
export type Coin = { ticker: string; name: string; mint_address: string; launch_signature: string | null; created_at: string; verify: string | null };
type Page<T> = { data: T[]; next_before: string | null; next_before_id?: string | null };

export class LiquidFun {
  constructor(private base = "https://liquidfun.io", private rpc = "https://api.mainnet-beta.solana.com") {}
  private async get<T>(path: string): Promise<T> {
    const r = await fetch(this.base + path);
    if (!r.ok) throw new Error(`liquidfun ${r.status}`);
    return r.json() as Promise<T>;
  }
  config() { return this.get<{ fee_wallet: string; liquid_mint: string; launch_fee_sol: number }>("/api/public/v1/config"); }
  payouts(o: { limit?: number; before?: string; beforeId?: string } = {}) {
    const p = new URLSearchParams(); if (o.limit) p.set("limit", String(o.limit)); if (o.before) p.set("before", o.before);
    if (o.beforeId) p.set("before_id", o.beforeId);
    return this.get<Page<Payout>>(`/api/public/v1/payouts?${p}`);
  }
  coins(o: { limit?: number; before?: string } = {}) {
    const p = new URLSearchParams(); if (o.limit) p.set("limit", String(o.limit)); if (o.before) p.set("before", o.before);
    return this.get<Page<Coin>>(`/api/public/v1/coins?${p}`);
  }
  async *allPayouts() {
    let before: string | undefined;
    let beforeId: string | undefined;
    for (;;) { const pg = await this.payouts({ limit: 200, ...(before ? { before } : {}), ...(beforeId ? { beforeId } : {}) }); yield* pg.data; if (!pg.next_before) return; before = pg.next_before; beforeId = pg.next_before_id ?? undefined; }
  }
  /** Transaction-only check. This is not independent verification of pool depth or LP locking. */
  async verifyPayout(p: Payout): Promise<{ ok: boolean; reason?: string }> {
    const { fee_wallet } = await this.config();
    const r = await fetch(this.rpc, { method: "POST", headers: { "content-type": "application/json" },
      body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "getTransaction", params: [p.signature, { encoding: "json", maxSupportedTransactionVersion: 0, commitment: "confirmed" }] }) });
    const tx = (await r.json()).result;
    if (!tx) return { ok: false, reason: "transaction not found" };
    if (tx.meta?.err) return { ok: false, reason: "transaction failed on-chain" };
    const keys: string[] = tx.transaction.message.accountKeys;
    if (keys[0] !== fee_wallet) return { ok: false, reason: "not signed by the fee wallet" };
    return { ok: true };
  }
}
