Skip to content

Commit 4856c98

Browse files
committed
Fixes brave/brave-browser#11046 - Shows ttotal amount to be received in a Gemini sale
1 parent 83c7844 commit 4856c98

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

components/brave_new_tab_ui/components/default/gemini/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ class Gemini extends React.PureComponent<Props, State> {
752752
const tradeLabel = isBuy ? 'geminiWidgetBuying' : 'geminiWidgetSelling'
753753
const quantity = this.formatCryptoBalance(currentTradeQuantityLive)
754754
const fee = this.formatCryptoBalance(currentTradeFee)
755-
const total = this.formatCryptoBalance(currentTradeTotalPrice, (isBuy ? 2 : 3))
756-
const totalAssetLabel = isBuy ? 'USD' : currentTradeAsset
755+
const total = this.formatCryptoBalance(currentTradeTotalPrice, 2)
757756
const totalLabel = isBuy ? 'geminiWidgetTotalPrice' : 'geminiWidgetTotalAmount'
758757

759758
return (
@@ -776,7 +775,7 @@ class Gemini extends React.PureComponent<Props, State> {
776775
</TradeInfoItem>
777776
<TradeInfoItem isLast={true}>
778777
<TradeItemLabel>{getLocale(totalLabel)}</TradeItemLabel>
779-
<TradeValue>{`${total} ${totalAssetLabel}`}</TradeValue>
778+
<TradeValue>{`${total} USD`}</TradeValue>
780779
</TradeInfoItem>
781780
</TradeInfoWrapper>
782781
<ActionsWrapper>

components/gemini/browser/gemini_service.cc

+29-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "base/files/file_enumerator.h"
1515
#include "base/files/file_util.h"
1616
#include "base/json/json_writer.h"
17+
#include "base/strings/string_number_conversions.h"
1718
#include "base/strings/utf_string_conversions.h"
1819
#include "base/task/post_task.h"
1920
#include "base/task_runner_util.h"
@@ -105,6 +106,29 @@ namespace {
105106
return encoded_payload;
106107
}
107108

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+
108132
} // namespace
109133

110134
GeminiService::GeminiService(content::BrowserContext* context)
@@ -286,7 +310,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
286310
const std::string& spend,
287311
GetOrderQuoteCallback callback) {
288312
auto internal_callback = base::BindOnce(&GeminiService::OnGetOrderQuote,
289-
base::Unretained(this), std::move(callback));
313+
base::Unretained(this), std::move(callback), side);
290314
std::string endpoint =
291315
std::string(api_path_get_quote) + "/" + side + "/" + symbol;
292316
std::string payload = GetEncodedRequestPayload(endpoint);
@@ -297,7 +321,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
297321
}
298322

299323
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,
301325
const std::map<std::string, std::string>& headers) {
302326
std::string fee;
303327
std::string quote_id;
@@ -311,6 +335,9 @@ void GeminiService::OnGetOrderQuote(GetOrderQuoteCallback callback,
311335
json_body, &quote_id, &quantity,
312336
&fee, &price, &total_price, &error);
313337
}
338+
if (side == "sell") {
339+
CalculateSaleAmount(quantity, price, fee, &total_price);
340+
}
314341
std::move(callback).Run(
315342
quote_id, quantity, fee, price, total_price, error);
316343
}

components/gemini/browser/gemini_service.h

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class GeminiService : public KeyedService {
123123
const int status, const std::string& body,
124124
const std::map<std::string, std::string>& headers);
125125
void OnGetOrderQuote(GetOrderQuoteCallback callback,
126+
const std::string& side,
126127
const int status, const std::string& body,
127128
const std::map<std::string, std::string>& headers);
128129
void OnOrderExecuted(ExecuteOrderCallback callback,

components/gemini/browser/gemini_service_browsertest.cc

+29-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
5858
"totalSpend": "100",
5959
"totalSpendCurrency": "USD"
6060
})");
61+
} else if (request_path == std::string(api_path_get_quote) + "/sell/batusd") {
62+
http_response->set_content(R"({
63+
"quoteId": 1328,
64+
"maxAgeMs": 60000,
65+
"pair": "BATUSD",
66+
"price": "0.25635",
67+
"priceCurrency": "USD",
68+
"side": "sell",
69+
"quantity": "20.00",
70+
"quantityCurrency": "BAT",
71+
"fee": "0.99",
72+
"feeCurrency": "USD",
73+
"depositFee": "0",
74+
"depositFeeCurrency": "BAT",
75+
"totalSpend": "20",
76+
"totalSpendCurrency": "BAT"
77+
})");
6178
} else if (request_path == api_path_account_balances) {
6279
http_response->set_content(R"(
6380
[
@@ -404,7 +421,7 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, RefreshTokenServerError) {
404421
WaitForRefreshAccessToken(false);
405422
}
406423

407-
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
424+
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteBuy) {
408425
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
409426
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
410427
auto* service = GetGeminiService();
@@ -415,6 +432,17 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
415432
WaitForGetOrderQuote("1328", "0.01505181", "2.9900309233", "6445.07", "100");
416433
}
417434

435+
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteSell) {
436+
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
437+
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
438+
auto* service = GetGeminiService();
439+
ASSERT_TRUE(service->GetOrderQuote("sell", "batusd", "20",
440+
base::BindOnce(
441+
&GeminiAPIBrowserTest::OnGetOrderQuote,
442+
base::Unretained(this))));
443+
WaitForGetOrderQuote("1328", "20.00", "0.99", "0.25635", "4.137000");
444+
}
445+
418446
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteUnauthorized) {
419447
ResetHTTPSServer(base::BindRepeating(&HandleRequestUnauthorized));
420448
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());

components/resources/brave_components_strings.grd

+2-2
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,8 @@
919919
<message name="IDS_GEMINI_WIDGET_BOUGHT" desc="">You bought</message>
920920
<message name="IDS_GEMINI_WIDGET_SOLD" desc="">You sold</message>
921921
<message name="IDS_GEMINI_WIDGET_UNIT_PRICE" desc="">Unit Price</message>
922-
<message name="IDS_GEMINI_WIDGET_TOTAL_PRICE" desc="">Total Price</message>
923-
<message name="IDS_GEMINI_WIDGET_TOTAL_AMOUNT" desc="">Total Amount</message>
922+
<message name="IDS_GEMINI_WIDGET_TOTAL_PRICE" desc="">Total Spent</message>
923+
<message name="IDS_GEMINI_WIDGET_TOTAL_AMOUNT" desc="">Total Received</message>
924924

925925
<!-- WebUI Brave Toolbar resources -->
926926
<message name="IDS_WALLETS_TITLE" desc="The toolbar title for the brave://wallets page">Crypto Wallets</message>

0 commit comments

Comments
 (0)