# usainbluntmx/paykit architecture

> Architecture diagram of the GitHub repository usainbluntmx/paykit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/usainbluntmx/paykit
- Repository: https://github.com/usainbluntmx/paykit

## Overview

1. **Repository shape**
   - This is a three-part Solana dApp repo:
     - `programs/paykit` contains the on-chain Anchor program in Rust.
     - `sdk` contains the JavaScript/TypeScript client, tests, and an autonomous agent demo.
     - `frontend` contains the Next.js app for the landing page, docs, and dashboard.
   - The root also includes shared build/deploy config such as `Anchor.toml`, Rust toolchain settings, and workspace manifests.

2. **Core architecture: on-chain accountability layer**
   - The central system is the Solana program, not the frontend.
   - PayKit models each agent as an on-chain PDA account tied to an owner wallet.
   - The program is responsible for enforcing spend limits, daily limits, expiration, activation state, and payment history invariants at the protocol level.

3. **Smart contract boundary**
   - `programs/paykit/src/lib.rs` is the trust boundary for all spending logic.
   - The contract exposes instructions for:
     - registering an agent
     - recording a payment
     - agent-to-agent payment
     - updating spend limits
     - deactivating agents
     - renewing agents
   - Any client-side SDK or UI is a convenience layer; spending constraints are enforced only by the program.

4. **State model**
   - The main persisted account is `AgentAccount`, which stores:
     - owner, name, and PDA metadata
     - spend limit and total spent
     - payment count
     - active/inactive flag
     - daily spend and daily reset timestamp
     - last payment timestamp
     - expiration timestamp
   - This account is the source of truth for agent identity and budget status.

5. **Payment flow**
   - A typical flow is:
     1. the owner registers an agent PDA
     2. the agent spends through `record_payment` or `agent_to_agent_payment`
     3. the program checks total budget, daily budget, active status, and expiration
     4. the contract updates counters and emits on-chain activity for later indexing
   - Batch payment is implemented as a higher-level capability in the SDK and UI, but the actual enforcement still depends on the underlying program instructions.

6. **SDK as the application-facing layer**
   - `sdk/src/index.js` is the main client abstraction for developers.
   - It wraps Solana/Anchor interactions into operations like register, pay, renew, estimate fee, fetch agents, and read history.
   - `sdk/src/types.ts` provides type definitions for consumers.
   - `sdk/src/errors.js` centralizes program/client error handling.
   - The SDK is the integration layer for bots, services, and automation workflows.

7. **Agent automation demo**
   - `sdk/src/agent-demo.js` is a real autonomous-agent example rather than a library primitive.
   - It uses Anthropic API access plus the PayKit SDK to:
     - inspect on-chain state
     - decide to delegate work
     - execute payment transactions
     - verify results
   - This demonstrates the intended integration pattern: AI decides, Solana enforces, the SDK mediates.

8. **Frontend responsibilities**
   - `frontend` is a Next.js app with three visible surfaces:
     - `/` landing page
     - `/docs` documentation
     - `/dashboard` live demo / audit UI
   - It is presentation and interaction only; it does not own business rules.
   - The frontend likely reads the IDL from `frontend/public/idl/paykit.json`, which is the bridge between the UI and the Anchor program interface.

9. **Dashboard data flow**
   - The dashboard is the main operational UI for inspecting agents and payment history.
   - Its architecture is centered on:
     - wallet connection
     - querying on-chain agent accounts by PDA
     - rendering payment activity and expiration status
     - showing audit/history views for arbitrary agents
   - Because the state lives on Solana, the dashboard is effectively an index-and-display client over chain data.

10. **Contract-to-frontend coupling**
    - The IDL file in `frontend/public/idl/paykit.json` is the explicit compatibility boundary between the Rust program and the web UI.
    - When the contract interface changes, the IDL must be regenerated/copied so the frontend and SDK can continue to interpret instructions and account layouts correctly.

