# Stable Pool

## Contract Interface

### 1. Notations & Structs

#### 1.1. PoolResultType

```rust
pub type PoolResultType<BigUint> = ManagedVec<BigUint, EsdtTokenPayment<BigUint>>;
```

#### 1.2. TokenAttributes

```rust
pub struct TokenAttributes<M: ManagedTypeApi> {
    pub reserve: BigUint<M>,
    pub rate: BigUint<M>,
}
```

#### 1.3. AddLiquidityAttributes

```rust
pub struct AddLiquidityAttributes<M: ManagedTypeApi> {
    pub token: TokenIdentifier<M>,
    pub attribute: TokenAttributes<M>,
    pub amount_added: BigUint<M>,
    pub total_fee: BigUint<M>,
    pub admin_fee: BigUint<M>,
}
```

#### 1.4. AddLiquidityEvent

```rust
pub struct AddLiquidityEvent<M: ManagedTypeApi> {
    lp_token_amount: BigUint<M>,
    lp_token_supply: BigUint<M>,
    tokens: ManagedVec<M, AddLiquidityAttributes<M>>,
}
```

#### 1.5. RemoveLiquidityAttributes

```rust
pub struct RemoveLiquidityAttributes<M: ManagedTypeApi> {
    pub token: TokenIdentifier<M>,
    pub attribute: TokenAttributes<M>,
    pub amount_removed: BigUint<M>,
}
```

#### 1.6. RemoveLiquidityEvent

```rust
pub struct RemoveLiquidityEvent<M: ManagedTypeApi> {
    lp_token_amount: BigUint<M>,
    lp_token_supply: BigUint<M>,
    tokens: ManagedVec<M, RemoveLiquidityAttributes<M>>,
}
```

#### 1.7. RemoveLiquidityOneCoinEvent

```rust
pub struct RemoveLiquidityOneCoinEvent<M: ManagedTypeApi> {
    lp_token_amount: BigUint<M>,
    lp_token_supply: BigUint<M>,
    total_fee: BigUint<M>,
    admin_fee: BigUint<M>,
    token_out: RemoveLiquidityAttributes<M>,
}
```

#### 1.8. ExchangeAttributes

```rust
pub struct ExchangeAttributes<M: ManagedTypeApi> {
    pub token: TokenIdentifier<M>,
    pub attribute: TokenAttributes<M>,
    pub final_amount: BigUint<M>,
}
```

#### 1.9. ExchangeEvent

```rust
pub struct ExchangeEvent<M: ManagedTypeApi> {
    total_fee: BigUint<M>,
    admin_fee: BigUint<M>,
    token_in: ExchangeAttributes<M>,
    token_out: ExchangeAttributes<M>,
}
```

### 2. Write functions

#### 2.1. Add liquidity

```rust
#[payable("*")]
#[endpoint(addLiquidity)]
fn add_liquidity(&self, mint_amount_min: BigUint, lp_token_receiver: &ManagedAddress) -> PoolResultType<Self::Api>

```

#### 2.2. Remove liquidity

```rust
#[payable("*")]
#[endpoint(removeLiquidity)]
fn remove_liquidity(&self, token_amount_min: MultiValueEncoded<BigUint>) -> PoolResultType<Self::Api>
```

#### 2.3. Exchange

```rust
#[payable("*")]
#[endpoint(exchange)]
fn exchange(&self, token_out: TokenIdentifier, amount_out_min: BigUint) -> PoolResultType<Self::Api>
```

`token_out`: The token id that you want to receive after exchanging.

`amount_out_min`: The minimum amount of `token_out` that you will receive. If the pool returns an amount that is smaller than `amount_out_min` the transaction will be reverted.

### 3. Read functions

#### 3.1. Estimate the exchange output

