> For the complete documentation index, see [llms.txt](https://docs.zivoe.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zivoe.com/developer-docs/core-contracts/zivoemath.sol.md).

# ZivoeMath.sol

Mathematics for Yield Distributions

## Introduction

This contract facilitates mathematics, intended solely for the YDL.

*Note: This contract is deployed and tracked via ZivoeYDL, see* [ZivoeYDL.sol](/developer-docs/core-contracts/zivoeydl.sol.md) and for further information on the derivation of the equations, see [Broken mention](broken://pages/LArq3pPT9c3aAHXILOS5)

#### State Variables

<table><thead><tr><th width="144.33333333333331">Type</th><th width="150">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>BIPS</td><td>Private constant, <code>10000</code></td></tr><tr><td>uint256</td><td>WAD</td><td>Private constant, <code>10**18</code></td></tr><tr><td>uint256</td><td>RAY</td><td>Private constant, <code>10**27</code></td></tr></tbody></table>

## **Sections**

[#read-functions](#read-functions "mention")

* [#ema](#ema "mention") - Calculates the current EMA (exponential moving average).
* [#juniorproportion](#juniorproportion "mention") - Calculates proportion of yield attributable to junior tranche.
* [#seniorproportion](#seniorproportion "mention") - Calculates proportion of yield distributble which is attributable to the senior tranche.
* [#seniorproportionbase](#seniorproportionbase "mention") - Calculates proportion of yield attributed to senior tranche (no extenuating circumstances).
* [#seniorproportionshortfall](#seniorproportionshortfall "mention") - Calculates proportion of yield attributed to senior tranche (shortfall occurence).
* [#yieldtarget](#yieldtarget "mention") - Calculates amount of annual yield required to meet target rate for both tranches.

## Read Functions

#### `ema()`

Calculates the current EMA (exponential moving average).

```solidity
function ema(
    uint256 bV, 
    uint256 cV, 
    uint256 N
) external pure returns (uint256 eV);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>bV</td><td>The base value (typically an EMA from prior calculations).</td></tr><tr><td>uint256</td><td>cV</td><td>The current value, which is factored into bV.</td></tr><tr><td>uint256</td><td>N</td><td>Number of steps to average over.</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>eV</td><td>EMA-based value given prior and current conditions.</td></tr></tbody></table>

#### `juniorProportion()`

Calculates proportion of yield attributable to junior tranche.

```solidity
function juniorProportion(
    uint256 eSTT,
    uint256 eJTT, 
    uint256 sP,
    uint256 Q
) external pure returns (uint256 jP);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>eSTT</td><td>ema-based supply of zSTT (units = WEI)</td></tr><tr><td>uint256</td><td>eJTT</td><td>ema-based supply of zJTT (units = WEI)</td></tr><tr><td>uint256</td><td>sP</td><td>Proportion of yield attributable to seniors (units = RAY)</td></tr><tr><td>uint256</td><td>Q</td><td>senior to junior tranche target ratio (units = BIPS)</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>jP</td><td>Yield attributable to junior tranche in RAY.</td></tr></tbody></table>

#### `seniorProportion()`

Calculates proportion of yield distributable which is attributable to the senior tranche.

```solidity
function seniorProportion(
    uint256 yD,
    uint256 yT,
    uint256 eSTT, 
    uint256 eJTT, 
    uint256 Y, 
    uint256 Q, 
    uint256 T
) external pure returns (uint256 sP);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>yD</td><td>yield distributable (units = WEI)</td></tr><tr><td>uint256</td><td>yT</td><td>ema-based yield target (units = WEI)</td></tr><tr><td>uint256</td><td>eSTT</td><td>ema-based supply of zSTT (units = WEI)</td></tr><tr><td>uint256</td><td>eJTT</td><td>ema-based supply of zJTT (units = WEI)</td></tr><tr><td>uint256</td><td>Y</td><td>target annual yield for senior tranche (units = BIPS)</td></tr><tr><td>uint256</td><td>Q</td><td>multiple of Y (units = BIPS)</td></tr><tr><td>uint256</td><td>T</td><td># of days between distributions (units = integer)</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>sP</td><td>Proportion of yD attributable to senior tranche.</td></tr></tbody></table>

#### `seniorProportionBase()`

Calculates proportion of yield attributed to senior tranche (no extenuating circumstances).

```solidity
function seniorProportionBase(
    uint256 yD, 
    uint256 eSTT, 
    uint256 Y, 
    uint256 T
) public pure returns (uint256 sPB);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>yD</td><td>yield distributable (units = WEI)</td></tr><tr><td>uint256</td><td>eSTT</td><td>ema-based supply of zSTT (units = WEI)</td></tr><tr><td>uint256</td><td>Y</td><td>target annual yield for senior tranche (units = BIPS)</td></tr><tr><td>uint256</td><td>T</td><td># of days between distributions (units = integer)</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>sPB</td><td>Proportion of yield attributed to senior tranche in RAY.</td></tr></tbody></table>

#### `seniorProportionShortfall()`

Calculates proportion of yield attributed to senior tranche (shortfall occurence).

```solidity
function seniorProportionShortfall(
    uint256 eSTT, 
    uint256 eJTT, 
    uint256 Q
) public pure returns (uint256 sPS);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>eSTT</td><td>ema-based supply of zSTT (units = WEI)</td></tr><tr><td>uint256</td><td>eJTT</td><td>ema-based supply of zJTT (units = WEI)</td></tr><tr><td>uint256</td><td>Q</td><td>multiple of Y (units = BIPS)</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>sPS</td><td>Proportion of yield attributed to senior tranche in RAY.</td></tr></tbody></table>

#### `yieldTarget()`

Calculates amount of annual yield required to meet target rate for both tranches.

```solidity
function yieldTarget(
    uint256 eSTT,
    uint256 eJTT, 
    uint256 Y, 
    uint256 Q, 
    uint256 T
) public pure returns (uint256 yT);
```

<table><thead><tr><th width="143.33333333333331">Type</th><th width="120">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>eSTT</td><td>ema-based supply of zSTT (units = WEI)</td></tr><tr><td>uint256</td><td>eJTT</td><td>ema-based supply of zJTT (units = WEI)</td></tr><tr><td>uint256</td><td>Y</td><td>target annual yield for senior tranche (units = BIPS)</td></tr><tr><td>uint256</td><td>Q</td><td>multiple of Y (units = BIPS)</td></tr><tr><td>uint256</td><td>T</td><td># of days between distributions (units = integer)</td></tr></tbody></table>

*Returns*

<table><thead><tr><th width="148.33333333333331">Type</th><th width="121">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>yT</td><td>yield target for the senior and junior tranche combined.</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.zivoe.com/developer-docs/core-contracts/zivoemath.sol.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
