AMM (LLAMMA)
The AMM contract implements LLAMMA — the Lending-Liquidating AMM Algorithm. It holds borrower collateral distributed across discretized price bands and performs soft liquidation: as the collateral price drops into a borrower's band range, the AMM gradually converts their collateral to the borrowed token. If the price recovers, the process reverses (de-liquidation).
The AMM also serves as a regular DEX — arbitrageurs can swap between the collateral and borrowed tokens, which is the mechanism that drives soft liquidation.
Each lending market has its own AMM instance. The associated LendController is the AMM's admin and is the only address allowed to deposit/withdraw collateral.
AMM.vySwaps
exchange
AMM.exchange(i: uint256, j: uint256, in_amount: uint256, min_amount: uint256, _for: address) -> uint256[2]Swaps in_amount of token i for token j. Token index 0 is the borrowed token, index 1 is the collateral token.
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index (0 = borrowed, 1 = collateral) |
j | uint256 | Output token index |
in_amount | uint256 | Amount of input token |
min_amount | uint256 | Minimum output amount (slippage protection) |
_for | address | Address to receive the output tokens |
Returns: [in_amount_done, out_amount] (uint256[2]).
Emits: TokenExchange event.
<>Source code▼
▶Example▼
exchange_dy
AMM.exchange_dy(i: uint256, j: uint256, out_amount: uint256, max_amount: uint256, _for: address) -> uint256[2]Swaps to receive exactly out_amount of token j, paying at most max_amount of token i.
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index |
j | uint256 | Output token index |
out_amount | uint256 | Desired output amount |
max_amount | uint256 | Maximum input amount (slippage protection) |
_for | address | Address to receive the output tokens |
Returns: [in_amount, out_amount_done] (uint256[2]).
Emits: TokenExchange event.
<>Source code▼
▶Example▼
get_dy
AMM.get_dy(i: uint256, j: uint256, in_amount: uint256) -> uint256: viewCalculates the output amount for a given input amount (i.e., a swap quote).
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index |
j | uint256 | Output token index |
in_amount | uint256 | Input amount |
Returns: expected output amount (uint256).
<>Source code▼
▶Example▼
get_dx
AMM.get_dx(i: uint256, j: uint256, out_amount: uint256) -> uint256: viewCalculates the input amount needed to receive a given output amount.
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index |
j | uint256 | Output token index |
out_amount | uint256 | Desired output amount |
Returns: required input amount (uint256).
<>Source code▼
▶Example▼
get_dxdy
AMM.get_dxdy(i: uint256, j: uint256, in_amount: uint256) -> uint256[2]: viewReturns both the input amount used and the output amount for a given swap.
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index |
j | uint256 | Output token index |
in_amount | uint256 | Input amount |
Returns: [in_amount_done, out_amount] (uint256[2]).
<>Source code▼
▶Example▼
get_dydx
AMM.get_dydx(i: uint256, j: uint256, out_amount: uint256) -> uint256[2]: viewReturns both the output amount and the input amount needed for a given desired output.
| Input | Type | Description |
|---|---|---|
i | uint256 | Input token index |
j | uint256 | Output token index |
out_amount | uint256 | Desired output amount |
Returns: [out_amount_done, in_amount] (uint256[2]).
<>Source code▼
▶Example▼
get_amount_for_price
AMM.get_amount_for_price(_p: uint256) -> uint256: viewReturns the amount of token 0 (borrowed token) needed to move the AMM price to _p.
| Input | Type | Description |
|---|---|---|
_p | uint256 | Target price |
Returns: amount of token 0 needed (uint256).
<>Source code▼
▶Example▼
Price and Band Info
price_oracle
AMM.price_oracle() -> uint256: viewReturns the current price from the external price oracle, smoothed by the AMM's internal EMA.
Returns: oracle price (uint256).
<>Source code▼
▶Example▼
price_oracle_contract
AMM.price_oracle_contract() -> address: viewReturns the address of the external price oracle contract.
Returns: oracle address (address).
<>Source code▼
▶Example▼
get_p
AMM.get_p() -> uint256: viewReturns the current AMM price (the marginal price at the active band).
Returns: current price (uint256).
<>Source code▼
▶Example▼
get_base_price
AMM.get_base_price() -> uint256: viewReturns the base price used to calculate band boundaries, adjusted for accumulated interest rate.
Returns: base price (uint256).
<>Source code▼
▶Example▼
active_band
AMM.active_band() -> int256: viewReturns the index of the currently active band (the band where trading is happening).
Returns: active band index (int256).
<>Source code▼
▶Example▼
active_band_with_skip
AMM.active_band_with_skip() -> int256: viewReturns the active band, skipping over empty bands for gas efficiency.
Returns: active band index (int256).
<>Source code▼
▶Example▼
min_band
AMM.min_band() -> int256: viewReturns the lowest band index that has ever been used.
Returns: minimum band index (int256).
<>Source code▼
▶Example▼
max_band
AMM.max_band() -> int256: viewReturns the highest band index that has ever been used.
Returns: maximum band index (int256).
<>Source code▼
▶Example▼
p_current_up
AMM.p_current_up(_n: int256) -> uint256: viewReturns the upper price boundary of band _n, accounting for the current state of the band.
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: upper price boundary (uint256).
<>Source code▼
▶Example▼
p_current_down
AMM.p_current_down(_n: int256) -> uint256: viewReturns the lower price boundary of band _n, accounting for the current state of the band.
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: lower price boundary (uint256).
<>Source code▼
▶Example▼
p_oracle_up
AMM.p_oracle_up(_n: int256) -> uint256: viewReturns the upper oracle price boundary of band _n (based on the oracle price, not the current AMM state).
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: upper oracle price (uint256).
<>Source code▼
▶Example▼
p_oracle_down
AMM.p_oracle_down(_n: int256) -> uint256: viewReturns the lower oracle price boundary of band _n.
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: lower oracle price (uint256).
<>Source code▼
▶Example▼
dynamic_fee
AMM.dynamic_fee() -> uint256: viewReturns the current dynamic fee, which increases when the AMM price deviates from the oracle price to discourage trades that worsen the deviation.
Returns: dynamic fee (uint256).
<>Source code▼
▶Example▼
User Position Info
get_y_up
AMM.get_y_up(_user: address) -> uint256: viewReturns the amount of collateral token a user would have if the price goes up (full de-liquidation scenario).
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: collateral amount (uint256).
<>Source code▼
▶Example▼
get_x_down
AMM.get_x_down(_user: address) -> uint256: viewReturns the amount of borrowed token a user would have if the price goes down (full soft-liquidation scenario).
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: borrowed token amount (uint256).
<>Source code▼
▶Example▼
get_sum_xy
AMM.get_sum_xy(_user: address) -> uint256[2]: viewReturns the total amounts of borrowed token and collateral token held in a user's bands.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: [sum_x (borrowed), sum_y (collateral)] (uint256[2]).
<>Source code▼
▶Example▼
get_xy
AMM.get_xy(_user: address) -> IController.UserTicks[]: viewReturns the per-band breakdown of a user's position — the amount of borrowed and collateral tokens in each band.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: array of per-band positions (IController.UserTicks[]).
<>Source code▼
▶Example▼
read_user_tick_numbers
AMM.read_user_tick_numbers(_user: address) -> int256[2]: viewReturns the upper and lower band indices of a user's position.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: [n1 (upper), n2 (lower)] (int256[2]).
<>Source code▼
▶Example▼
has_liquidity
AMM.has_liquidity(_user: address) -> bool: viewReturns whether a user has any liquidity deposited in the AMM.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: has liquidity (bool).
<>Source code▼
▶Example▼
bands_x
AMM.bands_x(_n: int256) -> uint256: viewReturns the total amount of borrowed token in band _n.
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: borrowed token amount in band (uint256).
<>Source code▼
▶Example▼
bands_y
AMM.bands_y(_n: int256) -> uint256: viewReturns the total amount of collateral token in band _n.
| Input | Type | Description |
|---|---|---|
_n | int256 | Band index |
Returns: collateral token amount in band (uint256).
<>Source code▼
▶Example▼
user_shares
AMM.user_shares(_user: address) -> UserTicks: viewReturns a user's share information across their deposited bands.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
Returns: user tick/share info (UserTicks).
<>Source code▼
▶Example▼
Rate and Fee
rate
AMM.rate() -> uint256: viewReturns the current interest rate stored in the AMM, set by the controller from the monetary policy.
Returns: current rate (uint256).
<>Source code▼
▶Example▼
get_rate_mul
AMM.get_rate_mul() -> uint256: viewReturns the current rate multiplier — a cumulative value that tracks how much the base price has shifted due to interest accrual.
Returns: rate multiplier (uint256).
<>Source code▼
▶Example▼
fee
AMM.fee() -> uint256: viewReturns the current swap fee.
Returns: fee (uint256).
<>Source code▼
▶Example▼
Admin Methods
These functions are restricted to the AMM's admin, which is the LendController.
set_admin
AMM.set_admin(_admin: address)This function is only callable by the current admin (the LendController).
Transfers AMM admin to a new address.
| Input | Type | Description |
|---|---|---|
_admin | address | New admin address |
<>Source code▼
▶Example▼
deposit_range
AMM.deposit_range(_user: address, _amount: uint256, _n1: int256, _n2: int256)This function is only callable by the admin (LendController). It is called internally when a loan is created or collateral is added.
Deposits collateral for a user across bands _n1 to _n2.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
_amount | uint256 | Collateral amount |
_n1 | int256 | Upper band index |
_n2 | int256 | Lower band index |
Emits: Deposit event.
<>Source code▼
▶Example▼
withdraw
AMM.withdraw(_user: address, _frac: uint256) -> uint256[2]This function is only callable by the admin (LendController). It is called internally during repayment and liquidation.
Withdraws a fraction of a user's position from the AMM. A _frac of 10**18 means full withdrawal.
| Input | Type | Description |
|---|---|---|
_user | address | User address |
_frac | uint256 | Fraction to withdraw (scaled by 1e18) |
Returns: [withdrawn_borrowed, withdrawn_collateral] (uint256[2]).
Emits: Withdraw event.
<>Source code▼
▶Example▼
set_rate
AMM.set_rate(_rate: uint256) -> uint256This function is only callable by the admin (LendController).
Sets the interest rate in the AMM and returns the updated rate multiplier.
| Input | Type | Description |
|---|---|---|
_rate | uint256 | New rate value |
Returns: updated rate multiplier (uint256).
Emits: SetRate event.
<>Source code▼
▶Example▼
set_fee
AMM.set_fee(_fee: uint256)This function is only callable by the admin (LendController).
Sets the swap fee.
| Input | Type | Description |
|---|---|---|
_fee | uint256 | New fee value |
Emits: SetFee event.
<>Source code▼
▶Example▼
set_price_oracle
AMM.set_price_oracle(_price_oracle: address)This function is only callable by the admin (LendController).
Sets the external price oracle contract.
| Input | Type | Description |
|---|---|---|
_price_oracle | address | New oracle address |
Emits: SetPriceOracle event.
<>Source code▼
▶Example▼
set_callback
AMM.set_callback(_liquidity_mining_callback: address)This function is only callable by the admin (LendController).
Sets the liquidity mining callback (gauge) address.
| Input | Type | Description |
|---|---|---|
_liquidity_mining_callback | address | Callback address |
<>Source code▼
▶Example▼
Other Methods
coins
AMM.coins(_i: uint256) -> address: viewReturns the token address at index _i (0 = borrowed token, 1 = collateral token).
| Input | Type | Description |
|---|---|---|
_i | uint256 | Token index (0 or 1) |
Returns: token address (address).
<>Source code▼
▶Example▼
A
AMM.A() -> uint256: viewReturns the amplification coefficient. Band width is approximately 1/A.
Returns: amplification coefficient (uint256).
<>Source code▼
▶Example▼
admin
AMM.admin() -> address: viewReturns the admin address (the LendController).
Returns: admin address (address).
<>Source code▼
▶Example▼
can_skip_bands
AMM.can_skip_bands(_n_end: int256) -> bool: viewReturns whether bands up to _n_end can be skipped (are empty) during traversal.
| Input | Type | Description |
|---|---|---|
_n_end | int256 | Target band index |
Returns: whether bands can be skipped (bool).
<>Source code▼
▶Example▼
liquidity_mining_callback
AMM.liquidity_mining_callback() -> address: viewReturns the liquidity mining callback (gauge) address.
Returns: callback address (address).