|
| 1 | +// This example demonstrates a basic RPC price oracle server that implements the |
| 2 | +// QueryRateTick RPC method. The server listens on localhost:8095 and returns a |
| 3 | +// rate tick for a given transaction type, subject asset, and payment asset. The |
| 4 | +// rate tick is the exchange rate between the subject asset and the payment |
| 5 | +// asset. |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "net" |
| 12 | + "time" |
| 13 | + |
| 14 | + oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc" |
| 15 | + "google.golang.org/grpc" |
| 16 | + "google.golang.org/grpc/credentials/insecure" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // serviceListenAddress is the listening address of the service. |
| 21 | + serviceListenAddress = "localhost:8095" |
| 22 | +) |
| 23 | + |
| 24 | +// RpcPriceOracleServer is a basic example RPC price oracle server. |
| 25 | +type RpcPriceOracleServer struct { |
| 26 | + oraclerpc.UnimplementedPriceOracleServer |
| 27 | +} |
| 28 | + |
| 29 | +// isSupportedSubjectAsset returns true if the given subject asset is supported |
| 30 | +// by the price oracle, and false otherwise. |
| 31 | +func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool { |
| 32 | + // Ensure that the subject asset is set. |
| 33 | + if subjectAsset == nil { |
| 34 | + return false |
| 35 | + } |
| 36 | + |
| 37 | + // In this example we'll only support a single asset. |
| 38 | + assetIdStr := subjectAsset.GetAssetIdStr() |
| 39 | + supportedAssetId := "7b4336d33b019df9438e586f83c587ca00fa65602497b9" + |
| 40 | + "3ace193e9ce53b1a67" |
| 41 | + |
| 42 | + return assetIdStr == supportedAssetId |
| 43 | +} |
| 44 | + |
| 45 | +// getRateTick returns a rate tick for a given transaction type and subject |
| 46 | +// asset max amount. |
| 47 | +func getRateTick(transactionType oraclerpc.TransactionType, |
| 48 | + subjectAssetMaxAmount uint64) oraclerpc.RateTick { |
| 49 | + |
| 50 | + // Determine the rate based on the transaction type. |
| 51 | + var rate uint64 |
| 52 | + if transactionType == oraclerpc.TransactionType_PURCHASE { |
| 53 | + // The rate for a purchase transaction is 42,000 asset units per |
| 54 | + // mSAT. |
| 55 | + rate = 42_000 |
| 56 | + } else { |
| 57 | + // The rate for a sale transaction is 40,000 asset units per |
| 58 | + // mSAT. |
| 59 | + rate = 40_000 |
| 60 | + } |
| 61 | + |
| 62 | + // Set the rate expiry to 5 minutes by default. |
| 63 | + expiry := time.Now().Add(5 * time.Minute).Unix() |
| 64 | + |
| 65 | + // If the subject asset max amount is greater than 100,000, set the rate |
| 66 | + // expiry to 1 minute. |
| 67 | + if subjectAssetMaxAmount > 100_000 { |
| 68 | + expiry = time.Now().Add(1 * time.Minute).Unix() |
| 69 | + } |
| 70 | + |
| 71 | + return oraclerpc.RateTick{ |
| 72 | + Rate: rate, |
| 73 | + ExpiryTimestamp: uint64(expiry), |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// QueryRateTick queries the rate tick for a given transaction type, subject |
| 78 | +// asset, and payment asset. The rate tick is the exchange rate between the |
| 79 | +// subject asset and the payment asset. |
| 80 | +// |
| 81 | +// Example use case: |
| 82 | +// |
| 83 | +// Alice is trying to pay an invoice by spending an asset. Alice therefore |
| 84 | +// requests that Bob (her asset channel counterparty) purchase the asset from |
| 85 | +// her. Bob's payment, in BTC, will pay the invoice. |
| 86 | +// |
| 87 | +// Alice requests a bid quote from Bob. Her request includes a rate tick |
| 88 | +// hint (ask). Alice get the rate tick hint by calling this endpoint. She sets: |
| 89 | +// - `SubjectAsset` to the asset she is trying to sell. |
| 90 | +// - `SubjectAssetMaxAmount` to the max channel asset outbound. |
| 91 | +// - `PaymentAsset` to BTC. |
| 92 | +// - `TransactionType` to SALE. |
| 93 | +// - `RateTickHint` to nil. |
| 94 | +// |
| 95 | +// Bob calls this endpoint to get the bid quote rate tick that he will send as a |
| 96 | +// response to Alice's request. He sets: |
| 97 | +// - `SubjectAsset` to the asset that Alice is trying to sell. |
| 98 | +// - `SubjectAssetMaxAmount` to the value given in Alice's quote request. |
| 99 | +// - `PaymentAsset` to BTC. |
| 100 | +// - `TransactionType` to PURCHASE. |
| 101 | +// - `RateTickHint` to the value given in Alice's quote request. |
| 102 | +func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context, |
| 103 | + req *oraclerpc.QueryRateTickRequest) ( |
| 104 | + *oraclerpc.QueryRateTickResponse, error) { |
| 105 | + |
| 106 | + // Ensure that the payment asset is BTC. We only support BTC as the |
| 107 | + // payment asset in this example. |
| 108 | + if !oraclerpc.IsAssetBtc(req.PaymentAsset) { |
| 109 | + return &oraclerpc.QueryRateTickResponse{ |
| 110 | + Result: &oraclerpc.QueryRateTickResponse_Error{ |
| 111 | + Error: &oraclerpc.QueryRateTickErrResponse{ |
| 112 | + Message: "unsupported payment asset, " + |
| 113 | + "only BTC is supported", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, nil |
| 117 | + } |
| 118 | + |
| 119 | + // Ensure that the subject asset is set. |
| 120 | + if req.SubjectAsset == nil { |
| 121 | + return nil, fmt.Errorf("subject asset is not set") |
| 122 | + } |
| 123 | + |
| 124 | + // Ensure that the subject asset is supported. |
| 125 | + if !isSupportedSubjectAsset(req.SubjectAsset) { |
| 126 | + return &oraclerpc.QueryRateTickResponse{ |
| 127 | + Result: &oraclerpc.QueryRateTickResponse_Error{ |
| 128 | + Error: &oraclerpc.QueryRateTickErrResponse{ |
| 129 | + Message: "unsupported subject asset", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, nil |
| 133 | + } |
| 134 | + |
| 135 | + // Determine which rate tick to return. |
| 136 | + var rateTick oraclerpc.RateTick |
| 137 | + |
| 138 | + if req.RateTickHint != nil { |
| 139 | + // If a rate tick hint is provided, return it as the rate tick. |
| 140 | + // In doing so, we effectively accept the rate tick proposed by |
| 141 | + // our peer. |
| 142 | + rateTick.Rate = req.RateTickHint.Rate |
| 143 | + rateTick.ExpiryTimestamp = req.RateTickHint.ExpiryTimestamp |
| 144 | + } else { |
| 145 | + // If a rate tick hint is not provided, fetch a rate tick from |
| 146 | + // our internal system. |
| 147 | + rateTick = getRateTick( |
| 148 | + req.TransactionType, req.SubjectAssetMaxAmount, |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + return &oraclerpc.QueryRateTickResponse{ |
| 153 | + Result: &oraclerpc.QueryRateTickResponse_Success{ |
| 154 | + Success: &oraclerpc.QueryRateTickSuccessResponse{ |
| 155 | + RateTick: &rateTick, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, nil |
| 159 | +} |
| 160 | + |
| 161 | +// startService starts the given RPC server and blocks until the server is |
| 162 | +// shut down. |
| 163 | +func startService(grpcServer *grpc.Server) error { |
| 164 | + serviceAddr := fmt.Sprintf("rfqrpc://%s", serviceListenAddress) |
| 165 | + println("Starting RPC price oracle service at address: ", serviceAddr) |
| 166 | + |
| 167 | + server := RpcPriceOracleServer{} |
| 168 | + oraclerpc.RegisterPriceOracleServer(grpcServer, &server) |
| 169 | + grpcListener, err := net.Listen("tcp", serviceListenAddress) |
| 170 | + if err != nil { |
| 171 | + return fmt.Errorf("RPC server unable to listen on %s", |
| 172 | + serviceListenAddress) |
| 173 | + } |
| 174 | + return grpcServer.Serve(grpcListener) |
| 175 | +} |
| 176 | + |
| 177 | +func main() { |
| 178 | + // Start the mock RPC price oracle service. |
| 179 | + serverOpts := []grpc.ServerOption{ |
| 180 | + grpc.Creds(insecure.NewCredentials()), |
| 181 | + } |
| 182 | + backendService := grpc.NewServer(serverOpts...) |
| 183 | + _ = startService(backendService) |
| 184 | + backendService.Stop() |
| 185 | +} |
0 commit comments