Skip to content

Commit

Permalink
Bugfix/entity orders (#4)
Browse files Browse the repository at this point in the history
* Fix qx entity order conversion

* Update EntityOrder struct

* Replace uint64 with [8]byte as the response is an 8 byte null terminated string.
* Adapt converter to use byte array.

* Fix test
  • Loading branch information
qubicmio authored Oct 1, 2024
1 parent d182045 commit 6277122
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
.DS_Store
.DS_Store
app/qubic-node-proxy/qubic-node-proxy
*.iml
8 changes: 3 additions & 5 deletions sdk/qx/converter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package qx

import (
"bytes"
"encoding/base64"
"encoding/binary"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/common"
qubicpb "github.com/qubic/go-qubic/proto/v1"
Expand Down Expand Up @@ -54,15 +54,13 @@ func (eoc entityOrdersConverter) ToProto(entityOrders EntityOrders) (*qubicpb.En
if err != nil {
return nil, errors.Wrapf(err, "converting entity order issuer pubkey: %s to id", base64.StdEncoding.EncodeToString(entityOrder.Issuer[:]))
}
assetNameBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(assetNameBytes, entityOrder.AssetName)

order := &qubicpb.EntityOrders_Order{
IssuerId: issuerID.String(),
Price: entityOrder.Price,
AssetName: string(assetNameBytes),
AssetName: string(bytes.TrimRight(entityOrder.AssetName[:], "\x00")),
NumberOfShares: entityOrder.NumberOfShares,
}

orders = append(orders, order)
}

Expand Down
44 changes: 27 additions & 17 deletions sdk/qx/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,44 @@ struct qxGetEntityOrder_output{

type EntityOrder struct {
Issuer [32]byte
AssetName uint64
AssetName [8]byte
Price int64
NumberOfShares int64
}

type EntityOrders []EntityOrder

func (orders *EntityOrders) UnmarshallFromReader(r io.Reader) error {
for {
var header connector.RequestResponseHeader
err := binary.Read(r, binary.BigEndian, &header)
if err != nil {
return errors.Wrap(err, "reading header")
}
var emptyEntityOrder EntityOrder

if header.Type == connector.EndResponse {
break
}
func (eo *EntityOrders) UnmarshallFromReader(r io.Reader) error {
var header connector.RequestResponseHeader
err := binary.Read(r, binary.BigEndian, &header)
if err != nil {
return errors.Wrap(err, "reading header")
}

if header.Type != connector.ContractFunctionResponse {
return errors.Errorf("Invalid header type, expected %d, found %d", connector.ContractFunctionResponse, header.Type)
}
if header.Type == connector.EndResponse {
return nil
}

var order EntityOrder
err = binary.Read(r, binary.LittleEndian, &order)
if header.Type != connector.ContractFunctionResponse {
return errors.Errorf("Invalid header type, expected %d, found %d", connector.ContractFunctionResponse, header.Type)
}

*orders = append(*orders, order)
receivedOrders := make([]EntityOrder, 256)
err = binary.Read(r, binary.LittleEndian, receivedOrders)
if err != nil {
return errors.Wrap(err, "reading bytes from buffer")
}

orders := make([]EntityOrder, 0, 256)
for _, order := range receivedOrders {
if order == emptyEntityOrder {
continue
}
orders = append(orders, order)
}
*eo = orders

return nil
}
Expand Down
Loading

0 comments on commit 6277122

Please sign in to comment.