Natrium docs
  • ๐ŸงชOverview
    • ๐Ÿ“–Introducing Natrium
    • ๐Ÿ”ฎVision
  • โš™๏ธProtocol Overview
    • ๐ŸŒProtocol architecture
      • โšœ๏ธNatrium Core & Enterprise
    • ๐ŸŒŠLiquidations
    • ๐Ÿ“ˆInterest Rate Models
    • โšกOracles
    • โš–๏ธProtocol Fees
  • ๐Ÿ’ฐTokenomics
    • ๐Ÿš€Presale & Launch
    • ๐ŸงฌNTM & NaCl
  • โš’๏ธDev Docs
    • ๐Ÿ”‘Contract specs
      • Isolated Pools (Layer 1)
        • Bucket config
        • Supply underlying asset
        • Withdraw underlying asset
        • Deposit OverCollateralized asset
        • Borrow
        • Repay
        • Withdraw OverCollaterlized asset
        • Liquidate
      • Shufflers (Layer 2)
        • Contract functionalities
          • Shuffler Creation
          • Shuffler Management
          • Supply Cap Management
          • Risk Exposure Management
      • Error Library
        • Error lib
  • ๐Ÿ“šLibrary
    • Brand Assets
    • Community Links
    • Legal Disclaimer
Powered by GitBook
On this page
  1. โš’๏ธDev Docs
  2. ๐Ÿ”‘Contract specs
  3. Isolated Pools (Layer 1)

Borrow

PreviousDeposit OverCollateralized assetNextRepay

Last updated 1 year ago

CtrlK

borrow function is used in borrowing underlying assets from the protocol .

function borrow(
        MarketParams memory marketParams,
        uint256 assets,
        uint256 shares,
        address onBehalf,
        address receiver
    ) external returns (uint256, uint256) {
        Id id = marketParams.id();
        require(market[id].lastUpdate != 0, ErrorsLib.MARKET_NOT_CREATED);
        require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT);
        require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
        // No need to verify that onBehalf != address(0) thanks to the following authorization check.
        require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED);

        _accrueInterest(marketParams, id);

        if (assets > 0) shares = assets.toSharesUp(market[id].totalBorrowAssets, market[id].totalBorrowShares);
        else assets = shares.toAssetsDown(market[id].totalBorrowAssets, market[id].totalBorrowShares);

        position[id][onBehalf].borrowShares += shares.toUint128();
        market[id].totalBorrowShares += shares.toUint128();
        market[id].totalBorrowAssets += assets.toUint128();

        require(_isHealthy(marketParams, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL);
        require(market[id].totalBorrowAssets <= market[id].totalSupplyAssets, ErrorsLib.INSUFFICIENT_LIQUIDITY);

        emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, assets, shares);

        IERC20(marketParams.loanToken).safeTransfer(receiver, assets);

        return (assets, shares);
    }