|
| 1 | +import { |
| 2 | + ConfirmOptions, |
| 3 | + Connection, |
| 4 | + PublicKey, |
| 5 | + sendAndConfirmTransaction, |
| 6 | + Signer, |
| 7 | + Transaction, |
| 8 | + TransactionSignature, |
| 9 | +} from '@solana/web3.js'; |
| 10 | +import { TOKEN_2022_PROGRAM_ID } from '../../constants'; |
| 11 | +import { |
| 12 | + createTransferCheckedWithFeeInstruction, |
| 13 | + createHarvestWithheldTokensToMintInstruction, |
| 14 | + createWithdrawWithheldTokensFromAccountsInstruction, |
| 15 | + createWithdrawWithheldTokensFromMintInstruction, |
| 16 | +} from './instructions'; |
| 17 | +import { getSigners } from '../../actions/internal'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Transfer tokens from one account to another, asserting the transfer fee, token mint, and decimals |
| 21 | + * |
| 22 | + * @param connection Connection to use |
| 23 | + * @param payer Payer of the transaction fees |
| 24 | + * @param source Source account |
| 25 | + * @param mint Mint for the account |
| 26 | + * @param destination Destination account |
| 27 | + * @param owner Owner of the source account |
| 28 | + * @param amount Number of tokens to transfer |
| 29 | + * @param decimals Number of decimals in transfer amount |
| 30 | + * @param multiSigners Signing accounts if `owner` is a multisig |
| 31 | + * @param confirmOptions Options for confirming the transaction |
| 32 | + * @param programId SPL Token program account |
| 33 | + * |
| 34 | + * @return Signature of the confirmed transaction |
| 35 | + */ |
| 36 | +export async function transferCheckedWithFee( |
| 37 | + connection: Connection, |
| 38 | + payer: Signer, |
| 39 | + source: PublicKey, |
| 40 | + mint: PublicKey, |
| 41 | + destination: PublicKey, |
| 42 | + owner: Signer | PublicKey, |
| 43 | + amount: bigint, |
| 44 | + decimals: number, |
| 45 | + fee: bigint, |
| 46 | + multiSigners: Signer[] = [], |
| 47 | + confirmOptions?: ConfirmOptions, |
| 48 | + programId = TOKEN_2022_PROGRAM_ID |
| 49 | +): Promise<TransactionSignature> { |
| 50 | + const [ownerPublicKey, signers] = getSigners(owner, multiSigners); |
| 51 | + |
| 52 | + const transaction = new Transaction().add( |
| 53 | + createTransferCheckedWithFeeInstruction( |
| 54 | + source, |
| 55 | + mint, |
| 56 | + destination, |
| 57 | + ownerPublicKey, |
| 58 | + amount, |
| 59 | + decimals, |
| 60 | + fee, |
| 61 | + multiSigners, |
| 62 | + programId |
| 63 | + ) |
| 64 | + ); |
| 65 | + |
| 66 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Withdraw withheld tokens from mint |
| 71 | + * |
| 72 | + * @param connection Connection to use |
| 73 | + * @param payer Payer of the transaction fees |
| 74 | + * @param mint The token mint |
| 75 | + * @param destination The destination account |
| 76 | + * @param authority The mint's withdraw withheld tokens authority |
| 77 | + * @param multiSigners Signing accounts if `owner` is a multisig |
| 78 | + * @param confirmOptions Options for confirming the transaction |
| 79 | + * @param programId SPL Token program account |
| 80 | + * |
| 81 | + * @return Signature of the confirmed transaction |
| 82 | + */ |
| 83 | +export async function withdrawWithheldTokensFromMint( |
| 84 | + connection: Connection, |
| 85 | + payer: Signer, |
| 86 | + mint: PublicKey, |
| 87 | + destination: PublicKey, |
| 88 | + authority: Signer | PublicKey, |
| 89 | + multiSigners: Signer[] = [], |
| 90 | + confirmOptions?: ConfirmOptions, |
| 91 | + programId = TOKEN_2022_PROGRAM_ID |
| 92 | +): Promise<TransactionSignature> { |
| 93 | + const [authorityPublicKey, signers] = getSigners(authority, multiSigners); |
| 94 | + |
| 95 | + const transaction = new Transaction().add( |
| 96 | + createWithdrawWithheldTokensFromMintInstruction(mint, destination, authorityPublicKey, signers, programId) |
| 97 | + ); |
| 98 | + |
| 99 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Withdraw withheld tokens from accounts |
| 104 | + * |
| 105 | + * @param connection Connection to use |
| 106 | + * @param payer Payer of the transaction fees |
| 107 | + * @param mint The token mint |
| 108 | + * @param destination The destination account |
| 109 | + * @param authority The mint's withdraw withheld tokens authority |
| 110 | + * @param multiSigners Signing accounts if `owner` is a multisig |
| 111 | + * @param sources Source accounts from which to withdraw withheld fees |
| 112 | + * @param confirmOptions Options for confirming the transaction |
| 113 | + * @param programId SPL Token program account |
| 114 | + * |
| 115 | + * @return Signature of the confirmed transaction |
| 116 | + */ |
| 117 | +export async function withdrawWithheldTokensFromAccounts( |
| 118 | + connection: Connection, |
| 119 | + payer: Signer, |
| 120 | + mint: PublicKey, |
| 121 | + destination: PublicKey, |
| 122 | + authority: Signer | PublicKey, |
| 123 | + multiSigners: Signer[], |
| 124 | + sources: PublicKey[], |
| 125 | + confirmOptions?: ConfirmOptions, |
| 126 | + programId = TOKEN_2022_PROGRAM_ID |
| 127 | +): Promise<TransactionSignature> { |
| 128 | + const [authorityPublicKey, signers] = getSigners(authority, multiSigners); |
| 129 | + |
| 130 | + const transaction = new Transaction().add( |
| 131 | + createWithdrawWithheldTokensFromAccountsInstruction( |
| 132 | + mint, |
| 133 | + destination, |
| 134 | + authorityPublicKey, |
| 135 | + signers, |
| 136 | + sources, |
| 137 | + programId |
| 138 | + ) |
| 139 | + ); |
| 140 | + |
| 141 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Harvest withheld tokens from accounts to the mint |
| 146 | + * |
| 147 | + * @param connection Connection to use |
| 148 | + * @param payer Payer of the transaction fees |
| 149 | + * @param mint The token mint |
| 150 | + * @param sources Source accounts from which to withdraw withheld fees |
| 151 | + * @param confirmOptions Options for confirming the transaction |
| 152 | + * @param programId SPL Token program account |
| 153 | + * |
| 154 | + * @return Signature of the confirmed transaction |
| 155 | + */ |
| 156 | +export async function harvestWithheldTokensToMint( |
| 157 | + connection: Connection, |
| 158 | + payer: Signer, |
| 159 | + mint: PublicKey, |
| 160 | + sources: PublicKey[], |
| 161 | + confirmOptions?: ConfirmOptions, |
| 162 | + programId = TOKEN_2022_PROGRAM_ID |
| 163 | +): Promise<TransactionSignature> { |
| 164 | + const transaction = new Transaction().add(createHarvestWithheldTokensToMintInstruction(mint, sources, programId)); |
| 165 | + |
| 166 | + return await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions); |
| 167 | +} |
0 commit comments