Skip to content

Commit 1063afd

Browse files
committed
docs: add support for asset group key in price oracle
Extend the price oracle to support requests using an asset group key. Refactor by renaming two functions and extracting constants to make supported asset IDs and asset group keys easier to locate and modify.
1 parent 62cff13 commit 1063afd

File tree

1 file changed

+63
-9
lines changed
  • docs/examples/basic-price-oracle

1 file changed

+63
-9
lines changed

docs/examples/basic-price-oracle/main.go

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ import (
3434
const (
3535
// serviceListenAddress is the listening address of the service.
3636
serviceListenAddress = "localhost:8095"
37+
38+
// supportedAssetIdStr is the hex-encoded asset ID for which this price
39+
// oracle provides exchange rates.
40+
supportedAssetIdStr = "7b4336d33b019df9438e586f83c587ca00fa6560249" +
41+
"7b93ace193e9ce53b1a67"
42+
43+
// supportedGroupKeyStr is the hex-encoded asset group key for which
44+
// this price oracle provides exchange rates.
45+
supportedGroupKeyStr = "02875ce409b587a6656357639d099ad9eb08396d0d" +
46+
"fea8930a45e742c81d6fc782"
3747
)
3848

3949
// setupLogger sets up the logger to write logs to a file.
@@ -65,42 +75,86 @@ type RpcPriceOracleServer struct {
6575
oraclerpc.UnimplementedPriceOracleServer
6676
}
6777

68-
// isSupportedSubjectAsset returns true if the given subject asset is supported
69-
// by the price oracle, and false otherwise.
70-
func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool {
78+
// isSupportedAssetID returns true if the given asset ID is supported by the
79+
// price oracle, and false otherwise.
80+
func isSupportedAssetID(rpcAssetSpec *oraclerpc.AssetSpecifier) bool {
7181
// Ensure that the subject asset is set.
72-
if subjectAsset == nil {
82+
if rpcAssetSpec == nil {
7383
logrus.Info("Subject asset is not set (nil)")
7484
return false
7585
}
7686

77-
supportedAssetIdStr := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" +
78-
"3ace193e9ce53b1a67"
7987
supportedAssetIdBytes, err := hex.DecodeString(supportedAssetIdStr)
8088
if err != nil {
8189
fmt.Println("Error decoding supported asset hex string:", err)
8290
return false
8391
}
8492

8593
// Check the subject asset bytes if set.
86-
subjectAssetIdBytes := subjectAsset.GetAssetId()
94+
subjectAssetIdBytes := rpcAssetSpec.GetAssetId()
8795
if len(subjectAssetIdBytes) > 0 {
8896
logrus.Infof("Subject asset ID bytes populated: %x",
8997
supportedAssetIdBytes)
9098
return bytes.Equal(supportedAssetIdBytes, subjectAssetIdBytes)
9199
}
92100

93-
subjectAssetIdStr := subjectAsset.GetAssetIdStr()
101+
subjectAssetIdStr := rpcAssetSpec.GetAssetIdStr()
94102
if len(subjectAssetIdStr) > 0 {
95103
logrus.Infof("Subject asset ID str populated: %s",
96104
supportedAssetIdStr)
97105
return subjectAssetIdStr == supportedAssetIdStr
98106
}
99107

100108
logrus.Infof("Subject asset ID not set")
109+
110+
return false
111+
}
112+
113+
// isSupportedAssetGroupKey returns true if the given asset group key is
114+
// supported by the price oracle, and false otherwise.
115+
func isSupportedAssetGroupKey(
116+
rpcAssetSpec *oraclerpc.AssetSpecifier) bool {
117+
118+
// Ensure that the subject asset is not nil.
119+
if rpcAssetSpec == nil {
120+
logrus.Info("Subject asset is not set (nil)")
121+
return false
122+
}
123+
124+
supportedGroupKeyBytes, err := hex.DecodeString(supportedGroupKeyStr)
125+
if err != nil {
126+
fmt.Println("Error decoding supported asset group key hex "+
127+
"string:", err)
128+
return false
129+
}
130+
131+
// Check the subject asset group key bytes if set.
132+
subjectGroupKeyBytes := rpcAssetSpec.GetGroupKey()
133+
if len(subjectGroupKeyBytes) > 0 {
134+
logrus.Infof("Subject asset group key bytes populated: %x",
135+
supportedGroupKeyBytes)
136+
return bytes.Equal(supportedGroupKeyBytes, subjectGroupKeyBytes)
137+
}
138+
139+
subjectGroupKeyStr := rpcAssetSpec.GetGroupKeyStr()
140+
if len(subjectGroupKeyStr) > 0 {
141+
logrus.Infof("Subject asset group key str populated: %s",
142+
supportedGroupKeyStr)
143+
return subjectGroupKeyStr == supportedGroupKeyStr
144+
}
145+
146+
logrus.Infof("Subject asset group key not set")
147+
101148
return false
102149
}
103150

151+
// isSupportedAssetID returns true if the given subject asset is
152+
// supported by the price oracle, and false otherwise.
153+
func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool {
154+
return isSupportedAssetID(subjectAsset) ||
155+
isSupportedAssetGroupKey(subjectAsset)
156+
}
157+
104158
// getPurchaseRate returns the buy (purchase) rate for the asset. The unit of
105159
// the rate is the number of TAP asset units per BTC.
106160
//
@@ -265,7 +319,7 @@ func (p *RpcPriceOracleServer) QueryAssetRates(_ context.Context,
265319

266320
// Ensure that the subject asset is supported.
267321
if !isSupportedSubjectAsset(req.SubjectAsset) {
268-
logrus.Infof("Unsupported subject asset ID str: %v\n",
322+
logrus.Infof("Unsupported subject asset: %v\n",
269323
req.SubjectAsset)
270324

271325
return &oraclerpc.QueryAssetRatesResponse{

0 commit comments

Comments
 (0)