QueryAssetRates
QueryAssetRates retrieves the exchange rate between a tap asset and BTC for a specified transaction type, subject asset, and payment asset. The asset rate represents the number of tap asset units per BTC.
Source: priceoraclerpc/price_oracle.proto
gRPC
rpc QueryAssetRates (QueryAssetRatesRequest) returns (QueryAssetRatesResponse);
REST
HTTP Method | Path |
---|---|
GET | /v1/taproot-assets/rfq/priceoracle/assetrates |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10029'
const MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
const TLS_PATH = 'TAPROOT-ASSETS_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync('priceoraclerpc/price_oracle.proto', loaderOptions);
const priceoraclerpc = grpc.loadPackageDefinition(packageDefinition).priceoraclerpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new priceoraclerpc.PriceOracle(GRPC_HOST, creds);
let request = {
transaction_type: <TransactionType>,
subject_asset: <AssetSpecifier>,
subject_asset_max_amount: <uint64>,
payment_asset: <AssetSpecifier>,
payment_asset_max_amount: <uint64>,
asset_rates_hint: <AssetRates>,
};
client.queryAssetRates(request, function(err, response) {
console.log(response);
});
// Console output:
// {
// "ok": <QueryAssetRatesOkResponse>,
// "error": <QueryAssetRatesErrResponse>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the priceoraclerpc/price_oracle.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import price_oracle_pb2 as priceoraclerpc, price_oracle_pb2_grpc as price_oraclestub
GRPC_HOST = 'localhost:10029'
MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
TLS_PATH = 'TAPROOT-ASSETS_DIR/tls.cert'
# create macaroon credentials
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = price_oraclestub.PriceOracleStub(channel)
request = priceoraclerpc.QueryAssetRatesRequest(
transaction_type=<TransactionType>,
subject_asset=<AssetSpecifier>,
subject_asset_max_amount=<uint64>,
payment_asset=<AssetSpecifier>,
payment_asset_max_amount=<uint64>,
asset_rates_hint=<AssetRates>,
)
response = stub.QueryAssetRates(request)
print(response)
# {
# "ok": <QueryAssetRatesOkResponse>,
# "error": <QueryAssetRatesErrResponse>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8089'
const MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
let options = {
url: `https://${REST_HOST}/v1/taproot-assets/rfq/priceoracle/assetrates`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
}
request.get(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "ok": <object>, // <QueryAssetRatesOkResponse>
// "error": <object>, // <QueryAssetRatesErrResponse>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8089'
MACAROON_PATH = 'TAPROOT-ASSETS_DIR/regtest/taproot-assets.macaroon'
TLS_PATH = 'TAPROOT-ASSETS_DIR/tls.cert'
url = f'https://{REST_HOST}/v1/taproot-assets/rfq/priceoracle/assetrates'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
r = requests.get(url, headers=headers, verify=TLS_PATH)
print(r.json())
# {
# "ok": <QueryAssetRatesOkResponse>,
# "error": <QueryAssetRatesErrResponse>,
# }
# There is no CLI command for this RPC
Messages
priceoraclerpc.QueryAssetRatesRequest
Source: priceoraclerpc/price_oracle.proto
Field | gRPC Type | REST Type | REST Placement |
---|---|---|---|
transaction_type | TransactionType | object | query |
subject_asset | AssetSpecifier | object | query |
subject_asset_max_amount | uint64 | string | query |
payment_asset | AssetSpecifier | object | query |
payment_asset_max_amount | uint64 | string | query |
asset_rates_hint | AssetRates | object | query |
priceoraclerpc.QueryAssetRatesResponse
Source: priceoraclerpc/price_oracle.proto
Field | gRPC Type | REST Type |
---|---|---|
ok | QueryAssetRatesOkResponse | object |
error | QueryAssetRatesErrResponse | object |
Nested Messages
priceoraclerpc.AssetRates
Field | gRPC Type | REST Type |
---|---|---|
subjectAssetRate | FixedPoint | object |
paymentAssetRate | FixedPoint | object |
expiry_timestamp | uint64 | string |
priceoraclerpc.AssetSpecifier
Field | gRPC Type | REST Type |
---|---|---|
asset_id | bytes | string |
asset_id_str | string | string |
group_key | bytes | string |
group_key_str | string | string |
priceoraclerpc.FixedPoint
Field | gRPC Type | REST Type |
---|---|---|
coefficient | string | string |
scale | uint32 | integer |
priceoraclerpc.QueryAssetRatesErrResponse
Field | gRPC Type | REST Type |
---|---|---|
message | string | string |
code | uint32 | integer |
priceoraclerpc.QueryAssetRatesOkResponse
Field | gRPC Type | REST Type |
---|---|---|
asset_rates | AssetRates | object |
Enums
priceoraclerpc.TransactionType
Name | Number |
---|---|
PURCHASE | 0 |
SALE | 1 |