prices method
This endpoint can be used to fetch the indicative prices of available off-chain assets in exchange for a Stellar asset and vice versa.
It accepts following parameters:
sellAsset
The asset you want to sell, using the Asset Identification Format.
sellAmount
The amount of sell_asset the client would exchange for each of the buy_assets.
sellDeliveryMethod
Optional, one of the name values specified by the sell_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user is delivering an off-chain asset to the anchor but is not strictly required.
buyDeliveryMethod
Optional, one of the name values specified by the buy_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user intends to receive an off-chain asset from the anchor but is not strictly required.
countryCode
Optional, The ISO 3166-2 or ISO-3166-1 alpha-2 code of the user's current address. Should be provided if there are two or more country codes available for the desired asset in GET /info.
It also accepts an optional jwtToken
token obtained before with SEP-0010.
Implementation
Future<SEP38PricesResponse> prices(
{required String sellAsset,
required String sellAmount,
String? sellDeliveryMethod,
String? buyDeliveryMethod,
String? countryCode,
String? jwtToken}) async {
Uri requestURI = Util.appendEndpointToUrl(_serviceAddress, 'prices');
Map<String, String> queryParameters = {
'sell_asset': sellAsset,
'sell_amount': sellAmount
};
if (sellDeliveryMethod != null) {
queryParameters['sell_delivery_method'] = sellDeliveryMethod;
}
if (buyDeliveryMethod != null) {
queryParameters['buy_delivery_method'] = buyDeliveryMethod;
}
if (countryCode != null) {
queryParameters['country_code'] = countryCode;
}
requestURI = requestURI.replace(queryParameters: queryParameters);
Map<String, String> headers = {...RequestBuilder.headers};
if (jwtToken != null) {
headers["Authorization"] = "Bearer " + jwtToken;
}
headers.putIfAbsent("Content-Type", () => "application/json");
SEP38PricesResponse result =
await httpClient.get(requestURI, headers: headers).then((response) {
switch (response.statusCode) {
case 200:
return SEP38PricesResponse.fromJson(json.decode(response.body));
case 400:
throw SEP38BadRequest(errorFromResponseBody(response.body));
default:
throw new SEP38UnknownResponse(response.statusCode, response.body);
}
}).catchError((onError) {
throw onError;
});
return result;
}