# DInterest

## `deposit(uint256 depositAmount, uint64 maturationTimestamp) → uint64 depositID, uint256 interestAmount` (external) <a href="#deposit-uint256-depositamount-uint64-maturationtimestamp-uint64-depositid-uint256-interestamount-ext" id="deposit-uint256-depositamount-uint64-maturationtimestamp-uint64-depositid-uint256-interestamount-ext"></a>

Create a deposit using `depositAmount` stablecoin that matures at timestamp `maturationTimestamp`.

@dev The ERC-721 NFT representing deposit ownership is given to msg.sender

@param depositAmount The amount of deposit, in stablecoin

@param maturationTimestamp The Unix timestamp of maturation, in seconds

@return depositID The ID of the created deposit

@return interestAmount The amount of fixed-rate interest

## `topupDeposit(uint64 depositID, uint256 depositAmount) → uint256 interestAmount` (external) <a href="#topupdeposit-uint64-depositid-uint256-depositamount-uint256-interestamount-external" id="topupdeposit-uint64-depositid-uint256-depositamount-uint256-interestamount-external"></a>

Add `depositAmount` stablecoin to the existing deposit with ID `depositID`.

@dev The interest rate for the topped up funds will be the current oracle rate.

@param depositID The deposit to top up

@param depositAmount The amount to top up, in stablecoin

@return interestAmount The amount of interest that will be earned by the topped up funds at maturation

## `rolloverDeposit(uint64 depositID, uint64 maturationTimestamp) → uint256 newDepositID, uint256 interestAmount` (external) <a href="#rolloverdeposit-uint64-depositid-uint64-maturationtimestamp-uint256-newdepositid-uint256-interestamo" id="rolloverdeposit-uint64-depositid-uint64-maturationtimestamp-uint256-newdepositid-uint256-interestamo"></a>

Withdraw all funds from deposit with ID `depositID` and use them to create a new deposit that matures at time `maturationTimestamp`

@param depositID The deposit to roll over

@param maturationTimestamp The Unix timestamp of the new deposit, in seconds

@return newDepositID The ID of the new deposit

## `withdraw(uint64 depositID, uint256 virtualTokenAmount, bool early) → uint256 withdrawnStablecoinAmount` (external) <a href="#withdraw-uint64-depositid-uint256-virtualtokenamount-bool-early-uint256-withdrawnstablecoinamount-ex" id="withdraw-uint64-depositid-uint256-virtualtokenamount-bool-early-uint256-withdrawnstablecoinamount-ex"></a>

Withdraws funds from the deposit with ID `depositID`.

@dev Virtual tokens behave like zero coupon bonds, after maturation withdrawing 1 virtual token yields 1 stablecoin. The total supply is given by deposit.virtualTokenTotalSupply

@param depositID the deposit to withdraw from

@param virtualTokenAmount the amount of virtual tokens to withdraw

@param early True if intend to withdraw before maturation, false otherwise

@return withdrawnStablecoinAmount the amount of stablecoins withdrawn

## `fund(uint64 depositID, uint256 fundAmount) → uint64 fundingID` (external) <a href="#fund-uint64-depositid-uint256-fundamount-uint64-fundingid-external" id="fund-uint64-depositid-uint256-fundamount-uint64-fundingid-external"></a>

Funds the fixed-rate interest of the deposit with ID `depositID`. In exchange, the funder receives the future floating-rate interest generated by the portion of the deposit whose interest was funded.

@dev The sender receives ERC-1155 multitokens (fundingMultitoken) representing their floating-rate bonds.

@param depositID The deposit whose fixed-rate interest will be funded

@param fundAmount The amount of fixed-rate interest to fund. If it exceeds surplusOfDeposit(depositID), it will be set to the surplus value instead.

@param fundingID The ID of the fundingMultitoken the sender received

