Skip to content

Commit

Permalink
rfq: actually set subject asset in QueryRateTick
Browse files Browse the repository at this point in the history
Fixes the problem that because of the missing allocation, the subject
asset ID was always set to an empty slice.
  • Loading branch information
guggero committed Aug 12, 2024
1 parent a8d8b5a commit 0edbd88
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
22 changes: 14 additions & 8 deletions rfq/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
return nil, fmt.Errorf("asset ID is nil")
}

// Construct query request.
var subjectAssetId []byte
copy(subjectAssetId, assetId[:])
var (
subjectAssetId = make([]byte, 32)
paymentAssetId = make([]byte, 32)
)

paymentAssetId := make([]byte, 32)
// The payment asset ID is BTC, so we leave it at all zeroes. We only
// set the subject asset ID.
copy(subjectAssetId, assetId[:])

// Construct the RPC rate tick hint.
var rateTickHint *oraclerpc.RateTick
Expand Down Expand Up @@ -303,11 +306,14 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context, assetId *asset.ID,
return nil, fmt.Errorf("asset ID is nil")
}

// Construct query request.
var subjectAssetId []byte
copy(subjectAssetId, assetId[:])
var (
subjectAssetId = make([]byte, 32)
paymentAssetId = make([]byte, 32)
)

paymentAssetId := make([]byte, 32)
// The payment asset ID is BTC, so we leave it at all zeroes. We only
// set the subject asset ID.
copy(subjectAssetId, assetId[:])

req := &oraclerpc.QueryRateTickRequest{
TransactionType: oraclerpc.TransactionType_PURCHASE,
Expand Down
24 changes: 23 additions & 1 deletion rfq/oracle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rfq

import (
"bytes"
"context"
"fmt"
"net"
Expand Down Expand Up @@ -43,6 +44,27 @@ func (p *mockRpcPriceOracleServer) QueryRateTick(_ context.Context,
ExpiryTimestamp: uint64(expiry),
}

var zeroAssetID [32]byte
if req.SubjectAsset == nil {
return nil, fmt.Errorf("subject asset must be specified")
}
if len(req.SubjectAsset.GetAssetId()) != 32 {
return nil, fmt.Errorf("invalid subject asset ID length")
}
if bytes.Equal(req.SubjectAsset.GetAssetId(), zeroAssetID[:]) {
return nil, fmt.Errorf("subject asset ID must NOT be all zero")
}

if req.PaymentAsset == nil {
return nil, fmt.Errorf("payment asset must be specified")
}
if len(req.PaymentAsset.GetAssetId()) != 32 {
return nil, fmt.Errorf("invalid payment asset ID length")
}
if !bytes.Equal(req.PaymentAsset.GetAssetId(), zeroAssetID[:]) {
return nil, fmt.Errorf("payment asset ID must be all zero")
}

// If a rate tick hint is provided, return it as the rate tick.
if req.RateTickHint != nil {
rateTick.Rate = req.RateTickHint.Rate
Expand Down Expand Up @@ -95,7 +117,7 @@ func runQueryAskPriceTest(t *testing.T, tc *testCaseQueryAskPrice) {
defer backendService.Stop()

// Wait for the server to start.
time.Sleep(2 * time.Second)
time.Sleep(200 * time.Millisecond)

// Create a new RPC price oracle client and connect to the mock service.
serviceAddr := fmt.Sprintf("rfqrpc://%s", testServiceAddress)
Expand Down

0 comments on commit 0edbd88

Please sign in to comment.