price method
This endpoint can be used to fetch the indicative price for a given asset pair.
See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#get-price
The client must provide either sellAmount
or buyAmount
, but not both.
Parameters:
context
The context for what this quote will be used for. Must be one of 'sep6' or 'sep31'.
sellAsset
The asset the client would like to sell. Ex. stellar:USDC:G..., iso4217:ARS
buyAsset
The asset the client would like to exchange for sellAsset
.
sellAmount
optional, the amount of sellAsset
the client would like to exchange for buyAsset
.
buyAmount
optional, the amount of buyAsset
the client would like to exchange for sellAsset
.
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<SEP38PriceResponse> price(
{required String context,
required String sellAsset,
required String buyAsset,
String? sellAmount,
String? buyAmount,
String? sellDeliveryMethod,
String? buyDeliveryMethod,
String? countryCode,
String? jwtToken}) async {
Uri requestURI = Util.appendEndpointToUrl(_serviceAddress, 'price');
Map<String, String> queryParameters = {
'sell_asset': sellAsset,
'buy_asset': buyAsset,
'context': context,
};
if ((sellAmount != null && buyAmount != null) ||
(sellAmount == null && buyAmount == null)) {
throw ArgumentError(
'The caller must provide either [sellAmount] or [buyAmount], but not both.');
}
if (sellAmount != null) {
queryParameters['sell_amount'] = sellAmount;
}
if (buyAmount != null) {
queryParameters['buy_amount'] = buyAmount;
}
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");
SEP38PriceResponse result =
await httpClient.get(requestURI, headers: headers).then((response) {
switch (response.statusCode) {
case 200:
return SEP38PriceResponse.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;
}