style adjustments to registry and vault tech docs

This commit is contained in:
simsbluebox
2024-08-26 12:56:01 -03:00
parent 7716c26744
commit 5680782a54
2 changed files with 100 additions and 120 deletions

View File

@@ -1,6 +1,6 @@
# Contract: *amm-registry-v2-01.clar*.
# Registry
#### **Location**: *`./alex-dao-2/contracts/aux/amm-registry-v2-01.clar`*
#### Location: _`./alex-dao-2/contracts/aux/amm-registry-v2-01.clar`_
This document provides comprehensive technical details for the registry contract within ALEX's Automated Market Maker (AMM) Trading Pool system. The contract primarily functions as a persistence module for all pool-related information needed by the main contract `amm-pool-v2-01.clar`. [LINK xxx xxx xxx]
@@ -8,9 +8,10 @@ To achieve this, the contract allows for the creation and updating of pools. Poo
Additionally, the contract includes configuration getters and setters that support position and swap operations. It is also responsible for managing a list of blocklisted operators.
**Storage**:
## Storage
### Variables: (datamap)
DATA
* `pools-data-map` (datamap
key:
{
@@ -44,6 +45,11 @@ A datamap structure that persists complete pool information. The map key consist
* `pools-id-map` (datamap key: uint value: { token-x: principal, token-y: principal, factor: uint })
A datamap structure that facilitates the retrieval of pool details using the pool ID as the key. The stored values include `token-x` and `token-y` principals, and `factor`.
* `blocklist` (datamap key: principal value: bool)
A datamap structure that stores a persisted list of blocklisted addresses for operating within the Alex Trading Pool.
### Variables: (data-var)
* `pool-nonce` (uint)
A persisted variable used to generate a new pool ID incrementally. The stored value represents the last pool ID that was created.
@@ -53,23 +59,19 @@ An internal variable used to set a fixed threshold for calculations. It is initi
* `max-ratio-limit` (uint)
An internal variable that defines the maximum ratio limit. It is initialized with the value of the constant `ONE_8`.
* `blocklist` (datamap key: principal value: bool)
A datamap structure that stores a persisted list of blocklisted addresses for operating within the Alex Trading Pool.
#### Mathematical constants
Mathematical constants:
This symbolic constant is employed to define and restrict decimal precision to 8 decimal places.
* `ONE_8`
It is declared as `u100000000`.
* `ONE_8` It is declared as `u100000000`.
**Contract calls (interactions):**
* `executor-dao`
This call is used to verify whether a certain contract caller is designated as an extension.
## Contract calls (interactions)
**Features:**
* `executor-dao` This call is used to verify whether a certain contract caller is designated as an extension.
1) `create-pool`
This function establishes a liquidity pool for a specified token pair (token-x/token-y). It begins by verifying that the `tx-sender` is an ALEX admin operator (see the function `is-dao-or-extension`), as it is intended to be used by the main `amm-pool-v2-01.clar` contract in the current model.
## Features
1. `create-pool` This function establishes a liquidity pool for a specified token pair (token-x/token-y). It begins by verifying that the `tx-sender` is an ALEX admin operator (see the function `is-dao-or-extension`), as it is intended to be used by the main `amm-pool-v2-01.clar` contract in the current model.
The primary validation performed by this function ensures that the pool does not already exist; if it does, an error is thrown. This validation considers the factor and both token combinations (token-x/token/y or token-y/token-x) as unique identifiers. When a pool is created, an entry is added to the `pools-data-map` structure, using this unique identifier as the key to keep track of all pool information, including balances, fees, thresholds, and more. Additionally, the function generates an ID for the newly created pool (see `pool-nonce`).
All remaining values in the datamap are initialized to zero (`u0`), except for `oracle-enabled`, which is set to `false`. Additionally, `start-block` and `end-block` are initialized with the maximum uint value to ensure the pool remains in a non-operational status until properly initialized. For a complete list of fields, refer to the `pools-data-map`.
**Input**:
@@ -80,10 +82,8 @@ All remaining values in the datamap are initialized to zero (`u0`), except for `
(pool-owner principal)
```
2) `update-pool`
This function updates a liquidity pool identified by the unique combination of `token-x`, `token-y`, and `factor`. It is a governed function that restricts the `tx-sender` to be an ALEX admin operator (see the function `is-dao-or-extension`).
2. `update-pool` This function updates a liquidity pool identified by the unique combination of `token-x`, `token-y`, and `factor`. It is a governed function that restricts the `tx-sender` to be an ALEX admin operator (see the function `is-dao-or-extension`).
Similar to the aforementioned `create-pool` function, `update-pool` is designed to be used by the main `amm-pool-v2-01.clar` contract. However, in this case, it is used indirectly in position and swap operations.
**Input**:
```lisp
(token-x principal)
@@ -110,24 +110,19 @@ Similar to the aforementioned `create-pool` function, `update-pool` is designed
)
```
Governance features:
1) `is-dao-or-extension`
This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract.
### Governance features
1. `is-dao-or-extension` This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract.
**Input**:
None.
2) `is-blocklisted-or-default`
A read-only feature that verifies if a given address is blacklisted using the `blocklist` map.
2. `is-blocklisted-or-default` A read-only feature that verifies if a given address is blacklisted using the `blocklist` map.
**Input**:
```lisp
(sender principal)
```
3) `set-blocklist-many`
A public function, governed by the `is-dao-or-extension` mechanism, that allows setting or updating the blocklisted status for a list of addresses (up to 1000 addresses).
3. `set-blocklist-many` A public function, governed by the `is-dao-or-extension` mechanism, that allows setting or updating the blocklisted status for a list of addresses (up to 1000 addresses).
**Input**:
```lisp
(list 1000 {
@@ -137,57 +132,58 @@ A public function, governed by the `is-dao-or-extension` mechanism, that allows
```
### Getter and Setter functions
Pool administration setters:
* `set-switch-threshold`
* `set-max-ratio-limit`
#### Pool administration setters
* `set-fee-rebate`
* `set-pool-owner`
* `set-max-ratio-limit`
* `set-switch-threshold`
Pool operation setters and getters:
#### Pool operation setters and getters
The following groups of functions support pool usage and configuration features consumed by the main `amm-pool-v2-01.clar` contract.
Setters:
#### Setters
* `set-start-block`
* `set-end-block`
* `set-max-in-ratio`
* `set-max-out-ratio`
* `set-oracle-enabled`
* `set-oracle-average`
* `set-threshold-x`
* `set-threshold-y`
* `set-fee-rate-x`
* `set-fee-rate-y`
* `set-max-in-ratio`
* `set-max-out-ratio`
* `set-oracle-average`
* `set-oracle-enabled`
* `set-threshold-x`
* `set-threshold-y`
#### Getters
Getters:
* `get-switch-threshold`
* `get-max-ratio-limit`
* `get-pool-details-by-id`
* `get-pool-details`
* `get-pool-exists`
* `get-max-ratio-limit`
* `get-switch-threshold`
#### Internal helper functions
### Internal helper functions
* `set-blocklist`
This is a private function designed to complement the aforementioned governance function `set-blocklist-many`.
* `set-blocklist` This is a private function designed to complement the aforementioned governance function `set-blocklist-many`.
**Input**:
```lisp
(sender principal)
(blocked bool)
```
### Errors defined in the contract
* `ERR-NOT-AUTHORIZED`
* `ERR-INVALID-POOL`
* `ERR-INVALID-LIQUIDITY`
* `ERR-POOL-ALREADY-EXISTS`
* `ERR-PERCENT-GREATER-THAN-ONE`
## Errors defined in the contract
* `ERR-EXCEEDS-MAX-SLIPPAGE`
* `ERR-ORACLE-NOT-ENABLED`
* `ERR-ORACLE-AVERAGE-BIGGER-THAN-ONE`
* `ERR-PAUSED`
* `ERR-SWITCH-THRESHOLD-BIGGER-THAN-ONE`
* `ERR-NO-LIQUIDITY`
* `ERR-INVALID-LIQUIDITY`
* `ERR-INVALID-POOL`
* `ERR-MAX-IN-RATIO`
* `ERR-MAX-OUT-RATIO`
* `ERR-NO-LIQUIDITY`
* `ERR-NOT-AUTHORIZED`
* `ERR-ORACLE-AVERAGE-BIGGER-THAN-ONE`
* `ERR-ORACLE-NOT-ENABLED`
* `ERR-PAUSED`
* `ERR-PERCENT-GREATER-THAN-ONE`
* `ERR-POOL-ALREADY-EXISTS`
* `ERR-SWITCH-THRESHOLD-BIGGER-THAN-ONE`

