# Interactions

The SelfPeggingAsset contract exposes the primary trading and liquidity management functions for interacting with Tapio pools. This section details the available functions for minting SPAs, executing swaps, and redeeming assets, along with their parameters and return values for integration into trading interfaces and DeFi applications.

### **User Operations**

#### **mint**

Adds liquidity to the pool by depositing tokens in the amounts specified in `_amounts`. Returns the amount of SPA tokens minted, which must be at least `_minMintAmount`.

```solidity
function mint(uint256[] calldata _amounts, uint256 _minMintAmount) external returns (uint256);

```

**Parameters**

| Name             | Type        | Description                                        |
| ---------------- | ----------- | -------------------------------------------------- |
| `_amounts`       | `uint256[]` | Unconverted token balances used to mint pool token |
| `_minMintAmount` | `uint256`   | Minimum amount of pool token to mint               |

**Returns**

| Name     | Type      | Description                      |
| -------- | --------- | -------------------------------- |
| `<none>` | `uint256` | The amount of pool tokens minted |

#### **swap**

Exchange between two underlying tokens.

```solidity
function swap(uint256 _i, uint256 _j, uint256 _dx, uint256 _minDy) external returns (uint256);

```

**Parameters**

| Name     | Type      | Description                                         |
| -------- | --------- | --------------------------------------------------- |
| `_i`     | `uint256` | Token index to swap in                              |
| `_j`     | `uint256` | Token index to swap out                             |
| `_dx`    | `uint256` | Unconverted amount of token `_i` to swap in         |
| `_minDy` | `uint256` | Minimum token `_j` to swap out in converted balance |

**Returns**

| Name     | Type      | Description        |
| -------- | --------- | ------------------ |
| `<none>` | `uint256` | Amount of swap out |

#### **redeemProportion**

Redeems pool token to underlying tokens proportionally.

```solidity
function redeemProportion(uint256 _amount, uint256[] calldata _minRedeemAmounts) external returns (uint256[] memory);

```

**Parameters**

| Name                | Type        | Description                                |
| ------------------- | ----------- | ------------------------------------------ |
| `_amount`           | `uint256`   | Amount of pool token to redeem             |
| `_minRedeemAmounts` | `uint256[]` | Minimum amount of underlying tokens to get |

**Returns**

| Name     | Type        | Description                                     |
| -------- | ----------- | ----------------------------------------------- |
| `<none>` | `uint256[]` | An array of the amounts of each token to redeem |

#### **redeemSingle**

Redeem pool token to one specific underlying token.

```solidity
function redeemSingle(uint256 _amount, uint256 _i, uint256 _minRedeemAmount) external returns (uint256);

```

**Parameters**

| Name               | Type      | Description                                         |
| ------------------ | --------- | --------------------------------------------------- |
| `_amount`          | `uint256` | Amount of pool token to redeem                      |
| `_i`               | `uint256` | Index of the token to redeem to                     |
| `_minRedeemAmount` | `uint256` | Minimum amount of the underlying token to redeem to |

**Returns**

| Name     | Type      | Description     |
| -------- | --------- | --------------- |
| `<none>` | `uint256` | Amount received |

#### **redeemMulti**

Redeems underlying tokens.

```solidity
function redeemMulti(uint256[] calldata _amounts, uint256 _maxRedeemAmount) external returns (uint256[] memory);

```

**Parameters**

| Name               | Type        | Description                               |
| ------------------ | ----------- | ----------------------------------------- |
| `_amounts`         | `uint256[]` | Amounts of underlying tokens to redeem to |
| `_maxRedeemAmount` | `uint256`   | Maximum of pool token to redeem           |

**Returns**

| Name     | Type        | Description      |
| -------- | ----------- | ---------------- |
| `<none>` | `uint256[]` | Amounts received |

### **Governance Functions**

#### **pause**

Pause mint/swap/redeem actions.

```solidity
function pause() external;

```

**Access:** only Owner (via Keeper)

#### **unpause**

Unpause mint/swap/redeem actions.

```solidity
function unpause() external;
```

**Access:** only Owner (via Keeper)

