STEXAMM.sol
This page documents the Public & External Functions for the Liquidity Module contract in the Valantis Sovereign Pool Architecture for Stake Exchange (STEX) AMM.
This contract is critical for interfacing with pool reserves
Liquidity Providers
Deposit
andWithdraw
directly fromSTEXAMM.sol
The Sovereign Pool sources the exchange rate between
token0
&token1
using thegetLiquidityQuote()
function implemented inSTEXAMM.sol
The Withdrawal Module uses functions in
STEXAMM.sol
to interface with pool reservessupplyToken1Reserves()
to supply Native Token (token1
) pool reserves to an integrated borrow-lending market.unstakeToken0Reserves()
to send LST (token0
) pool reserves to the integrated LST Protocol Withdrawal Queue.
STEXAMM.sol
is the central contract coordinating the use of pool reserves.
Core Functions: Deposit, Withdrawal, Swap
1. deposit()
Purpose: Mint LP tokens by depositing Native Token (token1)
Note: Deposits are one-sided, only in Native Token (token1); LP tokens represent a share of all pool reserves.
Note: This function accepts the wrapped version of the Native Token. For LP deposits of unwrapped Native Token- Deposit Wrapper depositFromNative()
helper function must be used.
Note: owner
can pause this function.
Parameters:
_amount
: Native Token amount to deposit_minShares
: Minimum LP shares to receive_deadline
: Transaction validity timestamp_recipient
: LP shares recipient
Key Mechanics:
First deposit mints
MINIMUM_LIQUIDITY
toaddress(0)
Calculates shares using geometric mean formula
Transfers Native Token (
token1
) fromsender
viaDepositWrapper
Events:
emit
Deposit(address indexed sender, address indexed recipient, uint256 amount1, uint256 shares)
2. withdraw()
Purpose: Redeem LP shares for underlying assets
Parameters:
_shares
: LP tokens to burn_amount0Min
: Minimum token0 to receive_amount1Min
: Minimumtoken1
to receive_deadline
: Deadline for txns_recipient
: Assets recipient_unwrapToNativeToken
: Boolean to receive as native token (relevant if token1 is wrapped)_isInstantWithdrawal
: Boolean to swap the LPs share of LST (token0
) for Native Token (token1
) with internal swap; outputting only Native Token (token1
) while paying a fee.
Flow:
Calculates pro-rata share of reserves
If
_isInstantWithdrawal
is True:Process instant withdrawal pro-rata for Native Token shares (
token1
):Transfer pool reserve Native Token shares to the recipient
Withdrawal lending protocol Native Token shares via the Withdrawal Module and transfer to the recipient
Process instant withdrawal modeled as a swap() for LST shares (
token0
):Combine pro-rata shares of LST (
token0
) pool reserves & pro-rata shares of LST (token0
) in withdrawal queueSimulate a swap of the combined LST (
token0
) shares usinggetAmountOut()
and transfer the output Native Token (token1
) to the recipient
If
_isInstantWithdrawal
is False:Process instant withdrawal pro-rata for Native Token shares (
token1
) as aboveProcess delayed withdrawal pro-rata for LST shares (
token0
):Send pro-rata shares of LST pool reserves (
token0
) to the Withdrawal Queue for unstaking.Create an
LPWithdrawalRequest
for the claim of pro-rata shares of LST (token0
) pool reserves just sent to the Withdrawal Queue & pro-rata shares of LST (token0
) that was in the Withdrawal Queue beforewithdraw()
was called; claimable by recipient when mature.
Events:
emit
Withdraw(msg.sender, _recipient, amount0, amount1, _shares)
3. getLiquidityQuote()
Purpose: Provide a Swap Quote for the true rate of LST and Native Token
For further documentation on the getLiquidityQuote
function's role in Sovereign Pool check Liquidity Module Docs
Parameters:
_almLiquidityQuoteInput
: Swap direction and amounts
Returns:
quote.amountInFilled
: Actual input amountquote.amountOut
: Output amount
Note:
getLiquidityQuote()
calls the `StHYPEWithdrawalModule` functions `convertToToken0` or `convertToToken1` in order to obtain the ground truth exchange rate between LST and token1.owner
can pause this function, effectively pausing swaps.This function will revert when STEX AMM's reentrancy guard is active.
Pause Functions
1. pause()
Purpose: Pause deposit and getLiquidityQuote
Checks:
Only callable by owner
2. unpause()
Purpose: Unpause deposit and getLiquidityQuote
Checks:
Only callable by owner
Set Up Functions: Swap Fee Module, Withdrawal Module, & Manager Fees
The following functions are used for initial set up of the pool and future mutability.
Swap Fee Module
The STEXAMM
Liquidity Module as listed as the Sovereign Pool Pool Manager
allowing us to enable access controls and STEX specific logic for functions like Swap Fee Module upgrades.
1. proposeSwapFeeModule()
Purpose: Initiate fee module upgrade proposal
Parameters:
_swapFeeModule
: theaddress
of new module_timelockDelay
: the timelock in seconds before the upgrade can be executed
Checks:
Only callable by owner
New Swap Fee Module cannot be zero address
No active Swap Fee Module upgrade proposals
Proposed timelock within 3-7 days
Events:
emit
SwapFeeModuleProposed(_swapFeeModule, block.timestamp + _timelockDelay)
2. cancelSwapFeeModuleProposal()
Purpose: Delete proposal to set swap fee
Checks:
Only callable by owner
Events:
emit
SwapFeeModuleProposalCancelled()
3. setProposedSwapFeeModule()
Purpose: Execute fee module upgrade if the active proposal has become eligible
Checks:
Only callable by owner
Proposal exists and timelock expired
New module is contract
Events:
emit
SwapFeeModuleSet(proposal.swapFeeModule)
Withdrawal Module
The Withdrawal Module is upgradable under a 7 day timelock. This is a critical function as the withdrawal module is able to move pool reserves to potentially arbitrary contracts.
4. proposeWithdrawalModule()
Purpose: Initiate withdrawal module upgrade proposal under 7 day timelock
Parameter:
withdrawalModule_
: address of the new withdrawal module being proposed
Checks:
Only callable by owner
New Withdrawal Module cannot be zero address
No active Withdrawal Module upgrade proposals
Flow:
sets the
withdrawalModuleProposal
variable to a struct containing the proposal data
Events:
emit
WithdrawalModuleProposed(withdrawalModule_, block.timestamp + 7 days)
5. cancelWithdrawalModuleProposal()
Purpose: Cancel a withdrawal module upgrade proposal
Checks:
Only callable by owner
Flow:
deletes
withdrawalModuleProposal
data
Events:
emit
WithdrawalModuleProposalCancelled()
6. setProposedWithdrawalModule()
Purpose: Initiate fee module upgrade proposal under 7 day timelock
Checks:
Only callable by owner
There is an active Withdrawal Module Proposal
7 day timelock for the active proposal has surpassed
Flow:
Update Withdrawal Module address to the proposed address
Remove the active proposal
Events:
emit
WithdrawalModuleSet(proposal.withdrawalModule)
Manager Fees
7. setManagerFeeBips()
Purpose: Set protocol fee percentage
Parameters:
_managerFeeBips
: Fee in basis points (1-1000)
Checks:
Only callable by owner
Sovereign Pool checks that
_poolManagerFeeBips
is between0-1_000
Events:
emit
PoolManagerFeeSet(_poolManagerFeeBips)
;
STEX LST Withdrawal (Unstake)
1. unstakeToken0Reserves()
Purpose: Allow removal of LST reserves (token0) by the Withdrawal Module for unstaking
Checks:
only callable by WithdrawalModule
_unstakeAmountToken0
is less than or equal to the reserves of token0 in the pool
Flow:
STEXWithdrawalModule calls this function from inside its
unstakeToken0Reserves()
function to transfer the entire pool's LST reserves to the LST Protocol Unstake Queue._unstakeAmountToken0
oftoken0
reserve balance in the pool is transferred by the Withdrawal Module to the integrated unstake queue.
Events:
emit
Token0ReservesUnstaked(_unstakeAmountToken0)
STEX Reserve Lending
1. supplyToken1Reserves()
Purpose: Allows removal of Native Token reserves (token1) by Withdrawal Module for deposit in lending-borrowing protocol
Parameters:
_amount1
is the amount oftoken1
reserves to transfer to the Withdrawal Module
Checks:
Only callable by Withdrawal Module
Flow:
STEXWithdrawalModule
calls this function from inside itssupplyToken1ToLendingPool()
Protocol Fees
1. claimPoolManagerFees()
Purpose: Send accrued Pool Manager Fees to receiving addresses
Flow:
Collected fee is transferred to
poolFeeRecipient1
andpoolFeeRecipient2
in equal proportion
Events:
emit
PoolManagerFeesClaimed(fee0Received, fee1Received)
wherefee0Received
andfee1Received
are measured before splitting equal proportions betweenpoolFeeRecipient1
andpoolFeeRecipient2
Pool Manager Fees are accrued on each swap as a percentage of swap fee- specified by PoolManagerFeeBIPS
More documentation on Manager Fees can be found in Pool Manager Docs.
Helper Functions
1. getAmountOut()
Purpose: Helper function estimate amount out
Last updated
Was this helpful?