|
| 1 | +import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js'; |
| 2 | +import { sendAndConfirmTransaction, SystemProgram, Transaction } from '@solana/web3.js'; |
| 3 | +import { |
| 4 | + createInitializeGroupInstruction, |
| 5 | + createUpdateGroupMaxSizeInstruction, |
| 6 | + createUpdateGroupAuthorityInstruction, |
| 7 | + createInitializeMemberInstruction, |
| 8 | + TOKEN_GROUP_SIZE, |
| 9 | + TOKEN_GROUP_MEMBER_SIZE, |
| 10 | +} from '@solana/spl-token-group'; |
| 11 | + |
| 12 | +import { TOKEN_2022_PROGRAM_ID } from '../../constants.js'; |
| 13 | +import { getSigners } from '../../actions/internal.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Initialize a new `Group` |
| 17 | + * |
| 18 | + * Assumes one has already initialized a mint for the group. |
| 19 | + * |
| 20 | + * @param connection Connection to use |
| 21 | + * @param payer Payer of the transaction fee |
| 22 | + * @param mint Group mint |
| 23 | + * @param mintAuthority Group mint authority |
| 24 | + * @param updateAuthority Group update authority |
| 25 | + * @param maxSize Maximum number of members in the group |
| 26 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 27 | + * @param confirmOptions Options for confirming the transaction |
| 28 | + * @param programId SPL Token program account |
| 29 | + * |
| 30 | + * @return Signature of the confirmed transaction |
| 31 | + */ |
| 32 | +export async function tokenGroupInitializeGroup( |
| 33 | + connection: Connection, |
| 34 | + payer: Signer, |
| 35 | + mint: PublicKey, |
| 36 | + mintAuthority: PublicKey | Signer, |
| 37 | + updateAuthority: PublicKey | null, |
| 38 | + maxSize: number, |
| 39 | + multiSigners: Signer[] = [], |
| 40 | + confirmOptions?: ConfirmOptions, |
| 41 | + programId = TOKEN_2022_PROGRAM_ID |
| 42 | +): Promise<TransactionSignature> { |
| 43 | + const [mintAuthorityPublicKey, signers] = getSigners(mintAuthority, multiSigners); |
| 44 | + |
| 45 | + const transaction = new Transaction().add( |
| 46 | + createInitializeGroupInstruction({ |
| 47 | + programId, |
| 48 | + group: mint, |
| 49 | + mint, |
| 50 | + mintAuthority: mintAuthorityPublicKey, |
| 51 | + updateAuthority, |
| 52 | + maxSize, |
| 53 | + }) |
| 54 | + ); |
| 55 | + |
| 56 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Initialize a new `Group` with rent transfer. |
| 61 | + * |
| 62 | + * Assumes one has already initialized a mint for the group. |
| 63 | + * |
| 64 | + * @param connection Connection to use |
| 65 | + * @param payer Payer of the transaction fee |
| 66 | + * @param mint Group mint |
| 67 | + * @param mintAuthority Group mint authority |
| 68 | + * @param updateAuthority Group update authority |
| 69 | + * @param maxSize Maximum number of members in the group |
| 70 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 71 | + * @param confirmOptions Options for confirming the transaction |
| 72 | + * @param programId SPL Token program account |
| 73 | + * |
| 74 | + * @return Signature of the confirmed transaction |
| 75 | + */ |
| 76 | +export async function tokenGroupInitializeGroupWithRentTransfer( |
| 77 | + connection: Connection, |
| 78 | + payer: Signer, |
| 79 | + mint: PublicKey, |
| 80 | + mintAuthority: PublicKey | Signer, |
| 81 | + updateAuthority: PublicKey | null, |
| 82 | + maxSize: number, |
| 83 | + multiSigners: Signer[] = [], |
| 84 | + confirmOptions?: ConfirmOptions, |
| 85 | + programId = TOKEN_2022_PROGRAM_ID |
| 86 | +): Promise<TransactionSignature> { |
| 87 | + const [mintAuthorityPublicKey, signers] = getSigners(mintAuthority, multiSigners); |
| 88 | + |
| 89 | + const lamports = await connection.getMinimumBalanceForRentExemption(TOKEN_GROUP_SIZE); |
| 90 | + |
| 91 | + const transaction = new Transaction().add( |
| 92 | + SystemProgram.transfer({ |
| 93 | + fromPubkey: payer.publicKey, |
| 94 | + toPubkey: mint, |
| 95 | + lamports, |
| 96 | + }), |
| 97 | + createInitializeGroupInstruction({ |
| 98 | + programId, |
| 99 | + group: mint, |
| 100 | + mint, |
| 101 | + mintAuthority: mintAuthorityPublicKey, |
| 102 | + updateAuthority, |
| 103 | + maxSize, |
| 104 | + }) |
| 105 | + ); |
| 106 | + |
| 107 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Update the max size of a `Group` |
| 112 | + * |
| 113 | + * @param connection Connection to use |
| 114 | + * @param payer Payer of the transaction fee |
| 115 | + * @param mint Group mint |
| 116 | + * @param updateAuthority Group update authority |
| 117 | + * @param maxSize Maximum number of members in the group |
| 118 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 119 | + * @param confirmOptions Options for confirming the transaction |
| 120 | + * @param programId SPL Token program account |
| 121 | + * |
| 122 | + * @return Signature of the confirmed transaction |
| 123 | + */ |
| 124 | +export async function tokenGroupUpdateGroupMaxSize( |
| 125 | + connection: Connection, |
| 126 | + payer: Signer, |
| 127 | + mint: PublicKey, |
| 128 | + updateAuthority: PublicKey | Signer, |
| 129 | + maxSize: number, |
| 130 | + multiSigners: Signer[] = [], |
| 131 | + confirmOptions?: ConfirmOptions, |
| 132 | + programId = TOKEN_2022_PROGRAM_ID |
| 133 | +): Promise<TransactionSignature> { |
| 134 | + const [updateAuthorityPublicKey, signers] = getSigners(updateAuthority, multiSigners); |
| 135 | + |
| 136 | + const transaction = new Transaction().add( |
| 137 | + createUpdateGroupMaxSizeInstruction({ |
| 138 | + programId, |
| 139 | + group: mint, |
| 140 | + updateAuthority: updateAuthorityPublicKey, |
| 141 | + maxSize, |
| 142 | + }) |
| 143 | + ); |
| 144 | + |
| 145 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Update the authority of a `Group` |
| 150 | + * |
| 151 | + * @param connection Connection to use |
| 152 | + * @param payer Payer of the transaction fee |
| 153 | + * @param mint Group mint |
| 154 | + * @param updateAuthority Group update authority |
| 155 | + * @param newAuthority New authority for the token group, or unset |
| 156 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 157 | + * @param confirmOptions Options for confirming the transaction |
| 158 | + * @param programId SPL Token program account |
| 159 | + * |
| 160 | + * @return Signature of the confirmed transaction |
| 161 | + */ |
| 162 | +export async function tokenGroupUpdateGroupAuthority( |
| 163 | + connection: Connection, |
| 164 | + payer: Signer, |
| 165 | + mint: PublicKey, |
| 166 | + updateAuthority: PublicKey | Signer, |
| 167 | + newAuthority: PublicKey | null, |
| 168 | + multiSigners: Signer[] = [], |
| 169 | + confirmOptions?: ConfirmOptions, |
| 170 | + programId = TOKEN_2022_PROGRAM_ID |
| 171 | +): Promise<TransactionSignature> { |
| 172 | + const [updateAuthorityPublicKey, signers] = getSigners(updateAuthority, multiSigners); |
| 173 | + |
| 174 | + const transaction = new Transaction().add( |
| 175 | + createUpdateGroupAuthorityInstruction({ |
| 176 | + programId, |
| 177 | + group: mint, |
| 178 | + currentAuthority: updateAuthorityPublicKey, |
| 179 | + newAuthority, |
| 180 | + }) |
| 181 | + ); |
| 182 | + |
| 183 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Initialize a new `Member` of a `Group` |
| 188 | + * |
| 189 | + * Assumes the `Group` has already been initialized, |
| 190 | + * as well as the mint for the member. |
| 191 | + * |
| 192 | + * @param connection Connection to use |
| 193 | + * @param payer Payer of the transaction fee |
| 194 | + * @param mint Member mint |
| 195 | + * @param mintAuthority Member mint authority |
| 196 | + * @param group Group mint |
| 197 | + * @param groupUpdateAuthority Group update authority |
| 198 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 199 | + * @param confirmOptions Options for confirming the transaction |
| 200 | + * @param programId SPL Token program account |
| 201 | + * |
| 202 | + * @return Signature of the confirmed transaction |
| 203 | + */ |
| 204 | +export async function tokenGroupMemberInitialize( |
| 205 | + connection: Connection, |
| 206 | + payer: Signer, |
| 207 | + mint: PublicKey, |
| 208 | + mintAuthority: PublicKey | Signer, |
| 209 | + group: PublicKey, |
| 210 | + groupUpdateAuthority: PublicKey, |
| 211 | + multiSigners: Signer[] = [], |
| 212 | + confirmOptions?: ConfirmOptions, |
| 213 | + programId = TOKEN_2022_PROGRAM_ID |
| 214 | +): Promise<TransactionSignature> { |
| 215 | + const [mintAuthorityPublicKey, signers] = getSigners(mintAuthority, multiSigners); |
| 216 | + |
| 217 | + const transaction = new Transaction().add( |
| 218 | + createInitializeMemberInstruction({ |
| 219 | + programId, |
| 220 | + member: mint, |
| 221 | + memberMint: mint, |
| 222 | + memberMintAuthority: mintAuthorityPublicKey, |
| 223 | + group, |
| 224 | + groupUpdateAuthority, |
| 225 | + }) |
| 226 | + ); |
| 227 | + |
| 228 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 229 | +} |
| 230 | + |
| 231 | +/** |
| 232 | + * Initialize a new `Member` of a `Group` with rent transfer. |
| 233 | + * |
| 234 | + * Assumes the `Group` has already been initialized, |
| 235 | + * as well as the mint for the member. |
| 236 | + * |
| 237 | + * @param connection Connection to use |
| 238 | + * @param payer Payer of the transaction fee |
| 239 | + * @param mint Member mint |
| 240 | + * @param mintAuthority Member mint authority |
| 241 | + * @param group Group mint |
| 242 | + * @param groupUpdateAuthority Group update authority |
| 243 | + * @param multiSigners Signing accounts if `authority` is a multisig |
| 244 | + * @param confirmOptions Options for confirming the transaction |
| 245 | + * @param programId SPL Token program account |
| 246 | + * |
| 247 | + * @return Signature of the confirmed transaction |
| 248 | + */ |
| 249 | +export async function tokenGroupMemberInitializeWithRentTransfer( |
| 250 | + connection: Connection, |
| 251 | + payer: Signer, |
| 252 | + mint: PublicKey, |
| 253 | + mintAuthority: PublicKey | Signer, |
| 254 | + group: PublicKey, |
| 255 | + groupUpdateAuthority: PublicKey, |
| 256 | + multiSigners: Signer[] = [], |
| 257 | + confirmOptions?: ConfirmOptions, |
| 258 | + programId = TOKEN_2022_PROGRAM_ID |
| 259 | +): Promise<TransactionSignature> { |
| 260 | + const [mintAuthorityPublicKey, signers] = getSigners(mintAuthority, multiSigners); |
| 261 | + |
| 262 | + const lamports = await connection.getMinimumBalanceForRentExemption(TOKEN_GROUP_MEMBER_SIZE); |
| 263 | + |
| 264 | + const transaction = new Transaction().add( |
| 265 | + SystemProgram.transfer({ |
| 266 | + fromPubkey: payer.publicKey, |
| 267 | + toPubkey: mint, |
| 268 | + lamports, |
| 269 | + }), |
| 270 | + createInitializeMemberInstruction({ |
| 271 | + programId, |
| 272 | + member: mint, |
| 273 | + memberMint: mint, |
| 274 | + memberMintAuthority: mintAuthorityPublicKey, |
| 275 | + group, |
| 276 | + groupUpdateAuthority, |
| 277 | + }) |
| 278 | + ); |
| 279 | + |
| 280 | + return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); |
| 281 | +} |
0 commit comments