Skip to content

Instantly share code, notes, and snippets.

@Henry-E
Created April 25, 2024 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Henry-E/2ebb4c758782fdac39add4df490c9b6f to your computer and use it in GitHub Desktop.
Save Henry-E/2ebb4c758782fdac39add4df490c9b6f to your computer and use it in GitHub Desktop.
Close aux token account
import * as anchor from "@coral-xyz/anchor";
import { PublicKey, Transaction, ComputeBudgetProgram } from "@solana/web3.js";
import * as token from "@solana/spl-token";
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
// Assumes your running with `anchor run closeAux --provider.cluster mainnet --provider.wallet ~/.config/solana/your-key.json`
const payer = provider.wallet["payer"];
// Function to transfer all tokens and close the source token account
async function transferAllTokensAndClose(
sourceAccountPublicKey,
tokenMintPublicKey
) {
const sourceAccountInfo = await token.getAccount(
provider.connection,
sourceAccountPublicKey
);
const amount = sourceAccountInfo.amount;
const destinationAccount = await token.getOrCreateAssociatedTokenAccount(
provider.connection,
payer,
tokenMintPublicKey,
payer.publicKey
);
const transferIx = token.createTransferInstruction(
sourceAccountPublicKey,
destinationAccount.address,
payer.publicKey,
amount
);
const closeAccountIx = token.createCloseAccountInstruction(
sourceAccountPublicKey,
payer.publicKey,
payer.publicKey
);
const transaction = new Transaction();
// Set compute unit budget and priority fee
transaction.add(ComputeBudgetProgram.setComputeUnitLimit({ units: 30000 }));
transaction.add(
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1000 })
);
transaction.add(transferIx);
transaction.add(closeAccountIx);
transaction.feePayer = payer.publicKey;
transaction.recentBlockhash = (
await provider.connection.getLatestBlockhash()
).blockhash;
console.log("Sending transfer and close account transaction...");
const result = await provider.sendAndConfirm(transaction, [payer]);
return result;
}
async function main() {
// Use `spl-token accounts -v` to get the aux account public key
const auxAccountPublicKey = new PublicKey(
"[your aux account pubkey here]"
);
const tokenMintPublicKey = new PublicKey(
"[the token mint pubkey here]"
);
// Transfer all tokens and close the aux account
await transferAllTokensAndClose(auxAccountPublicKey, tokenMintPublicKey);
console.log("All tokens transferred and aux account closed successfully.");
}
main();
@Henry-E
Copy link
Author

Henry-E commented Apr 25, 2024

Instructions for use

  • Get your private key from the web wallet
  • Use base58 python package or some other approach to convert the private key to the array of integer format required by the solana CLI https://solana.stackexchange.com/a/2739/255
  • Save the private key in array format to a .json file, set that keypair as the default in the solana cli config
  • Install and call spl-token accounts -v to get the aux account pubkey and the mint pubkey
  • Create an anchor environment
  • Create a scripts folder and add the above file to it
  • Add closeAux = "yarn run ts-node scripts/closeAuxAccount.ts" to the Anchor.toml file
  • Run the script using anchor run closeAux --provider.cluster mainnet --provider.wallet ~/.config/solana/your-key.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment