### Setup Algorand Testbed Environment Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Navigates into the testbed directory, installs its dependencies, and starts the Docker Compose services. ```bash cd algorand-testbed poetry install docker compose up -d cd .. ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Installs the necessary dependencies for development using Poetry. ```bash poetry install ``` -------------------------------- ### Build or Get Pool Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/build_pool.md Deploys a new pool if a pool with the given parameters does not already exist. Ensure you have the necessary Algorand client setup and your mnemonic phrase for signing. ```python """This example deploys a new pool if pool with the given params doesn't exist yet.""" import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk private_key = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(private_key) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") factory = pact.get_constant_product_pool_factory() pool_params = pactsdk.PoolBuildParams( primary_asset_id=0, secondary_asset_id=14111329, fee_bps=100, ) pool, created = factory.build_or_get( sender=address, pool_build_params=pool_params, signer=lambda tx_group: tx_group.sign(private_key), ) print("New pool created." if created else "Pool with specified params already exists.") print(pool) ``` -------------------------------- ### Install Local Package Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Installs the locally built pact-py-sdk package using pip. ```bash pip install dist/pactsdk-.whl ``` -------------------------------- ### Deploy a New Escrow Contract Source: https://github.com/pactfi/pact-py-sdk/blob/main/pactsdk/farming/README.md Guides through the process of deploying a new escrow contract for a user. This involves preparing, signing, and sending transactions, then fetching the newly created escrow by its application ID. ```python farm.refresh_suggested_params() deploy_txs = farm.prepare_deploy_escrow_txs(sender=user_address) sign_send_and_wait(pactsdk.TransactionGroup(deploy_txs), user_private_key) txinfo = algod.pending_transaction_info(deploy_txs[-2].get_txid()) escrow_id = txinfo["application-index"] escrow = farm.fetch_escrow_by_id(escrow_id) escrow.refresh_suggested_params() ``` -------------------------------- ### Folks Lending Pool Basic Actions Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/folks_lending_pool.md This example performs basic actions on a lending pool, including fetching pools, adding liquidity, swapping assets, and removing liquidity. It requires setting up an Algod client and a Pact client, and assumes you have a mnemonic for private key generation. ```python """This example performs a basic actions on a lending pool.""" import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk FOLKS_POOL_A = 147169673 # ALGO FOLKS_POOL_B = 147170678 # USDC folks_lending_pool_ids = sorted([FOLKS_POOL_A, FOLKS_POOL_B]) pk = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(pk) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") # Folks pools. print("Fetching folks lending pools...") primary_folks_pool = pact.fetch_folks_lending_pool(folks_lending_pool_ids[0]) secondary_folks_pool = pact.fetch_folks_lending_pool(folks_lending_pool_ids[1]) # Pact pool. print("Fetching or creating pact pool...") factory = pact.get_constant_product_pool_factory() pool_build_params = pactsdk.PoolBuildParams( primary_asset_id=primary_folks_pool.f_asset.index, secondary_asset_id=secondary_folks_pool.f_asset.index, fee_bps=2, ) pact_pool, created = factory.build_or_get( sender=address, pool_build_params=pool_build_params, signer=lambda tx_group: tx_group.sign(pk), ) # Make an adapter. lending_pool_adapter = pact.get_folks_lending_pool_adapter( primary_lending_pool=primary_folks_pool, secondary_lending_pool=secondary_folks_pool, pact_pool=pact_pool, ) if created: # Adapter opt-in to all the assets. print("Opting in adapter to assets...") asset_ids = [ primary_folks_pool.original_asset.index, secondary_folks_pool.original_asset.index, primary_folks_pool.f_asset.index, secondary_folks_pool.f_asset.index, pact_pool.liquidity_asset.index, ] tx_group = lending_pool_adapter.prepare_opt_in_to_asset_tx_group(address, asset_ids) algod.send_transactions(tx_group.sign(pk)) print(tx_group.group_id) print() # Add liquidity. print("Adding liquidity...") liquidity_addition = lending_pool_adapter.prepare_add_liquidity(100_000, 100_000, 0.5) tx_group = lending_pool_adapter.prepare_add_liquidity_tx_group( address, liquidity_addition ) algod.send_transactions(tx_group.sign(pk)) print(tx_group.group_id) print() # Swap. print("Swapping...") swap = lending_pool_adapter.prepare_swap( primary_folks_pool.original_asset, amount=100_000, slippage_pct=100 ) tx_group = lending_pool_adapter.prepare_swap_tx_group(swap, address) algod.send_transactions(tx_group.sign(pk)) print(tx_group.group_id) print() # Remove liquidity. print("Removing liquidity...") tx_group = lending_pool_adapter.prepare_remove_liquidity_tx_group(address, 20_000) algod.send_transactions(tx_group.sign(pk)) print(tx_group.group_id) ``` -------------------------------- ### List Constant Product Pools Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/list_pools.md Lists constant product pools created by the factory. This example requires the `algosdk` and `pactsdk` libraries. Ensure you replace placeholders for mnemonic, token, and URL with your actual values. This method will not list pools created before the introduction of the pool factory. ```python """This examples lists constant product pools created by the factory. It will not list old pools created before introducing pool factory to the Pact architecture. Each pool type require using a dedicated factory.""" import pprint import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk private_key = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(private_key) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") factory = pact.get_constant_product_pool_factory() pool_params = factory.list_pools() print("pools:") pprint.pprint(pool_params) # To fully fetch the pool of choice... pool = factory.fetch_pool(pool_params[0]) print("Selected pool:") pprint.pprint(pool) ``` -------------------------------- ### prepare_zap Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/pool.md Creates a new zap instance for getting all required data for performing a zap. ```APIDOC ## prepare_zap ### Description Creates a new zap instance for getting all required data for performing a zap. ### Method prepare_zap ### Parameters #### Path Parameters - **asset** (Asset) - Required - The asset to zap. - **amount** (int) - Required - Amount used for the zap. - **slippage_pct** (float) - Required - The maximum allowed slippage in percents e.g. 10 is 10%. The swap will fail if the slippage will be higher. ### Returns A new zap object. ``` -------------------------------- ### Add Liquidity to a Pool Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/add_liquidity.md This example shows how to add liquidity to a specified asset pool. It requires opting in to the liquidity asset before adding the desired amounts of primary and secondary assets, with slippage protection. Ensure you have the necessary algod client, Pact client, and asset information set up. ```python """This example adds liquidity to the pool.""" import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk private_key = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(private_key) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") algo = pact.fetch_asset(0) usdc = pact.fetch_asset(31566704) pool = pact.fetch_pools_by_assets(algo, usdc)[0] # Opt-in for liquidity token. opt_in_txn = pool.liquidity_asset.prepare_opt_in_tx(address) sent_optin_txid = algod.send_transaction(opt_in_txn.sign(private_key)) print(f"OptIn transaction {sent_optin_txid}") # Add liquidity liquidity_addition = pool.prepare_add_liquidity( primary_asset_amount=1_000_000, secondary_asset_amount=500_000, slippage_pct=0.5, ) add_liq_tx_group = liquidity_addition.prepare_tx_group(address=address) signed_tx_group = add_liq_tx_group.sign(private_key) algod.send_transactions(signed_tx_group) print(f"Add liquidity transaction group {add_liq_tx_group.group_id}") ``` -------------------------------- ### Validate Pact-Py-SDK Installation Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Validates the installation by printing the installed pact-py-sdk version. ```python import pactsdk; print(pactsdk.__version__) ``` -------------------------------- ### build_zap_txs Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Builds the transactions required to perform a Zap operation, which involves swapping one asset to get the correct amount of the second asset and then adding liquidity with both. This is intended for constant product pools. ```APIDOC ## build_zap_txs ### Description 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. ### Method build_zap_txs(zap: Zap, address: str, suggested_params: SuggestedParams) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **zap** (Zap) - Required - The zap for which to generate transactions. * **address** (str) - Required - The address that is performing the Zap. * **suggested_params** (SuggestedParams) - Required - Algorand suggested parameters for transactions. ### Returns * **list[Transaction]** - List of transactions to perform the Zap. ``` -------------------------------- ### Initialize Pact Client Source: https://github.com/pactfi/pact-py-sdk/blob/main/pactsdk/farming/README.md Sets up the Algod client and Pact client for interacting with the Pact SDK. Replace placeholders with your actual token and URL. ```python from algosdk.v2client.algod import AlgodClient import pactsdk algod = AlgodClient('', '') pact = pactsdk.PactClient(algod) ``` -------------------------------- ### Initialize PactClient and Fetch Assets/Pools Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/client.md Demonstrates the typical usage of initializing the PactClient with an AlgodClient and then fetching asset and pool information. ```python import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk algod = AlgodClient("", "") pact = pactsdk.PactClient(algod) algo = pact.fetch_asset(0) other_coin = pact.fetch_asset(12345678) pools = pact.fetch_pools_by_assets(algo, other_coin) ``` -------------------------------- ### Get Holding from Account Info Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Extracts the asset holding amount from raw account information data. ```python pactsdk.asset.Asset.get_holding_from_account_info(account_info: dict) ``` -------------------------------- ### Get Asset Holding Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Retrieves the amount of a specific asset an account is currently holding. Returns None if the account has not opted into the asset. ```python pactsdk.asset.Asset.get_holding(address: str) ``` -------------------------------- ### Fetch Farm and Escrow Contracts Source: https://github.com/pactfi/pact-py-sdk/blob/main/pactsdk/farming/README.md Demonstrates how to retrieve farm and escrow contract instances using their IDs or addresses. If the escrow ID is unknown, it can be fetched by address. If the farm ID is unknown, the escrow can be used to access its associated farm. ```python farm = pact.farming.fetch_farm_by_id(farm_id) escrow = farm.fetch_escrow_by_id(escrow_id) # If you don't know the escrow id you can... escrow = farm.fetch_escrow_by_address(user_address) # If you don't know the farm id you can... escrow = pact.farming.fetch_escrow_by_id(escrow_id) farm = escrow.farm ``` -------------------------------- ### Build Transactions Manually Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/index.md Utilize `build_..._txs` methods to get a list of transactions that can be extended with custom transactions before creating a `TransactionGroup`. ```python # Example using build_swap_txs (assuming pool and address are defined) # swap_txs = pool.build_swap_txs( # address=address, # asset=algo, # amount=200_000, # slippage_pct=2, # ) # # You can add custom transactions here # # custom_tx = ... # # swap_txs.append(custom_tx) # # tx_group = algosdk.transaction.TransactionGroup(swap_txs) # # signed_tx_group = tx_group.sign(private_key) # # algod.send_transactions(signed_tx_group) ``` -------------------------------- ### Make a Swap Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Execute a swap transaction for a specified asset and amount, with a slippage tolerance. The `prepare_swap` method allows inspection of the swap's effects before submission. ```python swap = pool.prepare_swap( asset=algo, amount=200_000, slippage_pct=2, ) # You can inspect swap effect before submitting the transaction. print(swap.effect) # SwapEffect( # amount_deposited=200000, # amount_received=146529, # minimum_amount_received=143598, # fee=441, # price=0.73485, # primary_asset_price_after_swap=0.6081680080300244, # secondary_asset_price_after_swap=1.6442824791774173, # primary_asset_price_change_pct=-31.549580645715963, # secondary_asset_price_change_pct=46.091142966447585, # ) # Let's submit the swap. swap_tx_group = swap.prepare_tx_group(address) signed_tx_group = swap_tx_group.sign(private_key) algod.send_transactions(signed_tx_group) ``` -------------------------------- ### Build Transaction Group Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Use `build_..._txs` methods to get a list of transactions that can be extended with custom transactions before creating a `TransactionGroup`. ```python # Example using build_swap_txs (not directly shown in source, but implied by description) # txs = pool.build_swap_txs(address=address, asset=algo, amount=200_000) # # Add custom transactions here if needed # tx_group = algosdk.transaction.TransactionGroup(txs) # signed_tx_group = tx_group.sign(private_key) # algod.send_transactions(signed_tx_group) ``` -------------------------------- ### Get Pool State Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Retrieve the current state of a liquidity pool, including total liquidity, asset balances, and prices. State updates are necessary after pool operations. ```python print(pool.state) # PoolState( # total_liquidity=900000, # total_primary=956659, # total_secondary=849972, # primary_asset_price=0.8884795940873393, # secondary_asset_price=1.1255182523659604, # ) ``` ```python pool.update_state() pool.state # Now holds fresh values. ``` -------------------------------- ### Build Pact-Py-SDK Package Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Builds the pact-py-sdk package using Poetry. ```bash poetry build ``` -------------------------------- ### Get Last Timestamp for Folks Pool Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Retrieves the last timestamp used by the Folks lending pool contract. This is important for accurate conversion calculations, especially in testing scenarios. ```python from pactsdk.folks_lending_pool import FolksLendingPool # Assuming 'lending_pool' is a FolksLendingPool object last_timestamp = lending_pool.get_last_timestamp() print(f"Last timestamp for the pool: {last_timestamp}") ``` -------------------------------- ### prepare_swap() Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/pool.md Creates a new swap instance for receiving a specified amount of an asset within a given slippage tolerance from the pool. Allows for swapping a specific amount or receiving a specific amount. ```APIDOC ## prepare_swap(asset: Asset, amount: int, slippage_pct: float, swap_for_exact: bool = False) ### Description 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** (bool, optional) - 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. ### Returns A new swap object. ``` -------------------------------- ### Get Staked Amount Source: https://github.com/pactfi/pact-py-sdk/blob/main/pactsdk/farming/README.md Retrieves the total amount of tokens currently staked by the user in the escrow contract. This can be done by fetching the user's state or directly querying the asset holding. ```python user_state = escrow.fetch_user_state() staked_amount = user_state.staked ``` ```python staked_amount = escrow.farm.staked_asset.get_holding(escrow.address) # or if you want to reuse account_info and save some requests to algod. account_info = algod.account_info(escrow.address) ... staked_amount = escrow.farm.staked_asset.get_holding_from_account_info(account_info) ``` -------------------------------- ### Create Pact Client Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Instantiate a PactClient using an AlgodClient. The client defaults to mainnet; specify 'testnet' for testnet usage. ```python from algosdk.v2client.algod import AlgodClient import pactsdk algod = AlgodClient(token, url) pact = pactsdk.PactClient(algod) ``` ```python pact = pactsdk.PactClient(algod, network="testnet") ``` -------------------------------- ### Perform a Swap Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/swap.md This snippet demonstrates how to perform a swap between two assets on a liquidity pool. It includes opting into the necessary asset and preparing, signing, and sending the swap transaction group. ```python """This example performs a swap on a pool.""" import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk private_key = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(private_key) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") algo = pact.fetch_asset(0) usdc = pact.fetch_asset(31566704) pool = pact.fetch_pools_by_assets(algo, usdc)[0] # Opt-in for usdc. opt_in_txn = usdc.prepare_opt_in_tx(address) sent_optin_txid = algod.send_transaction(opt_in_txn.sign(private_key)) print(f"Opt-in transaction {sent_optin_txid}") # Do a swap. swap = pool.prepare_swap( asset=algo, amount=100_000, slippage_pct=2, ) swap_tx_group = swap.prepare_tx_group(address) signed_group = swap_tx_group.sign(private_key) algod.send_transactions(signed_group) print(f"Swap transaction group {swap_tx_group.group_id}") ``` -------------------------------- ### FolksLendingPoolAdapter.prepare_swap Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Prepares the data structure for performing a swap in the lending pool. ```APIDOC ## prepare_swap ### Description Prepares the data structure for performing a swap operation within the lending pool, handling asset conversions and slippage. ### Method N/A (Method of FolksLendingPoolAdapter) ### Parameters * **asset** ([Asset](../../pactsdk/asset.md#pactsdk.asset.Asset)) - The asset to be swapped. * **amount** (int) - The amount of the asset to swap. * **slippage_pct** (float) - The maximum slippage percentage allowed. * **swap_for_exact** (bool, optional) - If True, the `amount` parameter refers to the exact amount of the received asset. Defaults to False. ### Returns * [LendingSwap](../../pactsdk/folks_lending_pool.md#pactsdk.folks_lending_pool.LendingSwap) - An object containing the details for the swap operation. ``` -------------------------------- ### Zap.prepare_add_liq() Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/index.md Prepares to add liquidity through a zap operation. ```APIDOC ## Zap.prepare_add_liq() ### Description Prepares the necessary components for adding liquidity as part of a zap operation. This method is provided by the Zap class in the Pact SDK. ### Method Not specified (SDK method) ### Endpoint Not applicable (SDK method) ### Parameters Not specified in source ### Request Example Not specified in source ### Response Not specified in source ``` -------------------------------- ### Constant Product Calculator Methods Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/modules.md This section covers the functionalities related to calculating swap amounts and liquidity for constant product pools. ```APIDOC ## Constant Product Calculator ### Description Provides utilities for calculating swap amounts and liquidity for constant product pools. ### Methods - `get_swap_gross_amount_received(params: ConstantProductParams, asset_id: str, amount: int)` - `get_swap_amount_deposited(params: ConstantProductParams, asset_id: str, amount: int)` - `get_constant_product_minted_liquidity_tokens(params: ConstantProductParams, asset_id: str, amount: int)` - `ConstantProductCalculator.get_price(params: ConstantProductParams, asset_id: str)` - `ConstantProductCalculator.get_swap_gross_amount_received(params: ConstantProductParams, asset_id: str, amount: int)` - `ConstantProductCalculator.get_swap_amount_deposited(params: ConstantProductParams, asset_id: str, amount: int)` - `ConstantProductCalculator.get_minted_liquidity_tokens(params: ConstantProductParams, asset_id: str, amount: int)` ``` -------------------------------- ### Asset.prepare_opt_in_tx Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/asset.md Prepares a transaction to opt an account into an asset, allowing it to receive or manage the asset. ```APIDOC ## Asset.prepare_opt_in_tx ### Description Creates a transaction that will allow the account to “opt in” to the asset. This is necessary before receiving an asset or managing liquidity tokens. ### Method Asset.prepare_opt_in_tx(address: str) -> AssetTransferTxn ### Parameters #### Parameters - **address** (str) - Account to opt in to this asset. ### Returns - **AssetTransferTxn** - A ready-to-send transaction to opt-in into the ASA. ``` -------------------------------- ### FolksLendingPoolAdapter.prepare_add_liquidity Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Prepares the data structure for adding liquidity to the lending pool. ```APIDOC ## prepare_add_liquidity ### Description Prepares the data structure for adding liquidity to the lending pool, including asset conversion and slippage calculation. ### Method N/A (Method of FolksLendingPoolAdapter) ### Parameters * **primary_asset_amount** (int) - The amount of the primary asset to deposit. * **secondary_asset_amount** (int) - The amount of the secondary asset to deposit. * **slippage_pct** (float) - The maximum slippage percentage allowed. ### Returns * [LendingLiquidityAddition](../../pactsdk/folks_lending_pool.md#pactsdk.folks_lending_pool.LendingLiquidityAddition) - An object containing the details for the liquidity addition operation. ``` -------------------------------- ### build_opt_in_tx Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Creates the actual transaction for an account to opt-in to holding an asset, using suggested Algorand parameters. ```APIDOC ## build_opt_in_tx(address: str, suggested_params: SuggestedParams) ### Description Creates the actual transaction for the account to opt-in to holding the asset. ### Parameters #### Path Parameters * **address** (str) - Required - Address of the account to opt in to the asset. * **suggested_params** (SuggestedParams) - Required - Algorand suggested parameters for transactions. ### Returns A transaction to opt-in into asset. ``` -------------------------------- ### PactClient Constructor Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/client.md Initializes the PactClient with an Algorand client and network configuration. It can also accept additional keyword arguments to overwrite default configuration parameters. ```APIDOC ## PactClient Constructor ### Description Initializes the PactClient with an Algorand client and network configuration. It can also accept additional keyword arguments to overwrite default configuration parameters. ### Parameters * **algod** (AlgodClient) - Algorand client to work with. * **network** (Literal['mainnet', 'testnet', 'dev']) - The Algorand network to use the client with. Defaults to 'mainnet'. * **kwargs** - Use it to overwrite configuration parameters. ``` -------------------------------- ### Prepare Opt-In Transaction Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Creates a transaction to allow an account to opt-in to an asset. This is necessary before receiving an asset from a swap or managing liquidity tokens. ```python pactsdk.asset.Asset.prepare_opt_in_tx(address: str) ``` -------------------------------- ### Asset.build_opt_in_tx Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/asset.md Builds the actual opt-in transaction for an account to hold an asset, using suggested Algorand parameters. ```APIDOC ## Asset.build_opt_in_tx ### Description Creates the actual transaction for the account to opt-in to holding the asset. ### Method Asset.build_opt_in_tx(address: str, suggested_params: SuggestedParams) -> AssetTransferTxn ### Parameters #### Parameters - **address** (str) - Address of the account to opt in to the asset. - **suggested_params** (SuggestedParams) - Algorand suggested parameters for transactions. ### Returns - **AssetTransferTxn** - A transaction to opt-in into asset. ``` -------------------------------- ### Build Opt-In Transaction Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Constructs the actual transaction for an account to opt-in to holding an asset, using suggested Algorand parameters. ```python pactsdk.asset.Asset.build_opt_in_tx(address: str, suggested_params: SuggestedParams) ``` -------------------------------- ### build_swap_txs Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/pool.md Builds two transactions: deposit of the asset to swap and a 'SWAP' application call that performs the swap to receive the other asset. ```APIDOC ## build_swap_txs ### Description Builds two transactions: deposit of the asset to swap and a 'SWAP' application call that performs the swap to receive the other asset. ### Method build_swap_txs ### Parameters #### Path Parameters - **swap** (Swap) - Required - The swap for which to generate transactions. - **address** (str) - Required - The address that is performing the swap. - **suggested_params** (SuggestedParams) - Required - Algorand suggested parameters for transactions. ### Returns List of transactions to perform the swap. ``` -------------------------------- ### FolksLendingPoolAdapter.build_add_liquidity_txs Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Builds the list of transactions for adding liquidity to the lending pool. ```APIDOC ## build_add_liquidity_txs ### Description Builds the list of transactions necessary for adding liquidity to the lending pool, given the liquidity addition details and suggested transaction parameters. ### Method N/A (Method of FolksLendingPoolAdapter) ### Parameters * **address** (str) - The address initiating the transaction. * **liquidity_addition** ([LendingLiquidityAddition](../../pactsdk/folks_lending_pool.md#pactsdk.folks_lending_pool.LendingLiquidityAddition)) - The liquidity addition details. * **suggested_params** (SuggestedParams) - The suggested transaction parameters from the algod client. ### Returns * list[Transaction] - A list of transaction objects for the add liquidity operation. ``` -------------------------------- ### PoolFactory.build Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/factories.md Deploys a new pool to the network with the specified parameters. ```APIDOC ## PoolFactory.build ### Description Deploys a new pool to the network with the specified parameters. ### Method POST (implied) ### Endpoint (Not specified, inferred from class context) ### Parameters #### Path Parameters (None specified) #### Query Parameters (None specified) #### Request Body - **sender** (str) - Required - The address that is going to send the transactions. - **pool_build_params** (PoolBuildParams) - Required - Parameters of the pool that is going to be created. - **signer** (Callable[[TransactionGroup], list[SignedTransaction]]) - Required - A callback that allows signing the transaction. ### Returns - **Pool** - The created pool instance. ``` -------------------------------- ### Zap.prepare_add_liq Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/zap.md Prepares the liquidity addition part of the zap transaction. ```APIDOC ## Zap.prepare_add_liq ### Description Prepares the liquidity addition transaction for the zap. ``` -------------------------------- ### prepare_opt_in_tx Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Creates a transaction to allow an account to opt-in to an asset. This is necessary before an account can receive an asset. ```APIDOC ## prepare_opt_in_tx(address: str) ### Description This creates a transaction that will allow the account to “opt in” to the asset. In Algorand, every account has to explicitly opt-in for an asset before receiving it. Needed if you want to receive an asset from a swap or to manage liquidity tokens. ### Parameters #### Path Parameters * **address** (str) - Required - Account to opt in to this asset. ### Returns A ready to send transaction to opt-in into the ASA. ``` -------------------------------- ### PactClient Methods Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/modules.md This section details the methods available on the PactClient for interacting with the Pact protocol, including fetching assets, listing and fetching pools, and accessing specific pool types. ```APIDOC ## PactClient Methods ### Description Provides methods for interacting with the Pact protocol, such as fetching assets and pool information. ### Methods - `PactClient.algod` - `PactClient.config` - `PactClient.farming` - `PactClient.fetch_asset(asset_id: str)` - `PactClient.list_pools()` - `PactClient.fetch_pools_by_assets(asset_ids: list[str])` - `PactClient.fetch_pool_by_id(pool_id: str)` - `PactClient.fetch_folks_lending_pool(pool_id: str)` - `PactClient.get_folks_lending_pool_adapter(pool_id: str)` - `PactClient.get_constant_product_pool_factory()` - `PactClient.get_nft_constant_product_pool_factory()` ``` -------------------------------- ### PactClient Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md The main entry point for interacting with the Pact SDK, providing methods for fetching assets and pools. ```APIDOC ## class pactsdk.client.PactClient ### Description An entry point for interacting with the SDK. Exposes convenience methods for fetching assets and pools. ### Parameters * **algod** (AlgodClient) - Required - Algorand client to work with. * **network** (Literal['mainnet', 'testnet', 'dev']) - Optional - The Algorand network to use the client with. The configuration values depend on the chosen network. Defaults to 'mainnet'. * **kwargs** - Use it to overwrite configuration parameters. ### Attributes * **algod** (AlgodClient) - Algorand client to work with. * **config** ([Config](../../pactsdk/config.md#pactsdk.config.Config)) - Client configuration with global contracts ids etc. * **farming** (PactFarmingClient) - ``` -------------------------------- ### FolksLendingPoolAdapter Methods Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/folks_lending_pool.md This section details the methods available on the FolksLendingPoolAdapter class for managing liquidity and performing swaps with Folks Finance lending pools. ```APIDOC ## FolksLendingPoolAdapter ### Description The FolksLendingPoolAdapter class acts as an intermediary, allowing users to manage liquidity and execute swaps by leveraging both Pact pools and Folks Finance lending pools. It aims to provide a familiar interface similar to the standard `pactsdk.pool.Pool`. ### Methods #### `original_asset_to_f_asset(original_asset: Asset) -> Asset` Converts an original asset to its corresponding Folks Finance asset representation. #### `f_asset_to_original_asset(f_asset: Asset) -> Asset` Converts a Folks Finance asset back to its original asset representation. #### `prepare_add_liquidity(primary_asset_amount: int, secondary_asset_amount: int, slippage_pct: float) -> LendingLiquidityAddition` Prepares the necessary data structure for adding liquidity to the lending pools. #### `prepare_add_liquidity_tx_group(address: str, liquidity_addition: LendingLiquidityAddition) -> TransactionGroup` Builds a transaction group for adding liquidity to the lending pools. #### `build_add_liquidity_txs(address: str, liquidity_addition: LendingLiquidityAddition, suggested_params: SuggestedParams) -> list[Transaction]` Constructs the actual transactions required for adding liquidity. #### `prepare_remove_liquidity_tx_group(address: str, amount: int) -> TransactionGroup` Prepares a transaction group for removing liquidity from the lending pools. #### `build_remove_liquidity_txs(address: str, amount: int, suggested_params: SuggestedParams) -> list[Transaction]` Constructs the transactions needed to remove liquidity. #### `prepare_swap(asset: Asset, amount: int, slippage_pct: float, swap_for_exact=False) -> LendingSwap` Prepares the data structure for executing a swap within the lending pools. #### `prepare_swap_tx_group(swap: LendingSwap, address: str) -> TransactionGroup` Builds a transaction group for performing a swap. #### `build_swap_txs(swap: LendingSwap, address: str, suggested_params: SuggestedParams) -> list[Transaction]` Constructs the transactions required for a swap operation. #### `prepare_opt_in_to_asset_tx_group(address: str, asset_ids: list[int]) -> TransactionGroup` Prepares a transaction group for opting into specific assets. #### `build_opt_in_to_asset_tx_group(address: str, asset_ids: list[int], suggested_params: SuggestedParams)` Constructs the transactions for opting into specified assets. ``` -------------------------------- ### Opt-in for Assets Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Prepare and sign transactions to opt-in for specific assets before interacting with them. Algo does not require opt-in. This is necessary for managing liquidity. ```python import algosdk private_key = algosdk.mnemonic.to_private_key('') address = algosdk.account.address_from_private_key(private_key) def opt_in(asset): is_opted_in = asset.is_opted_in(address) if not is_opted_in: opt_in_tx = asset.prepare_opt_in_tx(address) signed_tx = opt_in_tx.sign(private_key) algod.send_transaction(signed_tx) opt_in(pool.primary_asset) opt_in(pool.secondary_asset) opt_in(pool.liquidity_asset) # Needed if you want to manage the liquidity. ``` -------------------------------- ### prepare_opt_in_to_asset_tx_group Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Prepares a transaction group for opting into specified assets. This is a preparatory step before building the actual transaction group. ```APIDOC ## prepare_opt_in_to_asset_tx_group ### Description Prepares a transaction group for opting into specified assets. ### Parameters - **address** (str) - Required - The user's account address. - **asset_ids** (list[int]) - Required - A list of asset IDs to opt into. ### Returns - TransactionGroup - A transaction group object. ``` -------------------------------- ### PoolFactory.build_or_get Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/factories.md Deploys a new pool if it does not exist, otherwise returns the existing pool. ```APIDOC ## PoolFactory.build_or_get ### Description Deploys a new pool to the network if the pool with the specified params does not exist yet. Otherwise, it returns the existing pool. ### Method POST (implied) ### Endpoint (Not specified, inferred from class context) ### Parameters #### Path Parameters (None specified) #### Query Parameters (None specified) #### Request Body - **sender** (str) - Required - The address that is going to send the transactions. - **pool_build_params** (PoolBuildParams) - Required - Parameters of the pool that is going to be created. - **signer** (Callable[[TransactionGroup], list[SignedTransaction]]) - Required - A callback that allows signing the transaction. ### Returns - **tuple[Pool, bool]** - The two items tuple. The first item is the created or existing pool. The second item is True if a new pool is created or False if an existing pool is returned. ``` -------------------------------- ### build_raw_add_liquidity_txs Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Builds raw transactions for adding liquidity, specifying amounts for primary and secondary assets, minimum liquidity tokens to be minted, and suggested transaction parameters. ```APIDOC ## build_raw_add_liquidity_txs ### Description Builds raw transactions for adding liquidity. ### Method Not applicable (SDK method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **address** (str) - Required - Account address that will deposit the primary and secondary assets and receive the LP token. * **primary_asset_amount** (int) - Required - The amount of primary asset to deposit. * **secondary_asset_amount** (int) - Required - The amount of secondary asset to deposit. * **minimum_minted_liquidity_tokens** (int) - Required - The minimum amount of LP tokens to mint. * **suggested_params** (SuggestedParams) - Required - Algorand suggested parameters for transactions. * **fee** (int) - Required - The transaction fee. * **note** (str) - Optional - An optional note for the transaction. ``` -------------------------------- ### prepare_add_liquidity() Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/pool.md Creates a new LiquidityAddition instance. This object can be used with other methods to prepare transactions for adding liquidity to the pool. ```APIDOC ## prepare_add_liquidity(primary_asset_amount: int, secondary_asset_amount: int, slippage_pct: float) ### Description Creates a new LiquidityAddition instance. ### Parameters * **primary_asset_amount** (int) - The amount of primary asset to deposit. * **secondary_asset_amount** (int) - The amount of secondary asset to deposit. * **slippage_pct** (float) - The maximum allowed slippage in percents e.g. 10 is 10%. The swap will fail if the slippage will be higher. ### Returns A new LiquidityAddition object. ``` -------------------------------- ### build_swap_txs Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/pactsdk.md Builds a list of transactions for a lending swap operation. This function takes a LendingSwap object, the user's address, and suggested transaction parameters to construct the necessary transactions. ```APIDOC ## build_swap_txs ### Description Builds a list of transactions for a lending swap operation. ### Parameters - **swap** (LendingSwap) - Required - The lending swap details. - **address** (str) - Required - The user's account address. - **suggested_params** (SuggestedParams) - Required - Suggested parameters for the transactions. ### Returns - list[Transaction] - A list of transaction objects. ``` -------------------------------- ### Run Tests with Poetry Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Executes the test suite using Poetry. ```bash poetry run pytest ``` -------------------------------- ### Clone Algorand Testbed Source: https://github.com/pactfi/pact-py-sdk/blob/main/README.md Clones the Pact contracts V1 repository, which is required for development. ```bash git clone git@github.com:pactfi/algorand-testbed.git ``` -------------------------------- ### Pool Module Methods Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/modules.md This section lists the core methods available on the Pool object for managing liquidity and executing swaps. These methods allow users to prepare and build transactions for various pool operations. ```APIDOC ## Pool Module Methods This section lists the core methods available on the Pool object for managing liquidity and executing swaps. These methods allow users to prepare and build transactions for various pool operations. ### `prepare_add_liquidity_tx_group()` Prepares a transaction group for adding liquidity to a pool. ### `build_add_liquidity_txs()` Builds transactions for adding liquidity to a pool. ### `build_raw_add_liquidity_txs()` Builds raw transactions for adding liquidity to a pool. ### `prepare_remove_liquidity_tx_group()` Prepares a transaction group for removing liquidity from a pool. ### `build_remove_liquidity_txs()` Builds transactions for removing liquidity from a pool. ### `prepare_swap()` Prepares a swap operation. ### `prepare_swap_tx_group()` Prepares a transaction group for a swap operation. ### `build_swap_txs()` Builds transactions for a swap operation. ### `is_asset_in_the_pool()` Checks if a given asset is present in the pool. ### `parse_internal_state()` Parses the internal state of the pool. ### `prepare_zap()` Prepares a ZAP operation. ### `prepare_zap_tx_group()` Prepares a transaction group for a ZAP operation. ### `build_zap_txs()` Builds transactions for a ZAP operation. ``` -------------------------------- ### fetch_folks_lending_pool Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/folks_lending_pool.md Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. ```APIDOC ## fetch_folks_lending_pool ### Description Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. ### Method Not specified (likely a client method) ### Endpoint Not applicable (SDK function) ### Parameters * **algod** (AlgodClient) - The Algorand client. * **app_id** (int) - The application ID of the Folks lending pool. ### Response #### Success Response * **FolksLendingPool** - An object representing the Folks lending pool. ``` -------------------------------- ### ConstantProductCalculator Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/pactsdk/constant_product_calculator.md Implements the mathematical logic for constant product liquidity pools. ```APIDOC ## class pactsdk.constant_product_calculator.ConstantProductCalculator ### Description An implementation of the math behind constant product pools. ### Methods #### get_price(liq_a: float, liq_b: float) → float Calculates the current price of the pool. #### get_swap_gross_amount_received(liq_a: int, liq_b: int, amount_deposited: int) → int Calculates the gross amount of tokens received from a swap operation. #### get_swap_amount_deposited(liq_a: int, liq_b: int, gross_amount_received: int) → int Calculates the amount of tokens deposited to receive a specific gross amount in a swap. #### get_minted_liquidity_tokens(added_liq_a: int, added_liq_b: int) → int Calculates the number of liquidity tokens minted when adding liquidity. ``` -------------------------------- ### FolksLendingPoolAdapter Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/docs/source/modules.md An adapter for interacting with Folks Lending Pools, facilitating liquidity management and swaps. ```APIDOC ## FolksLendingPoolAdapter ### Description An adapter for interacting with Folks Lending Pools, facilitating liquidity management and swaps. ### Properties - `algod` - `app_id` - `pact_pool` - `primary_lending_pool` - `secondary_lending_pool` - `escrow_address` ### Methods - `original_asset_to_f_asset()` - `f_asset_to_original_asset()` - `prepare_add_liquidity()` - `prepare_add_liquidity_tx_group()` - `build_add_liquidity_txs()` - `prepare_remove_liquidity_tx_group()` - `build_remove_liquidity_txs()` - `prepare_swap()` - `prepare_swap_tx_group()` ``` -------------------------------- ### Fetch Pool and Read State Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/get_pool_state.md Fetches a specific pool by its assets and prints its current state. Ensure you have initialized the AlgodClient and PactClient with valid credentials and network settings. ```python """This example fetches a pool and reads its state.""" from algosdk.v2client.algod import AlgodClient import pactsdk algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") algo = pact.fetch_asset(0) usdc = pact.fetch_asset(31566704) pool = pact.fetch_pools_by_assets(algo, usdc)[0] print(f"State {pool.state}") ``` -------------------------------- ### Perform a Zap Transaction Source: https://github.com/pactfi/pact-py-sdk/blob/main/docs/examples/zap.md This snippet demonstrates how to perform a zap operation on a liquidity pool. It requires setting up an AlgodClient, initializing a PactClient, fetching assets and the pool, and performing opt-in transactions for the asset and liquidity token before executing the zap. Ensure you have the necessary mnemonic, token, and URL for your algod client. ```python """This example performs a zap on a pool.""" import algosdk from algosdk.v2client.algod import AlgodClient import pactsdk private_key = algosdk.mnemonic.to_private_key("") address = algosdk.account.address_from_private_key(private_key) algod = AlgodClient("", "") pact = pactsdk.PactClient(algod, network="testnet") algo = pact.fetch_asset(0) usdc = pact.fetch_asset(31566704) pool = pact.fetch_pools_by_assets(algo, usdc)[0] # Opt-in for usdc. opt_in_txn = usdc.prepare_opt_in_tx(address) sent_optin_txid = algod.send_transaction(opt_in_txn.sign(private_key)) print(f"Opt-in transaction {sent_optin_txid}") # Opt-in for liquidity token. plp_opt_in_txn = pool.liquidity_asset.prepare_opt_in_tx(address) sent_plp_optin_txid = algod.send_transaction(plp_opt_in_txn.sign(private_key)) print(f"OptIn transaction {sent_plp_optin_txid}") # Do a zap. zap = pool.prepare_zap( asset=algo, amount=100_000, slippage_pct=2, ) zap_tx_group = zap.prepare_tx_group(address) signed_group = zap_tx_group.sign(private_key) algod.send_transactions(signed_group) print(f"Zap transaction group {zap_tx_group.group_id}") ``` -------------------------------- ### Compile Tealish Contract Source: https://github.com/pactfi/pact-py-sdk/blob/main/contract-mocks/README.md Use this command to compile a Tealish contract. The compiled .teal files are placed in the build directory. ```bash poetry run tealish compile contract-mocks/folks_lending_pool_mock.tl ```