11// 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
2+ // QueryAssetRates RPC method. The server listens on localhost:8095 and returns
3+ // the asset rates for a given transaction type, subject asset, and payment
54// asset.
65package main
76
@@ -103,10 +102,10 @@ func isSupportedSubjectAsset(subjectAsset *oraclerpc.AssetSpecifier) bool {
103102 return false
104103}
105104
106- // getRateTick returns a rate tick for a given transaction type and subject
107- // asset max amount.
108- func getRateTick (transactionType oraclerpc.TransactionType ,
109- subjectAssetMaxAmount uint64 ) (oraclerpc.RateTick , error ) {
105+ // getAssetRates returns the asset rates for a given transaction type and
106+ // subject asset max amount.
107+ func getAssetRates (transactionType oraclerpc.TransactionType ,
108+ subjectAssetMaxAmount uint64 ) (oraclerpc.AssetRates , error ) {
110109
111110 // Determine the rate based on the transaction type.
112111 var subjectAssetRate rfqmath.BigIntFixedPoint
@@ -142,61 +141,61 @@ func getRateTick(transactionType oraclerpc.TransactionType,
142141 subjectAssetRate ,
143142 )
144143 if err != nil {
145- return oraclerpc.RateTick {}, err
144+ return oraclerpc.AssetRates {}, err
146145 }
147146
148147 // Marshal payment asset rate to RPC format.
149148 rpcPaymentAssetToBtcRate , err := oraclerpc .MarshalBigIntFixedPoint (
150149 rfqmsg .MilliSatPerBtc ,
151150 )
152151 if err != nil {
153- return oraclerpc.RateTick {}, err
152+ return oraclerpc.AssetRates {}, err
154153 }
155154
156- return oraclerpc.RateTick {
155+ return oraclerpc.AssetRates {
157156 SubjectAssetRate : rpcSubjectAssetToBtcRate ,
158157 PaymentAssetRate : rpcPaymentAssetToBtcRate ,
159158 ExpiryTimestamp : uint64 (expiry ),
160159 }, nil
161160}
162161
163- // QueryRateTick queries the rate tick for a given transaction type, subject
164- // asset, and payment asset. The rate tick is the exchange rate between the
165- // subject asset and the payment asset .
162+ // QueryAssetRates queries the asset rates for a given transaction type, subject
163+ // asset, and payment asset. An asset rate is the number of asset units per
164+ // BTC .
166165//
167166// Example use case:
168167//
169168// Alice is trying to pay an invoice by spending an asset. Alice therefore
170169// requests that Bob (her asset channel counterparty) purchase the asset from
171170// her. Bob's payment, in BTC, will pay the invoice.
172171//
173- // Alice requests a bid quote from Bob. Her request includes a rate tick
174- // hint (ask). Alice get the rate tick hint by calling this endpoint. She sets:
172+ // Alice requests a bid quote from Bob. Her request includes an asset rates hint
173+ // (ask). Alice obtains the asset rates hint by calling this endpoint. She sets:
175174// - `SubjectAsset` to the asset she is trying to sell.
176175// - `SubjectAssetMaxAmount` to the max channel asset outbound.
177176// - `PaymentAsset` to BTC.
178177// - `TransactionType` to SALE.
179- // - `RateTickHint ` to nil.
178+ // - `AssetRateHint ` to nil.
180179//
181- // Bob calls this endpoint to get the bid quote rate tick that he will send as a
182- // response to Alice's request. He sets:
180+ // Bob calls this endpoint to get the bid quote asset rates that he will send as
181+ // a response to Alice's request. He sets:
183182// - `SubjectAsset` to the asset that Alice is trying to sell.
184183// - `SubjectAssetMaxAmount` to the value given in Alice's quote request.
185184// - `PaymentAsset` to BTC.
186185// - `TransactionType` to PURCHASE.
187- // - `RateTickHint ` to the value given in Alice's quote request.
188- func (p * RpcPriceOracleServer ) QueryRateTick (_ context.Context ,
189- req * oraclerpc.QueryRateTickRequest ) (
190- * oraclerpc.QueryRateTickResponse , error ) {
186+ // - `AssetRateHint ` to the value given in Alice's quote request.
187+ func (p * RpcPriceOracleServer ) QueryAssetRates (_ context.Context ,
188+ req * oraclerpc.QueryAssetRatesRequest ) (
189+ * oraclerpc.QueryAssetRatesResponse , error ) {
191190
192191 // Ensure that the payment asset is BTC. We only support BTC as the
193192 // payment asset in this example.
194193 if ! oraclerpc .IsAssetBtc (req .PaymentAsset ) {
195194 logrus .Infof ("Payment asset is not BTC: %v" , req .PaymentAsset )
196195
197- return & oraclerpc.QueryRateTickResponse {
198- Result : & oraclerpc.QueryRateTickResponse_Error {
199- Error : & oraclerpc.QueryRateTickErrResponse {
196+ return & oraclerpc.QueryAssetRatesResponse {
197+ Result : & oraclerpc.QueryAssetRatesResponse_Error {
198+ Error : & oraclerpc.QueryAssetRatesErrResponse {
200199 Message : "unsupported payment asset, " +
201200 "only BTC is supported" ,
202201 },
@@ -215,55 +214,55 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,
215214 logrus .Infof ("Unsupported subject asset ID str: %v\n " ,
216215 req .SubjectAsset )
217216
218- return & oraclerpc.QueryRateTickResponse {
219- Result : & oraclerpc.QueryRateTickResponse_Error {
220- Error : & oraclerpc.QueryRateTickErrResponse {
217+ return & oraclerpc.QueryAssetRatesResponse {
218+ Result : & oraclerpc.QueryAssetRatesResponse_Error {
219+ Error : & oraclerpc.QueryAssetRatesErrResponse {
221220 Message : "unsupported subject asset" ,
222221 },
223222 },
224223 }, nil
225224 }
226225
227- // Determine which rate tick to return.
226+ // Determine which asset rate to return.
228227 var (
229- rateTick oraclerpc.RateTick
230- err error
228+ assetRates oraclerpc.AssetRates
229+ err error
231230 )
232231
233- if req .RateTickHint != nil {
234- // If a rate tick hint is provided, return it as the rate tick.
235- // In doing so, we effectively accept the rate tick proposed by
236- // our peer.
232+ if req .AssetRatesHint != nil {
233+ // If the asset rates hint is provided, return it as the asset
234+ // rate. In doing so, we effectively accept the asset rates
235+ // proposed by our peer.
237236 logrus .Info ("Suggested asset to BTC rate provided, " +
238237 "returning rate as accepted rate" )
239238
240- rateTick = oraclerpc.RateTick {
241- SubjectAssetRate : req .RateTickHint .SubjectAssetRate ,
242- PaymentAssetRate : req .RateTickHint .PaymentAssetRate ,
243- ExpiryTimestamp : req .RateTickHint .ExpiryTimestamp ,
239+ assetRates = oraclerpc.AssetRates {
240+ SubjectAssetRate : req .AssetRatesHint .SubjectAssetRate ,
241+ PaymentAssetRate : req .AssetRatesHint .PaymentAssetRate ,
242+ ExpiryTimestamp : req .AssetRatesHint .ExpiryTimestamp ,
244243 }
245244 } else {
246- // If a rate tick hint is not provided, fetch a rate tick from
247- // our internal system.
245+ // If an asset rates hint is not provided, fetch asset rates
246+ // from our internal system.
248247 logrus .Info ("Suggested asset to BTC rate not provided, " +
249248 "querying internal system for rate" )
250249
251- rateTick , err = getRateTick (
250+ assetRates , err = getAssetRates (
252251 req .TransactionType , req .SubjectAssetMaxAmount ,
253252 )
254253 if err != nil {
255254 return nil , err
256255 }
257256 }
258257
259- logrus .Infof ("QueryRateTick returning rate (subject_asset_rate=%v, " +
260- "payment_asset_rate=%v)" , rateTick .SubjectAssetRate ,
261- rateTick .PaymentAssetRate )
258+ logrus .Infof ("QueryAssetRates returning rates (subject_asset_rate=%v, " +
259+ "payment_asset_rate=%v)" , assetRates .SubjectAssetRate ,
260+ assetRates .PaymentAssetRate )
262261
263- return & oraclerpc.QueryRateTickResponse {
264- Result : & oraclerpc.QueryRateTickResponse_Success {
265- Success : & oraclerpc.QueryRateTickSuccessResponse {
266- RateTick : & rateTick ,
262+ return & oraclerpc.QueryAssetRatesResponse {
263+ Result : & oraclerpc.QueryAssetRatesResponse_Ok {
264+ Ok : & oraclerpc.QueryAssetRatesOkResponse {
265+ AssetRates : & assetRates ,
267266 },
268267 },
269268 }, nil
0 commit comments