pool

pactsdk.pool.OperationType

The basic three operation types in a PACT liquidity pool, namely Add Liquidity (ADDLIQ), Remove Liquidity (REMLIQ) and making a swap (SWAP).

alias of Literal[‘SWAP’, ‘ADDLIQ’, ‘REMLIQ’]

pactsdk.pool.fetch_app_global_state(algod, app_id)

Fetches the global state of the of an application.

Parameters
  • algod (AlgodClient) – The algo client to query the app in.

  • app_id (int) – The application id to fetch the state of.

Return type

AppInternalState

Returns

The global state of the application.

pactsdk.pool.fetch_pool_by_id(algod, app_id)

Fetches the pool from the blockchain using the provided algod client.

Parameters
  • algod (AlgodClient) – The algo client to use.

  • app_id (int) – The application id to fetch.

Returns

The pool object for the application id passed in.

Return type

Pool

pactsdk.pool.fetch_pools_by_assets(algod, asset_a, asset_b, pact_api_url)

Returns the list of pools for the assets passed in.

There can be zero pools if there are no pools matching the assets, or multiple if there are multiple at different fees. The order of assets that you provide is irrelevant.

Parameters
  • algod (AlgodClient) – The algo client to use.

  • asset_a (Union[Asset, int]) – One of the assets in the pool (asset id or asset instance).

  • asset_b (Union[Asset, int]) – The other asset in the pool (asset id or asset instance).

  • pact_api_url (str) – The API url to use.

Return type

list[Pool]

Returns

A list of pools matching the provided assets.

pactsdk.pool.get_app_ids_from_assets(pact_api_url, primary_asset_index, secondary_asset_index)

Returns the application ids for any pools that match the primary and secondary asset.

This function finds any pools using the pact_api_url passed in that match the asset ids passed in.

Parameters
  • pact_api_url (str) – The API url to use.

  • primary_asset_index (int) – The asset id for the primary asset of the pool.

  • secondary_asset_index (int) – The asset id for the secondary asset of the pool.

Return type

list[int]

Returns

List of asset ids.

class pactsdk.pool.Pool(algod, app_id, primary_asset, secondary_asset, liquidity_asset, internal_state, fee_bps=30)

Bases: object

Pool represents a liquidity pool in the PACT AMM.

Typically, users don’t have to instantiate this class manually. Use pactsdk.client.PactClient.fetch_pool_by_id() or pactsdk.client.PactClient.fetch_pools_by_assets() instead.

The primary methods of the pool are to create the transaction groups to enable you to:

  • Add Liquidity,

  • Removing Liquidity,

  • Create a Swap on the Pool.

algod: algosdk.v2client.algod.AlgodClient

The Algorand client to use.

app_id: int

The application id for the pool.

primary_asset: pactsdk.asset.Asset

The asset of the liquidity pool with the lower index.

secondary_asset: pactsdk.asset.Asset

The asset of the liquidity pool with the higher index.

liquidity_asset: pactsdk.asset.Asset

The asset for the liquidity pool token (LP token) that is given when liquidity is added, and burned when liquidity is withdrawn.

internal_state: pactsdk.pool_state.AppInternalState

The global state on the blockchain for this pool.

fee_bps: int = 30

The fee in basis points for swaps trading on the pool.

pool_type: Literal['CONSTANT_PRODUCT', 'NFT_CONSTANT_PRODUCT', 'STABLESWAP']

Different pool types use different formulas for making swaps.

version: int

The version of the contract. May be 0 for some old pools which don’t expose the version in the global state.

__eq__(other_pool)

Return equal by comparing the pools app_id value.

Return type

bool

get_escrow_address()

Get the escrow address of the pool.

Return type

str

Returns

The address corresponding to that pools’s escrow account.

get_other_asset(asset)

Returns the “other” asset, i.e. primary if secondary is passed in and vice versa.

Parameters

asset (Asset) – The primary or secondary asset of the pool.

Raises

PactSdkError – If the asset passed in is not the primary or secondary asset.

Return type

Asset

Returns

The other asset, if the primary asset was passed in it will be the secondary asset and vice versa.

update_state()

Updates the internal and pool state properties by re-reading the global state in the blockchain.

Updating the pool state is recommended if there is a pause between the construction of the pool and the creation of the transactions on the pool. Calling this method ensures that the the pool state is not stale.

Return type

PoolState

Returns

The new pool state.

prepare_add_liquidity(primary_asset_amount, secondary_asset_amount, slippage_pct)

Creates a new LiquidityAddition instance.

Parameters

options – Options for adding the liquidity.

Return type

LiquidityAddition

Returns

A new LiquidityAddition object.

prepare_add_liquidity_tx_group(address, liquidity_addition)

Prepares a pactsdk.transaction_group.TransactionGroup for adding liquidity to the pool. See pactsdk.pool.Pool.buildAddLiquidityTxs() for details.

Parameters
  • address (str) – Account address that will deposit the primary and secondary assets and receive the LP token.

  • primary_asset_amount – The amount of primary asset to deposit.

  • secondary_asset_amount – The amount of secondary asset to deposit.

Return type

TransactionGroup

Returns

A transaction group that when executed will add liquidity to the pool.

build_add_liquidity_txs(address, liquidity_addition, suggested_params)

