repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Conway Credits Management |
| #3 | * |
| #4 | * Monitors the automaton's compute credit balance and triggers |
| #5 | * survival mode transitions. |
| #6 | */ |
| #7 | |
| #8 | import type { |
| #9 | ConwayClient, |
| #10 | FinancialState, |
| #11 | SurvivalTier, |
| #12 | AutomatonDatabase, |
| #13 | } from "../types.js"; |
| #14 | import { SURVIVAL_THRESHOLDS } from "../types.js"; |
| #15 | |
| #16 | /** |
| #17 | * Check the current financial state of the automaton. |
| #18 | */ |
| #19 | export async function checkFinancialState( |
| #20 | conway: ConwayClient, |
| #21 | usdcBalance: number, |
| #22 | ): Promise<FinancialState> { |
| #23 | const creditsCents = await conway.getCreditsBalance(); |
| #24 | |
| #25 | return { |
| #26 | creditsCents, |
| #27 | usdcBalance, |
| #28 | lastChecked: new Date().toISOString(), |
| #29 | }; |
| #30 | } |
| #31 | |
| #32 | /** |
| #33 | * Determine the survival tier based on current credits. |
| #34 | */ |
| #35 | export function getSurvivalTier(creditsCents: number): SurvivalTier { |
| #36 | if (creditsCents > SURVIVAL_THRESHOLDS.normal) return "normal"; |
| #37 | if (creditsCents > SURVIVAL_THRESHOLDS.low_compute) |
| #38 | return "low_compute"; |
| #39 | if (creditsCents > SURVIVAL_THRESHOLDS.dead) return "critical"; |
| #40 | return "dead"; |
| #41 | } |
| #42 | |
| #43 | /** |
| #44 | * Format a credit amount for display. |
| #45 | */ |
| #46 | export function formatCredits(cents: number): string { |
| #47 | return `$${(cents / 100).toFixed(2)}`; |
| #48 | } |
| #49 | |
| #50 | /** |
| #51 | * Log a credit check to the database. |
| #52 | */ |
| #53 | export function logCreditCheck( |
| #54 | db: AutomatonDatabase, |
| #55 | state: FinancialState, |
| #56 | ): void { |
| #57 | const { ulid } = await_ulid(); |
| #58 | db.insertTransaction({ |
| #59 | id: ulid(), |
| #60 | type: "credit_check", |
| #61 | amountCents: state.creditsCents, |
| #62 | description: `Balance check: ${formatCredits(state.creditsCents)} credits, ${state.usdcBalance.toFixed(4)} USDC`, |
| #63 | timestamp: state.lastChecked, |
| #64 | }); |
| #65 | } |
| #66 | |
| #67 | // Lazy ulid import helper |
| #68 | function await_ulid() { |
| #69 | // Dynamic import would be async; for synchronous usage in better-sqlite3 |
| #70 | // we use a simple counter-based ID as fallback |
| #71 | let counter = 0; |
| #72 | return { |
| #73 | ulid: () => { |
| #74 | const timestamp = Date.now().toString(36); |
| #75 | const random = Math.random().toString(36).substring(2, 8); |
| #76 | counter++; |
| #77 | return `${timestamp}-${random}-${counter.toString(36)}`; |
| #78 | }, |
| #79 | }; |
| #80 | } |
| #81 |