Skip to content
This new developer portal is under construction. For complete documentation, please refer to the old developer portal.

Opting in and out of assets

Authorized by: The account opting out

The asset management functions include opting in and out of assets, which are fundamental to asset interaction in a blockchain environment. To see some usage examples check out the automated tests.

An account can opt out of an asset at any time. This means that the account will no longer hold the asset, and the account will no longer be able to receive the asset. The account also recovers the Minimum Balance Requirement for the asset (0.1A).

sp = algod_client.suggested_params()
opt_out_txn = transaction.AssetTransferTxn(
sender=acct2.address,
sp=sp,
index=created_asset,
receiver=acct1.address,
# an opt out transaction sets its close_asset_to parameter
# it is always possible to close an asset to the creator
close_assets_to=acct1.address,
amt=0,
)
signed_opt_out = opt_out_txn.sign(acct2.private_key)
txid = algod_client.send_transaction(signed_opt_out)
print(f"Sent opt out transaction with txid: {txid}")
results = transaction.wait_for_confirmation(algod_client, txid, 4)
print(f"Result confirmed in round: {results['confirmed-round']}")

Receiving an asset

Authorized by: The account opting in

Before an account can receive a specific asset it must opt-in to receive it. An opt-in transaction places an asset holding of 0 into the account and increases its minimum balance by 100,000 microAlgos. An opt-in transaction is simply an asset transfer with an amount of 0, both to and from the account opting in. The following code illustrates this transaction.

sp = algod_client.suggested_params()
# Create opt-in transaction
# asset transfer from me to me for asset id we want to opt-in to with amt==0
optin_txn = transaction.AssetOptInTxn(
sender=acct2.address, sp=sp, index=created_asset
)
signed_optin_txn = optin_txn.sign(acct2.private_key)
txid = algod_client.send_transaction(signed_optin_txn)
print(f"Sent opt in transaction with txid: {txid}")
# Wait for the transaction to be confirmed
results = transaction.wait_for_confirmation(algod_client, txid, 4)
print(f"Result confirmed in round: {results['confirmed-round']}")

See also

Opt-in/out

Before an account can receive a specific asset, it must opt-in to receive it. An opt-in transaction places an asset holding of 0 into the account and increases its minimum balance by 100,000 microAlgos.

An account can opt out of an asset at any time. This means that the account will no longer hold the asset, and the account will no longer be able to receive the asset. The account also recovers the Minimum Balance Requirement for the asset (100,000 microAlgos).

When opting-out you generally want to be careful to ensure you have a zero-balance otherwise you will forfeit the balance you do have. By default, AlgoKit Utils protects you from making this mistake by checking you have a zero-balance before issuing the opt-out transaction. You can turn this check off if you want to avoid the extra calls to Algorand and are confident in what you are doing.

AlgoKit Utils gives you functions that allow you to do opt-ins in bulk or as a single operation. The bulk operations give you less control over the sending semantics as they automatically send the transactions to Algorand in the most optimal way using transaction groups.

assetOptIn

To opt-in an account to a single asset you can use the algokit.assetOptIn(optIn, algod) function. The optIn argument is an object containing:

  • All properties in SendTransactionParams
  • account: SendTransactionFrom - The account that will opt-in to the asset
  • assetId: number - The asset id that will be opted-in to
  • transactionParams: SuggestedParams - The optional transaction parameters
  • note: TransactionNote - The optional transaction note
  • lease: string | Uint8Array: A lease to assign to the transaction to enforce a mutually exclusive transaction (useful to prevent double-posting and other scenarios)
1
// Example
2
await algokit.assetOptIn({
3
account: account,
4
assetId: 12345,
5
// Can optionally also specify transactionParams, note, lease and other send params
6
})

assetOptOut

To opt-out an account from a single asset you can use the algokit.assetOptOut(optOut, algod) function. The optOut argument is an object containing:

  • All properties from assetOptIn
  • assetCreatorAddress: string - The address of the creator account for the asset; if unspecified then it looks it up using algod
  • ensureZeroBalance: boolean - Whether or not to validate the account has a zero-balance before issuing the opt-out; defaults to true
1
// Example
2
await algokit.assetOptOut({
3
account: account,
4
assetId: 12345,
5
assetCreatorAddress: creator,
6
// Can optionally also specify ensureZeroBalance, transactionParams, note, lease and other send params
7
})

assetBulkOptIn

The assetBulkOptIn function facilitates the opt-in process for an account to multiple assets, allowing the account to receive and hold those assets.

1
// Example
2
await algokit.assetBulkOptIn(
3
{
4
account: account,
5
assetIds: [12354, 673453],
6
// Can optionally also specify validateBalances, transactionParams, note
7
},
8
algod,
9
)

assetBulkOptOut

The assetBulkOptOut function manages the opt-out process for a number of assets, permitting the account to discontinue holding a group of assets.

1
// Example
2
await algokit.assetBulkOptOut(
3
{
4
account: account,
5
assetIds: [12354, 673453],
6
// Can optionally also specify validateBalances, transactionParams, note
7
},
8
algod,
9
)