14
14
#include " base/files/file_enumerator.h"
15
15
#include " base/files/file_util.h"
16
16
#include " base/json/json_writer.h"
17
+ #include " base/strings/string_number_conversions.h"
17
18
#include " base/strings/utf_string_conversions.h"
18
19
#include " base/task/post_task.h"
19
20
#include " base/task_runner_util.h"
@@ -105,6 +106,29 @@ namespace {
105
106
return encoded_payload;
106
107
}
107
108
109
+ void CalculateSaleAmount (const std::string quantity,
110
+ const std::string price,
111
+ const std::string fee,
112
+ std::string* total_price) {
113
+ if (quantity.empty () || price.empty () || fee.empty ()) {
114
+ return ;
115
+ }
116
+
117
+ double parsed_quantity;
118
+ double parsed_price;
119
+ double parsed_fee;
120
+
121
+ if (!base::StringToDouble (quantity, &parsed_quantity) ||
122
+ !base::StringToDouble (price, &parsed_price) ||
123
+ !base::StringToDouble (fee, &parsed_fee)) {
124
+ return ;
125
+ }
126
+
127
+ // Sale amount is (quantity * price) - fee
128
+ double sale_amount = (parsed_quantity * parsed_price) - parsed_fee;
129
+ *total_price = std::to_string (sale_amount);
130
+ }
131
+
108
132
} // namespace
109
133
110
134
GeminiService::GeminiService (content::BrowserContext* context)
@@ -286,7 +310,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
286
310
const std::string& spend,
287
311
GetOrderQuoteCallback callback) {
288
312
auto internal_callback = base::BindOnce (&GeminiService::OnGetOrderQuote,
289
- base::Unretained (this ), std::move (callback));
313
+ base::Unretained (this ), std::move (callback), side );
290
314
std::string endpoint =
291
315
std::string (api_path_get_quote) + " /" + side + " /" + symbol;
292
316
std::string payload = GetEncodedRequestPayload (endpoint);
@@ -297,7 +321,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
297
321
}
298
322
299
323
void GeminiService::OnGetOrderQuote (GetOrderQuoteCallback callback,
300
- const int status, const std::string& body,
324
+ const std::string& side, const int status, const std::string& body,
301
325
const std::map<std::string, std::string>& headers) {
302
326
std::string fee;
303
327
std::string quote_id;
@@ -311,6 +335,9 @@ void GeminiService::OnGetOrderQuote(GetOrderQuoteCallback callback,
311
335
json_body, "e_id, &quantity,
312
336
&fee, &price, &total_price, &error);
313
337
}
338
+ if (side == " sell" ) {
339
+ CalculateSaleAmount (quantity, price, fee, &total_price);
340
+ }
314
341
std::move (callback).Run (
315
342
quote_id, quantity, fee, price, total_price, error);
316
343
}
0 commit comments