#### **setMintFee**

Updates the mint fee on pool.

```solidity

function setMintFee(uint256 _mintFee) external;
```

**Parameters**

| Name       | Type      | Description                                                                                                          |
| ---------- | --------- | -------------------------------------------------------------------------------------------------------------------- |
| `_mintFee` | `uint256` | The new mint fee value. Uses 10 decimal precision (denominator = 10^10). Examples: 1e6 = 0.01%, 1e7 = 0.1%, 1e8 = 1% |

**Access:** only Owner (via Keeper)

#### **setSwapFee**

Updates the swap fee on pool.

```solidity

function setSwapFee(uint256 _swapFee) external;
```

**Parameters**

| Name       | Type      | Description                                                                                                          |
| ---------- | --------- | -------------------------------------------------------------------------------------------------------------------- |
| `_swapFee` | `uint256` | The new swap fee value. Uses 10 decimal precision (denominator = 10^10). Examples: 1e6 = 0.01%, 1e7 = 0.1%, 1e8 = 1% |

**Access:** only Owner (via Keeper)

#### **setRedeemFee**

Updates the redeem fee on pool.

```solidity

function setRedeemFee(uint256 _redeemFee) external;
```

**Parameters**

| Name         | Type      | Description                                                                                                            |
| ------------ | --------- | ---------------------------------------------------------------------------------------------------------------------- |
| `_redeemFee` | `uint256` | The new redeem fee value. Uses 10 decimal precision (denominator = 10^10). Examples: 1e6 = 0.01%, 1e7 = 0.1%, 1e8 = 1% |

**Access:** only Owner (via Keeper)

#### **setOffPegFeeMultiplier**

Updates the off peg fee multiplier of pool.

```solidity

function setOffPegFeeMultiplier(uint256 _offPegFeeMultiplier) external;
```

**Parameters**

| Name                   | Type      | Description                          |
| ---------------------- | --------- | ------------------------------------ |
| `_offPegFeeMultiplier` | `uint256` | The new off peg fee multiplier value |

**Access:** only Owner (via Keeper)

#### **setExchangeRateFeeFactor**

Updates the exchange rate fee factor of pool.

```solidity

function setExchangeRateFeeFactor(uint256 _exchangeRateFeeFactor) external;
```

**Parameters**

| Name                     | Type      | Description                            |
| ------------------------ | --------- | -------------------------------------- |
| `_exchangeRateFeeFactor` | `uint256` | The new exchange rate fee factor value |

**Access:** only Owner (via Keeper)

#### **setDecayPeriod**

Updates the decay period

```solidity

function setDecayPeriod(uint256 _decayPeriod) external;
```

**Parameters**

| Name           | Type      | Description                |
| -------------- | --------- | -------------------------- |
| `_decayPeriod` | `uint256` | The new decay period value |

**Access:** only Owner (via Keeper)

#### **setRateChangeSkipPeriod**

Updates the rate change skip period of pool.

```solidity

function setRateChangeSkipPeriod(uint256 _rateChangeSkipPeriod) external;
```

**Parameters**

| Name                    | Type      | Description                           |
| ----------------------- | --------- | ------------------------------------- |
| `_rateChangeSkipPeriod` | `uint256` | The new rate change skip period value |

**Access:** only Owner (via Keeper)

#### **updateFeeErrorMargin**

Updates the fee error margin of pool.

```solidity

function updateFeeErrorMargin(uint256 newValue) external;
```

**Parameters**

| Name       | Type      | Description                    |
| ---------- | --------- | ------------------------------ |
| `newValue` | `uint256` | The new fee error margin value |

**Access:** only Owner (via Keeper)

#### **updateYieldErrorMargin**

Updates yield error margin of pool.

```solidity

function updateYieldErrorMargin(uint256 newValue) external;
```

**Parameters**

| Name       | Type      | Description                      |
| ---------- | --------- | -------------------------------- |
| `newValue` | `uint256` | The new yield error margin value |

**Access:** only Owner (via Keeper)

#### **distributeLoss**

Distribute losses by rebasing negatively

```solidity
function distributeLoss() external;
```

