# Storage overview

### Base types

```pascaligo
type tokenId is nat

type assetType          is
| FA12 of address // TZIP-007 compatible token
| FA2  of (address * nat) // TZIP-012 compatible token

type token_metadata_info is [@layout:comb] record [
  token_id              : tokenId;
  token_info            : map(string, bytes);
] // token metadata by TZIP-016 for each TZIP-012 token
```

### Account type

| Field           |      Type      | Description                                                                     |
| --------------- | :------------: | ------------------------------------------------------------------------------- |
| allowances      | `set(address)` | allowances (operators) - set of address allowed to transfer on account's behalf |
| borrow          |      `nat`     | amount of borrowed tokens                                                       |
| lastBorrowIndex |      `nat`     | account's last borrow index to apply interest to borrowed tokens                |

```pascaligo
type account            is [@layout:comb] record [
  allowances            : set(address);
  borrow                : nat;
  lastBorrowIndex       : nat;
]
```

### yToken type (market)

<table><thead><tr><th width="212">Field</th><th width="150" align="center">Type</th><th width="232.14246575342466">Hint</th><th>Description</th></tr></thead><tbody><tr><td>mainToken</td><td align="center"><code>assetType</code></td><td>FA12/FA2</td><td>underlying asset</td></tr><tr><td>interestRateModel</td><td align="center"><code>address</code></td><td>KT1....</td><td>Interest rate contract</td></tr><tr><td>interestUpdateTime</td><td align="center"><code>timestamp</code></td><td><a data-mention href="updateinterest">updateinterest</a> called with <code>tokenId</code> of this token.</td><td>last time when interest of token updated</td></tr><tr><td>priceUpdateTime</td><td align="center"><code>timestamp</code></td><td><a href="../pricefeed-contract/getprice">PriceFeed.getPrice</a> called with <code>tokenId</code> of this token.</td><td>last time when price of token updated</td></tr><tr><td>totalBorrowsF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>total amount of borrowed tokens</td></tr><tr><td>totalLiquidF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>total liquidity locked at market</td></tr><tr><td>totalSupplyF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>total supply of yToken </td></tr><tr><td>totalReservesF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>"subsidiary" reserves of protocol</td></tr><tr><td>borrowIndex</td><td align="center"><code>nat</code></td><td><span class="math">token.borrowIndex *(\textstyle\frac{borrowRate * blockDelta}{ precision} + 1)</span></td><td>index collecting interest</td></tr><tr><td>maxBorrowRate</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>limit of borrow rate response. Used to verify borrow rate in <a data-mention href="accrueinterest">accrueinterest</a></td></tr><tr><td>collateralFactorF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>Rate represents by which the borrow limit increases if the yToken is minted</td></tr><tr><td>liquidReserveRateF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>Percent of liquidate collateral amount that goes to reserves</td></tr><tr><td>reserveFactorF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>Rate that represents what part of the interest goes to the protocol reserves</td></tr><tr><td>lastPrice</td><td align="center"><code>nat</code></td><td></td><td>last price of underlying token to XTZ</td></tr><tr><td>borrowPause</td><td align="center"><code>bool</code></td><td></td><td>flag if borrowing paused for token <a data-mention href="admin-methods/manage-entrypoints/setborrowpause">setborrowpause</a></td></tr><tr><td>isInterestUpdating</td><td align="center"><code>bool</code></td><td></td><td>flag of waiting response from <a data-mention href="../interestrate-contract">interestrate-contract</a></td></tr><tr><td>threshold</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>threshold of collateral borrow rate</td></tr></tbody></table>

```pascaligo
type tokenType          is [@layout:comb] record [
  mainToken             : assetType;
  interestRateModel     : address;
  interestUpdateTime    : timestamp;
  priceUpdateTime       : timestamp;
  totalBorrowsF         : nat;
  totalLiquidF          : nat;
  totalSupplyF          : nat;
  totalReservesF        : nat;
  borrowIndex           : nat;
  maxBorrowRate         : nat;
  collateralFactorF     : nat;
  liquidReserveRateF    : nat;
  reserveFactorF        : nat;
  lastPrice             : nat;
  borrowPause           : bool;
  isInterestUpdating    : bool;
  threshold             : nat;
]
```

### yStorage - main Yupana storage values

