# dcablorh/aegis architecture

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

- Interactive diagram: https://gitdiagram.com/dcablorh/aegis
- Repository: https://github.com/dcablorh/aegis

## Overview

AegisMargin is a prototype for confidential repayment protection on NEAR. The intended keeper workflow moves from a signed position and price snapshot through vault checks and an OutLayer enclave decision, then to a verified ticket, reserve-token deposit, intent-status handling, and Burrow repayment. The web app supports read-only position discovery, local policy drafting, and quote views; it does not connect a wallet or submit transactions. Several page and integration implementations were not sampled, so those branches follow README descriptions and are cited there.

Main entry points: [`contracts/aegis-vault/src/rescue.rs`](https://github.com/dcablorh/aegis/blob/main/contracts/aegis-vault/src/rescue.rs) (Vault lifecycle), [`enclave/aegis-wasi/src/lib.rs`](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/lib.rs) (Rescue decision), [`apps/web/src/App.tsx`](https://github.com/dcablorh/aegis/blob/main/apps/web/src/App.tsx) (Web routes), [`contracts/aegis-position/src/lib.rs`](https://github.com/dcablorh/aegis/blob/main/contracts/aegis-position/src/lib.rs) (Position account)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web application"]
  node_webapp["Web routes<br/>[App.tsx]"]
  node_position_views["Position views"]
  node_policy_draft["Policy draft"]
  node_quote_views["Quote views<br/>[QuoteNewPage.tsx]"]
  node_session[("Browser session<br/>[session.ts]")]
end

subgraph group_clients["Client services"]
  node_rhea["Burrow reader<br/>[burrow.ts]"]
  node_rhea_math["Position math<br/>[math.ts]"]
  node_policy_crypto["Policy crypto<br/>[commitment.ts]"]
  node_intent_quote["1Click quote client<br/>[quote.ts]"]
end

subgraph group_protection["Protection protocol"]
  node_vault["Vault lifecycle<br/>[rescue.rs]"]
  node_oracle_check["Oracle verification<br/>[oracle.rs]"]
  node_wire_types["Protocol types<br/>[lib.rs]"]
end

subgraph group_enclave["Enclave decisioning"]
  node_enclave["Rescue decision<br/>[lib.rs]"]
  node_enclave_math["Health sizing<br/>[math.rs]"]
  node_enclave_oracle["Feed verification<br/>[oracle.rs]"]
  node_quote_check["Quote checks<br/>[quote.rs]"]
  node_enclave_crypto["Decrypt and sign<br/>[crypto.rs]"]
end

subgraph group_settlement["Settlement"]
  node_position_contract["Position account<br/>[lib.rs]"]
  node_near_burrow["Burrow protocol"]
  node_intents["NEAR Intents"]
end

node_borrower(("Borrower"))
node_keeper(("Keeper"))

node_borrower -->|"browses"| node_webapp
node_webapp -->|"routes to"| node_position_views
node_webapp -->|"routes to"| node_policy_draft
node_webapp -->|"routes to"| node_quote_views
node_position_views -->|"records reads"| node_session
node_policy_draft -->|"encrypts policy"| node_policy_crypto
node_policy_draft -->|"stores draft"| node_session
node_position_views -->|"reads position"| node_rhea
node_rhea -->|"uses"| node_rhea_math
node_quote_views -->|"requests quote"| node_intent_quote
node_keeper -->|"triggers rescue"| node_vault
node_vault -->|"verifies feed"| node_oracle_check
node_vault -->|"invokes"| node_enclave
node_enclave -->|"verifies prices"| node_enclave_oracle
node_enclave -->|"decrypts and signs"| node_enclave_crypto
node_enclave -->|"sizes repayment"| node_enclave_math
node_enclave -->|"checks quote"| node_quote_check
node_enclave -->|"requests quote"| node_intents
node_vault -->|"dispatches deposit"| node_position_contract
node_position_contract -->|"sends reserve tokens"| node_intents
node_keeper -->|"executes ticket"| node_vault
node_keeper -->|"reports status"| node_vault
node_vault -->|"settles repayment"| node_position_contract
node_position_contract -->|"repays debt"| node_near_burrow
node_vault -->|"uses protocol data"| node_wire_types

click node_webapp "https://github.com/dcablorh/aegis/blob/main/apps/web/src/App.tsx"
click node_position_views "https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/PositionDetailPage.tsx"
click node_policy_draft "https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/ProtectionNewPage.tsx"
click node_quote_views "https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/QuoteNewPage.tsx"
click node_session "https://github.com/dcablorh/aegis/blob/main/apps/web/src/lib/session.ts"
click node_rhea "https://github.com/dcablorh/aegis/blob/main/packages/rhea-client/src/burrow.ts"
click node_rhea_math "https://github.com/dcablorh/aegis/blob/main/packages/rhea-client/src/math.ts"
click node_policy_crypto "https://github.com/dcablorh/aegis/blob/main/packages/shared-types/src/commitment.ts"
click node_intent_quote "https://github.com/dcablorh/aegis/blob/main/packages/intents-client/src/quote.ts"
click node_vault "https://github.com/dcablorh/aegis/blob/main/contracts/aegis-vault/src/rescue.rs"
click node_oracle_check "https://github.com/dcablorh/aegis/blob/main/contracts/aegis-vault/src/oracle.rs"
click node_wire_types "https://github.com/dcablorh/aegis/blob/main/crates/aegis-types/src/lib.rs"
click node_enclave "https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/lib.rs"
click node_enclave_math "https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/math.rs"
click node_enclave_oracle "https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/oracle.rs"
click node_quote_check "https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/quote.rs"
click node_enclave_crypto "https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/crypto.rs"
click node_position_contract "https://github.com/dcablorh/aegis/blob/main/contracts/aegis-position/src/lib.rs"

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_webapp,node_position_views,node_policy_draft,node_quote_views,node_session toneBlue
class node_rhea,node_rhea_math,node_policy_crypto,node_intent_quote toneAmber
class node_vault,node_oracle_check,node_wire_types toneMint
class node_enclave,node_enclave_math,node_enclave_oracle,node_quote_check,node_enclave_crypto toneRose
class node_position_contract,node_near_burrow,node_intents,node_borrower,node_keeper toneIndigo
```

## Components

### Web application

- [Web routes](https://github.com/dcablorh/aegis/blob/main/apps/web/src/App.tsx): `apps/web/src/App.tsx`
- [Position views](https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/PositionDetailPage.tsx): `apps/web/src/pages/PositionDetailPage.tsx`
- [Policy draft](https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/ProtectionNewPage.tsx): `apps/web/src/pages/ProtectionNewPage.tsx`
- [Quote views](https://github.com/dcablorh/aegis/blob/main/apps/web/src/pages/QuoteNewPage.tsx): `apps/web/src/pages/QuoteNewPage.tsx`
- [Browser session](https://github.com/dcablorh/aegis/blob/main/apps/web/src/lib/session.ts): `apps/web/src/lib/session.ts`

### Client services

- [Burrow reader](https://github.com/dcablorh/aegis/blob/main/packages/rhea-client/src/burrow.ts): `packages/rhea-client/src/burrow.ts`
- [Position math](https://github.com/dcablorh/aegis/blob/main/packages/rhea-client/src/math.ts): `packages/rhea-client/src/math.ts`
- [Policy crypto](https://github.com/dcablorh/aegis/blob/main/packages/shared-types/src/commitment.ts): `packages/shared-types/src/commitment.ts`
- [1Click quote client](https://github.com/dcablorh/aegis/blob/main/packages/intents-client/src/quote.ts): `packages/intents-client/src/quote.ts`

### Protection protocol

- [Vault lifecycle](https://github.com/dcablorh/aegis/blob/main/contracts/aegis-vault/src/rescue.rs): `contracts/aegis-vault/src/rescue.rs`
- [Oracle verification](https://github.com/dcablorh/aegis/blob/main/contracts/aegis-vault/src/oracle.rs): `contracts/aegis-vault/src/oracle.rs`
- [Protocol types](https://github.com/dcablorh/aegis/blob/main/crates/aegis-types/src/lib.rs): `crates/aegis-types/src/lib.rs`

### Enclave decisioning

- [Rescue decision](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/lib.rs): `enclave/aegis-wasi/src/lib.rs`
- [Health sizing](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/math.rs): `enclave/aegis-wasi/src/math.rs`
- [Feed verification](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/oracle.rs): `enclave/aegis-wasi/src/oracle.rs`
- [Quote checks](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/quote.rs): `enclave/aegis-wasi/src/quote.rs`
- [Decrypt and sign](https://github.com/dcablorh/aegis/blob/main/enclave/aegis-wasi/src/crypto.rs): `enclave/aegis-wasi/src/crypto.rs`

### Settlement

- [Position account](https://github.com/dcablorh/aegis/blob/main/contracts/aegis-position/src/lib.rs): `contracts/aegis-position/src/lib.rs`
- **Burrow protocol**
- **NEAR Intents**

### Other components

- **Borrower**
- **Keeper**

## Connections

- Borrower → Web routes: browses (evidence: `README.md`)
- Web routes → Position views: routes to (evidence: `apps/web/src/App.tsx`)
- Web routes → Policy draft: routes to (evidence: `apps/web/src/App.tsx`)
- Web routes → Quote views: routes to (evidence: `apps/web/src/App.tsx`)
- Position views → Browser session: records reads (evidence: `apps/web/src/pages/PositionDetailPage.tsx`)
- Policy draft → Policy crypto: encrypts policy (evidence: `apps/web/src/pages/ProtectionNewPage.tsx`)
- Policy draft → Browser session: stores draft (evidence: `apps/web/src/pages/ProtectionNewPage.tsx`)
- Position views → Burrow reader: reads position (evidence: `README.md`)
- Burrow reader → Position math: uses (evidence: `packages/rhea-client/src/burrow.ts`)
- Quote views → 1Click quote client: requests quote (evidence: `README.md`)
- Keeper → Vault lifecycle: triggers rescue (evidence: `README.md`)
- Vault lifecycle → Oracle verification: verifies feed (evidence: `contracts/aegis-vault/src/rescue.rs`)
- Vault lifecycle → Rescue decision: invokes (evidence: `README.md`)
- Rescue decision → Feed verification: verifies prices (evidence: `enclave/aegis-wasi/src/lib.rs`)
- Rescue decision → Decrypt and sign: decrypts and signs (evidence: `enclave/aegis-wasi/src/lib.rs`)
- Rescue decision → Health sizing: sizes repayment (evidence: `enclave/aegis-wasi/src/lib.rs`)
- Rescue decision → Quote checks: checks quote (evidence: `enclave/aegis-wasi/src/lib.rs`)
- Rescue decision → NEAR Intents: requests quote (evidence: `README.md`)
- Vault lifecycle → Position account: dispatches deposit (evidence: `contracts/aegis-vault/src/rescue.rs`)
- Position account → NEAR Intents: sends reserve tokens (evidence: `README.md`)
- Keeper → Vault lifecycle: executes ticket (evidence: `README.md`)
- Keeper → Vault lifecycle: reports status (evidence: `README.md`)
- Vault lifecycle → Position account: settles repayment (evidence: `README.md`)
- Position account → Burrow protocol: repays debt (evidence: `README.md`)
- Vault lifecycle → Protocol types: uses protocol data (evidence: `contracts/aegis-vault/src/rescue.rs`)