Builds the transactions to add liquidity for the primary asset and secondary asset of the pool.

In typical circumstances 3 transactions are generated:

  • deposit of asset A

  • deposit of asset B

  • “ADDLIQ” application call to add liquidity with the above deposits

The initial liquidity must satisfy the expression sqrt(a * b) - 1000 > 0.

Parameters
  • address (str) – Account address that will deposit the primary and secondary assets and receive the LP token.

  • primary_asset_amount – The amount of primary asset to deposit.

  • secondary_asset_amount – The amount of secondary asset to deposit.

  • suggested_params (SuggestedParams) – Algorand suggested parameters for transactions.

  • note – An optional note that can be added to the application ADDLIQ transaction.

Raises

AssertionError – If initial liquidity is too low.

Return type

list[Transaction]

Returns

List of transactions to add the liquidity.

build_raw_add_liquidity_txs(address, primary_asset_amount, secondary_asset_amount, minimum_minted_liquidity_tokens, suggested_params, fee, note=b'')
prepare_remove_liquidity_tx_group(address, amount)

Prepares the transaction group for removing liquidity from the pool.

Parameters
  • address (str) – Account address that will deposit the LP token and receive the primary and secondary assets.

  • amount (int) – The amount of the LP token to return to the pool.

Return type

TransactionGroup

Returns

Transaction group that when executed will remove liquidity from the pool.

build_remove_liquidity_txs(address, amount, suggested_params)

This creates two transactions in a group for the remove operation.

  • deposit of the liquidity asset

  • “REMLIQ” application call to remove the LP token from the account and receive the deposited assets in return.

Parameters
  • address (str) – Account address that will deposit the LP token and receive the primary and secondary assets.

  • amount (int) – The amount of the LP token to return to the pool.

  • suggested_params (SuggestedParams) – Algorand suggested parameters for transactions.

Return type

list[Transaction]

Returns

List of transactions to remove the liquidity.

prepare_swap(asset, amount, slippage_pct, swap_for_exact=False)

Creates a new swap instance for receiving the amount of asset within the slippage percent from the pool.

Parameters
  • asset (Asset) – The asset to swap.

  • amount (int) – Amount to swap or to receive. Look at swap_for_exact flag for details.

  • slippage_pct (float) – The maximum allowed slippage in percents e.g. 10 is 10%. The swap will fail if the slippage will be higher.

  • swap_for_exact – If false or not provided, the amount is the amount to swap (deposit in the contract). If true, the amount is the amount to receive from the swap.

Return type

Swap

Returns

A new swap object.

prepare_swap_tx_group(swap, address)

Prepares a transaction group that when executed will perform a swap on the pool.

Parameters
  • swap (Swap) – The swap for which to generate transactions.

  • address (str) – The address that is performing the swap.

Return type

TransactionGroup

Returns

Transaction group that when executed will perform a swap on the pool.

build_swap_txs(swap, address, suggested_params)

Builds two transactions:

  • deposit of the asset to swap

  • “SWAP’ application call that performs the swap to receive the other asset

Parameters
  • swap (Swap) – The swap for which to generate transactions.

  • address (str) – The address that is performing the swap.

  • suggested_params (SuggestedParams) – Algorand suggested parameters for transactions.

Return type

list[Transaction]

Returns

List of transactions to perform the swap.

is_asset_in_the_pool(asset)

Check if the asset is the primary or secondary asset of this pool.

Parameters

asset (Asset) – The asset to check is in the pool.

Return type

bool

Returns

True if the asset is in the pool or False otherwise.

parse_internal_state(state)

Read the new pool state from the global state of the application.

Parameters

state (AppInternalState) – Global state for the application.

Return type

PoolState

Returns

Parsed state.

prepare_zap(asset, amount, slippage_pct)

Creates a new zap instance for getting all required data for performing a zap.

Parameters
  • asset (Asset) – The asset to zap.

  • amount (int) – Amount used for the zap.

  • slippage_pct (float) – The maximum allowed slippage in percents e.g. 10 is 10%. The swap will fail if the slippage will be higher.

Return type

Zap

Returns

A new zap object.

prepare_zap_tx_group(zap, address)

Prepares a transaction group that when executed will perform a Zap on the pool.

Parameters
  • zap (Zap) – The zap for which to generate transactions.

  • address (str) – The address that is performing the Zap.

Return type

TransactionGroup

Returns

Transaction group that when executed will perform a Zap on the pool.

build_zap_txs(zap, address, suggested_params)

Builds the transactions to perform a Zap on the pool as per the options passed in. Zap allows to add liquidity to the pool by providing only one asset.

This function will generate swap Txs to get a proper amount of the second asset and then generate add liquidity Txs with both of those assets. See pactsdk.pool.Pool.buildSwapTxs() and pactsdk.pool.Pool.buildAddLiquidityTxs() for more details.

This feature is supposed to work with constant product pools only. Stableswaps can accept one asset to add liquidity by default.

Parameters
  • zap (Zap) – The zap for which to generate transactions.

  • address (str) – The address that is performing the Zap.

  • suggested_params (SuggestedParams) – Algorand suggested parameters for transactions.

Return type

list[Transaction]

Returns

List of transactions to perform the Zap.