# Storage and types overview

### Base types

```pascaligo
type token_id_t        is nat

type pool_id_t         is nat

type token_pool_idx_t  is nat

type fa12_token_t      is address

type fa2_token_t       is [@layout:comb] record[ 
    token_address        : address; 
    token_id             : token_id_t; 
]

type token_t           is 
| Fa12                   of fa12_token_t 
| Fa2                    of fa2_token_t

type tokens_map_t      is map(nat, token_t);
```

### Staker accumulator - accumulator of QUIPU staking rewards.

<table><thead><tr><th width="157.23880597014926">Field</th><th width="241.7580619284454" align="center">Type</th><th width="237">Hint</th><th>Description</th></tr></thead><tbody><tr><td>accumulator</td><td align="center"><code>map(token_pool_idx_t, nat)</code></td><td>For better accuracy, stored with multiplication by <span class="math">10^{10}</span>.</td><td>Mapping of token index and corresponding underlying accumulated balance of token</td></tr><tr><td>total_staked</td><td align="center"><code>nat</code></td><td></td><td>balance of staked QUIPU tokens to current pool</td></tr></tbody></table>

```pascaligo
type staker_accum_t     is [@layout:comb] record [
  accumulator             : map(token_pool_idx_t, nat);
  total_staked            : nat;
]
```

### Fee storage - fee rates record

<table><thead><tr><th width="206.35180549489237">Field</th><th width="150" align="center">Type</th><th width="237">Hint</th><th>Description</th></tr></thead><tbody><tr><td>lp</td><td align="center"><code>nat</code></td><td>Decimal value. Multiplied by <span class="math">10^{10}</span>.</td><td><p>Percent of fee goes to liquidity providers.</p><p>This fee stays in liquidity pool to increase LP token price.</p></td></tr><tr><td>stakers</td><td align="center"><code>nat</code></td><td>Decimal value. Multiplied by <span class="math">10^{10}</span>.</td><td>Percent of fee goes to QUIPU token stakers of pool. This fee goes to staking accumulator and spreads between users who staked QUIPU token to pool. If noone staked this fee part goes to liquidity pool as additional fee.</td></tr><tr><td>ref</td><td align="center"><code>nat</code></td><td>Decimal value. Multiplied by <span class="math">10^{10}</span>.</td><td>Percent of fee goes to referral of DEX call. This fee goes to referral address passed to DEX call. If referral not passed, fee goes to default referral.</td></tr></tbody></table>

```pascaligo
type fees_storage_t     is [@layout:comb] record [
  lp                      : nat;
  stakers                 : nat;
  ref                     : nat;
]
```

### Token information type - pool underlying token info

<table><thead><tr><th width="206.35180549489237">Field</th><th width="150" align="center">Type</th><th width="237">Hint</th><th>Description</th></tr></thead><tbody><tr><td>rate</td><td align="center"><code>nat</code></td><td>Calculates with pecisions. By default LP precision is 1e18. Rate is the value allowing to set custom exchange ratios between underlying tokens</td><td>Indicates how much LP token belongs to each underlying stablecoin</td></tr><tr><td>precision_multiplier</td><td align="center"><code>nat</code></td><td>By default LP precision is 1e18. Than <code>precision_multiplier</code> is <span class="math">10^{decimals_{LP} - decimals_{token}}</span></td><td>value that underlying token reserves are multiplied by in order to adjust their precision to LP decimal places</td></tr><tr><td>reserves</td><td align="center"><code>nat</code></td><td></td><td>balance of underlying token, locked in pool</td></tr></tbody></table>

```pascaligo
type token_info_t       is  [@layout:comb] record [
  rate                    : nat;
  precision_multiplier    : nat;
  reserves                : nat;
]
```

### Pool type - DEX pool storage