## `payInterestToFunders(uint64 fundingID) → uint256 interestAmount` (external) <a href="#payinteresttofunders-uint64-fundingid-uint256-interestamount-external" id="payinteresttofunders-uint64-fundingid-uint256-interestamount-external"></a>

Distributes the floating-rate interest accrued by a deposit to the floating-rate bond holders.

@param fundingID The ID of the floating-rate bond

@return interestAmount The amount of interest distributed, in stablecoins

## `calculateInterestAmount(uint256 depositAmount, uint256 depositPeriodInSeconds) → uint256 interestAmount` (public) <a href="#calculateinterestamount-uint256-depositamount-uint256-depositperiodinseconds-uint256-interestamount" id="calculateinterestamount-uint256-depositamount-uint256-depositperiodinseconds-uint256-interestamount"></a>

Computes the amount of fixed-rate interest (before fees) that will be given to a deposit of `depositAmount` stablecoins that matures in `depositPeriodInSeconds` seconds.

@param depositAmount The deposit amount, in stablecoins

@param depositPeriodInSeconds The deposit period, in seconds

@return interestAmount The amount of fixed-rate interest (before fees)

## `surplus() → bool isNegative, uint256 surplusAmount` (public) <a href="#surplus-bool-isnegative-uint256-surplusamount-public" id="surplus-bool-isnegative-uint256-surplusamount-public"></a>

Computes the pool's overall surplus, which is the value of its holdings in the `moneyMarket` minus the amount owed to depositors, funders, and the fee beneficiary.

@return isNegative True if the surplus is negative, false otherwise

@return surplusAmount The absolute value of the surplus, in stablecoins

## `depositsLength() → uint256` (external) <a href="#depositslength-uint256-external" id="depositslength-uint256-external"></a>

Returns the total number of deposits.

@return deposits.length

## `fundingListLength() → uint256` (external) <a href="#fundinglistlength-uint256-external" id="fundinglistlength-uint256-external"></a>

Returns the total number of floating-rate bonds.

@return fundingList.length

## `getDeposit(uint64 depositID) → struct DInterest.Deposit` (external) <a href="#getdeposit-uint64-depositid-struct-dinterest-deposit-external" id="getdeposit-uint64-depositid-struct-dinterest-deposit-external"></a>

Returns the Deposit struct associated with the deposit with ID `depositID`.

@param depositID The ID of the deposit

@return The deposit struct

```
// User deposit data
// Each deposit has an ID used in the depositNFT, which is equal to its index in `deposits` plus 1
struct Deposit {
    uint256 virtualTokenTotalSupply; // depositAmount + interestAmount, behaves like a zero coupon bond
    uint256 interestRate; // interestAmount = interestRate * depositAmount
    uint256 feeRate; // feeAmount = feeRate * depositAmount
    uint256 averageRecordedIncomeIndex; // Average income index at time of deposit, used for computing deposit surplus
    uint64 maturationTimestamp; // Unix timestamp after which the deposit may be withdrawn, in seconds
    uint64 fundingID; // The ID of the associated Funding struct. 0 if not funded.
}
```

## `getFunding(uint64 fundingID) → struct DInterest.Funding` (external) <a href="#getfunding-uint64-fundingid-struct-dinterest-funding-external" id="getfunding-uint64-fundingid-struct-dinterest-funding-external"></a>

Returns the Funding struct associated with the floating-rate bond with ID `fundingID`.

@param fundingID The ID of the floating-rate bond

@return The Funding struct

```
// Funding data
// Each funding has an ID used in the fundingMultitoken, which is equal to its index in `fundingList` plus 1
struct Funding {
    uint64 depositID; // The ID of the associated Deposit struct.
    uint64 lastInterestPayoutTimestamp; // Unix timestamp of the most recent interest payout, in seconds
    uint256 recordedMoneyMarketIncomeIndex; // the income index at the last update (creation or withdrawal)
    uint256 principalPerToken; // The amount of stablecoins that's earning interest for you per funding token you own. Scaled to 18 decimals regardless of stablecoin decimals.
}
```