View File

@@ -1,75 +1,69 @@
# Contract: *amm-vault-v2-01.clar*.
# Vault
#### **Location**: *`./alex-dao-2/contracts/aux/amm-vault-v2-01.clar`*
#### Location: _`./alex-dao-2/contracts/aux/amm-vault-v2-01.clar`_
This document provides comprehensive technical details for the vault contract within ALEX's Automated Market Maker (AMM) Trading Pool system. The vault contract supports the primary contract `amm-pool-v2-01.clar` in position and swap operations by keeping record of the reserves accumulated from fees and securing pool assets. It ensures asset security through transfer transactions where the vault contract is the recipient of token transfers, thereby holding and safeguarding the assets within the pool. [LINK xxx xxx xxx].
In addition to supporting Trading Pool operations, the vault contract also offers a flash-loan feature for registered tokens, available to authorized users.
**Storage**:
## Storage
DATA
### Variables: (data-var)
* `paused` (bool)
This data variable acts as a flag to determine and control the operational status of the contract within the system.
TOKEN DATA:
#### Token variables
* `approved-tokens` (datamap key: principal value: bool)
A datamap structure that lists all the approved tokens for transfer and loan operations.
* `reserve` (datamap key: principal value: uint)
A datamap structure that keeps record of all the reserves in the vault for a specific token.
FLASH-LOAN DATA:
#### Flash-loan variables
* `approved-flash-loan-users` (datamap key: principal value: bool)
A datamap structure that lists all the approved users for loan operations.
* `flash-loan-enabled` (bool)
A flag variable indicating the status of the contract's flash-loan feature. Currently, this is only an informative flag.
* `flash-loan-fee-rate` (uint)
This variable defines the fee rate charged for loan operations.
* `approved-flash-loan-users` (datamap key: principal value: bool)
A datamap structure that lists all the approved users for loan operations.
#### Mathematical constants
Mathematical constants:
This symbolic constant is employed to define and restrict decimal precision to 8 decimal places.
* `ONE_8`
It is declared as `u100000000`.
* `ONE_8` It is declared as `u100000000`.
**Contract calls (interactions):**
* `executor-dao`
This call is used to verify whether a certain contract caller is designated as an extension.
## Contract calls (interactions)
* `token-trait`
Calls are made to token contracts that comply with the <ft-trait> defined in each token-trait variable to facilitate token transfers and retrieve balances during transfer and loan operations.
* `executor-dao` This call is used to verify whether a certain contract caller is designated as an extension.
* `flash-loan-user-trait`
Calls are made to token contracts that comply with the <flash-loan-trait> defined in the flash-loan-user-trait variable to execute loans for the approved flash-loan users and tokens.
* `token-trait` Calls are made to token contracts that comply with the <ft-trait> defined in each token-trait variable to facilitate token transfers and retrieve balances during transfer and loan operations.
**Features:**
* `flash-loan-user-trait` Calls are made to token contracts that comply with the <flash-loan-trait> defined in the flash-loan-user-trait variable to execute loans for the approved flash-loan users and tokens.
1) `add-to-reserve`
This function increases the existing reserves of a specific token by the given amount. It is governed by the `is-dao-or-extension` check to ensure that the `tx-sender` is an ALEX admin operator. Intended to be invoked by the main `amm-pool-v2-01.clar` contract during swap operations, this function is called following a transfer of the incoming token (token-x) to the vault contract. The `add-to-reserve` function is then executed with the token principal and the specified amount, representing the swap fees charged by the system for the transaction.
## Features
1. `add-to-reserve` This function increases the existing reserves of a specific token by the given amount. It is governed by the `is-dao-or-extension` check to ensure that the `tx-sender` is an ALEX admin operator. Intended to be invoked by the main `amm-pool-v2-01.clar` contract during swap operations, this function is called following a transfer of the incoming token (token-x) to the vault contract. The `add-to-reserve` function is then executed with the token principal and the specified amount, representing the swap fees charged by the system for the transaction.
**Input**:
```lisp
(token-trait principal)
(amount uint)
```
2) `remove-from-reserve`
This function serves as the reverse operation of `add-to-reserve`, decreasing the specified amount for the given token. An additional check ensures that the amount is less than or equal to the token's current reserve in the vault.
2. `remove-from-reserve` This function serves as the reverse operation of `add-to-reserve`, decreasing the specified amount for the given token. An additional check ensures that the amount is less than or equal to the token's current reserve in the vault.
Although it is not currently called by any existing module, the function also verifies that the `tx-sender` is an ALEX admin operator via the `is-dao-or-extension` control.
**Input**:
```lisp
(token-trait principal)
(amount uint)
```
3) `transfer-ft`
Like `add-to-reserve`, this function is intended to be called by the `amm-pool-v2-01.clar` contract during swap operations and is governed by the `is-dao-or-extension` check. Controls are in place to ensure that the contract is operational (not paused) and that the given token is approved in the contract's list.
3. `transfer-ft` Like `add-to-reserve`, this function is intended to be called by the `amm-pool-v2-01.clar` contract during swap operations and is governed by the `is-dao-or-extension` check. Controls are in place to ensure that the contract is operational (not paused) and that the given token is approved in the contract's list.
If all these conditions are met, the function will execute a transfer of the specified amount from the vault to the designated recipient.
**Input**:
```lisp
(token-trait <ft-trait>)
@@ -77,9 +71,7 @@ If all these conditions are met, the function will execute a transfer of the spe
(recipient principal)
```
4) `transfer-ft-two`
This function serves as a helper for calling the `transfer-ft` function twice with two different tokens and amounts for the same recipient within a single transaction. In the current model, this feature is utilized by the `amm-pool-v2-01.clar` contract during position reduction operations to return assets to the user.
4. `transfer-ft-two` This function serves as a helper for calling the `transfer-ft` function twice with two different tokens and amounts for the same recipient within a single transaction. In the current model, this feature is utilized by the `amm-pool-v2-01.clar` contract during position reduction operations to return assets to the user.
**Input**:
```lisp
(token-x-trait <ft-trait>)
@@ -89,9 +81,7 @@ This function serves as a helper for calling the `transfer-ft` function twice wi
(recipient principal)
```
5) `transfer-sft`
This function is similar to `transfer-ft`, but it is designed for semi-fungible tokens, which operate with a token ID. It includes the same controls as `transfer-ft` regarding the vault's operational status and approved tokens. Although this function is not called by the `amm-pool-v2-01.clar` contract in the current system design, it requires invocation by an ALEX admin operator, as governed by the `is-dao-or-extension` check.
5. `transfer-sft` This function is similar to `transfer-ft`, but it is designed for semi-fungible tokens, which operate with a token ID. It includes the same controls as `transfer-ft` regarding the vault's operational status and approved tokens. Although this function is not called by the `amm-pool-v2-01.clar` contract in the current system design, it requires invocation by an ALEX admin operator, as governed by the `is-dao-or-extension` check.
**Input**:
```lisp
(token-trait <sft-trait>)
@@ -100,10 +90,8 @@ This function is similar to `transfer-ft`, but it is designed for semi-fungible
(recipient principal)
```
6) `flash-loan`
This function is designed to lend the recipient (the `tx-sender`) a specified amount of tokens to execute an embedded function declared in the `flash-loan-trait`. The recipient then transfers back the same amount plus the corresponding fee (refer to `flash-loan-fee-rate`).
6. `flash-loan` This function is designed to lend the recipient (the `tx-sender`) a specified amount of tokens to execute an embedded function declared in the `flash-loan-trait`. The recipient then transfers back the same amount plus the corresponding fee (refer to `flash-loan-fee-rate`).
As with previous features, controls are in place to check the vault's operational status, approve tokens and flash-loan users, and ensure there is sufficient balance to make the transfer.
**Input**:
```lisp
(flash-loan-user-trait <flash-loan-trait>)
@@ -112,68 +100,64 @@ As with previous features, controls are in place to check the vault's operationa
(memo (optional (buff 16)))
```
Governance features:
1) `is-dao-or-extension`
This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract.
### Governance features
1. `is-dao-or-extension` This standard protocol function checks whether a caller (`tx-sender`) is the DAO executor or an authorized extension, delegating the extensions check to the `executor-dao` contract.
**Input**:
None.
2) `is-paused`
A read-only function that checks the operational status of the contract.
2. `is-paused` A read-only function that checks the operational status of the contract.
**Input**:
None.
3) `pause`
A public function, governed through the `is-dao-or-extension`, that can change the contract's operational status.
3. `pause` A public function, governed through the `is-dao-or-extension`, that can change the contract's operational status.
**Input**:
```lisp
(new-paused bool)
```
### Getter and Setter functions
Vault administration getter:
#### Vault administration getter
* `get-reserve`
Flash-loan operation setters and getters:
Setters:
* `set-flash-loan-enabled`
* `set-flash-loan-fee-rate`
#### Flash-loan operation setters and getters
**Setters**
* `set-approved-flash-loan-user`
* `set-approved-token`
* `set-flash-loan-enabled`
* `set-flash-loan-fee-rate`
Getters:
**Getters**
* `get-flash-loan-enabled`
* `get-flash-loan-fee-rate`
### Internal helper functions
* `check-is-approved-flash-loan-user`
This is a private function designed to verify whether a flash-loan user is approved in the contract's persisted datamap, `approved-flash-loan-users`.
#### Internal helper functions
* `check-is-approved-flash-loan-user` This is a private function designed to verify whether a flash-loan user is approved in the contract's persisted datamap, `approved-flash-loan-users`.
**Input**:
```lisp
(flash-loan-user-trait principal)
```
* `check-is-approved-token`
This is a private function designed to verify whether a token is approved in the contract's persisted datamap, `approved-tokens`.
* `check-is-approved-token` This is a private function designed to verify whether a token is approved in the contract's persisted datamap, `approved-tokens`.
**Input**:
```lisp
(flash-loan-token principal)
```
Mathematical helpers:
**Mathematical helpers**
These helper functions aid in various calculations within the context of the contract.
* `mul-down`
* `mul-up`
### Errors defined in the contract
* `ERR-NOT-AUTHORIZED`
* `ERR-PAUSED`
## Errors defined in the contract
* `ERR-AMOUNT-EXCEED-RESERVE`
* `ERR-INVALID-BALANCE`
* `ERR-INVALID-TOKEN`
* `ERR-AMOUNT-EXCEED-RESERVE`
* `ERR-NOT-AUTHORIZED`
* `ERR-PAUSED`