Skip to content

Commit

Permalink
examples: make price oracle asset check more robust
Browse files Browse the repository at this point in the history
Compare subject asset to supported asset using both string and byte
slice instead of just string.
  • Loading branch information
ffranr committed Oct 7, 2024
1 parent 967415b commit 66c0cae
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions docs/examples/basic-price-oracle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package main

import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
Expand Down Expand Up @@ -71,12 +73,31 @@ func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool {
return false
}

// In this example we'll only support a single asset.
assetIdStr := subjectAsset.GetAssetIdStr()
supportedAssetId := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" +
supportedAssetIdStr := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" +
"3ace193e9ce53b1a67"
supportedAssetIdBytes, err := hex.DecodeString(supportedAssetIdStr)
if err != nil {
fmt.Println("Error decoding supported asset hex string:", err)
return false
}

// Check the subject asset bytes if set.
subjectAssetIdBytes := subjectAsset.GetAssetId()
if len(subjectAssetIdBytes) > 0 {
logrus.Infof("Subject asset ID bytes populated: %v",
supportedAssetIdBytes)
return bytes.Equal(supportedAssetIdBytes, subjectAssetIdBytes)
}

subjectAssetIdStr := subjectAsset.GetAssetIdStr()
if len(subjectAssetIdStr) > 0 {
logrus.Infof("Subject asset ID str populated: %v",
supportedAssetIdStr)
return subjectAssetIdStr == supportedAssetIdStr
}

return assetIdStr == supportedAssetId
logrus.Infof("Subject asset ID not set")
return false
}

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

0 comments on commit 66c0cae

Please sign in to comment.