<table><thead><tr><th width="208.35180549489237">Field</th><th width="252" align="center">Type</th><th width="151">Hint</th><th>Description</th></tr></thead><tbody><tr><td>initial_A</td><td align="center"><code>nat</code></td><td>A constant stores as multiplied value. <span class="math"> A * n^{n-1} * 10^2</span>(10^2 is precision)</td><td>Start value of ramping A contant.</td></tr><tr><td>initial_A_time</td><td align="center"><code>timestamp</code></td><td>Timestamp in seconds</td><td>Time when ramping A constant was started.</td></tr><tr><td>future_A</td><td align="center"><code>nat</code></td><td>A constant stores as multiplied value. <span class="math"> A * n^{n-1} * 10^2</span>(10^2 is precision)</td><td>End value of ramping A contant.</td></tr><tr><td>future_A_time</td><td align="center"><code>timestamp</code></td><td>Timestamp in seconds</td><td>Time when ramping A constant will be finished</td></tr><tr><td>tokens_info</td><td align="center"><code>map(token_pool_idx_t, token_info_t)</code></td><td></td><td><a data-mention href="#token-information-type-pool-underlying-token-info">#token-information-type-pool-underlying-token-info</a></td></tr><tr><td>fee</td><td align="center"><code>fees_storage_t</code></td><td></td><td><a data-mention href="#fee-storage-fee-rates-record">#fee-storage-fee-rates-record</a></td></tr><tr><td>staker_accumulator</td><td align="center"><code>staker_accum_t</code></td><td></td><td><a data-mention href="#staker-accumulator-accumulator-of-quipu-staking-rewards.">#staker-accumulator-accumulator-of-quipu-staking-rewards.</a></td></tr><tr><td>total_supply</td><td align="center"><code>nat</code></td><td></td><td>Total supply of LP token.</td></tr></tbody></table>

```pascaligo
type pool_t             is [@layout:comb] record [
  initial_A               : nat;
  initial_A_time          : timestamp;
  future_A                : nat;
  future_A_time           : timestamp;
  tokens_info             : map(token_pool_idx_t, token_info_t);
  fee                     : fees_storage_t;
  staker_accumulator      : staker_accum_t;
  total_supply            : nat;
]
```

### Storage - main contract storage

<table><thead><tr><th width="185.35180549489237">Field</th><th width="254" align="center">Type</th><th width="233">Hint</th><th>Description</th></tr></thead><tbody><tr><td>admin</td><td align="center"><code>address</code></td><td></td><td>Administator of current contract</td></tr><tr><td>default_referral</td><td align="center"><code>address</code></td><td></td><td>Default referral address to apply fees</td></tr><tr><td>managers</td><td align="center"><code>set(address)</code></td><td>Manager could edit LP token metadata.</td><td>Set of managers addresses</td></tr><tr><td>pools_count</td><td align="center"><code>nat</code></td><td>Counter. Always <code>1</code></td><td>Amount of pools created inside current contract.</td></tr><tr><td>tokens</td><td align="center"><code>big_map(pool_id_t, tokens_map_t)</code></td><td></td><td>Mapping of tokens, that exchanges inside created pool. </td></tr><tr><td>pool_to_id</td><td align="center"><code>big_map(bytes, nat)</code></td><td>Bytes - packed by <code>Bytes.pack(tokens)</code> where tokens is valid <code>tokens_map_t</code> (sorted tokens).</td><td>Mapping that allows finding pool id by packed bytes of <code>tokens_map_t</code></td></tr><tr><td>pools</td><td align="center"><code>big_map(pool_id_t, pool_t)</code></td><td></td><td>Mapping of pool to it's corresponding pool <a data-mention href="#pool-type-dex-pool-storage">#pool-type-dex-pool-storage</a></td></tr><tr><td>ledger</td><td align="center"><p><code>big_map(</code></p><p><code>(address * pool_id_t), nat)</code></p></td><td></td><td>Mapping of user's LP token balance related to pool</td></tr><tr><td>allowances</td><td align="center"><p><code>big_map(</code></p><p><code>(address * pool_id_t), allowances_data_t)</code></p></td><td></td><td>Storage of operators allowed to transfer LP tokens of user's behalf.</td></tr><tr><td>dev_rewards</td><td align="center"><code>big_map(token_t, nat)</code></td><td></td><td>Mapping of accrued developer rewards by each token.</td></tr><tr><td>referral_rewards</td><td align="center"><p><code>big_map(</code></p><p><code>(address * token_t), nat)</code></p></td><td></td><td>Mapping of accrued referral rewards by each user-token key.</td></tr><tr><td>stakers_balance</td><td align="center"><p><code>big_map(</code></p><p><code>(address * pool_id_t), staker_info_t)</code></p></td><td></td><td>Mapping of accrued staking rewards by each user-token key.</td></tr><tr><td>quipu_token</td><td align="center"><code>fa2_token_t</code></td><td></td><td>QUIPU token address and token ID</td></tr><tr><td>started</td><td align="center"><code>bool</code></td><td><a data-mention href="initialization/freeze">freeze</a></td><td>flag that used in initialization stage</td></tr><tr><td>factory_address</td><td align="center"><code>address</code></td><td>this field setted at deploy and has no methods for changing</td><td>address of factory</td></tr></tbody></table>

