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.
To achieve this, the contract allows for the creation and updating of pools. Pool creation involves persisting an entry in a datamap, using token-x, token-y, and factor as the key and containing all relevant pool information.
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
Variables: (datamap)
pools-data-map(datamap key: { token-x: principal, token-y: principal, factor: uint } value: { pool-id: uint, total-supply: uint, balance-x: uint, balance-y: uint, pool-owner: principal, fee-rate-x: uint, fee-rate-y: uint, fee-rebate: uint, oracle-enabled: bool, oracle-average: uint, oracle-resilient: uint, start-block: uint, end-block: uint, threshold-x: uint, threshold-y: uint, max-in-ratio: uint, max-out-ratio: uint } ) A datamap structure that persists complete pool information. The map key consists of the unique pool identifier{token-x, token-y, factor}, and the map value contains detailed pool attributes.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 includetoken-xandtoken-yprincipals, andfactor.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.switch-threshold(uint) An internal variable used to set a fixed threshold for calculations. It is initialized withu80000000and can be retrieved and modified usingget-switch-thresholdandset-switch-thresholdfunctions. The value ofswitch-thresholdmust be less than or equal to the constantONE_8. This value is crucial for the mathematical formulas used within theamm-pool-v2-01.clarcontract.max-ratio-limit(uint) This variable sets the upper limit for the ratio in a token pool. These ratios are evaluated during each pool swap operation to determine the maximum amount that can be deposited or exchanged in the pool. It is initialized with the value of the constantONE_8.
Mathematical constants
This symbolic constant is employed to define and restrict decimal precision to 8 decimal places.
ONE_8It is declared asu100000000.
Contract calls (interactions)
executor-daoThis call is used to verify whether a certain contract caller is designated as an extension.
Features
create-poolThis function establishes a liquidity pool for a specified token pair (token-x/token-y). It begins by verifying that thetx-senderis an ALEX admin operator (see the functionis-dao-or-extension), as it is intended to be used by the mainamm-pool-v2-01.clarcontract 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 thepools-data-mapstructure, 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 (seepool-nonce). All remaining values in the datamap are initialized to zero (u0), except fororacle-enabled, which is set tofalse. Additionally,start-blockandend-blockare 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 thepools-data-map. Input:
update-poolThis function updates a liquidity pool identified by the unique combination oftoken-x,token-y, andfactor. It is a governed function that restricts thetx-senderto be an ALEX admin operator (see the functionis-dao-or-extension). Similar to the aforementionedcreate-poolfunction,update-poolis designed to be used by the mainamm-pool-v2-01.clarcontract. However, in this case, it is used indirectly in position and swap operations. Input:
Governance features
is-dao-or-extensionThis standard protocol function checks whether a caller (tx-sender) is the DAO executor or an authorized extension, delegating the extensions check to theexecutor-daocontract. Input: None.is-blocklisted-or-defaultA read-only feature that verifies if a given address is blacklisted using theblocklistmap. Input:
set-blocklist-manyA public function, governed by theis-dao-or-extensionmechanism, that allows setting or updating the blocklisted status for a list of addresses (up to 1000 addresses). Input:
Getter and Setter functions
Pool administration setters
set-fee-rebateset-pool-ownerset-max-ratio-limitset-switch-threshold
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
set-start-blockset-end-blockset-fee-rate-xset-fee-rate-yset-max-in-ratioset-max-out-ratioset-oracle-averageset-oracle-enabledset-threshold-xset-threshold-y
Getters
get-pool-details-by-idget-pool-detailsget-pool-existsget-max-ratio-limitget-switch-threshold
Internal helper functions
set-blocklistThis is a private function designed to complement the aforementioned governance functionset-blocklist-many. Input:
Errors defined in the contract
ERR-EXCEEDS-MAX-SLIPPAGEERR-INVALID-LIQUIDITYERR-INVALID-POOLERR-MAX-IN-RATIOERR-MAX-OUT-RATIOERR-NO-LIQUIDITYERR-NOT-AUTHORIZEDERR-ORACLE-AVERAGE-BIGGER-THAN-ONEERR-ORACLE-NOT-ENABLEDERR-PAUSEDERR-PERCENT-GREATER-THAN-ONEERR-POOL-ALREADY-EXISTSERR-SWITCH-THRESHOLD-BIGGER-THAN-ONE
Last updated
Was this helpful?