@solana/spl-token
Version:
SPL Token Program JS API
86 lines • 5.33 kB
JavaScript
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js';
import { getSigners } from '../../actions/internal.js';
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../../constants.js';
import { createInitializeTransferHookInstruction, createTransferCheckedWithFeeAndTransferHookInstruction, createTransferCheckedWithTransferHookInstruction, createUpdateTransferHookInstruction, } from './instructions.js';
/**
* Initialize a transfer hook on a mint
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param mint Mint to initialize with extension
* @param authority Transfer hook authority account
* @param transferHookProgramId The transfer hook program account
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function initializeTransferHook(connection, payer, mint, authority, transferHookProgramId, confirmOptions, programId = TOKEN_2022_PROGRAM_ID) {
const transaction = new Transaction().add(createInitializeTransferHookInstruction(mint, authority, transferHookProgramId, programId));
return await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions);
}
/**
* Update the transfer hook program on a mint
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param mint Mint to modify
* @param transferHookProgramId New transfer hook program account
* @param authority Transfer hook update authority
* @param multiSigners Signing accounts if `freezeAuthority` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function updateTransferHook(connection, payer, mint, transferHookProgramId, authority, multiSigners = [], confirmOptions, programId = TOKEN_2022_PROGRAM_ID) {
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);
const transaction = new Transaction().add(createUpdateTransferHookInstruction(mint, authorityPublicKey, transferHookProgramId, signers, programId));
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
}
/**
* Transfer tokens from one account to another, asserting the token mint, and decimals
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param source Source account
* @param mint Mint for the account
* @param destination Destination account
* @param authority Authority of the source account
* @param amount Number of tokens to transfer
* @param decimals Number of decimals in transfer amount
* @param multiSigners Signing accounts if `owner` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function transferCheckedWithTransferHook(connection, payer, source, mint, destination, authority, amount, decimals, multiSigners = [], confirmOptions, programId = TOKEN_PROGRAM_ID) {
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);
const transaction = new Transaction().add(await createTransferCheckedWithTransferHookInstruction(connection, source, mint, destination, authorityPublicKey, amount, decimals, signers, confirmOptions?.commitment, programId));
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
}
/**
* Transfer tokens from one account to another, asserting the transfer fee, token mint, and decimals
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param source Source account
* @param mint Mint for the account
* @param destination Destination account
* @param authority Authority of the source account
* @param amount Number of tokens to transfer
* @param decimals Number of decimals in transfer amount
* @param fee The calculated fee for the transfer fee extension
* @param multiSigners Signing accounts if `owner` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function transferCheckedWithFeeAndTransferHook(connection, payer, source, mint, destination, authority, amount, decimals, fee, multiSigners = [], confirmOptions, programId = TOKEN_PROGRAM_ID) {
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);
const transaction = new Transaction().add(await createTransferCheckedWithFeeAndTransferHookInstruction(connection, source, mint, destination, authorityPublicKey, amount, decimals, fee, signers, confirmOptions?.commitment, programId));
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
}
//# sourceMappingURL=actions.js.map