Contract Architecture

The protocol consists of several core contracts that work together to provide lending functionality:

Core Contracts

  1. Nectra.sol

  • The main contract that inherits from all other functionality contracts

  • Handles position creation, modification, and management

  • Key functions:

    • modifyPosition: Create, modify, or close a position

    • quoteModifyPosition: Get a quote for position modification

    • updatePosition: Update position state with the latest interest

  1. NectraLiquidate.sol

  • Handles position liquidation

  • Implements:

    • Partial liquidation

    • Full liquidation

    • Liquidator rewards

  • Key functions:

    • liquidate: Partially liquidate an undercollateralized position

    • fullLiquidate: Fully liquidate a severely undercollateralized position

  1. NectraRedeem.sol

  • Handles redemption of nUSD for collateral

  • Implements:

    • Dynamic redemption fees

    • Bucket-based redemption

    • Redemption buffer management

  • Key functions:

    • redeem: Redeem nUSD for collateral

    • _redeemFromBuckets: Internal redemption logic

    • _calculateRedemptionFeeAndUpdateBuffer: Fee calculation

  1. NectraFlash.sol

  • Handles flash loans and flash mints

  • Implements:

    • Flash loan cBTC functionality

    • Flash mint nUSD functionality

    • Circuit breaker protection

  • Key functions:

    • flashLoan: Execute a flash loan

    • flashMint: Execute a flash mint

    • _requireFlashBorrowUnlocked: Circuit breaker check

  1. NectraNFT.sol

  • ERC721 token representing positions

  • Handles position ownership and permissions

  • Key functions:

    • mint: Create a new position

    • authorize: Grant permission to manage the position

    • revoke: Revoke position management permission

  1. NUSDToken.sol

  • ERC20 token representing borrowed nUSD

  • Implements minting and burning with access control

  • Key functions:

    • mint: Create new nUSD tokens

    • burn: Destroy nUSD tokens

    • permit: Gasless approvals

  1. OracleAggregator.sol

  • Price feed aggregation

  • Provides:

    • Collateral price updates

    • Price staleness checks

    • Fallback oracle support

  • Key functions:

    • getLatestPrice: Get the most recent valid price

    • _tryGetPrice: Internal price fetching with staleness check

    • _normalize: Normalize price to 18 decimals

Supporting Contracts

  1. NectraBase.sol

  • Base contract containing shared state and constants

  • Defines core data structures:

    • Globals: System-wide state

    • Bucket: Interest rate bucket state

    • Position: Individual position state

  1. NectraLib.sol

  • Core library for state management and calculations

  • Handles:

    • Position and bucket state updates

    • Interest calculations

    • Debt share conversions

    • Liquidation calculations

  1. NectraMathLib.sol

  • Mathematical operations library

  • Provides:

    • Share/asset conversions

    • Safe math operations

    • Bit searching utilities

  1. NectraViews.sol

  • Read-only functions for querying protocol state

  • Key functions:

    • getPosition: Get position collateral and debt

    • getBucketState: Get bucket state

    • getGlobalState: Get system-wide state

    • canLiquidate: Check liquidation eligibility