Skip to content

Commit 133fb4c

Browse files
committed
clean up p-token instructions for PR
1 parent 620b2a4 commit 133fb4c

4 files changed

Lines changed: 16 additions & 1063 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: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -388,36 +388,24 @@ func decodePTokenInstruction(accounts []*ag_solanago.AccountMeta, data []byte, d
388388
inst := new(Instruction)
389389
inst.TypeID = ag_binary.TypeIDFromUint8(discriminator)
390390

391-
switch impl := vt.Type.(type) {
391+
var impl ag_solanago.AccountsSettable
392+
switch vt.Type.(type) {
392393
case *WithdrawExcessLamports:
393-
_ = impl
394-
obj := new(WithdrawExcessLamports)
395-
if err := ag_binary.NewBinDecoder(data[1:]).Decode(obj); err != nil {
396-
return nil, fmt.Errorf("unable to decode WithdrawExcessLamports: %w", err)
397-
}
398-
inst.Impl = obj
394+
impl = new(WithdrawExcessLamports)
399395
case *UnwrapLamports:
400-
_ = impl
401-
obj := new(UnwrapLamports)
402-
if err := ag_binary.NewBinDecoder(data[1:]).Decode(obj); err != nil {
403-
return nil, fmt.Errorf("unable to decode UnwrapLamports: %w", err)
404-
}
405-
inst.Impl = obj
396+
impl = new(UnwrapLamports)
406397
case *Batch:
407-
_ = impl
408-
obj := new(Batch)
409-
if err := ag_binary.NewBinDecoder(data[1:]).Decode(obj); err != nil {
410-
return nil, fmt.Errorf("unable to decode Batch: %w", err)
411-
}
412-
inst.Impl = obj
398+
impl = new(Batch)
413399
default:
414400
return nil, fmt.Errorf("unknown p-token instruction type for discriminator %d", discriminator)
415401
}
416402

417-
if v, ok := inst.Impl.(ag_solanago.AccountsSettable); ok {
418-
if err := v.SetAccounts(accounts); err != nil {
419-
return nil, fmt.Errorf("unable to set accounts for instruction: %w", err)
420-
}
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)
421408
}
409+
inst.Impl = impl
422410
return inst, nil
423411
}

0 commit comments

Comments
 (0)