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

@@ -158,6 +158,35 @@ export class PricingService {
const amountSats = usdToSats(estimatedCostUsd, btcPriceUsd);
return { amountSats, estimatedCostUsd, marginPct: this.marginPct, btcPriceUsd };
}
// ── Post-work honest accounting ──────────────────────────────────────────
/**
* Full actual charge in USD: raw Anthropic token cost + DO infra amortisation + margin.
* Pass in the already-computed actualCostUsd (raw token cost, no extras).
*/
calculateActualChargeUsd(actualCostUsd: number): number {
const rawCostUsd = actualCostUsd + DO_INFRA_PER_REQUEST_USD;
return rawCostUsd * (1 + this.marginPct / 100);
}
/**
* Convert the actual charge to satoshis using the BTC price that was locked
* at invoice-creation time. This keeps pre- and post-work accounting in the
* same BTC denomination without a second oracle call.
*/
calculateActualChargeSats(actualCostUsd: number, lockedBtcPriceUsd: number): number {
const chargeUsd = this.calculateActualChargeUsd(actualCostUsd);
return usdToSats(chargeUsd, lockedBtcPriceUsd);
}
/**
* Refund amount in sats: what was overpaid by the user.
* Always >= 0 (clamped — never ask the user to top up due to BTC price swings).
*/
calculateRefundSats(workAmountSats: number, actualAmountSats: number): number {
return Math.max(0, workAmountSats - actualAmountSats);
}
}
export const pricingService = new PricingService();