@blockassetlabs/minter
Version:
Blockasset Minter
25 lines • 1.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withFindOrInitAssociatedTokenAccount = void 0;
const spl_token_1 = require("@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
*/
async function withFindOrInitAssociatedTokenAccount(transaction, connection, mint, owner, payer, allowOwnerOffCurve) {
const associatedToken = await (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, allowOwnerOffCurve);
const account = await connection.getAccountInfo(associatedToken);
if (!account) {
transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(payer, associatedToken, owner, mint));
}
return associatedToken;
}
exports.withFindOrInitAssociatedTokenAccount = withFindOrInitAssociatedTokenAccount;
//# sourceMappingURL=withFindOrInitAssociatedTokenAccount.js.map