Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand Constant Product AMM's description #252

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/applications/constant-product-amm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

This is the Cairo adaptation of the [Solidity by Example - Constant Product AMM](https://solidity-by-example.org/defi/constant-product-amm/).

In this contract, we implement a simple Automated Market Maker (AMM) following
the **constant product formula**: \\( x \cdot y = k \\). This formula ensures
that the product of the two token reserves (`x` and `y` representing the tokens
being swapper) remains constant, regardless of trades. Here, we provide
liquidity pools that allow users to trade between two tokens or add and remove
liquidity from the pool.

## Key Concepts

1. **approve() before swap or adding liquidity**:
Before interacting with the AMM (whether through swaps or adding liquidity),
the user must approve the contract to spend their tokens. This is done by
calling the `approve()` function on the ERC20 token contracts, allowing the
AMM to transfer the required tokens on behalf of the user.

2. **Constant Product Formula for Swaps**:
The swap function operates based on the constant product formula \\( x \cdot
y = k \\), where `x` and `y` are the token reserves. When a user swaps one
token for another, the product of the reserves remains constant, which
determines how much of the other token the user will receive.

3. **Shares and Token Ratios for Liquidity**:
When adding liquidity, users provide both tokens in the ratio of the current
reserves. The number of shares (liquidity tokens) the user receives
represents their contribution to the pool. Similarly, when removing
liquidity, users receive back tokens proportional to the number of shares
they burn.

```cairo
{{#include ../../listings/applications/constant_product_amm/src/contracts.cairo:ConstantProductAmmContract}}
```