Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shows total amount to be received in a Gemini sell order #6307

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,7 @@ class Gemini extends React.PureComponent<Props, State> {
const tradeLabel = isBuy ? 'geminiWidgetBuying' : 'geminiWidgetSelling'
const quantity = this.formatCryptoBalance(currentTradeQuantityLive)
const fee = this.formatCryptoBalance(currentTradeFee)
const total = this.formatCryptoBalance(currentTradeTotalPrice, (isBuy ? 2 : 3))
const totalAssetLabel = isBuy ? 'USD' : currentTradeAsset
const total = this.formatCryptoBalance(currentTradeTotalPrice, 2)
const totalLabel = isBuy ? 'geminiWidgetTotalPrice' : 'geminiWidgetTotalAmount'

return (
Expand All @@ -776,7 +775,7 @@ class Gemini extends React.PureComponent<Props, State> {
</TradeInfoItem>
<TradeInfoItem isLast={true}>
<TradeItemLabel>{getLocale(totalLabel)}</TradeItemLabel>
<TradeValue>{`${total} ${totalAssetLabel}`}</TradeValue>
<TradeValue>{`${total} USD`}</TradeValue>
</TradeInfoItem>
</TradeInfoWrapper>
<ActionsWrapper>
Expand Down
31 changes: 29 additions & 2 deletions components/gemini/browser/gemini_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/task_runner_util.h"
Expand Down Expand Up @@ -105,6 +106,29 @@ namespace {
return encoded_payload;
}

void CalculateSaleAmount(const std::string quantity,
const std::string price,
const std::string fee,
std::string* total_price) {
if (quantity.empty() || price.empty() || fee.empty()) {
return;
}

double parsed_quantity;
double parsed_price;
double parsed_fee;

if (!base::StringToDouble(quantity, &parsed_quantity) ||
!base::StringToDouble(price, &parsed_price) ||
!base::StringToDouble(fee, &parsed_fee)) {
return;
}

// Sale amount is (quantity * price) - fee
double sale_amount = (parsed_quantity * parsed_price) - parsed_fee;
*total_price = std::to_string(sale_amount);
}

} // namespace

GeminiService::GeminiService(content::BrowserContext* context)
Expand Down Expand Up @@ -286,7 +310,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
const std::string& spend,
GetOrderQuoteCallback callback) {
auto internal_callback = base::BindOnce(&GeminiService::OnGetOrderQuote,
base::Unretained(this), std::move(callback));
base::Unretained(this), std::move(callback), side);
std::string endpoint =
std::string(api_path_get_quote) + "/" + side + "/" + symbol;
std::string payload = GetEncodedRequestPayload(endpoint);
Expand All @@ -297,7 +321,7 @@ bool GeminiService::GetOrderQuote(const std::string& side,
}

void GeminiService::OnGetOrderQuote(GetOrderQuoteCallback callback,
const int status, const std::string& body,
const std::string& side, const int status, const std::string& body,
const std::map<std::string, std::string>& headers) {
std::string fee;
std::string quote_id;
Expand All @@ -311,6 +335,9 @@ void GeminiService::OnGetOrderQuote(GetOrderQuoteCallback callback,
json_body, &quote_id, &quantity,
&fee, &price, &total_price, &error);
}
if (side == "sell") {
CalculateSaleAmount(quantity, price, fee, &total_price);
}
std::move(callback).Run(
quote_id, quantity, fee, price, total_price, error);
}
Expand Down
1 change: 1 addition & 0 deletions components/gemini/browser/gemini_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class GeminiService : public KeyedService {
const int status, const std::string& body,
const std::map<std::string, std::string>& headers);
void OnGetOrderQuote(GetOrderQuoteCallback callback,
const std::string& side,
const int status, const std::string& body,
const std::map<std::string, std::string>& headers);
void OnOrderExecuted(ExecuteOrderCallback callback,
Expand Down
30 changes: 29 additions & 1 deletion components/gemini/browser/gemini_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
"totalSpend": "100",
"totalSpendCurrency": "USD"
})");
} else if (request_path == std::string(api_path_get_quote) + "/sell/batusd") {
http_response->set_content(R"({
"quoteId": 1328,
"maxAgeMs": 60000,
"pair": "BATUSD",
"price": "0.25635",
"priceCurrency": "USD",
"side": "sell",
"quantity": "20.00",
"quantityCurrency": "BAT",
"fee": "0.99",
"feeCurrency": "USD",
"depositFee": "0",
"depositFeeCurrency": "BAT",
"totalSpend": "20",
"totalSpendCurrency": "BAT"
})");
} else if (request_path == api_path_account_balances) {
http_response->set_content(R"(
[
Expand Down Expand Up @@ -404,7 +421,7 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, RefreshTokenServerError) {
WaitForRefreshAccessToken(false);
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteBuy) {
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
Expand All @@ -415,6 +432,17 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
WaitForGetOrderQuote("1328", "0.01505181", "2.9900309233", "6445.07", "100");
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteSell) {
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
ASSERT_TRUE(service->GetOrderQuote("sell", "batusd", "20",
base::BindOnce(
&GeminiAPIBrowserTest::OnGetOrderQuote,
base::Unretained(this))));
WaitForGetOrderQuote("1328", "20.00", "0.99", "0.25635", "4.137000");
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteUnauthorized) {
ResetHTTPSServer(base::BindRepeating(&HandleRequestUnauthorized));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
Expand Down
4 changes: 2 additions & 2 deletions components/resources/brave_components_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -919,8 +919,8 @@
<message name="IDS_GEMINI_WIDGET_BOUGHT" desc="">You bought</message>
<message name="IDS_GEMINI_WIDGET_SOLD" desc="">You sold</message>
<message name="IDS_GEMINI_WIDGET_UNIT_PRICE" desc="">Unit Price</message>
<message name="IDS_GEMINI_WIDGET_TOTAL_PRICE" desc="">Total Price</message>
<message name="IDS_GEMINI_WIDGET_TOTAL_AMOUNT" desc="">Total Amount</message>
<message name="IDS_GEMINI_WIDGET_TOTAL_PRICE" desc="">Total Spent</message>
<message name="IDS_GEMINI_WIDGET_TOTAL_AMOUNT" desc="">Total Received</message>

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