@@ -279,43 +279,38 @@ func (inst *Instruction) EncodeToTree(parent ag_treeout.Branches) {
279279 }
280280}
281281
282- // instructionImplDefs contains the variant types for instruction IDs 0-24.
283- // IDs 0-20 are the original SPL Token instructions, IDs 21-24 are additional
284- // instructions added for Token-2022 compatibility.
285- var instructionImplDefs = []ag_binary.VariantType {
286- {"InitializeMint" , (* InitializeMint )(nil )}, // 0
287- {"InitializeAccount" , (* InitializeAccount )(nil )}, // 1
288- {"InitializeMultisig" , (* InitializeMultisig )(nil )}, // 2
289- {"Transfer" , (* Transfer )(nil )}, // 3
290- {"Approve" , (* Approve )(nil )}, // 4
291- {"Revoke" , (* Revoke )(nil )}, // 5
292- {"SetAuthority" , (* SetAuthority )(nil )}, // 6
293- {"MintTo" , (* MintTo )(nil )}, // 7
294- {"Burn" , (* Burn )(nil )}, // 8
295- {"CloseAccount" , (* CloseAccount )(nil )}, // 9
296- {"FreezeAccount" , (* FreezeAccount )(nil )}, // 10
297- {"ThawAccount" , (* ThawAccount )(nil )}, // 11
298- {"TransferChecked" , (* TransferChecked )(nil )}, // 12
299- {"ApproveChecked" , (* ApproveChecked )(nil )}, // 13
300- {"MintToChecked" , (* MintToChecked )(nil )}, // 14
301- {"BurnChecked" , (* BurnChecked )(nil )}, // 15
302- {"InitializeAccount2" , (* InitializeAccount2 )(nil )}, // 16
303- {"SyncNative" , (* SyncNative )(nil )}, // 17
304- {"InitializeAccount3" , (* InitializeAccount3 )(nil )}, // 18
305- {"InitializeMultisig2" , (* InitializeMultisig2 )(nil )}, // 19
306- {"InitializeMint2" , (* InitializeMint2 )(nil )}, // 20
307- {"GetAccountDataSize" , (* GetAccountDataSize )(nil )}, // 21
308- {"InitializeImmutableOwner" , (* InitializeImmutableOwner )(nil )}, // 22
309- {"AmountToUiAmount" , (* AmountToUiAmount )(nil )}, // 23
310- {"UiAmountToAmount" , (* UiAmountToAmount )(nil )}, // 24
311- }
312-
313- // InstructionImplDef is the variant definition for instruction IDs 0-24.
314- // For p-token instructions with non-contiguous IDs (38, 45, 255),
315- // use DecodeInstruction which handles them via custom dispatch.
282+ // InstructionImplDef is the variant definition for contiguous instruction IDs 0–24.
283+ // P-token instructions with non-contiguous IDs (38, 45, 255) are handled separately
284+ // by DecodeInstruction via pTokenInstructionMap.
316285var InstructionImplDef = ag_binary .NewVariantDefinition (
317286 ag_binary .Uint8TypeIDEncoding ,
318- instructionImplDefs ,
287+ []ag_binary.VariantType {
288+ {"InitializeMint" , (* InitializeMint )(nil )},
289+ {"InitializeAccount" , (* InitializeAccount )(nil )},
290+ {"InitializeMultisig" , (* InitializeMultisig )(nil )},
291+ {"Transfer" , (* Transfer )(nil )},
292+ {"Approve" , (* Approve )(nil )},
293+ {"Revoke" , (* Revoke )(nil )},
294+ {"SetAuthority" , (* SetAuthority )(nil )},
295+ {"MintTo" , (* MintTo )(nil )},
296+ {"Burn" , (* Burn )(nil )},
297+ {"CloseAccount" , (* CloseAccount )(nil )},
298+ {"FreezeAccount" , (* FreezeAccount )(nil )},
299+ {"ThawAccount" , (* ThawAccount )(nil )},
300+ {"TransferChecked" , (* TransferChecked )(nil )},
301+ {"ApproveChecked" , (* ApproveChecked )(nil )},
302+ {"MintToChecked" , (* MintToChecked )(nil )},
303+ {"BurnChecked" , (* BurnChecked )(nil )},
304+ {"InitializeAccount2" , (* InitializeAccount2 )(nil )},
305+ {"SyncNative" , (* SyncNative )(nil )},
306+ {"InitializeAccount3" , (* InitializeAccount3 )(nil )},
307+ {"InitializeMultisig2" , (* InitializeMultisig2 )(nil )},
308+ {"InitializeMint2" , (* InitializeMint2 )(nil )},
309+ {"GetAccountDataSize" , (* GetAccountDataSize )(nil )},
310+ {"InitializeImmutableOwner" , (* InitializeImmutableOwner )(nil )},
311+ {"AmountToUiAmount" , (* AmountToUiAmount )(nil )},
312+ {"UiAmountToAmount" , (* UiAmountToAmount )(nil )},
313+ },
319314)
320315
321316// pTokenInstructionMap maps non-contiguous p-token instruction IDs to their types.
@@ -393,36 +388,24 @@ func decodePTokenInstruction(accounts []*ag_solanago.AccountMeta, data []byte, d
393388 inst := new (Instruction )
394389 inst .TypeID = ag_binary .TypeIDFromUint8 (discriminator )
395390
396- switch impl := vt .Type .(type ) {
391+ var impl ag_solanago.AccountsSettable
392+ switch vt .Type .(type ) {
397393 case * WithdrawExcessLamports :
398- _ = impl
399- obj := new (WithdrawExcessLamports )
400- if err := ag_binary .NewBinDecoder (data [1 :]).Decode (obj ); err != nil {
401- return nil , fmt .Errorf ("unable to decode WithdrawExcessLamports: %w" , err )
402- }
403- inst .Impl = obj
394+ impl = new (WithdrawExcessLamports )
404395 case * UnwrapLamports :
405- _ = impl
406- obj := new (UnwrapLamports )
407- if err := ag_binary .NewBinDecoder (data [1 :]).Decode (obj ); err != nil {
408- return nil , fmt .Errorf ("unable to decode UnwrapLamports: %w" , err )
409- }
410- inst .Impl = obj
396+ impl = new (UnwrapLamports )
411397 case * Batch :
412- _ = impl
413- obj := new (Batch )
414- if err := ag_binary .NewBinDecoder (data [1 :]).Decode (obj ); err != nil {
415- return nil , fmt .Errorf ("unable to decode Batch: %w" , err )
416- }
417- inst .Impl = obj
398+ impl = new (Batch )
418399 default :
419400 return nil , fmt .Errorf ("unknown p-token instruction type for discriminator %d" , discriminator )
420401 }
421402
422- if v , ok := inst .Impl .(ag_solanago.AccountsSettable ); ok {
423- if err := v .SetAccounts (accounts ); err != nil {
424- return nil , fmt .Errorf ("unable to set accounts for instruction: %w" , err )
425- }
403+ if err := ag_binary .NewBinDecoder (data [1 :]).Decode (impl ); err != nil {
404+ return nil , fmt .Errorf ("unable to decode %T: %w" , impl , err )
405+ }
406+ if err := impl .SetAccounts (accounts ); err != nil {
407+ return nil , fmt .Errorf ("unable to set accounts for instruction: %w" , err )
426408 }
409+ inst .Impl = impl
427410 return inst , nil
428411}
0 commit comments