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. In addition to supporting Trading Pool operations, the vault contract also offers a flash-loan feature for registered tokens, available to approved users.
Storage
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 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 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.
Mathematical constants
This symbolic constant is employed to define and restrict decimal precision to 8 decimal places.
ONE_8
It is declared asu100000000
.
Contract calls (interactions)
executor-dao
This call is used to verify whether a certain contract caller is designated as an extension.token-trait
Calls are made to token contracts that comply with the defined in each token-trait parameter to facilitate token transfers and retrieve balances during transfer and loan operations.flash-loan-user-trait
Calls are made to token contracts that comply with the defined in the flash-loan-user-trait variable to execute loans for the approved flash-loan users and tokens.
Features
add-to-reserve
This function increases the existing reserves of a specific token by the given amount. It is governed by theis-dao-or-extension
check to ensure that thetx-sender
is an ALEX admin operator. Intended to be invoked by the mainamm-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. Theadd-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:
remove-from-reserve
This function serves as the reverse operation ofadd-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 thetx-sender
is an ALEX admin operator via theis-dao-or-extension
control. Input:
transfer-ft
Likeadd-to-reserve
, this function is intended to be called by theamm-pool-v2-01.clar
contract during swap operations and is governed by theis-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:
transfer-ft-two
This function serves as a helper for calling thetransfer-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 theamm-pool-v2-01.clar
contract during position reduction operations to return assets to the user. Input:
transfer-sft
This function is similar totransfer-ft
, but it is designed for semi-fungible tokens, which operate with a token ID. It includes the same controls astransfer-ft
regarding the vault's operational status and approved tokens. Although this function is not called by theamm-pool-v2-01.clar
contract in the current system design, it requires invocation by an ALEX admin operator, as governed by theis-dao-or-extension
check. Input:
flash-loan
This function is designed to lend the recipient (thetx-sender
) a specified amount of tokens to execute an embedded function declared in theflash-loan-trait
. The recipient then transfers back the same amount plus the corresponding fee (refer toflash-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:
Governance features
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 theexecutor-dao
contract. Input: None.is-paused
A read-only function that checks the operational status of the contract. Input: None.pause
A public function, governed through theis-dao-or-extension
, that can change the contract's operational status. Input:
Getter and Setter functions
Vault administration getter
get-reserve
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
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
. Input:
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:
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-AMOUNT-EXCEED-RESERVE
ERR-INVALID-BALANCE
ERR-INVALID-TOKEN
ERR-NOT-AUTHORIZED
ERR-PAUSED
Last updated