11. **Testing and validation**
    - `sdk/src/__tests__/paykit.test.js` contains unit tests for SDK behavior.
    - `sdk/src/test.js` is an integration test against Devnet.
    - `tests/paykit.ts` suggests additional TypeScript-level integration or harness testing around the program or SDK.
    - The repo therefore validates both client logic and live-chain behavior.

12. **Deployment/runtime context**
    - The architecture is built around Solana Devnet for development, with mainnet support planned/possible through the SDK.
    - Anchor 0.31.1 is the contract framework and build/deploy system.
    - The frontend is a Next.js application, while the SDK runs in Node.js and can be used in scripts, services, or agent runtimes.

13. **Important system boundaries**
    - **On-chain boundary:** all authoritative state and spend enforcement.
    - **Client boundary:** SDK and frontend, which initiate transactions and display state.
    - **External AI boundary:** Anthropic-powered demo agent, which can make decisions but cannot bypass on-chain constraints.
    - **Indexing/audit boundary:** history is derived from on-chain events and queried by the dashboard.

14. **What to draw in an architecture diagram**
    - Draw Solana as the center with the PayKit program and agent PDAs.
    - Connect the SDK as the primary transaction client.
    - Connect the Next.js dashboard/docs/landing page as the human-facing layer, reading the IDL and chain state.
    - Connect the Anthropic-based demo as an external AI consumer of the SDK.
    - Show tests and deployment tooling around the repo, but outside the runtime path.