**Access:** only Owner (via Keeper)

### **View Functions**

#### **getSwapAmount**

Computes the output amount after the swap.

```solidity
function getSwapAmount(uint256 _i, uint256 _j, uint256 _dx) external view returns (uint256, uint256);

```

**Parameters**

| Name  | Type      | Description                                 |
| ----- | --------- | ------------------------------------------- |
| `_i`  | `uint256` | Token index to swap in                      |
| `_j`  | `uint256` | Token index to swap out                     |
| `_dx` | `uint256` | Unconverted amount of token `_i` to swap in |

**Returns**

| Name     | Type      | Description                                  |
| -------- | --------- | -------------------------------------------- |
| `<none>` | `uint256` | Unconverted amount of token `_j` to swap out |
| `<none>` | `uint256` | The amount of fees charged                   |

#### **getMintAmount**

Compute the amount of pool token that can be minted.

```solidity
function getMintAmount(uint256[] calldata _amounts) external view returns (uint256, uint256);

```

**Parameters**

| Name       | Type        | Description                |
| ---------- | ----------- | -------------------------- |
| `_amounts` | `uint256[]` | Unconverted token balances |

**Returns**

| Name     | Type      | Description                            |
| -------- | --------- | -------------------------------------- |
| `<none>` | `uint256` | The amount of pool tokens to be minted |
| `<none>` | `uint256` | The amount of fees charged             |

#### **getCurrentA**

Get the current A value from the controller if set, or use the local value.

```solidity
function getCurrentA() external view returns (uint256);

```

**Returns**

| Name     | Type      | Description         |
| -------- | --------- | ------------------- |
| `<none>` | `uint256` | The current A value |

#### **getTokens**

Returns the array of token addresses in the pool.

```solidity
function getTokens() external view returns (address[] memory);

```

**Returns**

| Name     | Type        | Description                          |
| -------- | ----------- | ------------------------------------ |
| `<none>` | `address[]` | Array of token addresses in the pool |

#### **getRedeemProportionAmount**

Computes the amounts of underlying tokens when redeeming pool token.

```solidity
function getRedeemProportionAmount(uint256 _amount) external view returns (uint256[] memory, uint256);

```

**Parameters**

| Name      | Type      | Description                     |
| --------- | --------- | ------------------------------- |
| `_amount` | `uint256` | Amount of pool tokens to redeem |

**Returns**

| Name     | Type        | Description                                     |
| -------- | ----------- | ----------------------------------------------- |
| `<none>` | `uint256[]` | An array of the amounts of each token to redeem |
| `<none>` | `uint256`   | The amount of fee charged                       |

#### **getRedeemSingleAmount**

Computes the amount when redeeming pool token to one specific underlying token.

```solidity
function getRedeemSingleAmount(uint256 _amount, uint256 _i) external view returns (uint256, uint256);

```

**Parameters**

| Name      | Type      | Description                                |
| --------- | --------- | ------------------------------------------ |
| `_amount` | `uint256` | Amount of pool token to redeem             |
| `_i`      | `uint256` | Index of the underlying token to redeem to |

**Returns**

| Name     | Type      | Description                                         |
| -------- | --------- | --------------------------------------------------- |
| `<none>` | `uint256` | The amount of single token that will be redeemed    |
| `<none>` | `uint256` | The amount of pool token charged for redemption fee |

#### **getRedeemMultiAmount**

Compute the amount of pool token that needs to be redeemed.

```solidity
function getRedeemMultiAmount(uint256[] calldata _amounts) external view returns (uint256, uint256);

```

**Parameters**

| Name       | Type        | Description                |
| ---------- | ----------- | -------------------------- |
| `_amounts` | `uint256[]` | Unconverted token balances |

**Returns**

| Name     | Type      | Description                                         |
| -------- | --------- | --------------------------------------------------- |
| `<none>` | `uint256` | The amount of pool token that needs to be redeemed  |
| `<none>` | `uint256` | The amount of pool token charged for redemption fee |


---

# 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.tapio.finance/smart-contracts/selfpeggingasset/interactions.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.
