Skip to content
12 changes: 6 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ XRP is the native currency on the XRP Ledger network. The balance is tracked in

Aside from being a tradable asset, XRP serves two essential functions in the network. First, all low-level transaction fees (not to be confused with transfer fees) are paid in XRP.[^xrp-fees] When a transaction is processed, the fee is deducted from the sender's XRP balance and destroyed (burned), permanently removing it from circulation. Second, every account must maintain a minimum XRP balance called the reserve.[^xrp-reserve] The reserve requirement increases with each object the account owns on the ledger, such as trust lines, offers, or other entries.

[^xrp-fees]: Transaction fee deduction: [`Transactor.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/Transactor.cpp#L413-L414)
[^xrp-reserve]: Account reserve calculation: [`Fees.h`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/include/xrpl/protocol/Fees.h#L24-L33)
[^xrp-fees]: Transaction fee deduction: [`Transactor.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/Transactor.cpp#L443)
[^xrp-reserve]: Account reserve calculation: [`Fees.h`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/include/xrpl/protocol/Fees.h#L37-L46)

## 2.2. IOU

Expand All @@ -61,10 +61,10 @@ Issuer accounts can set the RequireAuth flag[^require-auth] to control which tru

For an explanation about different transactions used to create and modify trust lines see [Trust Lines](trust_lines/README.md). Their usage in payments will be covered in later reading.

[^quality-fields]: Quality fields on RippleState: [`ledger_entries.macro`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/include/xrpl/protocol/detail/ledger_entries.macro#L283-L287)
[^transfer-rate]: TransferRate field on AccountRoot: [`ledger_entries.macro`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/include/xrpl/protocol/detail/ledger_entries.macro#L142)
[^require-auth]: RequireAuth flag on AccountRoot: [`LedgerFormats.h`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/include/xrpl/protocol/LedgerFormats.h#L109-L110)
[^default-ripple]: DefaultRipple flag on AccountRoot: [`LedgerFormats.h`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/include/xrpl/protocol/LedgerFormats.h#L115-L116)
[^quality-fields]: Quality fields on RippleState: [`ledger_entries.macro`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/include/xrpl/protocol/detail/ledger_entries.macro#L284-L288)
[^transfer-rate]: TransferRate field on AccountRoot: [`ledger_entries.macro`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/include/xrpl/protocol/detail/ledger_entries.macro#L142)
[^require-auth]: RequireAuth flag on AccountRoot: [`LedgerFormats.h`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/include/xrpl/protocol/LedgerFormats.h#L130)
[^default-ripple]: DefaultRipple flag on AccountRoot: [`LedgerFormats.h`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/include/xrpl/protocol/LedgerFormats.h#L135)

## 2.3. MPT

Expand Down
142 changes: 71 additions & 71 deletions docs/amms/README.md

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/amms/deposit.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The `applyGuts` function[^apply-guts] is the main entry point for processing AMM

It retrieves the AMM ledger entry and current pool balances, then determines which trading fee applies to the depositor (regular or discounted for [auction slot holders](#3-gettradingfee)). Based on the transaction flags and provided fields, it dispatches to one of deposit mode handlers: three [multi-asset modes](#4-multi-asset-deposit-modes) that maintain proportional deposits or reinitialize empty pools, and three [single-asset modes](#5-single-asset-deposit-modes) that perform single-sided deposits. Each mode handler calculates the deposit amounts and LP tokens to issue, then calls the [common deposit function](#6-common-deposit-function) to execute the actual asset transfers and update the pool state.

[^apply-guts]: `AMMDeposit::applyGuts`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L383-L496)
[^apply-guts]: `AMMDeposit::applyGuts`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L383-L484)

## 2.1. applyGuts Pseudo-Code

Expand Down Expand Up @@ -188,7 +188,7 @@ The `getTradingFee` function[^get-trading-fee] is called by [`applyGuts`](#2-app

It checks if the depositor holds the [auction slot](README.md#121-auction-slot) or is listed in the slot's authorized accounts and if the auction slot has not expired. If so, it returns the discounted fee (1/10th of the regular fee). Otherwise, it returns the AMM's standard trading fee.

[^get-trading-fee]: `getTradingFee`: [`AMMUtils.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/misc/detail/AMMUtils.cpp#L163-L192)
[^get-trading-fee]: `getTradingFee`: [`AMMUtils.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/ledger/helpers/AMMHelpers.cpp#L566-L593)

## 3.1. getTradingFee Pseudo-Code

Expand Down Expand Up @@ -236,7 +236,7 @@ The three modes are:

The `equalDepositLimit` function[^equal-deposit-limit] handles proportional deposits where the depositor specifies maximum amounts they're willing to provide for both assets (`Amount` and `Amount2`). Since the deposit must maintain the pool's ratio, the function cannot simply use both maximum amounts - one will typically be limiting while the other has excess.

[^equal-deposit-limit]: `AMMDeposit::equalDepositLimit`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L738-L804)
[^equal-deposit-limit]: `AMMDeposit::equalDepositLimit`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L710-L780)

The function tries two strategies to maximize the deposit within the user's constraints. First, it attempts to use all of `Amount` by calculating the pool fraction this represents (`frac = Amount / amountBalance`), converting this to LP tokens with proper rounding, then recalculating the fraction from the rounded LP tokens (`frac = tokensAdj / lptAMMBalance`) to ensure precision consistency. Using this adjusted fraction, it calculates the proportional amount2 needed. If this amount2 fits within `Amount2`, the deposit proceeds immediately.

Expand Down Expand Up @@ -343,7 +343,7 @@ def equalDepositLimit(

Proportional deposit for exact LP tokens.[^equal-deposit-tokens]

[^equal-deposit-tokens]: `AMMDeposit::equalDepositTokens`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L662-L707)
[^equal-deposit-tokens]: `AMMDeposit::equalDepositTokens`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L637-L679)

This function handles the reverse calculation from [`equalDepositLimit`](#41-equaldepositlimit-tfTwoAsset): the user specifies the exact number of LP tokens they want to receive, and the function calculates the required proportional amounts of both assets. The user provides `LPTokenOut` (exact LP tokens desired) and optionally `Amount` and `Amount2` as minimum constraints on the deposit amounts. The function first adjusts the requested LP tokens for precision using [`adjustLPTokensOut`](helpers.md#24-adjustlptokensout-deposits), then calculates the pool fraction these tokens represent (`frac = tokensAdj / lptAMMBalance`). Using this fraction, it calculates the required amounts of both assets by multiplying each pool balance by the fraction, rounding up with [`getRoundedAsset`](helpers.md#23-getroundedasset) to ensure the pool receives sufficient assets. If the calculated deposit amounts are less than the optional `Amount` and `Amount2` minimums, the transaction fails with `tecAMM_FAILED`.

Expand Down Expand Up @@ -405,7 +405,7 @@ def equalDepositTokens(

Reinitialize an empty AMM pool.[^equal-deposit-in-empty-state]

[^equal-deposit-in-empty-state]: `AMMDeposit::equalDepositInEmptyState`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L1025-L1045)
[^equal-deposit-in-empty-state]: `AMMDeposit::equalDepositInEmptyState`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L994-L1014)

This is a special mode that handles the case where all LP tokens have been withdrawn from an AMM, which means both asset balances are also zero (enforced by the [withdrawal system](README.md#13-ammwithdraw)). The depositor specifies both `Amount` and `Amount2` to set a new pool ratio, similar to [`AMMCreate`](README.md#11-ammcreate). The function calculates initial LP tokens using the geometric mean formula (`sqrt(amount * amount2)`), the same formula used when creating a new AMM. The depositor can optionally provide `TradingFee` to set a new fee for the pool. Unlike other deposit modes, there are no minimum constraints since the depositor is establishing the initial terms for the reinitialized pool.

Expand Down Expand Up @@ -455,7 +455,7 @@ Single-asset deposits allow users to deposit only one asset instead of both asse

Deposit a single asset to receive calculated LP tokens.[^single-deposit]

[^single-deposit]: `AMMDeposit::singleDeposit`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L815-L852)
[^single-deposit]: `AMMDeposit::singleDeposit`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L791-L828)

The user specifies `Amount` (the asset to deposit) and the function calculates how many LP tokens they receive. Since this is a single-asset deposit that changes the pool ratio, a [trading fee](#3-gettradingfee) applies. The function uses [`lpTokensOut`](helpers.md#321-lptokensout-equation-3) to calculate the LP tokens based on the deposit amount and trading fee, then adjusts the result for precision with [`adjustLPTokensOut`](helpers.md#24-adjustlptokensout-deposits). The adjusted tokens are passed to [`adjustAssetInByTokens`](helpers.md#26-adjustassetinbytokens) to recalculate the deposit amount, ensuring the reverse calculation doesn't exceed the user's specified amount due to rounding. The optional `LPTokenOut` field provides a minimum constraint - if the calculated LP tokens are less than this value, the transaction fails with `tecAMM_FAILED`.

Expand Down Expand Up @@ -508,7 +508,7 @@ def singleDeposit(

Deposit a single asset to receive exact LP tokens.[^single-deposit-tokens]

[^single-deposit-tokens]: `AMMDeposit::singleDepositTokens`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L862-L892)
[^single-deposit-tokens]: `AMMDeposit::singleDepositTokens`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L838-L866)

This function handles the reverse calculation from [`singleDeposit`](#51-singledeposit-tfsingleasset): the user specifies the exact number of LP tokens they want to receive using `LPTokenOut`, and the function calculates the required deposit amount of a single asset. The user provides `Amount` as a maximum constraint on how much they're willing to deposit. The function first adjusts the requested LP tokens for precision using [`adjustLPTokensOut`](helpers.md#24-adjustlptokensout-deposits), then uses [`ammAssetIn`](helpers.md#322-ammassetin-equation-4) (Equation 4) to calculate the required deposit amount by solving the inverse single-asset deposit problem. If the calculated amount exceeds the user's `Amount` constraint, the transaction fails with `tecAMM_FAILED`.

Expand Down Expand Up @@ -559,7 +559,7 @@ def singleDepositTokens(

Deposit a single asset with an effective price limit.[^single-deposit-eprice]

[^single-deposit-eprice]: `AMMDeposit::singleDepositEPrice`: [`AMMDeposit.cpp`](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L920-L1022)
[^single-deposit-eprice]: `AMMDeposit::singleDepositEPrice`: [`AMMDeposit.cpp`](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L894-L991)

This mode allows users to control the maximum [effective price](README.md#121-effective-price) they're willing to pay per LP token (effective price = asset deposited / LP tokens received). The user provides `EPrice` (maximum effective price) and `Amount` (deposit amount, or zero).

Expand Down Expand Up @@ -706,7 +706,7 @@ def singleDepositEPrice(

The `deposit()` function[^deposit] is called by all deposit modes to perform the actual asset transfers. This function validates minimum constraints for slippage protection, checks the depositor has sufficient funds for the deposit, transfers the assets from the depositor to the AMM account, and issues LP tokens to the depositor (creating a trust line if needed).

[^deposit]: AMMDeposit::deposit: [AMMDeposit.cpp:513-645](https://github.com/gregtatcam/rippled/blob/a72c3438eb0591a76ac829305fcbcd0ed3b8c325/src/xrpld/app/tx/detail/AMMDeposit.cpp#L513-L645)
[^deposit]: AMMDeposit::deposit: [AMMDeposit.cpp:513-645](https://github.com/XRPLF/rippled/blob/0fffe23abc3a42e7d8016fbbd9a0beed3c40bbc9/src/libxrpl/tx/transactors/dex/AMMDeposit.cpp#L501-L620)

## 6.1. deposit Pseudo-Code

Expand Down
Loading