Burn Tokens & Close Accounts

Efficiently burn SPL tokens and close associated token accounts with the BurnManager class. This functionality is particularly useful for cleaning up unused accounts and managing token supplies.

Code Breakdown

1. Burn Tokens

Burns all tokens in a specified token account:

Copy

burn_instruction = burn(
    BurnParams(
        program_id=TOKEN_PROGRAM_ID,
        account=token_account_pubkey,
        mint=mint,
        owner=owner,
        amount=token_balance
    )
)
transaction.add(burn_instruction)
  • Token Balance Check: Fetches the account’s token balance before proceeding.

  • Mint Address: Identifies the token mint associated with the account.

2. Close Account

Closes the token account to reclaim rent-exempt lamports:

Copy

close_account_instruction = close_account(
    CloseAccountParams(
        program_id=TOKEN_PROGRAM_ID,
        account=token_account_pubkey,
        dest=owner,
        owner=owner
    )
)
transaction.add(close_account_instruction)
  • Destination: Remaining lamports are transferred to the owner.

3. Enhanced Compute Budget

Adjusts the compute budget to handle complex operations:

Copy

transaction.add(set_compute_unit_price(100_000))
transaction.add(set_compute_unit_limit(100_000))

4. Transaction Execution

Signs and sends the transaction:

Copy

transaction.sign(agent.wallet)
txn_sig = client.send_transaction(transaction, agent.wallet, opts=TxOpts(skip_preflight=True)).value
  • Ensures secure and authenticated processing.


Key Features

  1. Comprehensive Functionality

    • Handles both token burning and account closure within a single process.

  2. Batch Processing

    • The process_multiple_accounts method allows for the efficient processing of multiple accounts:

    Copy

    for token_account in token_accounts:
        BurnManager.burn_and_close_account(agent, token_account)
  3. Error Handling

    • Captures errors during balance fetching, instruction preparation, and transaction execution:

    Copy

    except Exception as e:
        print(f"Error sending transaction for {token_account}: {e}")
  4. Compute Budget Adjustment

    • Ensures successful execution of complex transactions by increasing compute unit limits.

Last updated