> For the complete documentation index, see [llms.txt](https://docs.union.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.union.finance/developers/core/lendingmarket.md).

# uToken

@title UToken Contract @dev Union accountBorrows can borrow and repay thru this component.

### BorrowSnapshot

```solidity
struct BorrowSnapshot {
  uint256 principal;
  uint256 interest;
  uint256 interestIndex;
  uint256 lastRepay;
}
```

### WAD

```solidity
uint256 WAD
```

*Wad do you want*

### BORROW\_RATE\_MAX\_MANTISSA

```solidity
uint256 BORROW_RATE_MAX_MANTISSA
```

*Maximum borrow rate that can ever be applied (.005% / block)*

### RESERVE\_FACTORY\_MAX\_MANTISSA

```solidity
uint256 RESERVE_FACTORY_MAX_MANTISSA
```

@dev Maximum fraction of interest that can be set aside for reserves

### initialExchangeRateMantissa

```solidity
uint256 initialExchangeRateMantissa
```

@dev Initial exchange rate used when minting the first UTokens (used when totalSupply = 0)

### reserveFactorMantissa

```solidity
uint256 reserveFactorMantissa
```

@dev Fraction of interest currently set aside for reserves

### accrualBlockNumber

```solidity
uint256 accrualBlockNumber
```

@dev Block number that interest was last accrued at

### borrowIndex

```solidity
uint256 borrowIndex
```

@dev Accumulator of the total earned interest rate since the opening of the market

### totalBorrows

```solidity
uint256 totalBorrows
```

@dev Total amount of outstanding borrows of the underlying in this market

### totalReserves

```solidity
uint256 totalReserves
```

@dev Total amount of reserves of the underlying held in this marke

### totalRedeemable

```solidity
uint256 totalRedeemable
```

@dev Calculates the exchange rate from the underlying to the uToken

### overdueBlocks

```solidity
uint256 overdueBlocks
```

@dev overdue duration, based on the number of blocks

### originationFee

```solidity
uint256 originationFee
```

@dev fee paid at loan origin

### debtCeiling

```solidity
uint256 debtCeiling
```

@dev The debt limit for the whole system

### maxBorrow

```solidity
uint256 maxBorrow
```

@dev Max amount that can be borrowed by a single member

### minBorrow

```solidity
uint256 minBorrow
```

@dev Min amount that can be borrowed by a single member

### assetManager

```solidity
address assetManager
```

@dev Asset manager contract address

### userManager

```solidity
address userManager
```

@dev User manager contract address

### underlying

```solidity
address underlying
```

*Address of underlying token*

### interestRateModel

```solidity
contract IInterestRateModel interestRateModel
```

*Interest rate model used for calculating interest rate*

### accountBorrows

```solidity
mapping(address => struct UToken.BorrowSnapshot) accountBorrows
```

Mapping of account addresses to outstanding borrow balances

### AccrueInterestFailed

```solidity
error AccrueInterestFailed()
```

### AmountExceedGlobalMax

```solidity
error AmountExceedGlobalMax()
```

### AmountExceedMaxBorrow

```solidity
error AmountExceedMaxBorrow()
```

### AmountLessMinBorrow

```solidity
error AmountLessMinBorrow()
```

### AmountZero

```solidity
error AmountZero()
```

### BorrowRateExceedLimit

```solidity
error BorrowRateExceedLimit()
```

### WithdrawFailed

```solidity
error WithdrawFailed()
```

### CallerNotMember

```solidity
error CallerNotMember()
```

### CallerNotUserManager

```solidity
error CallerNotUserManager()
```

### InitExchangeRateNotZero

```solidity
error InitExchangeRateNotZero()
```

### InsufficientFundsLeft

```solidity
error InsufficientFundsLeft()
```

### MemberIsOverdue

```solidity
error MemberIsOverdue()
```

### ReserveFactoryExceedLimit

```solidity
error ReserveFactoryExceedLimit()
```

### DepositToAssetManagerFailed

```solidity
error DepositToAssetManagerFailed()
```

### LogNewMarketInterestRateModel

```solidity
event LogNewMarketInterestRateModel(address oldInterestRateModel, address newInterestRateModel)
```

@dev Change of the interest rate model @param oldInterestRateModel Old interest rate model address @param newInterestRateModel New interest rate model address

### LogMint

```solidity
event LogMint(address minter, uint256 underlyingAmount, uint256 uTokenAmount)
```

@dev Mint uToken by depositing token @param minter address of minter @param underlyingAmount amount of underlying token @param uTokenAmount amount of uToken

### LogRedeem

```solidity
event LogRedeem(address redeemer, uint256 amountIn, uint256 amountOut, uint256 redeemAmount)
```

@dev Redeem token for uToken

### LogReservesAdded

```solidity
event LogReservesAdded(address reserver, uint256 actualAddAmount, uint256 totalReservesNew)
```

@dev Token added to the reserves @param reserver address of sender that added to reservers @param actualAddAmount amount of tokens added @param totalReservesNew new total reserve amount

### LogReservesReduced

```solidity
event LogReservesReduced(address receiver, uint256 reduceAmount, uint256 totalReservesNew)
```

@dev Token removed from the reserves @param receiver reciever address of tokens @param reduceAmount amount of tokens to withdraw @param totalReservesNew new total reserves amount

### LogBorrow

```solidity
event LogBorrow(address account, address to, uint256 amount, uint256 fee)
```

@dev Event borrow @param account Member address @param amount Borrow amount @param fee Origination fee

### LogRepay

```solidity
event LogRepay(address payer, address account, uint256 amount)
```

@dev Event repay @param account Member address @param amount Repay amount

### onlyMember

```solidity
modifier onlyMember(address account)
```

@dev modifier limit member

### onlyUserManager

```solidity
modifier onlyUserManager()
```

### \_\_UToken\_init

```solidity
function __UToken_init(string name_, string symbol_, address underlying_, uint256 initialExchangeRateMantissa_, uint256 reserveFactorMantissa_, uint256 originationFee_, uint256 debtCeiling_, uint256 maxBorrow_, uint256 minBorrow_, uint256 overdueBlocks_, address admin_) public
```

### setAssetManager

```solidity
function setAssetManager(address assetManager_) external
```

@dev set Asset Manager contract address Accept claims only from the admin

### setUserManager

```solidity
function setUserManager(address userManager_) external
```

@dev set User Manager contract address Accept claims only from the admin

### setOriginationFee

```solidity
function setOriginationFee(uint256 originationFee_) external
```

@dev Change loan origination fee value Accept claims only from the admin @param originationFee\_ Fees deducted for each loan transaction

### setDebtCeiling

```solidity
function setDebtCeiling(uint256 debtCeiling_) external
```

@dev Update the market debt ceiling to a fixed amount, for example, 1 billion DAI etc. Accept claims only from the admin @param debtCeiling\_ The debt limit for the whole system

### setMinBorrow

```solidity
function setMinBorrow(uint256 minBorrow_) external
```

@dev Update the minimum loan size Accept claims only from the admin @param minBorrow\_ Minimum loan amount per user

### setMaxBorrow

```solidity
function setMaxBorrow(uint256 maxBorrow_) external
```

@dev Update the max loan size Accept claims only from the admin @param maxBorrow\_ Max loan amount per user

### setOverdueBlocks

```solidity
function setOverdueBlocks(uint256 overdueBlocks_) external
```

@dev Change loan overdue duration, based on the number of blocks Accept claims only from the admin @param overdueBlocks\_ Maximum late repayment block. The number of arrivals is a default

### setInterestRateModel

```solidity
function setInterestRateModel(address newInterestRateModel_) external
```

@dev Change to a different interest rate model Accept claims only from the admin @param newInterestRateModel\_ New interest rate model address

### setReserveFactor

```solidity
function setReserveFactor(uint256 reserveFactorMantissa_) external
```

@dev set reserve factor mantissa Accept claims only from the admin

### getRemainingDebtCeiling

```solidity
function getRemainingDebtCeiling() public view returns (uint256)
```

@dev Returns the remaining amount that can be borrowed from the market. @return Remaining total amount

### getLastRepay

```solidity
function getLastRepay(address account) public view returns (uint256)
```

@dev Get the last repay block @param account Member address @return lastRepay

### checkIsOverdue

```solidity
function checkIsOverdue(address account) public view returns (bool isOverdue)
```

@dev Check if the member's loan is overdue @param account Member address @return isOverdue

### calculatingFee

```solidity
function calculatingFee(uint256 amount) public view returns (uint256)
```

@dev Get the origination fee @param amount Amount to be calculated @return Handling fee

### getBorrowed

```solidity
function getBorrowed(address account) public view returns (uint256)
```

@dev Get the borrowed principle @param account Member address @return borrowed

### borrowBalanceView

```solidity
function borrowBalanceView(address account) public view returns (uint256)
```

@dev Get a member's current owed balance, including the principle and interest but without updating the user's states. @param account Member address @return Borrowed amount

### borrowBalanceStoredInternal

```solidity
function borrowBalanceStoredInternal(address account) internal view returns (uint256)
```

@dev Get a member's total owed, including the principle and the interest calculated based on the interest index. @param account Member address @return Borrowed amount

### borrowRatePerBlock

```solidity
function borrowRatePerBlock() public view returns (uint256)
```

@dev Get the borrowing interest rate per block @return Borrow rate

### supplyRatePerBlock

```solidity
function supplyRatePerBlock() external view returns (uint256)
```

Returns the current per-block supply interest rate for this UToken

#### Return Values

| Name | Type    | Description                                        |
| ---- | ------- | -------------------------------------------------- |
| \[0] | uint256 | The supply interest rate per block, scaled by 1e18 |

### exchangeRateStored

```solidity
function exchangeRateStored() public view returns (uint256)
```

Calculates the exchange rate from the underlying to the UToken

*This function does not accrue interest before calculating the exchange rate*

#### Return Values

| Name | Type    | Description                             |
| ---- | ------- | --------------------------------------- |
| \[0] | uint256 | Calculated exchange rate scaled by 1e18 |

### calculatingInterest

```solidity
function calculatingInterest(address account) public view returns (uint256)
```

@dev Calculating member's borrowed interest @param account Member address @return Interest amount

### exchangeRateCurrent

```solidity
function exchangeRateCurrent() public returns (uint256)
```

Accrue interest then return the up-to-date exchange rate

#### Return Values

| Name | Type    | Description                             |
| ---- | ------- | --------------------------------------- |
| \[0] | uint256 | Calculated exchange rate scaled by 1e18 |

### balanceOfUnderlying

```solidity
function balanceOfUnderlying(address owner) external view returns (uint256)
```

Get the underlying balance of the `owner`

*This also accrues interest in a transaction*

#### Parameters

| Name  | Type    | Description                         |
| ----- | ------- | ----------------------------------- |
| owner | address | The address of the account to query |

#### Return Values

| Name | Type    | Description                               |
| ---- | ------- | ----------------------------------------- |
| \[0] | uint256 | The amount of underlying owned by `owner` |

### borrow

```solidity
function borrow(address to, uint256 amount) external
```

@dev Borrowing from the market Accept claims only from the member Borrow amount must in the range of creditLimit, minBorrow, maxBorrow, debtCeiling and not overdue @param amount Borrow amount

### repayBorrow

```solidity
function repayBorrow(address borrower, uint256 repayAmount) external
```

Repay outstanding borrow

*Repay borrow see \_repayBorrowFresh*

### \_repayBorrowFresh

```solidity
function _repayBorrowFresh(address payer, address borrower, uint256 amount) internal
```

@dev Repay the loan Accept claims only from the member Updated member lastPaymentEpoch only when the repayment amount is greater than interest @param payer Payer address @param borrower Borrower address @param amount Repay amount

### accrueInterest

```solidity
function accrueInterest() public returns (bool)
```

@dev Accrue interest @return Accrue interest finished

### debtWriteOff

```solidity
function debtWriteOff(address borrower, uint256 amount) external
```

### mint

```solidity
function mint(uint256 mintAmount) external
```

*Mint uTokens by depositing tokens*

#### Parameters

| Name       | Type    | Description               |
| ---------- | ------- | ------------------------- |
| mintAmount | uint256 | Amount of uTokens to mint |

### redeem

```solidity
function redeem(uint256 amountIn, uint256 amountOut) external
```

User redeems uTokens in exchange for the underlying asset

*Assumes interest has already been accrued up to the current block*

#### Parameters

| Name      | Type    | Description                                                                                         |
| --------- | ------- | --------------------------------------------------------------------------------------------------- |
| amountIn  | uint256 | The number of uTokens to redeem into underlying (only one of amountIn or amountOut may be non-zero) |
| amountOut | uint256 | The number of underlying tokens to receive from (only one of amountIn or amountOut may be non-zero) |

### addReserves

```solidity
function addReserves(uint256 addAmount) external
```

*Add tokens to the reseve*

#### Parameters

| Name      | Type    | Description             |
| --------- | ------- | ----------------------- |
| addAmount | uint256 | amount of tokens to add |

### removeReserves

```solidity
function removeReserves(address receiver, uint256 reduceAmount) external
```

*Remove tokens to the reseve*

#### Parameters

| Name         | Type    | Description                |
| ------------ | ------- | -------------------------- |
| receiver     | address | address to recieve tokens  |
| reduceAmount | uint256 | amount of tokens to remove |

### getBlockNumber

```solidity
function getBlockNumber() internal view returns (uint256)
```

@dev Function to simply retrieve block number This exists mainly for inheriting test contracts to stub this result.

### \_depositToAssetManager

```solidity
function _depositToAssetManager(uint256 amount) internal
```

@dev Deposit tokens to the asset manager