Main entry points: [`programs/paykit/src/lib.rs`](https://github.com/usainbluntmx/paykit/blob/main/programs/paykit/src/lib.rs) (PayKit Program), [`sdk/src/index.js`](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/index.js) (SDK Core), [`frontend/app/dashboard/page.tsx`](https://github.com/usainbluntmx/paykit/blob/main/frontend/app/dashboard/page.tsx) (Dashboard), [`frontend/app`](https://github.com/usainbluntmx/paykit/tree/main/frontend/app) (Next.js App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_onchain["On-chain Core"]
  node_solana[("Solana<br/>chain runtime")]
  node_paykit_program["PayKit Program<br/>anchor contract<br/>[lib.rs]"]
  node_agent_account[("Agent Account<br/>pda state")]
  node_payment_rules{{"Spend Rules<br/>validation logic<br/>[lib.rs]"}}
  node_payment_events["Payment Events<br/>on-chain activity"]
end

subgraph group_sdk["SDK Layer"]
  node_sdk_index["SDK Core<br/>client api<br/>[index.js]"]
  node_sdk_types["Types<br/>ts defs<br/>[types.ts]"]
  node_sdk_errors{{"Error Layer<br/>error handling<br/>[errors.js]"}}
  node_agent_demo(("Agent Demo<br/>automation demo<br/>[agent-demo.js]"))
  node_sdk_tests["SDK Tests<br/>test suite<br/>[paykit.test.js]"]
  node_devnet_test["Devnet Test<br/>integration script<br/>[test.js]"]
end

subgraph group_frontend["Web App"]
  node_next_app["Next.js App<br/>web app"]
  node_landing["Landing<br/>page<br/>[page.tsx]"]
  node_docs_page["Docs<br/>page<br/>[page.tsx]"]
  node_dashboard["Dashboard<br/>audit ui<br/>[page.tsx]"]
  node_idl["Program IDL<br/>compat file<br/>[paykit.json]"]
end

subgraph group_repo["Repo Infra"]
  node_workspace["Workspace<br/>monorepo config<br/>[Cargo.toml]"]
  node_program_tests["Program Tests<br/>ts harness<br/>[paykit.ts]"]
end

node_solana -->|"hosts"| node_paykit_program
node_paykit_program -->|"owns"| node_agent_account
node_paykit_program -->|"enforces"| node_payment_rules
node_paykit_program -->|"emits"| node_payment_events
node_sdk_index -->|"invokes"| node_paykit_program
node_sdk_index -->|"reads"| node_agent_account
node_sdk_index -->|"exports"| node_sdk_types
node_sdk_index -->|"uses"| node_sdk_errors
node_agent_demo -->|"drives"| node_sdk_index
node_agent_demo -.->|"respects"| node_payment_rules
node_sdk_tests -->|"verifies"| node_sdk_index
node_devnet_test -->|"exercises"| node_paykit_program
node_next_app -->|"contains"| node_landing
node_next_app -->|"contains"| node_docs_page
node_next_app -->|"contains"| node_dashboard
node_dashboard -->|"loads"| node_idl
node_dashboard -->|"queries"| node_agent_account
node_dashboard -->|"renders"| node_payment_events
node_idl -.->|"matches"| node_paykit_program
node_workspace -->|"builds"| node_paykit_program
node_workspace -->|"builds"| node_next_app
node_workspace -->|"packages"| node_sdk_index
node_program_tests -->|"checks"| node_paykit_program

click node_paykit_program "https://github.com/usainbluntmx/paykit/blob/main/programs/paykit/src/lib.rs"
click node_payment_rules "https://github.com/usainbluntmx/paykit/blob/main/programs/paykit/src/lib.rs"
click node_sdk_index "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/index.js"
click node_sdk_types "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/types.ts"
click node_sdk_errors "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/errors.js"
click node_agent_demo "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/agent-demo.js"
click node_sdk_tests "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/__tests__/paykit.test.js"
click node_devnet_test "https://github.com/usainbluntmx/paykit/blob/main/sdk/src/test.js"
click node_next_app "https://github.com/usainbluntmx/paykit/tree/main/frontend/app"
click node_landing "https://github.com/usainbluntmx/paykit/blob/main/frontend/app/page.tsx"
click node_docs_page "https://github.com/usainbluntmx/paykit/blob/main/frontend/app/docs/page.tsx"
click node_dashboard "https://github.com/usainbluntmx/paykit/blob/main/frontend/app/dashboard/page.tsx"
click node_idl "https://github.com/usainbluntmx/paykit/blob/main/frontend/public/idl/paykit.json"
click node_workspace "https://github.com/usainbluntmx/paykit/blob/main/Cargo.toml"
click node_program_tests "https://github.com/usainbluntmx/paykit/blob/main/tests/paykit.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_solana,node_paykit_program,node_agent_account,node_payment_rules,node_payment_events toneBlue
class node_sdk_index,node_sdk_types,node_sdk_errors,node_agent_demo,node_sdk_tests,node_devnet_test toneAmber
class node_next_app,node_landing,node_docs_page,node_dashboard,node_idl toneMint
class node_workspace,node_program_tests toneRose
```

## Components

### On-chain Core

Anchor program and Solana state that enforce PayKit spending rules.

- **Solana**: chain runtime · Execution and state layer where PayKit accounts and transactions live.
- [PayKit Program](https://github.com/usainbluntmx/paykit/blob/main/programs/paykit/src/lib.rs): anchor contract · Trust boundary for registration, payments, limits, renewal, and deactivation. · `programs/paykit/src/lib.rs`
- **Agent Account**: pda state · Persistent source of truth for agent identity, limits, activity, and payment history.
- [Spend Rules](https://github.com/usainbluntmx/paykit/blob/main/programs/paykit/src/lib.rs): validation logic · Enforces total spend, daily limits, expiration, activation state, and invariants. · `programs/paykit/src/lib.rs`
- **Payment Events**: on-chain activity · Recorded activity used for later indexing and audit views.

### SDK Layer

Developer-facing client, automation demo, and tests.

- [SDK Core](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/index.js): client api · Main JavaScript client abstraction for developers and services. · `sdk/src/index.js`
- [Types](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/types.ts): ts defs · Consumer-facing type definitions for SDK integration. · `sdk/src/types.ts`
- [Error Layer](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/errors.js): error handling · Centralized mapping and handling for program and client errors. · `sdk/src/errors.js`
- [Agent Demo](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/agent-demo.js): automation demo · Anthropic-powered autonomous agent example that delegates work and executes payments. · `sdk/src/agent-demo.js`
- [SDK Tests](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/__tests__/paykit.test.js): test suite · Unit and integration checks for SDK behavior and live-chain flows. · `sdk/src/__tests__/paykit.test.js`
- [Devnet Test](https://github.com/usainbluntmx/paykit/blob/main/sdk/src/test.js): integration script · Live Solana test script against development network behavior. · `sdk/src/test.js`

### Web App

Next.js surfaces for landing, docs, and dashboard.

- [Next.js App](https://github.com/usainbluntmx/paykit/tree/main/frontend/app): web app · Human-facing frontend shell for landing, docs, and dashboard. · `frontend/app`
- [Landing](https://github.com/usainbluntmx/paykit/blob/main/frontend/app/page.tsx): page · Public marketing and entry page. · `frontend/app/page.tsx`
- [Docs](https://github.com/usainbluntmx/paykit/blob/main/frontend/app/docs/page.tsx): page · Documentation surface for the PayKit product and protocol. · `frontend/app/docs/page.tsx`
- [Dashboard](https://github.com/usainbluntmx/paykit/blob/main/frontend/app/dashboard/page.tsx): audit ui · Operational UI for inspecting agents, payments, and expiration status. · `frontend/app/dashboard/page.tsx`
- [Program IDL](https://github.com/usainbluntmx/paykit/blob/main/frontend/public/idl/paykit.json): compat file · Compatibility boundary between the Rust program and UI/client code. · `frontend/public/idl/paykit.json`

### Repo Infra

Workspace, build, and deployment configuration.

- [Workspace](https://github.com/usainbluntmx/paykit/blob/main/Cargo.toml): monorepo config · Root manifests, Anchor config, and Rust toolchain settings. · `Cargo.toml`
- [Program Tests](https://github.com/usainbluntmx/paykit/blob/main/tests/paykit.ts): ts harness · Additional TypeScript-level testing around program or SDK behavior. · `tests/paykit.ts`

## Connections

- Solana → PayKit Program: hosts · Solana executes the PayKit contract and stores its accounts.
- PayKit Program → Agent Account: owns · The program creates and updates the agent PDA state.
- PayKit Program → Spend Rules: enforces · All spend and lifecycle checks are enforced in-program.
- PayKit Program → Payment Events: emits · Successful activity becomes chain data for later indexing.
- SDK Core → PayKit Program: invokes · Client operations submit Anchor/Solana transactions to the program.
- SDK Core → Agent Account: reads · The SDK fetches agent state and history from chain accounts.
- SDK Core → Types: exports · Type definitions support downstream consumers.
- SDK Core → Error Layer: uses · Client calls normalize failures through shared error handling.
- Agent Demo → SDK Core: drives · The autonomous demo uses the SDK to inspect state and pay.
- Agent Demo → Spend Rules: respects · The demo can decide, but enforcement still comes from the contract.
- SDK Tests → SDK Core: verifies · Unit tests validate SDK behavior.
- Devnet Test → PayKit Program: exercises · Integration testing runs against live Devnet contract behavior.
- Next.js App → Landing: contains · The web app exposes a public landing surface.
- Next.js App → Docs: contains · The web app exposes documentation pages.
- Next.js App → Dashboard: contains · The web app exposes the live operational dashboard.
- Dashboard → Program IDL: loads · The dashboard uses the IDL to interpret accounts and instructions.
- Dashboard → Agent Account: queries · The UI reads on-chain agent state for display and audit.
- Dashboard → Payment Events: renders · History and audit views are derived from on-chain events.
- Program IDL → PayKit Program: matches · The IDL mirrors the program interface and account layout.
- Workspace → PayKit Program: builds · Root config drives Anchor/Rust contract builds and deployment.
- Workspace → Next.js App: builds · Root workspace also supports the frontend application.
- Workspace → SDK Core: packages · Workspace manifests and package config support the SDK.
- Program Tests → PayKit Program: checks · Additional TypeScript harness tests the program or client boundary.
