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).
1 """2 Send an asset opt out transaction for account_a opting out of asset with asset id 12343
4 Parameters for an asset opt out transaction.5 - sender: The address of the account that will opt out of the asset6 - asset_id: ID of the asset7 - creator: The creator address of the asset8 - ensure_zero_balance: Check if account has zero balance before opt-out, defaults to True9 """10 txn_result = algorand_client.send.asset_opt_out(11 params=AssetOptOutParams(12 sender=account_a.address,13 asset_id=1234,14 creator=account_b.address,15 ),16 ensure_zero_balance=True,17 )
// opt-out is an amount transfer with the `closeRemainderTo` field set to// any account that can receive the asset.// note that closing to the asset creator will always succeedconst optOutTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: receiver.addr, to: creator.addr, closeRemainderTo: creator.addr, suggestedParams, assetIndex, amount: 0,});
const signedOptOutTxn = optOutTxn.signTxn(receiver.privateKey);await algodClient.sendRawTransaction(signedOptOutTxn).do();await algosdk.waitForConfirmation( algodClient, optOutTxn.txID().toString(), 3);
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.
1 """2 Send an asset opt in transaction for account_a opting in to asset with asset id 12343
4 Parameters for an asset opt in transaction.5 - sender: The address of the account that will opt in to the asset6 - asset_id: ID of the asset7 """8 txn_result = algorand_client.send.asset_opt_in(9 AssetOptInParams(10 sender=account_a.address,11 asset_id=1234,12 )13 )
// opt-in is simply a 0 amount transfer of the asset to oneselfconst optInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: receiver.addr, to: receiver.addr, suggestedParams, assetIndex, amount: 0,});
const signedOptInTxn = optInTxn.signTxn(receiver.privateKey);await algodClient.sendRawTransaction(signedOptInTxn).do();await algosdk.waitForConfirmation(algodClient, optInTxn.txID().toString(), 3);
goal asset send -a 0 --asset <asset-name> -f <opt-in-account> -t <opt-in-account> --creator <asset-creator> -d data
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 assetassetId: number
- The asset id that will be opted-in totransactionParams: SuggestedParams
- The optional transaction parametersnote: TransactionNote
- The optional transaction notelease: string | Uint8Array
: A lease to assign to the transaction to enforce a mutually exclusive transaction (useful to prevent double-posting and other scenarios)
1// Example2await algokit.assetOptIn({3 account: account,4 assetId: 12345,5 // Can optionally also specify transactionParams, note, lease and other send params6});
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 algodensureZeroBalance: boolean
- Whether or not to validate the account has a zero-balance before issuing the opt-out; defaults to true
1// Example2await algokit.assetOptOut({3 account: account,4 assetId: 12345,5 assetCreatorAddress: creator,6 // Can optionally also specify ensureZeroBalance, transactionParams, note, lease and other send params7});
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// Example2await algokit.assetBulkOptIn(3 {4 account: account,5 assetIds: [12354, 673453],6 // Can optionally also specify validateBalances, transactionParams, note7 },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// Example2await algokit.assetBulkOptOut(3 {4 account: account,5 assetIds: [12354, 673453],6 // Can optionally also specify validateBalances, transactionParams, note7 },8 algod,9);