> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inkwell.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Embedded Architecture

> On-chain modules, SDK components, and data flows that power Leviathan's Embedded Lending product.

> **CONFIDENTIAL & PROPRIETARY © 2026 Inkwell Finance, Inc. All Rights Reserved.**
>
> This document is for informational purposes only and does not constitute legal, tax, or investment advice, nor an offer to sell or a solicitation to buy any security or other financial instrument. Any examples, structures, or flows described here are design intent only and may change.

## Overview

Embedded Lending is built on the same core Leviathan infrastructure — Solana programs for lending logic, Sui for policy enforcement, and EVM contracts for revenue capture — with additional modules that scope capital to a single integrating protocol.

This page describes the on-chain modules, the integration flow, and the security model at a high level.

## On-Chain Modules

```
┌────────────────────────────────────────────────────────┐
│                  Leviathan Core (Solana)                │
│                                                        │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │ Embedded Loan│  │ Credit Line  │  │  Agreement   │  │
│  │  Lifecycle   │  │  (Pre-Auth)  │  │   Bridge     │  │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘  │
│         │                 │                 │          │
│  ┌──────▼───────┐  ┌──────▼───────┐  ┌──────▼───────┐  │
│  │ Function     │  │  Protocol    │  │  Splitter    │  │
│  │   Gate       │  │  Whitelist   │  │  Registry    │  │
│  └──────────────┘  └──────────────┘  └──────────────┘  │
└────────────────────────────────────────────────────────┘
```

### Embedded Loan Lifecycle

Manages the full lifecycle of an embedded loan — origination, activation, repayment tracking, and completion. This module operates within the `leviathan-core` program but scopes the loan to a single protocol's sandbox.

Key differences from standard loans:

* The loan is bound to a specific protocol whitelist at creation
* Policy parameters are derived from the protocol's approved function set
* Completion or default triggers protocol-specific settlement flows

### Credit Line (Pre-Authorization)

Enables pre-approved borrowing limits for users who meet credit criteria. Rather than applying for individual loans, users receive a credit line that they can draw against as needed within the protocol.

* Credit limits are determined by the user's on-chain score from `leviathan-score`
* Drawing against the credit line creates individual loan positions
* The credit line can be revoked or adjusted based on ongoing behavior

### Function Gate

Restricts which smart contract functions borrowed capital can interact with. This is the core caging mechanism that ensures capital stays within the protocol's approved operations.

* Each protocol defines an approved function set during onboarding
* Every transaction signed with borrowed capital is checked against the gate
* Transactions targeting non-approved functions are rejected before signing

Function-level gating goes beyond simple address whitelisting — a borrower might be approved to call `swap()` on a DEX contract but blocked from calling `withdraw()` on the same contract.

### Protocol Whitelist

Maintains the set of approved protocol destinations — contract addresses, program IDs, and account structures that borrowed capital is permitted to interact with.

* Configured during protocol onboarding
* Immutable for the duration of individual loans
* Shared across all embedded loans for a given protocol integration

### Agreement Bridge

Links legal agreements to on-chain loan state using cryptographic hashes. This extends the InkPact system to embedded contexts, ensuring that the terms governing a user's credit line are verifiable on-chain.

* Agreement documents are hashed and stored alongside loan accounts
* Both parties can independently verify term accuracy
* Supports protocol-specific agreement templates

### Splitter Registry

Manages revenue split configurations for embedded loans. Each protocol integration has a registered splitter configuration that determines how revenue flows between the borrower, the protocol, and loan repayment.

* Split ratios are set during protocol onboarding
* Individual loans inherit the protocol's splitter configuration
* Revenue oracles report split events back to the loan account

## Integration Flow

The end-to-end flow from protocol onboarding through loan completion:

```
┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  Protocol   │     │    User     │     │   Revenue   │
│  Onboards   │     │ Pre-Qualifies│    │   Splits    │
│             │     │             │     │   Begin     │
│ • Whitelist │     │ • Credit    │     │             │
│ • Functions │     │   scoring   │     │ • Auto      │
│ • Splitter  │     │ • Agreement │     │   repayment │
│ • Agreement │     │ • Credit    │     │ • Oracle    │
│   template  │     │   line set  │     │   reports   │
└──────┬──────┘     └──────┬──────┘     └──────┬──────┘
       │                   │                   │
       ▼                   ▼                   ▼
┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│    Loan     │     │   Capital   │     │  Repayment  │
│  Originated │     │   Caged to  │     │  or         │
│             │     │   Protocol  │     │  Completion │
│ • Terms set │     │             │     │             │
│ • Policy    │     │ • Function  │     │ • Cap       │
│   bound     │     │   gating    │     │   reached   │
│ • Capital   │     │ • Tx verify │     │ • Loan      │
│   deployed  │     │ • Session   │     │   closed    │
│             │     │   keys      │     │             │
└─────────────┘     └─────────────┘     └─────────────┘
```

1. **Protocol Onboards** — the protocol registers its contract set, approved functions, and revenue split configuration with Leviathan
2. **User Pre-Qualifies** — the user's credit score is assessed via `leviathan-score`, and a credit line is established with protocol-specific agreement terms
3. **Loan Originated** — when the user draws on their credit line, a loan position is created with policy parameters derived from the protocol's configuration
4. **Capital Caged** — borrowed capital is deployed into a policy-gated environment where it can only interact with the protocol's approved functions. Every transaction is verified by `leviathan-verifier`
5. **Revenue Splits** — as the user generates revenue through protocol activity, splitter contracts automatically direct a portion to loan repayment
6. **Completion** — once the repayment cap is reached, the loan closes and all restrictions are lifted

## Security Model

### Function-Level Gating

The function gate is the primary security mechanism for embedded loans:

* **Pre-signing verification** — every transaction is checked against the approved function set before it reaches the dWallet signing flow
* **Defense in depth** — the `leviathan-verifier` program provides an independent validation layer, parsing transaction calldata to confirm policy compliance
* **No bypass path** — there is no mechanism to override or relax the function gate during an active loan

### Capital Isolation

Borrowed capital is isolated to the integrating protocol through multiple enforcement layers:

| Layer                    | Enforcement                                           |
| ------------------------ | ----------------------------------------------------- |
| **Function Gate**        | Only approved functions on approved contracts         |
| **Policy dWallet**       | Threshold signing rejects non-compliant transactions  |
| **Transaction Verifier** | Independent calldata parsing and validation           |
| **Session Key Scope**    | Ephemeral keys scoped to protocol-specific operations |

### Revenue Oracle Integrity

Revenue split events on EVM chains are reported back to Solana via oracle attestations:

* Oracle operators submit cryptographically signed attestations
* The core program validates attestation signatures before updating loan state
* Stale or invalid attestations are rejected

## Integration Points

### SDK Client

The `@leviathan/sdk/embedded` subpath exports the `EmbeddedLendingClient`, which provides:

* Credit line eligibility checks
* Agreement creation and signing
* Loan origination and drawdown
* Capital usage validation
* Splitter management and revenue tracking

### On-Chain Programs

Embedded Lending uses all four Leviathan Solana programs:

| Program              | Role in Embedded                                           |
| -------------------- | ---------------------------------------------------------- |
| `leviathan-core`     | Embedded loan lifecycle, function gate, protocol whitelist |
| `leviathan-pool`     | Capital sourcing from lending pools                        |
| `leviathan-score`    | User credit assessment and pre-qualification               |
| `leviathan-verifier` | Transaction validation against protocol function set       |

### Revenue Oracle

A dedicated revenue oracle monitors splitter contract events on EVM chains and reports repayment progress to the Solana loan account. The oracle is configured per-protocol during onboarding.