```pascaligo
type storage_t          is [@layout:comb] record [
  (* Management *)
  admin                   : address;
  default_referral        : address;
  managers                : set(address);

  (* Pools data *)
  pools_count             : nat; (* total pools count *)
  tokens                  : big_map(pool_id_t, tokens_map_t); (* all the tokens list *)
  pool_to_id              : big_map(bytes, nat); (* all the tokens list *)
  pools                   : big_map(pool_id_t, pool_t); (* pool info per token id *)

  (* FA2 data *)
  ledger                  : big_map((address * pool_id_t), nat); (* account info per address *)
  allowances              : big_map((address * pool_id_t), allowances_data_t); (* account info per each lp provider *)

  (* Rewards and accumulators *)
  dev_rewards             : big_map(token_t, nat);
  referral_rewards        : big_map((address * token_t), nat);
  stakers_balance         : big_map((address * pool_id_t), staker_info_t);
  quipu_token             : fa2_token_t;
  (* dev storage params *)
  dev_store               : dev_storage_t;
]
```

### Full storage type - storage root

<table><thead><tr><th width="183.44565217391303">Field</th><th width="221" align="center">Type</th><th width="150">Hint</th><th>Description</th></tr></thead><tbody><tr><td>storage</td><td align="center"><code>storage_t</code></td><td><a data-mention href="#storage-main-contract-storage">#storage-main-contract-storage</a></td><td>Indicates how much LP token belongs to each underlying stablecoin</td></tr><tr><td>metadata</td><td align="center"><code>big_map(string, bytes)</code></td><td>TZIP-016</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(token_id_t, token_meta_info_t)</code></td><td>TZIP-016, TZIP-012</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>admin_lambdas</td><td align="center"><code>big_map(nat, bytes)</code></td><td></td><td>Administrative lambda-methods storage</td></tr><tr><td>dex_lambdas</td><td align="center"><code>big_map(nat, bytes)</code></td><td></td><td>DEX stable swap protocol lambda-methods storage</td></tr><tr><td>token_lambdas</td><td align="center"><code>big_map(nat, bytes)</code></td><td></td><td>FA2 lambda-methods storage</td></tr></tbody></table>

```pascaligo
type full_storage_t     is [@layout:comb] record [
  storage                 : storage_t; (* real dex storage_t *)
  (* Token Metadata *)
  metadata                : big_map(string, bytes); (* metadata storage_t according to TZIP-016 *)
  token_metadata          : big_map(token_id_t, token_meta_info_t);
  (* Contract lambdas storage *)
  admin_lambdas           : big_map(nat, bytes); (* map with admin-related functions code *)
  dex_lambdas             : big_map(nat, bytes); (* map with exchange-related functions code *)
  token_lambdas           : big_map(nat, bytes); (* map with token-related functions code *)
]
```
