- Update all AGENTS.md watermarks to HEAD (b276392)
- Clean up dust.jsonl: remove already-bundled items (601,627,739,741)
- Pending actions: promote #1099/#1100/#1101 to backlog, close stale
prediction issues #1020/#1103/#1107, comment on partial resolution
of #1022 (holdout resolved, user-test still empty)
3.9 KiB
3.9 KiB
Kraiken Library - Agent Guide
Shared TypeScript helpers used by the landing app, txnBot, and other services to talk to KRAIKEN contracts and the Ponder GraphQL API.
Responsibilities
- Provide type-safe contract interaction helpers (encoding inputs, parsing results).
- Supply Ponder GraphQL query helpers and generated types for protocol stats and staking positions.
- Centralise staking math (tax calculations, snatch selection, share conversions) for reuse across clients.
Key Modules
src/staking.ts- Harberger staking helpers for delinquency checks and snatch math.src/snatch.ts- Snatch selection engine and supporting types.src/ids.ts- Position ID encoding helpers.src/subgraph.ts- Byte utilities shared between the GraphQL layer and clients.src/abis.ts- Contract ABIs imported directly fromonchain/out/forge artifacts. Single source of truth for all ABI consumers.src/taxRates.ts- Generated fromonchain/src/Stake.solbyscripts/sync-tax-rates.mjs; never edit by hand.src/version.ts- Version validation system trackingKRAIKEN_LIB_VERSIONandCOMPATIBLE_CONTRACT_VERSIONSfor runtime dependency checking.
GraphQL Code Generation
- Schema source points to the Ponder GraphQL endpoint for the active environment.
- Scalar mappings:
BigInt->string,BigDecimal->string,Bytes->string,Int8->number. - Always import the generated DocumentNodes; avoid
anycasts to keep inference intact.
Package Scripts
npm install --legacy-peer-depsnpm run compile- Regenerate GraphQL types.npm run watch- Continuously regenerate on schema changes.npm test- Execute Jest suite for helper coverage.
Integration Notes
- Landing app consumes
kraiken-lib/abis,kraiken-lib/staking, andkraiken-lib/subgraphfor ABI resolution and ID conversion. - txnBot relies on
kraiken-lib/stakingandkraiken-lib/idsto evaluate profitability and tax windows. - Ponder imports
kraiken-lib/abisfor indexing, andkraiken-lib/versionfor cross-service version checks. - When the Ponder schema changes, rerun
npm run compileand commit regenerated types to prevent drift.
Import Guidance
- The legacy
helpers.tsbarrel has been removed. Always import from the narrow subpaths (e.g.kraiken-lib/abis,kraiken-lib/staking,kraiken-lib/snatch,kraiken-lib/subgraph). - Avoid importing
kraiken-libdirectly; the root module no longer re-exports the helper surface and exists only to raise build-time errors for bundle imports.
ES Module Architecture
- Module Type: This package is built as ES modules (
"type": "module"in package.json). All consumers must support ES modules. - Import Extensions: All relative imports in TypeScript source files MUST include
.jsextensions (e.g.,from "./staking.js"). This is required for ES module resolution even though the source files are.ts. - JSON Imports: JSON files (like ABI artifacts) must use import assertions:
import Foo from './path.json' assert { type: 'json' }. - TypeScript Config:
tsconfig.jsonmust specify:"module": "esnext"- Generate ES module syntax"moduleResolution": "node"- Enable proper module resolution"rootDir": "./src"- Ensure flat output structure indist/
- Build Output: Running
npx tscproduces ES module.jsfiles indist/that can be consumed by both browser (Vite) and Node.js (≥14 with"type": "module"). - Container Mount: Docker services bind-mount
dist/read-only from the host. Run./scripts/build-kraiken-lib.shbeforedocker-compose upor keepscripts/watch-kraiken-lib.shrunning to rebuild automatically.
Quality Guidelines
- Keep helpers pure and side-effect free; they should accept explicit dependencies.
- Prefer narrow exports so downstream consumers can tree-shake bundles.
- Extend typings instead of loosening them; reject
anyas a quick fix.