Skip to main content

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.vy

The source code for the AMM.vy contract can be found on GitHub. The contract is written in Vyper version 0.4.3.

Deployment addresses will be added once contracts are finalized.


Swaps

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.

InputTypeDescription
iuint256Input token index (0 = borrowed, 1 = collateral)
juint256Output token index
in_amountuint256Amount of input token
min_amountuint256Minimum output amount (slippage protection)
_foraddressAddress 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.

InputTypeDescription
iuint256Input token index
juint256Output token index
out_amountuint256Desired output amount
max_amountuint256Maximum input amount (slippage protection)
_foraddressAddress 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: view

Calculates the output amount for a given input amount (i.e., a swap quote).

InputTypeDescription
iuint256Input token index
juint256Output token index
in_amountuint256Input amount

Returns: expected output amount (uint256).

<>Source code
Example

get_dx

AMM.get_dx(i: uint256, j: uint256, out_amount: uint256) -> uint256: view

Calculates the input amount needed to receive a given output amount.

InputTypeDescription
iuint256Input token index
juint256Output token index
out_amountuint256Desired output amount

Returns: required input amount (uint256).

<>Source code
Example

get_dxdy

AMM.get_dxdy(i: uint256, j: uint256, in_amount: uint256) -> uint256[2]: view

Returns both the input amount used and the output amount for a given swap.

InputTypeDescription
iuint256Input token index
juint256Output token index
in_amountuint256Input 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]: view

Returns both the output amount and the input amount needed for a given desired output.

InputTypeDescription
iuint256Input token index
juint256Output token index
out_amountuint256Desired 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: view

Returns the amount of token 0 (borrowed token) needed to move the AMM price to _p.

InputTypeDescription
_puint256Target price

Returns: amount of token 0 needed (uint256).

<>Source code
Example

Price and Band Info

price_oracle

AMM.price_oracle() -> uint256: view

Returns 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: view

Returns the address of the external price oracle contract.

Returns: oracle address (address).

<>Source code
Example

get_p

AMM.get_p() -> uint256: view

Returns 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: view

Returns 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: view

Returns 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: view

Returns the active band, skipping over empty bands for gas efficiency.

Returns: active band index (int256).

<>Source code
Example

min_band

AMM.min_band() -> int256: view

Returns the lowest band index that has ever been used.

Returns: minimum band index (int256).

<>Source code
Example

max_band

AMM.max_band() -> int256: view

Returns 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: view

Returns the upper price boundary of band _n, accounting for the current state of the band.

InputTypeDescription
_nint256Band index

Returns: upper price boundary (uint256).

<>Source code
Example

p_current_down

AMM.p_current_down(_n: int256) -> uint256: view

Returns the lower price boundary of band _n, accounting for the current state of the band.

InputTypeDescription
_nint256Band index

Returns: lower price boundary (uint256).

<>Source code
Example

p_oracle_up

AMM.p_oracle_up(_n: int256) -> uint256: view

Returns the upper oracle price boundary of band _n (based on the oracle price, not the current AMM state).

InputTypeDescription
_nint256Band index

Returns: upper oracle price (uint256).

<>Source code
Example

p_oracle_down

AMM.p_oracle_down(_n: int256) -> uint256: view

Returns the lower oracle price boundary of band _n.

InputTypeDescription
_nint256Band index

Returns: lower oracle price (uint256).

<>Source code
Example

dynamic_fee

AMM.dynamic_fee() -> uint256: view

Returns 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: view

Returns the amount of collateral token a user would have if the price goes up (full de-liquidation scenario).

InputTypeDescription
_useraddressUser address

Returns: collateral amount (uint256).

<>Source code
Example

get_x_down

AMM.get_x_down(_user: address) -> uint256: view

Returns the amount of borrowed token a user would have if the price goes down (full soft-liquidation scenario).

InputTypeDescription
_useraddressUser address

Returns: borrowed token amount (uint256).

<>Source code
Example

get_sum_xy

AMM.get_sum_xy(_user: address) -> uint256[2]: view

Returns the total amounts of borrowed token and collateral token held in a user's bands.

InputTypeDescription
_useraddressUser address

Returns: [sum_x (borrowed), sum_y (collateral)] (uint256[2]).

<>Source code
Example

get_xy

