> For the complete documentation index, see [llms.txt](https://move-developers-dao.gitbook.io/aptos-move-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://move-developers-dao.gitbook.io/aptos-move-by-example/decentralized-finance/simple-swap-protocol-contract.md).

# Simple Swap Protocol Contract

## Overview

The Simple Swap Protocol contract implements a basic swap protocol for generating LP (Liquidity Provider) tokens, creating liquidity pools, adding/removing liquidity, and swapping tokens. It allows users to participate in liquidity provision and token swapping in a decentralized manner.

### Structs:

1. `LP<phantom X, phantom Y>`
   * A generic struct representing LP tokens for a specific token pair X-Y in the contract.
   * LP tokens represent ownership in liquidity pools.
2. `Pair<phantom X, phantom Y>`
   * A generic struct representing a liquidity pool for a specific token pair X-Y in the contract.
   * Contains fields to store the reserves of token X and Y, locked LP tokens, and mint/burn capabilities for LP tokens.

### Functions:

**`generate_lp_name_symbol<X, Y>(): String`**

* Generates a name symbol for LP tokens corresponding to the token pair X-Y.
* Concatenates "LP-", the symbol of token X, "-", and the symbol of token Y to form the LP token name symbol.

**`create_pool<X, Y>(sender: &signer)`**

* Creates a new liquidity pool for the token pair X-Y.
* Ensures no duplicate pools are created for the same token pair.
* Initializes the LP token for the pool to track liquidity provided by users.

**`add_liquidity<X, Y>(sender: &signer, x_amount: u64, y_amount: u64) acquires Pair`**

* Allows users to add liquidity to the pool by providing amounts of token X and Y.
* Calculates the optimal amount of Y tokens to be added given the amount of X tokens (or vice versa) to maintain a balanced pool.
* Mints LP tokens corresponding to the provided liquidity and deposits them into the user's account.

**`remove_liquidity<X, Y>(sender: &signer, liquidity: u64) acquires Pair`**

* Allows users to remove liquidity from the pool by burning their LP tokens.
* Calculates the amounts of token X and Y that the user will receive based on the proportion of their LP tokens to the total supply.
* Deposits the corresponding amounts of X and Y tokens into the user's account.

**`swap_x_to_y<X, Y>(sender: &signer, amount_in: u64) acquires Pair`**

* Allows users to swap token X for token Y.
* Calculates the amount of token Y that the user will receive in exchange for the provided amount of token X, considering a 0.3% fee.
* Deposits the received token Y into the user's account.

### Helper Functions:

**`pair_exist<X, Y>(addr: address) -> bool`**

* Checks if a liquidity pool exists for the token pair X-Y or Y-X.
* Returns true if a pool exists, otherwise false.

**`quote(x_amount: u128, x_reserve: u128, y_reserve: u128) -> u128`**

* Calculates the optimal amount of token Y given the amount of token X, X reserve, and Y reserve.

**`get_amount_out(amount_in: u128, in_reserve: u128, out_reserve: u128) -> u128`**

* Calculates the amount of output token for a given amount of input token in a swap, considering a 0.3% fee.

**`get_coin<X, Y>() -> (u64, u64) acquires Pair`**

* Returns the current reserves of token X and Y in the liquidity pool.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://move-developers-dao.gitbook.io/aptos-move-by-example/decentralized-finance/simple-swap-protocol-contract.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
