# Security Architecture

Morfa is built around a **closed-loop, noncustodial security model**: user USDC never enters a Morfa-controlled custody wallet, never moves through arbitrary external routers or swaps, and remains inside immutable **Morpho** smart contracts between transactions.

***

## Mode B Security Model (`Morpho Vault V2`)

Mode B runs entirely on official, immutable **Morpho Vault V2** smart contracts deployed via Morpho's permissionless `VaultV2Factory` (`0xA1D94F746dEfa1928926b84fB2596c06926C0405`):

* **On-chain native NAV accounting:** Vault share value derives strictly from on-chain balances and Morpho Blue positions via `realAssets()`; there is no off-chain oracle pricing `mfUSDC` shares.
* **ERC-4626 virtual share offset:** `mfUSDC` uses `18` share decimals (`6` USDC decimals + `12` virtual offset) to defend against first-depositor inflation attacks.
* **Adapter Registry abdication:** The vault's `AdapterRegistry` (`0x3696c5eAe4a7Ffd04Ea163564571E9CD8Ed9364e`) is **permanently abdicated**, ensuring only registry-verified Morpho adapters can ever be attached.
* **Zero transfer/deposit gates:** `setSendSharesGate`, `setReceiveSharesGate`, and `setReceiveAssetsGate` are **permanently abdicated** (no transfer restrictions or deposit blacklists can be introduced on `mfUSDC`).
* **Strictly bounded Allocator:** The automated worker only holds Morpho Vault V2's `Allocator` and `Sentinel` roles. It can reallocate USDC only among Curator-approved markets within Curator-defined absolute, relative, and collateral caps. It cannot withdraw assets to itself or to any external address.

***

## Mode A Security Model (`MorfaRebalancer.sol`)

Mode A uses a single custom smart contract, **`MorfaRebalancer.sol`**, written in Solidity `0.8.28` targeting the Cancun EVM. Its attack surface is intentionally minimal and enforced by bytecode immutability:

| Property | Design Guarantee |
| --- | --- |
| **Immutability** | Non-upgradeable. **No proxy**, **no `delegatecall`**, **no `selfdestruct`**, and **no arbitrary external calls**. |
| **Narrow Morpho surface** | Only calls `Morpho.withdraw` and `Morpho.supply` (plus `setAuthorizationWithSig` during EOA `enableWithSig`). Contains **no `borrow`** and **no `withdrawCollateral`** functions anywhere in the bytecode. |
| **Audited OpenZeppelin v5 base** | Built on `AccessControlDefaultAdminRules`, `Pausable`, `ReentrancyGuardTransient` (EIP-1153 transient storage), `SafeERC20`, `Math.mulDiv`, `SafeCast`, `Time`, and `EnumerableSet`. |
| **Checks-Effects-Interactions (CEI)** | Updates `lastRebalanceAt`, increments `nonce`, and snapshots `usdcBefore` before calling Morpho Blue; any failure reverts state atomically. |
| **Donation-safe balance conservation** | Snapshots `usdcBefore = USDC.balanceOf(address(this))` prior to withdrawal and asserts `USDC.balanceOf(address(this)) == usdcBefore + res.fee` after re-supply. Unexpected direct USDC transfers to the contract cannot break accounting or trap user funds. |
| **Zero user principal custody** | Between transactions, the contract holds **zero wei** of user principal, retaining only accrued protocol fees awaiting pull-based `claimFees()`. |
| **Hard on-chain safety checks** | Enforces `loanToken == USDC`, `fromStatus != Disallowed`, `toStatus == Active`, target `totalSupplyAssets ≥ 100,000 USDC`, target post-deposit utilization `≤ 95%`, `sharesOut ≥ minSharesOut`, and `≤ 2 wei` rounding loss (`MAX_ROUNDING_LOSS`). |
| **Per-user batch isolation** | `rebalanceBatch` wraps each user item inside a self-called `executeBatchItem` `try / catch`, ensuring one reverting user never blocks or corrupts another user's rebalance. |

***

## Execution & Frontend Defenses

Security continues across both the user-facing application and the automated worker:

* **Client-side transaction construction:** `morfa.finance` builds all user transactions locally in the browser using `@morpho-org/morpho-sdk` v10 (`VaultBundlesV1` / `BlueBundlesV1`) and `viem`. Morfa servers never see private keys and never sign on a user's behalf.
* **Mandatory pre-flight simulation (`eth_call`):** Both `morfa-app` (before prompting your wallet) and `morfa-worker` (before signing a rebalance) simulate transactions against live Ethereum state and abort on any revert.
* **Private RPC broadcast:** Automated keeper and allocator transactions are sent over a **Private RPC** channel to protect against public-mempool front-running and sandwich extraction.
* **Fail-closed telemetry:** If Morpho Core REST, Merkl v4, or single-block RPC verification returns incomplete or missing market data, the worker skips automated reallocation rather than acting on assumptions.

## Related Security Pages

* [Roles, Governance & Timelocks](/security/governance)
* [Morpho Authorization Scope (Mode A)](/security/authorization-scope)
* [Risks & Mitigations](/security/risks)
* [Pauses & Incident Response](/security/emergency-and-pauses)
* [Audits & Verification](/security/audits)
