|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { |
| 10 | + assertIsInstructionWithAccounts, |
10 | 11 | containsBytes, |
11 | 12 | getU8Encoder, |
12 | 13 | type Address, |
| 14 | + type Instruction, |
| 15 | + type InstructionWithData, |
13 | 16 | type ReadonlyUint8Array, |
14 | 17 | } from '@solana/kit'; |
15 | 18 | import { |
| 19 | + parseAllocateInstruction, |
| 20 | + parseCloseInstruction, |
| 21 | + parseExtendInstruction, |
| 22 | + parseInitializeInstruction, |
| 23 | + parseSetAuthorityInstruction, |
| 24 | + parseSetDataInstruction, |
| 25 | + parseSetImmutableInstruction, |
| 26 | + parseTrimInstruction, |
| 27 | + parseWriteInstruction, |
16 | 28 | type ParsedAllocateInstruction, |
17 | 29 | type ParsedCloseInstruction, |
18 | 30 | type ParsedExtendInstruction, |
@@ -110,3 +122,78 @@ export type ParsedProgramMetadataInstruction< |
110 | 122 | | ({ |
111 | 123 | instructionType: ProgramMetadataInstruction.Extend; |
112 | 124 | } & ParsedExtendInstruction<TProgram>); |
| 125 | + |
| 126 | +export function parseProgramMetadataInstruction<TProgram extends string>( |
| 127 | + instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> |
| 128 | +): ParsedProgramMetadataInstruction<TProgram> { |
| 129 | + const instructionType = identifyProgramMetadataInstruction(instruction); |
| 130 | + switch (instructionType) { |
| 131 | + case ProgramMetadataInstruction.Write: { |
| 132 | + assertIsInstructionWithAccounts(instruction); |
| 133 | + return { |
| 134 | + instructionType: ProgramMetadataInstruction.Write, |
| 135 | + ...parseWriteInstruction(instruction), |
| 136 | + }; |
| 137 | + } |
| 138 | + case ProgramMetadataInstruction.Initialize: { |
| 139 | + assertIsInstructionWithAccounts(instruction); |
| 140 | + return { |
| 141 | + instructionType: ProgramMetadataInstruction.Initialize, |
| 142 | + ...parseInitializeInstruction(instruction), |
| 143 | + }; |
| 144 | + } |
| 145 | + case ProgramMetadataInstruction.SetAuthority: { |
| 146 | + assertIsInstructionWithAccounts(instruction); |
| 147 | + return { |
| 148 | + instructionType: ProgramMetadataInstruction.SetAuthority, |
| 149 | + ...parseSetAuthorityInstruction(instruction), |
| 150 | + }; |
| 151 | + } |
| 152 | + case ProgramMetadataInstruction.SetData: { |
| 153 | + assertIsInstructionWithAccounts(instruction); |
| 154 | + return { |
| 155 | + instructionType: ProgramMetadataInstruction.SetData, |
| 156 | + ...parseSetDataInstruction(instruction), |
| 157 | + }; |
| 158 | + } |
| 159 | + case ProgramMetadataInstruction.SetImmutable: { |
| 160 | + assertIsInstructionWithAccounts(instruction); |
| 161 | + return { |
| 162 | + instructionType: ProgramMetadataInstruction.SetImmutable, |
| 163 | + ...parseSetImmutableInstruction(instruction), |
| 164 | + }; |
| 165 | + } |
| 166 | + case ProgramMetadataInstruction.Trim: { |
| 167 | + assertIsInstructionWithAccounts(instruction); |
| 168 | + return { |
| 169 | + instructionType: ProgramMetadataInstruction.Trim, |
| 170 | + ...parseTrimInstruction(instruction), |
| 171 | + }; |
| 172 | + } |
| 173 | + case ProgramMetadataInstruction.Close: { |
| 174 | + assertIsInstructionWithAccounts(instruction); |
| 175 | + return { |
| 176 | + instructionType: ProgramMetadataInstruction.Close, |
| 177 | + ...parseCloseInstruction(instruction), |
| 178 | + }; |
| 179 | + } |
| 180 | + case ProgramMetadataInstruction.Allocate: { |
| 181 | + assertIsInstructionWithAccounts(instruction); |
| 182 | + return { |
| 183 | + instructionType: ProgramMetadataInstruction.Allocate, |
| 184 | + ...parseAllocateInstruction(instruction), |
| 185 | + }; |
| 186 | + } |
| 187 | + case ProgramMetadataInstruction.Extend: { |
| 188 | + assertIsInstructionWithAccounts(instruction); |
| 189 | + return { |
| 190 | + instructionType: ProgramMetadataInstruction.Extend, |
| 191 | + ...parseExtendInstruction(instruction), |
| 192 | + }; |
| 193 | + } |
| 194 | + default: |
| 195 | + throw new Error( |
| 196 | + `Unrecognized instruction type: ${instructionType as string}` |
| 197 | + ); |
| 198 | + } |
| 199 | +} |
0 commit comments