Skip to content

Commit 332a19a

Browse files
committed
clean up p-token instructions for PR
1 parent 8c4996b commit 332a19a

4 files changed

Lines changed: 46 additions & 1098 deletions

File tree

programs/token/Batch.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package token
1616

1717
import (
18-
"bytes"
1918
"errors"
2019
"fmt"
2120

@@ -96,21 +95,14 @@ func (inst *Batch) EncodeToTree(parent ag_treeout.Branches) {
9695

9796
func (obj Batch) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
9897
for _, ix := range obj.Instructions {
99-
accountCount := uint8(len(ix.Accounts()))
100-
10198
data, err := ix.Data()
10299
if err != nil {
103100
return fmt.Errorf("unable to encode batch sub-instruction: %w", err)
104101
}
105-
// data includes the discriminator byte from the outer Instruction encoding,
106-
// but for batch sub-instructions we need the raw inner data (discriminator + params).
107-
// The ix.Data() already produces [discriminator | params], which is what we need.
108-
dataLen := uint8(len(data))
109-
110-
if err = encoder.WriteUint8(accountCount); err != nil {
102+
if err = encoder.WriteUint8(uint8(len(ix.Accounts()))); err != nil {
111103
return err
112104
}
113-
if err = encoder.WriteUint8(dataLen); err != nil {
105+
if err = encoder.WriteUint8(uint8(len(data))); err != nil {
114106
return err
115107
}
116108
if _, err = encoder.Write(data); err != nil {
@@ -145,23 +137,10 @@ func (obj *Batch) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
145137
return nil
146138
}
147139

148-
// BuildBatchData constructs the complete instruction data for a batch,
149-
// including the batch discriminator (255) and all sub-instruction data.
140+
// BuildBatchData is a convenience wrapper that returns the fully-encoded batch
141+
// instruction bytes (discriminator + sub-instruction headers + data).
150142
func BuildBatchData(instructions []*Instruction) ([]byte, error) {
151-
buf := new(bytes.Buffer)
152-
buf.WriteByte(Instruction_Batch)
153-
for _, ix := range instructions {
154-
accountCount := uint8(len(ix.Accounts()))
155-
data, err := ix.Data()
156-
if err != nil {
157-
return nil, fmt.Errorf("unable to encode batch sub-instruction: %w", err)
158-
}
159-
dataLen := uint8(len(data))
160-
buf.WriteByte(accountCount)
161-
buf.WriteByte(dataLen)
162-
buf.Write(data)
163-
}
164-
return buf.Bytes(), nil
143+
return NewBatchInstruction(instructions...).Build().Data()
165144
}
166145

167146
func NewBatchInstruction(instructions ...*Instruction) *Batch {

programs/token/instructions.go

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
316285
var 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

Comments
 (0)