Skip to content

Commit ef73f75

Browse files
committed
rpcutils: move price oracle gRPC marshal functionality from taprpc
Move price oracle related gRPC marshalling functionality from the taprpc package to the rpcutils package as preparation for future modularization.
1 parent bdfbc76 commit ef73f75

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

itest/oracle_harness.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/lightninglabs/taproot-assets/asset"
1414
"github.com/lightninglabs/taproot-assets/rfqmath"
1515
"github.com/lightninglabs/taproot-assets/rfqmsg"
16+
"github.com/lightninglabs/taproot-assets/rpcutils"
1617
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
1718
"github.com/lightningnetwork/lnd/cert"
1819
"github.com/stretchr/testify/require"
@@ -128,15 +129,15 @@ func (o *oracleHarness) getAssetRates(specifier asset.Specifier,
128129
}
129130

130131
// Marshal subject asset rate to RPC format.
131-
rpcSubjectAssetToBtcRate, err := oraclerpc.MarshalBigIntFixedPoint(
132+
rpcSubjectAssetToBtcRate, err := rpcutils.MarshalBigIntFixedPoint(
132133
subjectAssetRate,
133134
)
134135
if err != nil {
135136
return oraclerpc.AssetRates{}, err
136137
}
137138

138139
// Marshal payment asset rate to RPC format.
139-
rpcPaymentAssetToBtcRate, err := oraclerpc.MarshalBigIntFixedPoint(
140+
rpcPaymentAssetToBtcRate, err := rpcutils.MarshalBigIntFixedPoint(
140141
rfqmsg.MilliSatPerBtc,
141142
)
142143
if err != nil {
@@ -182,7 +183,7 @@ func (o *oracleHarness) QueryAssetRates(_ context.Context,
182183

183184
// Ensure that the payment asset is BTC. We only support BTC as the
184185
// payment asset in this example.
185-
if !oraclerpc.IsAssetBtc(req.PaymentAsset) {
186+
if !rpcutils.IsAssetBtc(req.PaymentAsset) {
186187
log.Infof("Payment asset is not BTC: %v", req.PaymentAsset)
187188

188189
return &oraclerpc.QueryAssetRatesResponse{

rfq/oracle.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightninglabs/taproot-assets/asset"
1212
"github.com/lightninglabs/taproot-assets/fn"
1313
"github.com/lightninglabs/taproot-assets/rfqmsg"
14+
"github.com/lightninglabs/taproot-assets/rpcutils"
1415
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
1516
"github.com/lightningnetwork/lnd/lnwire"
1617
"google.golang.org/grpc"
@@ -203,7 +204,7 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
203204

204205
// Construct the RPC asset rates hint.
205206
rpcAssetRatesHint, err := fn.MapOptionZ(
206-
assetRateHint, oraclerpc.MarshalAssetRates,
207+
assetRateHint, rpcutils.MarshalAssetRates,
207208
).Unpack()
208209
if err != nil {
209210
return nil, err
@@ -242,7 +243,7 @@ func (r *RpcPriceOracle) QueryAskPrice(ctx context.Context,
242243
}
243244

244245
// Unmarshal the subject asset to BTC rate.
245-
rate, err := oraclerpc.UnmarshalFixedPoint(
246+
rate, err := rpcutils.UnmarshalFixedPoint(
246247
result.Ok.AssetRates.SubjectAssetRate,
247248
)
248249
if err != nil {
@@ -299,7 +300,7 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context,
299300

300301
// Construct the RPC asset rates hint.
301302
rpcAssetRatesHint, err := fn.MapOptionZ(
302-
assetRateHint, oraclerpc.MarshalAssetRates,
303+
assetRateHint, rpcutils.MarshalAssetRates,
303304
).Unpack()
304305
if err != nil {
305306
return nil, err
@@ -333,7 +334,7 @@ func (r *RpcPriceOracle) QueryBidPrice(ctx context.Context,
333334
}
334335

335336
// Unmarshal the subject asset to BTC rate.
336-
rate, err := oraclerpc.UnmarshalFixedPoint(
337+
rate, err := rpcutils.UnmarshalFixedPoint(
337338
result.Ok.AssetRates.SubjectAssetRate,
338339
)
339340
if err != nil {

rfq/oracle_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/lightninglabs/taproot-assets/internal/test"
1414
"github.com/lightninglabs/taproot-assets/rfqmath"
1515
"github.com/lightninglabs/taproot-assets/rfqmsg"
16+
"github.com/lightninglabs/taproot-assets/rpcutils"
1617
"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
1718
"github.com/lightningnetwork/lnd/lnwire"
1819
"github.com/stretchr/testify/require"
@@ -41,7 +42,7 @@ func (p *mockRpcPriceOracleServer) QueryAssetRates(_ context.Context,
4142
subjectAssetRate := rfqmath.NewBigIntFixedPoint(testAssetRate, 3)
4243

4344
// Marshal the subject asset rate to a fixed point.
44-
subjectAssetFp, err := priceoraclerpc.MarshalBigIntFixedPoint(
45+
subjectAssetFp, err := rpcutils.MarshalBigIntFixedPoint(
4546
subjectAssetRate,
4647
)
4748
if err != nil {

taprpc/priceoraclerpc/marshal.go renamed to rpcutils/price_oracle_marshal.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package priceoraclerpc
1+
package rpcutils
22

33
import (
44
"bytes"
@@ -8,12 +8,13 @@ import (
88

99
"github.com/lightninglabs/taproot-assets/rfqmath"
1010
"github.com/lightninglabs/taproot-assets/rfqmsg"
11+
"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
1112
lfn "github.com/lightningnetwork/lnd/fn/v2"
1213
)
1314

1415
// IsAssetBtc is a helper function that returns true if the given asset
1516
// specifier represents BTC, and false otherwise.
16-
func IsAssetBtc(assetSpecifier *AssetSpecifier) bool {
17+
func IsAssetBtc(assetSpecifier *priceoraclerpc.AssetSpecifier) bool {
1718
// An unset asset specifier does not represent BTC.
1819
if assetSpecifier == nil {
1920
return false
@@ -49,40 +50,46 @@ func IsAssetBtc(assetSpecifier *AssetSpecifier) bool {
4950
// MarshalAssetRates converts an asset rate to an RPC AssetRates.
5051
// The OK result has a pointer type so that it is nil if there is an error.
5152
// NOTE: The payment asset is assumed to be BTC.
52-
func MarshalAssetRates(assetRate rfqmsg.AssetRate) lfn.Result[*AssetRates] {
53+
func MarshalAssetRates(
54+
assetRate rfqmsg.AssetRate) lfn.Result[*priceoraclerpc.AssetRates] {
55+
5356
// Marshal the subject asset rate.
5457
subjectAssetRate, err := MarshalBigIntFixedPoint(assetRate.Rate)
5558
if err != nil {
56-
return lfn.Err[*AssetRates](err)
59+
return lfn.Err[*priceoraclerpc.AssetRates](err)
5760
}
5861

5962
// Marshal the payment asset rate. For now, we only support BTC as the
6063
// payment asset.
6164
paymentAssetRate, err := MarshalBigIntFixedPoint(rfqmsg.MilliSatPerBtc)
6265
if err != nil {
63-
return lfn.Err[*AssetRates](err)
66+
return lfn.Err[*priceoraclerpc.AssetRates](err)
6467
}
6568

6669
// Compute an expiry unix timestamp from the given asset rate expiry.
6770
expiryTimestamp := uint64(assetRate.Expiry.Unix())
6871

69-
return lfn.Ok[*AssetRates](&AssetRates{
72+
return lfn.Ok[*priceoraclerpc.AssetRates](&priceoraclerpc.AssetRates{
7073
SubjectAssetRate: subjectAssetRate,
7174
PaymentAssetRate: paymentAssetRate,
7275
ExpiryTimestamp: expiryTimestamp,
7376
})
7477
}
7578

7679
// MarshalBigIntFixedPoint converts a BigIntFixedPoint to an RPC FixedPoint.
77-
func MarshalBigIntFixedPoint(fp rfqmath.BigIntFixedPoint) (*FixedPoint, error) {
78-
return &FixedPoint{
80+
func MarshalBigIntFixedPoint(
81+
fp rfqmath.BigIntFixedPoint) (*priceoraclerpc.FixedPoint, error) {
82+
83+
return &priceoraclerpc.FixedPoint{
7984
Coefficient: fp.Coefficient.String(),
8085
Scale: uint32(fp.Scale),
8186
}, nil
8287
}
8388

8489
// UnmarshalFixedPoint converts an RPC FixedPoint to a BigIntFixedPoint.
85-
func UnmarshalFixedPoint(fp *FixedPoint) (*rfqmath.BigIntFixedPoint, error) {
90+
func UnmarshalFixedPoint(
91+
fp *priceoraclerpc.FixedPoint) (*rfqmath.BigIntFixedPoint, error) {
92+
8693
// Return an error is the scale component of the fixed point is greater
8794
// than the max value of uint8.
8895
if fp.Scale > 255 {

taprpc/priceoraclerpc/marshal_test.go renamed to rpcutils/price_oracle_marshal_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
package priceoraclerpc
1+
package rpcutils
22

33
import (
44
"encoding/hex"
55
"testing"
66

7+
"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
78
"github.com/stretchr/testify/require"
89
)
910

1011
// isAssetBtcTC is a test case for the IsAssetBtc function.
1112
type isAssetBtcTC struct {
1213
testName string
1314

14-
assetSpecifier *AssetSpecifier
15+
assetSpecifier *priceoraclerpc.AssetSpecifier
1516
expected bool
1617
}
1718

@@ -32,13 +33,13 @@ func TestIsAssetBtc(t *testing.T) {
3233
},
3334
{
3435
testName: "empty asset specifier",
35-
assetSpecifier: &AssetSpecifier{},
36+
assetSpecifier: &priceoraclerpc.AssetSpecifier{},
3637
expected: false,
3738
},
3839
{
3940
testName: "asset specifier with zero asset ID bytes",
40-
assetSpecifier: &AssetSpecifier{
41-
Id: &AssetSpecifier_AssetId{
41+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
42+
Id: &priceoraclerpc.AssetSpecifier_AssetId{
4243
AssetId: zeroAssetId[:],
4344
},
4445
},
@@ -47,53 +48,53 @@ func TestIsAssetBtc(t *testing.T) {
4748
{
4849
testName: "asset specifier with incorrect length " +
4950
"zero asset ID bytes",
50-
assetSpecifier: &AssetSpecifier{
51-
Id: &AssetSpecifier_AssetId{
51+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
52+
Id: &priceoraclerpc.AssetSpecifier_AssetId{
5253
AssetId: []byte{0, 0, 0},
5354
},
5455
},
5556
expected: false,
5657
},
5758
{
5859
testName: "asset specifier with empty asset ID bytes",
59-
assetSpecifier: &AssetSpecifier{
60-
Id: &AssetSpecifier_AssetId{
60+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
61+
Id: &priceoraclerpc.AssetSpecifier_AssetId{
6162
AssetId: []byte{},
6263
},
6364
},
6465
expected: false,
6566
},
6667
{
6768
testName: "asset specifier with zero asset ID string",
68-
assetSpecifier: &AssetSpecifier{
69-
Id: &AssetSpecifier_AssetIdStr{
69+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
70+
Id: &priceoraclerpc.AssetSpecifier_AssetIdStr{
7071
AssetIdStr: zeroAssetHexStr,
7172
},
7273
},
7374
expected: true,
7475
},
7576
{
7677
testName: "asset specifier with empty asset ID string",
77-
assetSpecifier: &AssetSpecifier{
78-
Id: &AssetSpecifier_AssetIdStr{
78+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
79+
Id: &priceoraclerpc.AssetSpecifier_AssetIdStr{
7980
AssetIdStr: "",
8081
},
8182
},
8283
expected: false,
8384
},
8485
{
8586
testName: "asset specifier with set group key bytes",
86-
assetSpecifier: &AssetSpecifier{
87-
Id: &AssetSpecifier_GroupKey{
87+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
88+
Id: &priceoraclerpc.AssetSpecifier_GroupKey{
8889
GroupKey: []byte{0, 0, 0},
8990
},
9091
},
9192
expected: false,
9293
},
9394
{
9495
testName: "asset specifier with set group key string",
95-
assetSpecifier: &AssetSpecifier{
96-
Id: &AssetSpecifier_GroupKeyStr{
96+
assetSpecifier: &priceoraclerpc.AssetSpecifier{
97+
Id: &priceoraclerpc.AssetSpecifier_GroupKeyStr{
9798
GroupKeyStr: "test-group-key",
9899
},
99100
},

0 commit comments

Comments
 (0)