@blockassetlabs/minter
Version:
Blockasset Minter
21 lines • 1.1 kB
JavaScript
import { createAssociatedTokenAccountInstruction, getAssociatedTokenAddress, } from "@solana/spl-token";
/**
* Utility function for adding a find or init associated token account instruction to a transaction
* Useful when using associated token accounts so you can be sure they are created before hand
* @param transaction
* @param connection
* @param mint
* @param owner
* @param payer
* @param allowOwnerOffCurve
* @returns The associated token account ID that was found or will be created. This also adds the relevent instruction to create it to the transaction if not found
*/
export async function withFindOrInitAssociatedTokenAccount(transaction, connection, mint, owner, payer, allowOwnerOffCurve) {
const associatedToken = await getAssociatedTokenAddress(mint, owner, allowOwnerOffCurve);
const account = await connection.getAccountInfo(associatedToken);
if (!account) {
transaction.add(createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint));
}
return associatedToken;
}
//# sourceMappingURL=withFindOrInitAssociatedTokenAccount.js.map