|
15 | 15 | package token |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "bytes" |
19 | 18 | "errors" |
20 | 19 | "fmt" |
21 | 20 |
|
@@ -96,21 +95,14 @@ func (inst *Batch) EncodeToTree(parent ag_treeout.Branches) { |
96 | 95 |
|
97 | 96 | func (obj Batch) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) { |
98 | 97 | for _, ix := range obj.Instructions { |
99 | | - accountCount := uint8(len(ix.Accounts())) |
100 | | - |
101 | 98 | data, err := ix.Data() |
102 | 99 | if err != nil { |
103 | 100 | return fmt.Errorf("unable to encode batch sub-instruction: %w", err) |
104 | 101 | } |
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 { |
111 | 103 | return err |
112 | 104 | } |
113 | | - if err = encoder.WriteUint8(dataLen); err != nil { |
| 105 | + if err = encoder.WriteUint8(uint8(len(data))); err != nil { |
114 | 106 | return err |
115 | 107 | } |
116 | 108 | if _, err = encoder.Write(data); err != nil { |
@@ -145,23 +137,10 @@ func (obj *Batch) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) { |
145 | 137 | return nil |
146 | 138 | } |
147 | 139 |
|
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). |
150 | 142 | 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() |
165 | 144 | } |
166 | 145 |
|
167 | 146 | func NewBatchInstruction(instructions ...*Instruction) *Batch { |
|
0 commit comments