cred402/lib/services/safe_draw.ts
caelum0x 5cf86893e3 feat(contracts): deploy Cred402 Odra contracts to Casper Testnet (Casper 2.0)
Migrate the contract suite to Odra 2.x (builds on the Casper 2.0 ABI; fixes the
1.x nightly/edition2024 toolchain deadlock) and deploy 9 contracts live to
casper-test via the Odra livenet deployer:

  AgentRegistry · X402ReceiptRegistry · RWAEvidenceRegistry · RWAAssetRegistry
  ReputationEngine · AgentCreditPool · RiskPolicyManager · AgentPassport · DisputeCourt

- contracts: odra "2", no_std headers, build.rs (odra-build), per-crate Odra.toml +
  build_contract/livenet bins; pinned nightly-2025-01-15 for reproducible wasm.
- tooling: scaffold_livenet.py, deploy_all_testnet.sh, deploy_one.mjs.
- deploys.testnet.json: live manifest; README: on-chain contract hashes.
- Deployer key kept out of git (.secrets/ gitignored); wasm artifacts gitignored.

Verified on-chain: AgentRegistry ContractPackage present in global state.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 02:34:49 +03:00

72 lines
3.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Ledger } from "../ledger/ledger.js";
/**
* Safe-draw advisor — how much more can an agent borrow without slipping out of a
* healthy band? Drawing to the limit pins the health factor at 1.0 (max_credit ==
* drawn), which trips risk monitoring; a prudent borrower targets a cushion. Given a
* target health factor this returns the largest additional draw that keeps the line
* at or above it, bounded by the line's headroom and the pool's free liquidity.
*/
const HEALTHY_HF_BPS = 15_000; // 1.5× — the "healthy" band in risk_alerts
export interface SafeDrawAdvice {
agent_id: string;
target_health_factor_bps: number;
max_credit_motes: string;
drawn_motes: string;
available_motes: string; // raw headroom on the line
pool_free_liquidity_motes: string;
safe_additional_draw_motes: string; // additional draw that keeps HF ≥ target
resulting_drawn_motes: string;
resulting_health_factor_bps: number;
limited_by: "target_health" | "line_headroom" | "pool_liquidity" | "none";
}
export function computeSafeDraw(ledger: Ledger, agentId: string, targetHfBps = HEALTHY_HF_BPS): SafeDrawAdvice | { error: string } {
const line = ledger.pool.get(agentId);
if (!line) return { error: `no credit line for ${agentId}` };
if (line.status !== "active") return { error: `credit line is ${line.status}` };
const target = Math.max(10_000, targetHfBps); // never below 1.0×
const maxCredit = line.max_credit;
const drawn = line.drawn;
const headroom = maxCredit - drawn;
const pool = ledger.pool.poolState();
const free = pool.total_liquidity - pool.outstanding_credit;
// HF = max_credit / drawn ≥ target/10000 ⇒ drawn ≤ max_credit * 10000 / target.
const maxDrawnForTarget = (maxCredit * 10_000n) / BigInt(target);
const byTarget = maxDrawnForTarget > drawn ? maxDrawnForTarget - drawn : 0n;
// The binding constraint is the smallest of target-room, line headroom, free liquidity.
let safe = byTarget;
let limited: SafeDrawAdvice["limited_by"] = "target_health";
if (headroom < safe) {
safe = headroom < 0n ? 0n : headroom;
limited = "line_headroom";
}
if (free < safe) {
safe = free < 0n ? 0n : free;
limited = "pool_liquidity";
}
if (safe === byTarget && byTarget > 0n && headroom >= byTarget && free >= byTarget) limited = "target_health";
if (safe === 0n && byTarget === 0n) limited = "none";
const resultingDrawn = drawn + safe;
const resultingHf = resultingDrawn === 0n ? 50_000 : Math.min(50_000, Number((maxCredit * 10_000n) / resultingDrawn));
return {
agent_id: agentId,
target_health_factor_bps: target,
max_credit_motes: maxCredit.toString(),
drawn_motes: drawn.toString(),
available_motes: (headroom < 0n ? 0n : headroom).toString(),
pool_free_liquidity_motes: (free < 0n ? 0n : free).toString(),
safe_additional_draw_motes: safe.toString(),
resulting_drawn_motes: resultingDrawn.toString(),
resulting_health_factor_bps: resultingHf,
limited_by: limited,
};
}