|
| 1 | +import { struct, u8 } from '@solana/buffer-layout'; |
| 2 | +import { publicKey } from '@solana/buffer-layout-utils'; |
| 3 | +import type { Signer } from '@solana/web3.js'; |
| 4 | +import { PublicKey, TransactionInstruction } from '@solana/web3.js'; |
| 5 | +import { TOKEN_2022_PROGRAM_ID, programSupportsExtensions } from '../../constants.js'; |
| 6 | +import { TokenUnsupportedInstructionError } from '../../errors.js'; |
| 7 | +import { TokenInstruction } from '../../instructions/types.js'; |
| 8 | +import { addSigners } from '../../instructions/internal.js'; |
| 9 | + |
| 10 | +export enum PausableInstruction { |
| 11 | + Initialize = 0, |
| 12 | + Pause = 1, |
| 13 | + Resume = 2, |
| 14 | +} |
| 15 | + |
| 16 | +export interface InitializePausableConfigInstructionData { |
| 17 | + instruction: TokenInstruction.PausableExtension; |
| 18 | + pausableInstruction: PausableInstruction.Initialize; |
| 19 | + authority: PublicKey; |
| 20 | +} |
| 21 | + |
| 22 | +export const initializePausableConfigInstructionData = struct<InitializePausableConfigInstructionData>([ |
| 23 | + u8('instruction'), |
| 24 | + u8('pausableInstruction'), |
| 25 | + publicKey('authority'), |
| 26 | +]); |
| 27 | + |
| 28 | +/** |
| 29 | + * Construct a InitializePausableConfig instruction |
| 30 | + * |
| 31 | + * @param mint Token mint account |
| 32 | + * @param authority Optional authority that can pause or resume mint |
| 33 | + * @param programId SPL Token program account |
| 34 | + */ |
| 35 | +export function createInitializePausableConfigInstruction( |
| 36 | + mint: PublicKey, |
| 37 | + authority: PublicKey | null, |
| 38 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID, |
| 39 | +): TransactionInstruction { |
| 40 | + if (!programSupportsExtensions(programId)) { |
| 41 | + throw new TokenUnsupportedInstructionError(); |
| 42 | + } |
| 43 | + const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; |
| 44 | + |
| 45 | + const data = Buffer.alloc(initializePausableConfigInstructionData.span); |
| 46 | + initializePausableConfigInstructionData.encode( |
| 47 | + { |
| 48 | + instruction: TokenInstruction.PausableExtension, |
| 49 | + pausableInstruction: PausableInstruction.Initialize, |
| 50 | + authority: authority ?? PublicKey.default, |
| 51 | + }, |
| 52 | + data, |
| 53 | + ); |
| 54 | + |
| 55 | + return new TransactionInstruction({ keys, programId, data: data }); |
| 56 | +} |
| 57 | + |
| 58 | +export interface PauseInstructionData { |
| 59 | + instruction: TokenInstruction.PausableExtension; |
| 60 | + pausableInstruction: PausableInstruction.Pause; |
| 61 | +} |
| 62 | + |
| 63 | +export const pauseInstructionData = struct<PauseInstructionData>([u8('instruction'), u8('pausableInstruction')]); |
| 64 | + |
| 65 | +/** |
| 66 | + * Construct a Pause instruction |
| 67 | + * |
| 68 | + * @param mint Token mint account |
| 69 | + * @param authority The pausable mint's authority |
| 70 | + * @param multiSigners Signing accounts if authority is a multisig |
| 71 | + * @param programId SPL Token program account |
| 72 | + */ |
| 73 | +export function createPauseInstruction( |
| 74 | + mint: PublicKey, |
| 75 | + authority: PublicKey, |
| 76 | + multiSigners: (Signer | PublicKey)[] = [], |
| 77 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID, |
| 78 | +): TransactionInstruction { |
| 79 | + if (!programSupportsExtensions(programId)) { |
| 80 | + throw new TokenUnsupportedInstructionError(); |
| 81 | + } |
| 82 | + const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); |
| 83 | + |
| 84 | + const data = Buffer.alloc(pauseInstructionData.span); |
| 85 | + pauseInstructionData.encode( |
| 86 | + { |
| 87 | + instruction: TokenInstruction.PausableExtension, |
| 88 | + pausableInstruction: PausableInstruction.Pause, |
| 89 | + }, |
| 90 | + data, |
| 91 | + ); |
| 92 | + |
| 93 | + return new TransactionInstruction({ keys, programId, data: data }); |
| 94 | +} |
| 95 | + |
| 96 | +export interface ResumeInstructionData { |
| 97 | + instruction: TokenInstruction.PausableExtension; |
| 98 | + pausableInstruction: PausableInstruction.Resume; |
| 99 | +} |
| 100 | + |
| 101 | +export const resumeInstructionData = struct<ResumeInstructionData>([u8('instruction'), u8('pausableInstruction')]); |
| 102 | + |
| 103 | +/** |
| 104 | + * Construct a Resume instruction |
| 105 | + * |
| 106 | + * @param mint Token mint account |
| 107 | + * @param authority The pausable mint's authority |
| 108 | + * @param multiSigners Signing accounts if authority is a multisig |
| 109 | + * @param programId SPL Token program account |
| 110 | + */ |
| 111 | +export function createResumeInstruction( |
| 112 | + mint: PublicKey, |
| 113 | + authority: PublicKey, |
| 114 | + multiSigners: (Signer | PublicKey)[] = [], |
| 115 | + programId: PublicKey = TOKEN_2022_PROGRAM_ID, |
| 116 | +): TransactionInstruction { |
| 117 | + if (!programSupportsExtensions(programId)) { |
| 118 | + throw new TokenUnsupportedInstructionError(); |
| 119 | + } |
| 120 | + const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); |
| 121 | + |
| 122 | + const data = Buffer.alloc(resumeInstructionData.span); |
| 123 | + resumeInstructionData.encode( |
| 124 | + { |
| 125 | + instruction: TokenInstruction.PausableExtension, |
| 126 | + pausableInstruction: PausableInstruction.Resume, |
| 127 | + }, |
| 128 | + data, |
| 129 | + ); |
| 130 | + |
| 131 | + return new TransactionInstruction({ keys, programId, data: data }); |
| 132 | +} |
0 commit comments