|
6 | 6 | * @see https://github.com/codama-idl/codama |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { containsBytes, getU32Encoder, type Address, type ReadonlyUint8Array } from '@solana/kit'; |
10 | 9 | import { |
| 10 | + assertIsInstructionWithAccounts, |
| 11 | + containsBytes, |
| 12 | + getU32Encoder, |
| 13 | + type Address, |
| 14 | + type Instruction, |
| 15 | + type InstructionWithData, |
| 16 | + type ReadonlyUint8Array, |
| 17 | +} from '@solana/kit'; |
| 18 | +import { |
| 19 | + parseAdvanceNonceAccountInstruction, |
| 20 | + parseAllocateInstruction, |
| 21 | + parseAllocateWithSeedInstruction, |
| 22 | + parseAssignInstruction, |
| 23 | + parseAssignWithSeedInstruction, |
| 24 | + parseAuthorizeNonceAccountInstruction, |
| 25 | + parseCreateAccountInstruction, |
| 26 | + parseCreateAccountWithSeedInstruction, |
| 27 | + parseInitializeNonceAccountInstruction, |
| 28 | + parseTransferSolInstruction, |
| 29 | + parseTransferSolWithSeedInstruction, |
| 30 | + parseUpgradeNonceAccountInstruction, |
| 31 | + parseWithdrawNonceAccountInstruction, |
11 | 32 | type ParsedAdvanceNonceAccountInstruction, |
12 | 33 | type ParsedAllocateInstruction, |
13 | 34 | type ParsedAllocateWithSeedInstruction, |
@@ -107,3 +128,92 @@ export type ParsedSystemInstruction<TProgram extends string = '11111111111111111 |
107 | 128 | | ({ instructionType: SystemInstruction.AssignWithSeed } & ParsedAssignWithSeedInstruction<TProgram>) |
108 | 129 | | ({ instructionType: SystemInstruction.TransferSolWithSeed } & ParsedTransferSolWithSeedInstruction<TProgram>) |
109 | 130 | | ({ instructionType: SystemInstruction.UpgradeNonceAccount } & ParsedUpgradeNonceAccountInstruction<TProgram>); |
| 131 | + |
| 132 | +export function parseSystemInstruction<TProgram extends string>( |
| 133 | + instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>, |
| 134 | +): ParsedSystemInstruction<TProgram> { |
| 135 | + const instructionType = identifySystemInstruction(instruction); |
| 136 | + switch (instructionType) { |
| 137 | + case SystemInstruction.CreateAccount: { |
| 138 | + assertIsInstructionWithAccounts(instruction); |
| 139 | + return { instructionType: SystemInstruction.CreateAccount, ...parseCreateAccountInstruction(instruction) }; |
| 140 | + } |
| 141 | + case SystemInstruction.Assign: { |
| 142 | + assertIsInstructionWithAccounts(instruction); |
| 143 | + return { instructionType: SystemInstruction.Assign, ...parseAssignInstruction(instruction) }; |
| 144 | + } |
| 145 | + case SystemInstruction.TransferSol: { |
| 146 | + assertIsInstructionWithAccounts(instruction); |
| 147 | + return { instructionType: SystemInstruction.TransferSol, ...parseTransferSolInstruction(instruction) }; |
| 148 | + } |
| 149 | + case SystemInstruction.CreateAccountWithSeed: { |
| 150 | + assertIsInstructionWithAccounts(instruction); |
| 151 | + return { |
| 152 | + instructionType: SystemInstruction.CreateAccountWithSeed, |
| 153 | + ...parseCreateAccountWithSeedInstruction(instruction), |
| 154 | + }; |
| 155 | + } |
| 156 | + case SystemInstruction.AdvanceNonceAccount: { |
| 157 | + assertIsInstructionWithAccounts(instruction); |
| 158 | + return { |
| 159 | + instructionType: SystemInstruction.AdvanceNonceAccount, |
| 160 | + ...parseAdvanceNonceAccountInstruction(instruction), |
| 161 | + }; |
| 162 | + } |
| 163 | + case SystemInstruction.WithdrawNonceAccount: { |
| 164 | + assertIsInstructionWithAccounts(instruction); |
| 165 | + return { |
| 166 | + instructionType: SystemInstruction.WithdrawNonceAccount, |
| 167 | + ...parseWithdrawNonceAccountInstruction(instruction), |
| 168 | + }; |
| 169 | + } |
| 170 | + case SystemInstruction.InitializeNonceAccount: { |
| 171 | + assertIsInstructionWithAccounts(instruction); |
| 172 | + return { |
| 173 | + instructionType: SystemInstruction.InitializeNonceAccount, |
| 174 | + ...parseInitializeNonceAccountInstruction(instruction), |
| 175 | + }; |
| 176 | + } |
| 177 | + case SystemInstruction.AuthorizeNonceAccount: { |
| 178 | + assertIsInstructionWithAccounts(instruction); |
| 179 | + return { |
| 180 | + instructionType: SystemInstruction.AuthorizeNonceAccount, |
| 181 | + ...parseAuthorizeNonceAccountInstruction(instruction), |
| 182 | + }; |
| 183 | + } |
| 184 | + case SystemInstruction.Allocate: { |
| 185 | + assertIsInstructionWithAccounts(instruction); |
| 186 | + return { instructionType: SystemInstruction.Allocate, ...parseAllocateInstruction(instruction) }; |
| 187 | + } |
| 188 | + case SystemInstruction.AllocateWithSeed: { |
| 189 | + assertIsInstructionWithAccounts(instruction); |
| 190 | + return { |
| 191 | + instructionType: SystemInstruction.AllocateWithSeed, |
| 192 | + ...parseAllocateWithSeedInstruction(instruction), |
| 193 | + }; |
| 194 | + } |
| 195 | + case SystemInstruction.AssignWithSeed: { |
| 196 | + assertIsInstructionWithAccounts(instruction); |
| 197 | + return { |
| 198 | + instructionType: SystemInstruction.AssignWithSeed, |
| 199 | + ...parseAssignWithSeedInstruction(instruction), |
| 200 | + }; |
| 201 | + } |
| 202 | + case SystemInstruction.TransferSolWithSeed: { |
| 203 | + assertIsInstructionWithAccounts(instruction); |
| 204 | + return { |
| 205 | + instructionType: SystemInstruction.TransferSolWithSeed, |
| 206 | + ...parseTransferSolWithSeedInstruction(instruction), |
| 207 | + }; |
| 208 | + } |
| 209 | + case SystemInstruction.UpgradeNonceAccount: { |
| 210 | + assertIsInstructionWithAccounts(instruction); |
| 211 | + return { |
| 212 | + instructionType: SystemInstruction.UpgradeNonceAccount, |
| 213 | + ...parseUpgradeNonceAccountInstruction(instruction), |
| 214 | + }; |
| 215 | + } |
| 216 | + default: |
| 217 | + throw new Error(`Unrecognized instruction type: ${instructionType as string}`); |
| 218 | + } |
| 219 | +} |
0 commit comments