@@ -15,11 +15,15 @@ import (
1515 "crypto/x509/pkix"
1616 "encoding/pem"
1717 "fmt"
18+ "io"
1819 "log"
1920 "math/big"
2021 "net"
22+ "os"
2123 "time"
2224
25+ "github.com/sirupsen/logrus"
26+
2327 oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
2428 "google.golang.org/grpc"
2529 "google.golang.org/grpc/credentials"
@@ -30,6 +34,30 @@ const (
3034 serviceListenAddress = "localhost:8095"
3135)
3236
37+ // setupLogger sets up the logger to write logs to a file.
38+ func setupLogger () {
39+ // Create a log file.
40+ flags := os .O_CREATE | os .O_WRONLY | os .O_APPEND
41+ file , err := os .OpenFile ("basic-price-oracle-example.log" , flags , 0666 )
42+ if err != nil {
43+ logrus .Fatalf ("Failed to open log file: %v" , err )
44+ }
45+
46+ // Create a multi-writer to write to both stdout and the file.
47+ multiWriter := io .MultiWriter (os .Stdout , file )
48+
49+ // Set the output of logrus to the multi-writer.
50+ logrus .SetOutput (multiWriter )
51+
52+ // Set the log level (optional).
53+ logrus .SetLevel (logrus .DebugLevel )
54+
55+ // Set the log format (optional).
56+ logrus .SetFormatter (& logrus.TextFormatter {
57+ FullTimestamp : true ,
58+ })
59+ }
60+
3361// RpcPriceOracleServer is a basic example RPC price oracle server.
3462type RpcPriceOracleServer struct {
3563 oraclerpc.UnimplementedPriceOracleServer
@@ -115,6 +143,8 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
115143 // Ensure that the payment asset is BTC. We only support BTC as the
116144 // payment asset in this example.
117145 if ! oraclerpc .IsAssetBtc (req .PaymentAsset ) {
146+ logrus .Infof ("Payment asset is not BTC: %v" , req .PaymentAsset )
147+
118148 return & oraclerpc.QueryRateTickResponse {
119149 Result : & oraclerpc.QueryRateTickResponse_Error {
120150 Error : & oraclerpc.QueryRateTickErrResponse {
@@ -127,11 +157,15 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
127157
128158 // Ensure that the subject asset is set.
129159 if req .SubjectAsset == nil {
160+ logrus .Info ("Subject asset is not set" )
130161 return nil , fmt .Errorf ("subject asset is not set" )
131162 }
132163
133164 // Ensure that the subject asset is supported.
134165 if ! isSupportedSubjectAsset (req .SubjectAsset ) {
166+ logrus .Infof ("Unsupported subject asset ID str: %v\n " ,
167+ req .SubjectAsset )
168+
135169 return & oraclerpc.QueryRateTickResponse {
136170 Result : & oraclerpc.QueryRateTickResponse_Error {
137171 Error : & oraclerpc.QueryRateTickErrResponse {
@@ -148,16 +182,24 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
148182 // If a rate tick hint is provided, return it as the rate tick.
149183 // In doing so, we effectively accept the rate tick proposed by
150184 // our peer.
185+ logrus .Info ("Suggested asset to BTC rate provided, " +
186+ "returning rate as accepted rate" )
187+
151188 rateTick .Rate = req .RateTickHint .Rate
152189 rateTick .ExpiryTimestamp = req .RateTickHint .ExpiryTimestamp
153190 } else {
154191 // If a rate tick hint is not provided, fetch a rate tick from
155192 // our internal system.
193+ logrus .Info ("Suggested asset to BTC rate not provided, " +
194+ "querying internal system for rate" )
195+
156196 rateTick = getRateTick (
157197 req .TransactionType , req .SubjectAssetMaxAmount ,
158198 )
159199 }
160200
201+ logrus .Infof ("QueryRateTick returning rate: %v" , rateTick .Rate )
202+
161203 return & oraclerpc.QueryRateTickResponse {
162204 Result : & oraclerpc.QueryRateTickResponse_Success {
163205 Success : & oraclerpc.QueryRateTickSuccessResponse {
@@ -171,7 +213,8 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
171213// shut down.
172214func startService (grpcServer * grpc.Server ) error {
173215 serviceAddr := fmt .Sprintf ("rfqrpc://%s" , serviceListenAddress )
174- println ("Starting RPC price oracle service at address: " , serviceAddr )
216+ logrus .Infof ("Starting RPC price oracle service at address: %s\n " ,
217+ serviceAddr )
175218
176219 server := RpcPriceOracleServer {}
177220 oraclerpc .RegisterPriceOracleServer (grpcServer , & server )
@@ -234,6 +277,8 @@ func generateSelfSignedCert() (tls.Certificate, error) {
234277}
235278
236279func main () {
280+ setupLogger ()
281+
237282 // Start the mock RPC price oracle service.
238283 //
239284 // Generate self-signed certificate. This allows us to use TLS for the
0 commit comments