# theyuvan/lien architecture

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

- Interactive diagram: https://gitdiagram.com/theyuvan/lien
- Repository: https://github.com/theyuvan/lien

## Overview

Lien is an agent-lending service: agents register and apply for USDC credit, underwriting evaluates real split-contract sales, and approved loans are repaid automatically from future sales while ENS identity is held as collateral. Its agent API also supports MCP, paid inputs/reports, live ENS records, and public loan/books views; the frontend exposes borrower, lender, agent, and monitoring experiences. Source excerpts show the main runtime orchestration and selected integrations; other subsystem links below are included only where the README documents them.

Main entry points: [`agent/src/lien.ts`](https://github.com/theyuvan/lien/blob/main/agent/src/lien.ts) (Lien workflow), [`agent/src/server/api.ts`](https://github.com/theyuvan/lien/blob/main/agent/src/server/api.ts) (Agent API), [`agent/src/hedera/split.ts`](https://github.com/theyuvan/lien/blob/main/agent/src/hedera/split.ts) (Revenue split), [`agent/src/store.ts`](https://github.com/theyuvan/lien/blob/main/agent/src/store.ts) (Loan state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Loan operations"]
  node_lien["Lien workflow<br/>[lien.ts]"]
  node_keeper["Loan keeper<br/>[keeper.ts]"]
  node_store[("Loan state<br/>[store.ts]")]
  node_books["Public books<br/>[books.ts]"]
  node_rebuild["Book replay<br/>[rebuild.ts]"]
  node_config["Runtime config<br/>[config.ts]"]
end

subgraph group_underwriting["Credit assessment"]
  node_underwriting["Underwriting rules<br/>[terms.ts]"]
  node_funding["Funding checks<br/>[funding.ts]"]
  node_memo["Credit memo<br/>[memo.ts]"]
  node_sources["Revenue sources<br/>[sources.ts]"]
end

subgraph group_chain["Hedera settlement"]
  node_mirror["Mirror node data<br/>[mirror.ts]"]
  node_split["Revenue split<br/>[split.ts]"]
  node_pool["Lending pool<br/>[pool.ts]"]
  node_hcs["HCS ledger<br/>[hcs.ts]"]
  node_treasury["Treasury controls<br/>[treasury.ts]"]
end

subgraph group_identity["ENS identity"]
  node_ens["ENS records<br/>[records.ts]"]
  node_vault["Name vault<br/>[vault.ts]"]
  node_fleet["Credit subtree<br/>[fleet.ts]"]
end

subgraph group_experience["Agent and web access"]
  node_api["Agent API<br/>[api.ts]"]
  node_mcp["MCP access<br/>[mcp.ts]"]
  node_ccip["Live ENS reads<br/>[ccip.ts]"]
  node_views["Public views<br/>[views.ts]"]
  node_payname["Pay-by-name check<br/>[index.ts]"]
  node_web["Lien web app<br/>[NameScreen.tsx]"]
  node_ui["Shared UI<br/>[ui.tsx]"]
end

node_borrower(("Borrowing agent"))
node_hedera[("Hedera network")]
node_sepolia[("Sepolia ENS")]
node_llm["LLM provider"]
node_customers(("Borrower customers"))
node_lenders(("Outside lenders"))

node_borrower -->|"registers and applies"| node_api
node_api -.->|"routes loan actions"| node_lien
node_lien -->|"reads and updates"| node_store
node_lien -->|"assesses application"| node_underwriting
node_lien -->|"checks payer funding"| node_funding
node_lien -->|"requests memo"| node_memo
node_memo -->|"generates assessment"| node_llm
node_lien -->|"uses revenue evidence"| node_sources
node_sources -.->|"reads payment history"| node_mirror
node_lien -->|"creates and draws"| node_split
node_lien -->|"funds approved loan"| node_pool
node_customers -->|"pays for sales"| node_split
node_split -->|"sends remaining share"| node_borrower
node_split -.->|"routes repayment share"| node_lenders
node_lien -->|"requires name collateral"| node_vault
node_vault -->|"holds ENS name"| node_sepolia
node_ens -.->|"reads and updates records"| node_sepolia
node_lien -->|"checks name ownership"| node_ens
node_fleet -->|"publishes credit records"| node_sepolia
node_lien -->|"records decisions and loans"| node_hcs
node_hcs -->|"replays public books"| node_rebuild
node_rebuild -.->|"rebuilds state"| node_store
node_keeper -.->|"monitors loan lifecycle"| node_lien
node_lien -->|"settles USDC operations"| node_hedera
node_lenders -->|"deposits capital"| node_pool
node_api -.->|"offers agent operations"| node_mcp
node_api -.->|"serves live ENS data"| node_ccip
node_api -.->|"mounts public views"| node_views
node_views -->|"reads dashboard state"| node_store
node_borrower -->|"checks seller payment target"| node_payname
node_web -.->|"renders shared controls"| node_ui
node_web -.->|"displays loan data"| node_api

click node_lien "https://github.com/theyuvan/lien/blob/main/agent/src/lien.ts"
click node_keeper "https://github.com/theyuvan/lien/blob/main/agent/src/keeper.ts"
click node_store "https://github.com/theyuvan/lien/blob/main/agent/src/store.ts"
click node_books "https://github.com/theyuvan/lien/blob/main/agent/src/books.ts"
click node_rebuild "https://github.com/theyuvan/lien/blob/main/agent/src/rebuild.ts"
click node_config "https://github.com/theyuvan/lien/blob/main/agent/src/config.ts"
click node_underwriting "https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/terms.ts"
click node_funding "https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/funding.ts"
click node_memo "https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/memo.ts"
click node_sources "https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/sources.ts"
click node_mirror "https://github.com/theyuvan/lien/blob/main/agent/src/hedera/mirror.ts"
click node_split "https://github.com/theyuvan/lien/blob/main/agent/src/hedera/split.ts"
click node_pool "https://github.com/theyuvan/lien/blob/main/agent/src/hedera/pool.ts"
click node_hcs "https://github.com/theyuvan/lien/blob/main/agent/src/hedera/hcs.ts"
click node_treasury "https://github.com/theyuvan/lien/blob/main/agent/src/treasury.ts"
click node_ens "https://github.com/theyuvan/lien/blob/main/agent/src/ens/records.ts"
click node_vault "https://github.com/theyuvan/lien/blob/main/agent/src/ens/vault.ts"
click node_fleet "https://github.com/theyuvan/lien/blob/main/agent/src/ens/fleet.ts"
click node_api "https://github.com/theyuvan/lien/blob/main/agent/src/server/api.ts"
click node_mcp "https://github.com/theyuvan/lien/blob/main/agent/src/server/mcp.ts"
click node_ccip "https://github.com/theyuvan/lien/blob/main/agent/src/server/ccip.ts"
click node_views "https://github.com/theyuvan/lien/blob/main/agent/src/server/views.ts"
click node_payname "https://github.com/theyuvan/lien/blob/main/agent/packages/pay-by-name/src/index.ts"
click node_web "https://github.com/theyuvan/lien/blob/main/frontend/src/app/(site)/agents/%5Bname%5D/NameScreen.tsx"
click node_ui "https://github.com/theyuvan/lien/blob/main/frontend/src/components/ui.tsx"

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_lien,node_keeper,node_store,node_books,node_rebuild,node_config toneBlue
class node_underwriting,node_funding,node_memo,node_sources,node_hedera,node_sepolia toneAmber
class node_mirror,node_split,node_pool,node_hcs,node_treasury toneMint
class node_ens,node_vault,node_fleet toneRose
class node_api,node_mcp,node_ccip,node_views,node_payname,node_web,node_ui,node_borrower,node_llm,node_customers,node_lenders toneIndigo
```

## Components

### Loan operations

- [Lien workflow](https://github.com/theyuvan/lien/blob/main/agent/src/lien.ts): `agent/src/lien.ts`
- [Loan keeper](https://github.com/theyuvan/lien/blob/main/agent/src/keeper.ts): `agent/src/keeper.ts`
- [Loan state](https://github.com/theyuvan/lien/blob/main/agent/src/store.ts): `agent/src/store.ts`
- [Public books](https://github.com/theyuvan/lien/blob/main/agent/src/books.ts): `agent/src/books.ts`
- [Book replay](https://github.com/theyuvan/lien/blob/main/agent/src/rebuild.ts): `agent/src/rebuild.ts`
- [Runtime config](https://github.com/theyuvan/lien/blob/main/agent/src/config.ts): `agent/src/config.ts`

### Credit assessment

- [Underwriting rules](https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/terms.ts): `agent/src/underwriting/terms.ts`
- [Funding checks](https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/funding.ts): `agent/src/underwriting/funding.ts`
- [Credit memo](https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/memo.ts): `agent/src/underwriting/memo.ts`
- [Revenue sources](https://github.com/theyuvan/lien/blob/main/agent/src/underwriting/sources.ts): `agent/src/underwriting/sources.ts`

### Hedera settlement

- [Mirror node data](https://github.com/theyuvan/lien/blob/main/agent/src/hedera/mirror.ts): `agent/src/hedera/mirror.ts`
- [Revenue split](https://github.com/theyuvan/lien/blob/main/agent/src/hedera/split.ts): `agent/src/hedera/split.ts`
- [Lending pool](https://github.com/theyuvan/lien/blob/main/agent/src/hedera/pool.ts): `agent/src/hedera/pool.ts`
- [HCS ledger](https://github.com/theyuvan/lien/blob/main/agent/src/hedera/hcs.ts): `agent/src/hedera/hcs.ts`
- [Treasury controls](https://github.com/theyuvan/lien/blob/main/agent/src/treasury.ts): `agent/src/treasury.ts`

### ENS identity

- [ENS records](https://github.com/theyuvan/lien/blob/main/agent/src/ens/records.ts): `agent/src/ens/records.ts`
- [Name vault](https://github.com/theyuvan/lien/blob/main/agent/src/ens/vault.ts): `agent/src/ens/vault.ts`
- [Credit subtree](https://github.com/theyuvan/lien/blob/main/agent/src/ens/fleet.ts): `agent/src/ens/fleet.ts`

### Agent and web access

- [Agent API](https://github.com/theyuvan/lien/blob/main/agent/src/server/api.ts): `agent/src/server/api.ts`
- [MCP access](https://github.com/theyuvan/lien/blob/main/agent/src/server/mcp.ts): `agent/src/server/mcp.ts`
- [Live ENS reads](https://github.com/theyuvan/lien/blob/main/agent/src/server/ccip.ts): `agent/src/server/ccip.ts`
- [Public views](https://github.com/theyuvan/lien/blob/main/agent/src/server/views.ts): `agent/src/server/views.ts`
- [Pay-by-name check](https://github.com/theyuvan/lien/blob/main/agent/packages/pay-by-name/src/index.ts): `agent/packages/pay-by-name/src/index.ts`
- [Lien web app](https://github.com/theyuvan/lien/blob/main/frontend/src/app/(site)/agents/%5Bname%5D/NameScreen.tsx): `frontend/src/app/(site)/agents/[name]/NameScreen.tsx`
- [Shared UI](https://github.com/theyuvan/lien/blob/main/frontend/src/components/ui.tsx): `frontend/src/components/ui.tsx`

### Other components

- **Borrowing agent**
- **Hedera network**
- **Sepolia ENS**
- **LLM provider**
- **Borrower customers**
- **Outside lenders**

## Connections

- Borrowing agent → Agent API: registers and applies
- Agent API → Lien workflow: routes loan actions
- Lien workflow → Loan state: reads and updates (evidence: `agent/src/lien.ts`)
- Lien workflow → Underwriting rules: assesses application (evidence: `agent/src/lien.ts`)
- Lien workflow → Funding checks: checks payer funding (evidence: `agent/src/lien.ts`)
- Lien workflow → Credit memo: requests memo (evidence: `agent/src/lien.ts`)
- Credit memo → LLM provider: generates assessment (evidence: `agent/src/underwriting/memo.ts`)
- Lien workflow → Revenue sources: uses revenue evidence (evidence: `agent/src/lien.ts`)
- Revenue sources → Mirror node data: reads payment history
- Lien workflow → Revenue split: creates and draws (evidence: `agent/src/lien.ts`)
- Lien workflow → Lending pool: funds approved loan (evidence: `agent/src/lien.ts`)
- Borrower customers → Revenue split: pays for sales
- Revenue split → Borrowing agent: sends remaining share
- Revenue split → Outside lenders: routes repayment share
- Lien workflow → Name vault: requires name collateral (evidence: `agent/src/lien.ts`)
- Name vault → Sepolia ENS: holds ENS name
- ENS records → Sepolia ENS: reads and updates records
- Lien workflow → ENS records: checks name ownership (evidence: `agent/src/lien.ts`)
- Credit subtree → Sepolia ENS: publishes credit records
- Lien workflow → HCS ledger: records decisions and loans (evidence: `agent/src/lien.ts`)
- HCS ledger → Book replay: replays public books
- Book replay → Loan state: rebuilds state
- Loan keeper → Lien workflow: monitors loan lifecycle
- Lien workflow → Hedera network: settles USDC operations
- Outside lenders → Lending pool: deposits capital
- Agent API → MCP access: offers agent operations
- Agent API → Live ENS reads: serves live ENS data
- Agent API → Public views: mounts public views
- Public views → Loan state: reads dashboard state (evidence: `agent/src/server/views.ts`)
- Borrowing agent → Pay-by-name check: checks seller payment target (evidence: `agent/packages/pay-by-name/src/index.ts`)
- Lien web app → Shared UI: renders shared controls
- Lien web app → Agent API: displays loan data
