Skip to content

Commit 2d0b4a4

Browse files
committed
examples: make price oracle asset check more robust
Compare subject asset to supported asset using both string and byte slice instead of just string.
1 parent 967415b commit 2d0b4a4

File tree

1 file changed

+26
-4
lines changed
  • docs/examples/basic-price-oracle

1 file changed

+26
-4
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package main
77

88
import (
9+
"bytes"
910
"context"
1011
"crypto/ecdsa"
1112
"crypto/elliptic"
1213
"crypto/rand"
1314
"crypto/tls"
1415
"crypto/x509"
1516
"crypto/x509/pkix"
17+
"encoding/hex"
1618
"encoding/pem"
1719
"fmt"
1820
"io"
@@ -68,15 +70,35 @@ type RpcPriceOracleServer struct {
6870
func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool {
6971
// Ensure that the subject asset is set.
7072
if subjectAsset == nil {
73+
logrus.Info("Subject asset is not set (nil)")
7174
return false
7275
}
7376

74-
// In this example we'll only support a single asset.
75-
assetIdStr := subjectAsset.GetAssetIdStr()
76-
supportedAssetId := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" +
77+
supportedAssetIdStr := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" +
7778
"3ace193e9ce53b1a67"
79+
supportedAssetIdBytes, err := hex.DecodeString(supportedAssetIdStr)
80+
if err != nil {
81+
fmt.Println("Error decoding supported asset hex string:", err)
82+
return false
83+
}
84+
85+
// Check the subject asset bytes if set.
86+
subjectAssetIdBytes := subjectAsset.GetAssetId()
87+
if len(subjectAssetIdBytes) > 0 {
88+
logrus.Infof("Subject asset ID bytes populated: %x",
89+
supportedAssetIdBytes)
90+
return bytes.Equal(supportedAssetIdBytes, subjectAssetIdBytes)
91+
}
92+
93+
subjectAssetIdStr := subjectAsset.GetAssetIdStr()
94+
if len(subjectAssetIdStr) > 0 {
95+
logrus.Infof("Subject asset ID str populated: %s",
96+
supportedAssetIdStr)
97+
return subjectAssetIdStr == supportedAssetIdStr
98+
}
7899

79-
return assetIdStr == supportedAssetId
100+
logrus.Infof("Subject asset ID not set")
101+
return false
80102
}
81103

82104
// getRateTick returns a rate tick for a given transaction type and subject

0 commit comments

Comments
 (0)