Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 3.24 KB

.note.md

File metadata and controls

54 lines (39 loc) · 3.24 KB

AMM Trading Functions Explanation

getInputAmountBasedOnOutput

  • Purpose: Used when a user wants to buy a specific amount of token Y and needs to know how much of token X is required.
  • Description: This function is about purchasing a fixed amount of the output with variable input. It's used to calculate the necessary amount of token X to buy a predetermined amount of token Y.

getOutputAmountBasedOnInput

  • Purpose: Used when a user is willing to sell or spend a specific amount of token X and wants to find out how much of token Y they will get.
  • Description: This function is about selling or converting a fixed input for a variable amount of output. It determines how much of token Y will be received in exchange for a given amount of token X.

Understanding the Inverse Relationship

Constant Product Formula Basis

Both functions operate based on the principle that the product of the reserves of two tokens in a liquidity pool must remain constant after a trade. This is mathematically expressed as: $$ X \times Y = K $$ where (X) and (Y) are the reserves of the two tokens, and (K) is a constant.

Effect of a Trade

  • When token (X) is added to the pool (increased by (dx)), token (Y) must decrease by (dy) to maintain the constant product (K).
  • Conversely, to increase (Y) by (dy), (X) must decrease by (dx).

How Each Function Works

getOutputAmountBasedOnInput

  • Mathematical Operation: Given an increase (dx) in (X), it calculates the required decrease (dy) in (Y) to keep (K) constant.
  • Formula: $$ dy = \frac{Y \times dx}{X + dx} $$ This formula derives from solving the equation: $$ (X + dx) \times (Y - dy) = K $$ for (dy).

getInputAmountBasedOnOutput

  • Mathematical Operation: Given a desired decrease (dy) in (Y), it calculates how much (X) must increase by (dx) to maintain (K).
  • Formula: $$ dx = \frac{X \times dy}{Y - dy} $$ This is derived by rearranging the same constant product equation but solving for (dx) instead, ensuring that: $$ (X + dx) \times (Y - dy) = K $$

Why the Inverse Nature?

  • Inverse Problems: Each function addresses the inverse problem of the other:
    • getOutputAmountBasedOnInput calculates the output decrease based on an input increase.
    • getInputAmountBasedOnOutput calculates the input increase required for a desired output decrease.
  • Trading Intent:
    • Buying vs. Selling:
      • getOutputAmountBasedOnInput reflects a trading scenario where the trader knows how much they want to spend and needs to know what they will get.
      • getInputAmountBasedOnOutput suits a scenario where the trader knows what they want to receive and needs to find out what it will cost them.

Conclusion

The different mathematical formulas used in these functions reflect their roles in handling the bidirectional nature of trades within liquidity pools governed by the CPF. By having both functions, AMMs can efficiently facilitate trades regardless of whether users are entering the trade with a fixed amount they wish to spend or a fixed amount they hope to receive. This dual functionality underpins the flexibility and robustness of AMMs in maintaining liquidity and providing continuous market pricing.