AMM.get_xy(_user: address) -> IController.UserTicks[]: view

Returns the per-band breakdown of a user's position — the amount of borrowed and collateral tokens in each band.

InputTypeDescription
_useraddressUser 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]: view

Returns the upper and lower band indices of a user's position.

InputTypeDescription
_useraddressUser address

Returns: [n1 (upper), n2 (lower)] (int256[2]).

<>Source code
Example

has_liquidity

AMM.has_liquidity(_user: address) -> bool: view

Returns whether a user has any liquidity deposited in the AMM.

InputTypeDescription
_useraddressUser address

Returns: has liquidity (bool).

<>Source code
Example

bands_x

AMM.bands_x(_n: int256) -> uint256: view

Returns the total amount of borrowed token in band _n.

InputTypeDescription
_nint256Band index

Returns: borrowed token amount in band (uint256).

<>Source code
Example

bands_y

AMM.bands_y(_n: int256) -> uint256: view

Returns the total amount of collateral token in band _n.

InputTypeDescription
_nint256Band index

Returns: collateral token amount in band (uint256).

<>Source code
Example

user_shares

AMM.user_shares(_user: address) -> UserTicks: view

Returns a user's share information across their deposited bands.

InputTypeDescription
_useraddressUser address

Returns: user tick/share info (UserTicks).

<>Source code
Example

Rate and Fee

rate

AMM.rate() -> uint256: view

Returns 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: view

Returns 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: view

Returns 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)
Guarded Method

This function is only callable by the current admin (the LendController).

Transfers AMM admin to a new address.

InputTypeDescription
_adminaddressNew admin address
<>Source code
Example

deposit_range

AMM.deposit_range(_user: address, _amount: uint256, _n1: int256, _n2: int256)
Guarded Method

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.

InputTypeDescription
_useraddressUser address
_amountuint256Collateral amount
_n1int256Upper band index
_n2int256Lower band index

Emits: Deposit event.

<>Source code
Example

withdraw

AMM.withdraw(_user: address, _frac: uint256) -> uint256[2]
Guarded Method

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.

InputTypeDescription
_useraddressUser address
_fracuint256Fraction 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) -> uint256
Guarded Method

This function is only callable by the admin (LendController).

Sets the interest rate in the AMM and returns the updated rate multiplier.

InputTypeDescription
_rateuint256New rate value

Returns: updated rate multiplier (uint256).

Emits: SetRate event.

<>Source code
Example

set_fee

AMM.set_fee(_fee: uint256)
Guarded Method

This function is only callable by the admin (LendController).

Sets the swap fee.

InputTypeDescription
_feeuint256New fee value

Emits: SetFee event.

<>Source code
Example

set_price_oracle

AMM.set_price_oracle(_price_oracle: address)
Guarded Method

This function is only callable by the admin (LendController).

Sets the external price oracle contract.

InputTypeDescription
_price_oracleaddressNew oracle address

Emits: SetPriceOracle event.

<>Source code
Example

set_callback

AMM.set_callback(_liquidity_mining_callback: address)
Guarded Method

This function is only callable by the admin (LendController).

Sets the liquidity mining callback (gauge) address.

InputTypeDescription
_liquidity_mining_callbackaddressCallback address
<>Source code
Example

Other Methods

coins

AMM.coins(_i: uint256) -> address: view

Returns the token address at index _i (0 = borrowed token, 1 = collateral token).

InputTypeDescription
_iuint256Token index (0 or 1)

Returns: token address (address).

<>Source code
Example

A

AMM.A() -> uint256: view

Returns the amplification coefficient. Band width is approximately 1/A.

Returns: amplification coefficient (uint256).

<>Source code
Example

admin

AMM.admin() -> address: view

Returns the admin address (the LendController).

Returns: admin address (address).

<>Source code
Example

can_skip_bands

AMM.can_skip_bands(_n_end: int256) -> bool: view

Returns whether bands up to _n_end can be skipped (are empty) during traversal.

InputTypeDescription
_n_endint256Target band index

Returns: whether bands can be skipped (bool).

<>Source code
Example

liquidity_mining_callback

AMM.liquidity_mining_callback() -> address: view

Returns the liquidity mining callback (gauge) address.

Returns: callback address (address).

<>Source code
Example