Skip to content

Commit fbf0287

Browse files
committed
Implement ipo contract bids endpoint.
1 parent 215ef7d commit fbf0287

File tree

10 files changed

+1362
-802
lines changed

10 files changed

+1362
-802
lines changed

foundation/rpc_server/asset_converter.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package rpc
22

33
import (
4+
"fmt"
5+
46
"github.com/pkg/errors"
57
"github.com/qubic/go-node-connector/types"
68
"github.com/qubic/qubic-http/protobuff"
@@ -78,3 +80,30 @@ func convertAssetPossession(source types.AssetPossession) (*protobuff.AssetPosse
7880

7981
return &assetPossession, nil
8082
}
83+
84+
func convertContractIpo(source types.ContractIpo) (*protobuff.IpoBidData, error) {
85+
86+
ipoBidData := protobuff.IpoBidData{
87+
ContractIndex: source.ContractIndex,
88+
TickNumber: source.TickNumber,
89+
Bids: make(map[int32]*protobuff.IpoBid),
90+
}
91+
92+
for index := 0; index < types.NumberOfComputors; index++ {
93+
if source.Prices[index] == 0 {
94+
continue
95+
}
96+
97+
identity, err := new(types.Identity).FromPubKey(source.PubKeys[index], false)
98+
if err != nil {
99+
return nil, fmt.Errorf("failed to get identity for bid: %w", err)
100+
}
101+
102+
ipoBidData.Bids[int32(index)] = &protobuff.IpoBid{
103+
Identity: identity.String(),
104+
Amount: source.Prices[index],
105+
}
106+
}
107+
108+
return &ipoBidData, nil
109+
}

foundation/rpc_server/asset_converter_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,49 @@ func TestAssetConverter_convertAssetPossession(t *testing.T) {
109109
}, converted)
110110

111111
}
112+
113+
func TestAssetConverter_convertContractIpo(t *testing.T) {
114+
115+
id := types.Identity("TESTIOGXQKYYZEQXOXFSWWAJNYLCDBWFAPNBLNBUZFHDVFMYPJZXGMEEJEGI")
116+
pubKey, err := id.ToPubKey(false)
117+
assert.NoError(t, err)
118+
119+
source := types.ContractIpo{
120+
ContractIndex: 5,
121+
TickNumber: 100,
122+
}
123+
124+
// set one bid with a known identity and price
125+
source.PubKeys[0] = pubKey
126+
source.Prices[0] = 1000
127+
128+
// set another bid at a different index
129+
source.PubKeys[10] = pubKey
130+
source.Prices[10] = 2000
131+
132+
// index 1 has zero price, should be skipped
133+
source.PubKeys[1] = pubKey
134+
source.Prices[1] = 0
135+
136+
converted, err := convertContractIpo(source)
137+
assert.NoError(t, err)
138+
139+
assert.Equal(t, uint32(5), converted.ContractIndex)
140+
assert.Equal(t, uint32(100), converted.TickNumber)
141+
142+
// only 2 bids should be present (zero-price skipped)
143+
assert.Len(t, converted.Bids, 2)
144+
145+
assert.Equal(t, &protobuff.IpoBid{
146+
Identity: "TESTIOGXQKYYZEQXOXFSWWAJNYLCDBWFAPNBLNBUZFHDVFMYPJZXGMEEJEGI",
147+
Amount: 1000,
148+
}, converted.Bids[0])
149+
150+
assert.Equal(t, &protobuff.IpoBid{
151+
Identity: "TESTIOGXQKYYZEQXOXFSWWAJNYLCDBWFAPNBLNBUZFHDVFMYPJZXGMEEJEGI",
152+
Amount: 2000,
153+
}, converted.Bids[10])
154+
155+
// zero-price bid should not be in the map
156+
assert.Nil(t, converted.Bids[1])
157+
}

foundation/rpc_server/rpc_server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,28 @@ func (s *Server) GetActiveIpos(ctx context.Context, _ *emptypb.Empty) (*protobuf
699699
return &protobuff.GetActiveIposResponse{Ipos: ipos}, nil
700700
}
701701

702+
func (s *Server) GetContractIpoBids(ctx context.Context, request *protobuff.GetContractIpoBidsRequest) (*protobuff.GetContractIpoBidsResponse, error) {
703+
704+
client, err := s.qPool.Get()
705+
if err != nil {
706+
return nil, status.Errorf(codes.Internal, "getting connection from pool: %v", err)
707+
}
708+
709+
nodeResponse, err := client.GetContractIpo(ctx, request.ContractIndex)
710+
if err != nil {
711+
s.qPool.Close(client)
712+
return nil, status.Errorf(codes.Internal, "getting contract ipo data: %v", err)
713+
}
714+
s.qPool.Put(client)
715+
716+
ipoBidData, err := convertContractIpo(nodeResponse)
717+
if err != nil {
718+
return nil, status.Errorf(codes.Internal, "converting node response: %v", err)
719+
}
720+
721+
return &protobuff.GetContractIpoBidsResponse{BidData: ipoBidData}, nil
722+
}
723+
702724
func (s *Server) Start() error {
703725
srv := grpc.NewServer(
704726
grpc.MaxRecvMsgSize(600*1024*1024),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/google/gnostic v0.7.1
88
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3
99
github.com/pkg/errors v0.9.1
10-
github.com/qubic/go-node-connector v0.15.0
10+
github.com/qubic/go-node-connector v0.16.1-0.20260222094152-8ad2afbd46c5
1111
github.com/qubic/go-schnorrq v1.0.1
1212
github.com/stretchr/testify v1.11.1
1313
google.golang.org/genproto/googleapis/api v0.0.0-20251103181224-f26f9409b101

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
4545
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4646
github.com/qubic/go-node-connector v0.15.0 h1:fbd+wgEBtKMzmlsRdbYAKAGnDxew4HLjAPj4whGKWjY=
4747
github.com/qubic/go-node-connector v0.15.0/go.mod h1:GOQGJ6IKhm3CU62bfLJJbBerjYmt6NNhDKzxC1i7HhU=
48+
github.com/qubic/go-node-connector v0.16.1-0.20260222094152-8ad2afbd46c5 h1:PWCRRJPCJxqpqvLAYfKyngGNWeWKibWKiLb6h65/P6k=
49+
github.com/qubic/go-node-connector v0.16.1-0.20260222094152-8ad2afbd46c5/go.mod h1:GOQGJ6IKhm3CU62bfLJJbBerjYmt6NNhDKzxC1i7HhU=
4850
github.com/qubic/go-schnorrq v1.0.1 h1:F0R/BQVf+O7Bp57NGJmc3uXlqsaIzerg/1bmU4jMLLE=
4951
github.com/qubic/go-schnorrq v1.0.1/go.mod h1:j2qw/zHiyjH9GAScAAETWpZk6iELbjYnzIg7CQwc5wM=
5052
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=

0 commit comments

Comments
 (0)