Add honest accounting and automatic refund mechanism for completed jobs

Implement honest accounting post-job completion, calculating actual costs, adding margin, and enabling automatic refunds for overpayments via a new endpoint.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 418bf6f8-212b-4bb0-a7a5-8231a061da4e
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: c6386de2-d5f4-47cc-a557-73416f09e118
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9f85e954-647c-46a5-90a7-396e495a805a/418bf6f8-212b-4bb0-a7a5-8231a061da4e/sPDHkg8
Replit-Helium-Checkpoint-Created: true
This commit is contained in:
alexpaynex
2026-03-18 19:32:34 +00:00
parent e5bdae7159
commit dfc9ecdc7b
15 changed files with 587 additions and 38 deletions

View File

@@ -0,0 +1,13 @@
-- Migration: Add refund/honest-accounting columns to jobs table
-- Users are refunded the overpayment (work invoice amount - actual cost) after job completion.
ALTER TABLE jobs
ADD COLUMN IF NOT EXISTS actual_amount_sats INTEGER,
ADD COLUMN IF NOT EXISTS refund_amount_sats INTEGER,
ADD COLUMN IF NOT EXISTS refund_state TEXT,
ADD COLUMN IF NOT EXISTS refund_payment_hash TEXT;
COMMENT ON COLUMN jobs.actual_amount_sats IS 'Actual charge in sats (raw token cost + DO infra + margin), computed post-execution using the locked BTC price';
COMMENT ON COLUMN jobs.refund_amount_sats IS 'Overpayment in sats to return to the user (work_amount_sats - actual_amount_sats, clamped >= 0)';
COMMENT ON COLUMN jobs.refund_state IS 'Refund lifecycle: not_applicable | pending | paid';
COMMENT ON COLUMN jobs.refund_payment_hash IS 'Payment hash of the outgoing Lightning refund payment once sent';

View File

@@ -36,6 +36,12 @@ export const jobs = pgTable("jobs", {
actualOutputTokens: integer("actual_output_tokens"),
actualCostUsd: real("actual_cost_usd"),
// ── Post-work honest accounting & refund ─────────────────────────────────
actualAmountSats: integer("actual_amount_sats"),
refundAmountSats: integer("refund_amount_sats"),
refundState: text("refund_state").$type<"not_applicable" | "pending" | "paid">(),
refundPaymentHash: text("refund_payment_hash"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
});