```rust
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone, ManagedVecItem)]
pub struct TokenAttributes<M: ManagedTypeApi> {
    pub reserve: BigUint<M>,
    pub rate: BigUint<M>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone, ManagedVecItem)]
pub struct ExchangeAttributes<M: ManagedTypeApi> {
    pub token: TokenIdentifier<M>,
    pub attribute: TokenAttributes<M>,
    pub final_amount: BigUint<M>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone)]
pub struct ExchangeResultType<M: ManagedTypeApi> {
    pub total_fee: BigUint<M>,
    pub admin_fee: BigUint<M>,
    pub token_in: ExchangeAttributes<M>,
    pub token_out: ExchangeAttributes<M>,
}

#[view(estimateAmountOut)]
fn get_amount_out(&self, token_in: &TokenIdentifier, token_out: &TokenIdentifier, amount_in: BigUint) -> ExchangeResultType<Self::Api>
```

#### 3.2. Estimate add liquidity

```rust
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone)]
pub struct AddLiquidityResultType<M: ManagedTypeApi> {
    pub mint_amount: BigUint<M>,
    pub tokens: ManagedVec<M, AddLiquidityAttributes<M>>,
}

#[view(estimateAddLiquidity)]
fn pool_add_liquidity(&self, token_amount_added: &ManagedVec<BigUint>) -> AddLiquidityResultType<Self::Api>
```

#### 3.3. Estimate remove liquidity

```rust
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone)]
pub struct RemoveLiquidityResultType<M: ManagedTypeApi> {
    pub burn_amount: BigUint<M>,
    pub tokens: ManagedVec<M, RemoveLiquidityAttributes<M>>,
}

#[view(estimateRemoveLiquidity)]
fn pool_remove_liquidity(&self, burn_amount: BigUint) -> RemoveLiquidityResultType<Self::Api>
```

#### 3.4. Get AMP factor

```rust
#[view(getAmpFactor)]
fn get_amp_factor(&self) -> u64
```

#### 3.5. Get pool state

```rust
#[derive(TopEncode, TopDecode, PartialEq, TypeAbi, Debug)]
pub enum State { Inactive, Active, ActiveNoSwaps }

#[view(getState)]
fn state(&self) -> State;
```

#### 3.6. Get LP token identifier

```rust
#[view(getLpTokenIdentifier)]
fn get_lp_token_identifier(&self) -> TokenIdentifier
```

#### 3.7. Get LP token supply

```rust
#[view(getTotalSupply)]
fn get_lp_token_supply(&self) -> BigUint
```

#### 3.8. Get pool tokens

```rust
#[view(getTokens)]
fn get_tokens(&self) -> ManagedVec<TokenIdentifier>
```

#### 3.9. Get token balances

```rust
#[view(getBalances)]
fn get_balances(&self, token: &TokenIdentifier) -> BigUint
```

#### 3.10. Get swap fee

```rust
#[view(getSwapFeePercent)]
fn swap_fee_percent(&self) -> u64;
```

#### 3.11. Get admin fee

```rust
#[view(getAdminFeePercent)]
fn admin_fee_percent(&self) -> u64;
```

### 4. Events

#### 4.1. Add liquidity

```rust
#[event("add_liquidity")]
fn add_liquidity_event(
    &self,
    #[indexed] timestamp: u64,
    #[indexed] caller: &ManagedAddress,
    add_liquidity_event: &AddLiquidityEvent<Self::Api>,
);
```

#### 4.2. Remove liquidity

```rust
#[event("remove_liquidity")]
fn remove_liquidity_event(
    &self,
    #[indexed] timestamp: u64,
    #[indexed] caller: &ManagedAddress,
    remove_liquidity_event: &RemoveLiquidityEvent<Self::Api>,
);
```

#### 4.3. Remove liquidity one coin

```rust
#[event("remove_liquidity_one_coin")]
fn remove_liquidity_one_coin_event(
    &self,
    #[indexed] timestamp: u64,
    #[indexed] caller: &ManagedAddress,
    remove_liquidity_one_coin_event: &RemoveLiquidityOneCoinEvent<Self::Api>,
);
```

#### 4.4. Exchange

```rust
#[event("exchange")]
fn exchange_event(
    &self,
    #[indexed] timestamp: u64,
    #[indexed] caller: &ManagedAddress,
    exchange_event: &ExchangeEvent<Self::Api>,
);
```


---

# Agent Instructions: 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:

```
GET https://docs.ashswap.io/developers/smart-contracts/stable-pool.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
