Aqua Pool Integration Guide

Introduction

The Aqua Pool is a decentralized liquidity pool that enables efficient token swaps. Integrating with the Aqua Pool involves obtaining specific pool data, performing calculations to determine the expected output amount for a given input, and executing the swap transaction. This guide will walk you through the necessary steps and provide code examples to facilitate the integration process.

Step 1: Obtaining Aqua Pool Data

To interact with the Aqua Pool, you first need to obtain the pool's data. This can be achieved by either:

  • Calling the smart contract's functions directly.

  • Using the getAquaPoolData function.

Required Parameters

The following parameters are required to obtain the pool data:

  • sender: The address of the user initiating the transaction.

  • tokenIn: The address of the input token.

  • tokenOut: The address of the output token.

  • amountIn: The amount of the input token that the user wants to swap.

Data Freshness

If you already have the necessary pool data, you may proceed to Step 2. However, please note that using outdated data may lead to inaccurate results due to changes in the pool's state. It is recommended to always fetch the latest data before performing calculations.

Step 2: Calculating Output Amount

Once you have obtained the pool data, you can calculate the expected output amount by calling the getAmountOut function.

Function Usage

To use the getAmountOut function, provide the following arguments:

  • poolData: The data obtained in Step 1.

  • tokenIn: The address of the input token.

  • amountIn: The amount of the input token.

  • sender: The address of the user initiating the transaction.

  • shouldEstimateTweakPrice

The function will return:

  • amountOut: The expected amount of the output token that the user will receive.

Step 3: Performing the Swap

After estimating the output amount, you can proceed to perform the actual swap transaction by calling the swap function of the Aqua Pool contract.

Swap Function Parameters

To perform the swap, you need to call the swap function with the following parameters:

  • _data: Encoded data containing:

    • tokenIn: Address of the input token.

    • to: Address where the output tokens will be sent.

    • withdrawMode: A uint8 value indicating the withdrawal mode (explained below).

  • _sender: Address of the user initiating the swap.

  • _callback: Address of a callback contract (can be set to zero address if not used).

  • _callbackData: Additional data for the callback (can be empty if not used).

Withdraw Mode

The withdrawMode parameter determines how the output tokens are handled, particularly for tokens like ETH that can exist in native or wrapped form.

Withdraw modes:

  • 0 - Vault Internal Transfer: The output tokens remain within the vault system and are transferred internally.

  • 1 - Withdraw and Unwrap to Native ETH: The output tokens are withdrawn from the vault and unwrapped to native ETH. Use this mode if you want to receive ETH directly.

  • 2 - Withdraw and Wrap to wETH: The output tokens are withdrawn from the vault and wrapped into wETH (Wrapped Ether). Use this mode if you prefer wETH tokens.

Example Usage

Encoding Swap Data

Before calling the swap function, you need to encode the _data parameter using ABI encoding:

Examples and Code Snippets

Below are examples demonstrating how to implement the above steps in code. Please refer to the Appendix for detailed function definitions and additional code.

Example

Appendix

1. Get a aqua pool info

Params

token0

Returns the address of the first token in the pool.

token1

Returns the address of the second token in the pool.

getReserves

Returns the current reserves of the tokens in the pool.

priceScale

Returns the current price scale, which determines the price band around which liquidity is concentrated.

token0PrecisionMultiplie, token1PrecisionMultiplie

Multipliers for each pooled token's precision to get to the pool precision decimals.

which is agnostic to the pool, but usually is 18.

For example, TBTC has 18 decimals, so the multiplier should be 10 ** (18 - 18) = 1. WBTC has 8, so the multiplier should be 10 ** (18 - 8) => 10 ** 10.

The value is only for crypto pools, and has no effects on non-crypto pools.

invariantLast

Invariant of the pool as of immediately after the most recent liquidity event.

a, futureA, gamma

Ramp params

totalSupply

virtualPrice

virtual price.

PoolType

Pool type 3 for aqua pools.

Code

2. Quote amount out

typescript

solidity

3. Math functions details

3.1 computedD

Vyper

Math library

3.2 getY

Typescript

Vyper

Math library

3.3 estimateTweakPriceAqua

typescript

3.4 _tweakPrice

solidity

3.5 Math library

4. Swap function details

Last updated