asset

Utility functions and class for dealing with Algorand Standard Assets.

pactsdk.asset.ASSETS_CACHE: dict[tuple[algosdk.v2client.algod.AlgodClient, int], 'Asset'] = {}

Dictionary mapping the asset index number to the pactsdk.asset.Asset class to speed up look up of the asset information.

pactsdk.asset.get_cached_asset(algod, index, decimals)
Return type

Asset

pactsdk.asset.fetch_asset_by_index(algod, index)

Fetches an pactsdk.asset.Asset class with the details about the asset for a given id number.

The function uses an internal cache so as to minimize the number of times the actual Algorand client is used to look up the asset. This function is used through out the pact sdk to query asset information.

Parameters
  • algod (AlgodClient) – An Algorand client to query about the asset.

  • index (int) – An Algorand Asset number to look up.

Return type

Asset

Returns

An asset instance for the index passed in.

class pactsdk.asset.Asset(algod, index, decimals, name=None, unit_name=None)

Bases: object

Describes the basic data and the utility functions for an Algorand Standard Asset.

Typically you don’t create instances of this class manually. Use pactsdk.client.PactClient.fetch_asset() instead. Also, when instantiating the pool e.g. by using pactsdk.client.PactClient.fetch_pool_by_id() the missing pool assets are fetched automatically.

algod: algosdk.v2client.algod.AlgodClient

The Algorand sdk client to use for extracting asset details.

index: int

The ID of the asset.

decimals: int

The number of decimal places that the Asset supports.

name: Optional[str] = None

The name of the Asset if there is one. This may be None.

unit_name: Optional[str] = None

The name of a unit of the asset if there is one. This may be None.

property ratio: int

The ratio between a base unit and the unit of the asset.

This is used to convert between an integer and floating point representation of the asset without loss of precision.

Return type

int

prepare_opt_in_tx(address)

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

address (str) – Account to opt in to this asset.

Return type

AssetTransferTxn

Returns

A ready to send transaction to opt-in into the ASA.

build_opt_in_tx(address, suggested_params)

Creates the actual transaction for the account to opt-in to holding the asset.

Parameters
  • address (str) – Address of the account to opt in to the asset.

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

Return type

AssetTransferTxn

Returns

A transaction to opt-in into asset.

prepare_opt_out_tx(address, close_to)

This creates a transaction that will allow the account to “opt out” of the asset.

Parameters

address (str) – Account to opt out of this asset.

Return type

AssetTransferTxn

Returns

A ready to send transaction to opt-out of the ASA.

build_opt_out_tx(address, close_to, suggested_params)

Creates the actual transaction for the account to opt-out from asset.

Parameters
  • address (str) – Address of the account to opt out of the asset.

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

Return type

AssetTransferTxn

Returns

A transaction to opt-out of the asset.

is_opted_in(address)

Checks if the account is already able to hold this asset, that is it has already opted in.

This functions should be called to check if the opt-in transaction needs to be created. See pactsdk.asset.Asset.prepare_opt_in_tx().

Parameters

address (str) – The account to check if the asset is opted in on.

Return type

bool

Returns

True if the account is already opted in, false otherwise.

get_holding(address)

Returns the amount of holding of this asset the account has.

Note that this function may return None if the account has not opted in for this asset.

Parameters

address (str) – The account to check the current holding.

Return type

Optional[int]

Returns

The amount of this asset the account is holding, or None if the account is not opted into the asset.

get_holding_from_account_info(account_info)
Return type

Optional[int]

build_transfer_tx(sender, receiver, amount, suggested_params, note=None)
Return type

Transaction

__eq__(other_asset)

Return equal by comparing the assets index value.

Return type

bool