<table><thead><tr><th width="179">Field</th><th width="270.0134967799085" align="center">Type</th><th width="150">Hint</th><th>Description</th></tr></thead><tbody><tr><td>admin</td><td align="center"><code>address</code></td><td>tz/KT....</td><td>contract administrator</td></tr><tr><td>ledger</td><td align="center"><p><code>big_map(</code></p><p><code>(address * tokenId), nat</code></p><p><code>)</code></p></td><td></td><td>storage of token balances</td></tr><tr><td>accounts</td><td align="center"><p><code>big_map(</code></p><p><code>(address * tokenId), account</code></p><p><code>)</code></p></td><td></td><td><a data-mention href="#account-type">#account-type</a> storage</td></tr><tr><td>tokens</td><td align="center"><code>map(tokenId, tokenType)</code></td><td></td><td><a data-mention href="#ytoken-type-market">#ytoken-type-market</a>storage</td></tr><tr><td>lastTokenId</td><td align="center"><code>nat</code></td><td>counter, size of tokens </td><td>tokenId of last created market</td></tr><tr><td>priceFeedProxy</td><td align="center"><code>address</code></td><td>KT1...</td><td><a data-mention href="../pricefeed-contract">pricefeed-contract</a>address</td></tr><tr><td>closeFactorF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>max portion of the loan that can be liquidated per single transaction</td></tr><tr><td>liqIncentiveF</td><td align="center"><code>nat</code></td><td>float value multiplied by 1e+18</td><td>Rate that the liquidator will earn if liquidate the asset</td></tr><tr><td>markets</td><td align="center"><p><code>big_map(</code></p><p><code>address, set(tokenId)</code></p><p><code>)</code></p></td><td>size of set is limited by <code>maxMarkets</code> value</td><td>mapping of entered as collateral user markets</td></tr><tr><td>borrows</td><td align="center"><p><code>big_map(</code></p><p><code>address, set(tokenId)</code></p><p><code>)</code></p></td><td>size of set is limited by <code>maxMarkets</code> value</td><td>mapping of borrowed tokens of user markets</td></tr><tr><td>maxMarkets</td><td align="center"><code>nat</code></td><td></td><td>max markets allowed to enter as collateral or borrow</td></tr><tr><td>assets</td><td align="center"><p><code>big_map(</code></p><p><code>assetType, tokenId</code></p><p><code>)</code></p></td><td></td><td>mapping underlying asset (FA12/FA2) to corresponding <code>tokenId</code> of yToken market</td></tr></tbody></table>

```pascaligo
type yStorage is [@layout:comb] record [
  admin: address; 
  ledger: big_map((address * tokenId), nat); 
  accounts: big_map((address * tokenId), account); 
  tokens                : map(tokenId, tokenType);
  lastTokenId           : nat;
  priceFeedProxy        : address;
  closeFactorF          : nat;
  liqIncentiveF         : nat;
  markets               : big_map(address, set(tokenId));
  borrows               : big_map(address, set(tokenId));
  maxMarkets            : nat;
  assets                : big_map(assetType, tokenId);
]
```

### fullStorage - contract storage root

<table><thead><tr><th width="186.9818587084244">Field</th><th width="267.53866155788165" align="center">Type</th><th>Description</th></tr></thead><tbody><tr><td>storage</td><td align="center"><code>yStorage</code></td><td><a data-mention href="#ystorage-main-yupana-storage-values">#ystorage-main-yupana-storage-values</a></td></tr><tr><td>metadata</td><td align="center"><code>big_map(string, bytes)</code></td><td>contract metadata by <a href="https://gitlab.com/tezos/tzip/-/blob/master/proposals/tzip-16/tzip-16.md">TZIP-016</a></td></tr><tr><td>token_metadata</td><td align="center"><code>big_map(tokenId, token_metadata_info)</code></td><td>mapping each token metadata by <a href="https://gitlab.com/tezos/tzip/-/blob/master/proposals/tzip-12/tzip-12.md">TZIP-012</a></td></tr><tr><td>tokenLambdas</td><td align="center"><code>big_map(nat, bytes)</code></td><td>FA2 lambda-methods storage</td></tr><tr><td>useLambdas</td><td align="center"><code>big_map(nat, bytes)</code></td><td>Yupana protocol lambda-methods storage</td></tr></tbody></table>

```pascaligo
type fullStorage   is record [
  storage               : yStorage;
  metadata              : big_map(string, bytes);
  token_metadata        : big_map(tokenId, token_metadata_info);
  tokenLambdas          : big_map(nat, bytes);
  useLambdas            : big_map(nat, bytes);
]
```
