getXmrMarketPrices method
Retrieves the current market prices for XMR (Monero) from the Haveno gRPC service.
This method sends a MarketPricesRequest to the Haveno gRPC server and expects a MarketPricesReply containing the list of MarketPriceInfo data. Each MarketPriceInfo contains pricing information about XMR in various markets.
If the Haveno client is not connected to the server, a DaemonNotConnectedException is thrown, indicating that the gRPC connection is required to fetch the data.
Example:
try {
final prices = await priceClient.getXmrMarketPrices();
if (prices != null) {
for (final price in prices) {
print('Market: ${price.market}, Price: ${price.price}');
}
}
} catch (e) {
print('Error fetching prices: $e');
}
Returns:
- A
Future
that resolves to aList<MarketPriceInfo>?
containing the current market prices for XMR, ornull
if an error occurs.
Throws:
- DaemonNotConnectedException if the client is not connected to the gRPC server.
- Catches
GrpcError
to handle gRPC-specific errors.
This method also leverages the GrpcErrorHandler
mixin to provide custom error
handling for gRPC failures such as network issues or invalid responses.
Implementation
Future<List<MarketPriceInfo>?> getXmrMarketPrices() async {
// Ensure that the Haveno client is connected to the gRPC server.
if (!havenoChannel.isConnected) {
throw DaemonNotConnectedException();
}
// Variable to hold the gRPC response.
MarketPricesReply getMarketPricesReply;
try {
// Make the gRPC call to retrieve market prices.
getMarketPricesReply = await havenoChannel.priceClient
!.getMarketPrices(MarketPricesRequest());
// Return the list of market prices from the gRPC response.
return getMarketPricesReply.marketPrice;
} on GrpcError catch (e) {
// Handle any gRPC errors using the GrpcErrorHandler mixin.
handleGrpcError(e);
}
// Return null if an error occurred during the gRPC request.
return null;
}