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

Adds total price/amount line item to trade preview (Gemini) #6094

Merged
merged 1 commit into from
Jul 16, 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
3 changes: 2 additions & 1 deletion browser/extensions/api/gemini_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ GeminiGetOrderQuoteFunction::Run() {
void GeminiGetOrderQuoteFunction::OnOrderQuoteResult(
const std::string& quote_id, const std::string& quantity,
const std::string& fee, const std::string& price,
const std::string& error) {
const std::string& total_price, const std::string& error) {
auto quote = std::make_unique<base::Value>(base::Value::Type::DICTIONARY);
quote->SetStringKey("id", quote_id);
quote->SetStringKey("quantity", quantity);
quote->SetStringKey("fee", fee);
quote->SetStringKey("price", price);
quote->SetStringKey("totalPrice", total_price);
Respond(TwoArguments(
std::move(quote), std::make_unique<base::Value>(error)));
}
Expand Down
1 change: 1 addition & 0 deletions browser/extensions/api/gemini_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class GeminiGetOrderQuoteFunction :
const std::string& quantity,
const std::string& fee,
const std::string& price,
const std::string& total_price,
const std::string& error);

ResponseAction Run() override;
Expand Down
4 changes: 3 additions & 1 deletion browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "geminiWidgetBought", IDS_GEMINI_WIDGET_BOUGHT },
{ "geminiWidgetSold", IDS_GEMINI_WIDGET_SOLD },
{ "geminiWidgetFee", IDS_BINANCE_WIDGET_FEE },
{ "geminiWidgetPrice", IDS_GEMINI_WIDGET_PRICE }
{ "geminiWidgetUnitPrice", IDS_GEMINI_WIDGET_UNIT_PRICE },
{ "geminiWidgetTotalPrice", IDS_GEMINI_WIDGET_TOTAL_PRICE },
{ "geminiWidgetTotalAmount", IDS_GEMINI_WIDGET_TOTAL_AMOUNT },
}
}, {
std::string("wallet"), {
Expand Down
6 changes: 5 additions & 1 deletion common/extensions/api/gemini.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@
},
"price": {
"type": "string",
"description": "order price"
"description": "order unit price"
},
"totalPrice": {
"type": "string",
"description": "order total price"
}
}
},
Expand Down
28 changes: 21 additions & 7 deletions components/brave_new_tab_ui/components/default/gemini/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ interface State {
currentTradeId: string
currentTradeFee: string
currentTradePrice: string
currentTradeTotalPrice: string
currentTradeQuantityLive: string
currentTradeExpiryTime: number
}
Expand Down Expand Up @@ -157,6 +158,7 @@ class Gemini extends React.PureComponent<Props, State> {
currentTradeId: '',
currentTradeFee: '',
currentTradePrice: '',
currentTradeTotalPrice: '',
currentTradeQuantityLive: '',
currentTradeExpiryTime: 60
}
Expand Down Expand Up @@ -318,12 +320,12 @@ class Gemini extends React.PureComponent<Props, State> {
)
}

formatCryptoBalance = (balance: string) => {
formatCryptoBalance = (balance: string, precision: number = 3) => {
if (!balance) {
return '0'
}

return parseFloat(balance).toFixed(3)
return parseFloat(balance).toFixed(precision)
}

getAccountUSDValue = () => {
Expand Down Expand Up @@ -572,6 +574,7 @@ class Gemini extends React.PureComponent<Props, State> {
currentTradeId: '',
currentTradeFee: '',
currentTradePrice: '',
currentTradeTotalPrice: '',
currentTradeQuantity: '',
currentTradeExpiryTime: 60
})
Expand All @@ -587,6 +590,7 @@ class Gemini extends React.PureComponent<Props, State> {
currentTradeId: '',
currentTradeFee: '',
currentTradePrice: '',
currentTradeTotalPrice: '',
currentTradeQuantity: '',
currentTradeExpiryTime: 60,
currentTradeQuantityLive: '',
Expand Down Expand Up @@ -648,7 +652,7 @@ class Gemini extends React.PureComponent<Props, State> {
}

chrome.gemini.getOrderQuote(currentTradeMode, `${currentTradeAsset}usd`, currentTradeQuantity, (quote: any, error: string) => {
if (!quote.id || !quote.quantity || !quote.fee || !quote.price) {
if (!quote.id || !quote.quantity || !quote.fee || !quote.price || !quote.totalPrice) {
if (error) {
this.setState({
tradeFailed: true,
Expand All @@ -666,6 +670,7 @@ class Gemini extends React.PureComponent<Props, State> {
currentTradeId: quote.id,
currentTradeFee: quote.fee,
currentTradePrice: quote.price,
currentTradeTotalPrice: quote.totalPrice,
currentTradeQuantityLive: quote.quantity,
showTradePreview: true
})
Expand Down Expand Up @@ -752,11 +757,16 @@ class Gemini extends React.PureComponent<Props, State> {
currentTradeQuantityLive,
currentTradeMode,
currentTradeExpiryTime,
currentTradePrice
currentTradePrice,
currentTradeTotalPrice
} = this.state
const tradeLabel = currentTradeMode === 'buy' ? 'geminiWidgetBuying' : 'geminiWidgetSelling'
const isBuy = currentTradeMode === 'buy'
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 totalLabel = isBuy ? 'geminiWidgetTotalPrice' : 'geminiWidgetTotalAmount'

return (
<InvalidWrapper>
Expand All @@ -769,13 +779,17 @@ class Gemini extends React.PureComponent<Props, State> {
<TradeValue>{`${quantity} ${currentTradeAsset}`}</TradeValue>
</TradeInfoItem>
<TradeInfoItem>
<TradeItemLabel>{getLocale('geminiWidgetPrice')}</TradeItemLabel>
<TradeItemLabel>{getLocale('geminiWidgetUnitPrice')}</TradeItemLabel>
<TradeValue>{`${currentTradePrice} USD/${currentTradeAsset}`}</TradeValue>
</TradeInfoItem>
<TradeInfoItem isLast={true}>
<TradeInfoItem>
<TradeItemLabel>{getLocale('geminiWidgetFee')}</TradeItemLabel>
<TradeValue>{`${fee} USD`}</TradeValue>
</TradeInfoItem>
<TradeInfoItem isLast={true}>
<TradeItemLabel>{getLocale(totalLabel)}</TradeItemLabel>
<TradeValue>{`${total} ${totalAssetLabel}`}</TradeValue>
</TradeInfoItem>
</TradeInfoWrapper>
<ActionsWrapper>
<ConnectButton isSmall={true} onClick={this.processTrade}>
Expand Down
7 changes: 4 additions & 3 deletions components/gemini/browser/gemini_json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ bool GeminiJSONParser::GetDepositInfoFromJSON(
bool GeminiJSONParser::GetOrderQuoteInfoFromJSON(
const std::string& json, std::string *quote_id,
std::string *quantity, std::string *fee, std::string *price,
std::string *error) {
if (!quote_id || !quantity || !fee || !price) {
std::string *total_price, std::string *error) {
if (!quote_id || !quantity || !fee || !price || !total_price) {
return false;
}

Expand Down Expand Up @@ -167,7 +167,8 @@ bool GeminiJSONParser::GetOrderQuoteInfoFromJSON(
if (!data_dict->GetInteger("quoteId", &temp_id) ||
!data_dict->GetString("quantity", quantity) ||
!data_dict->GetString("fee", fee) ||
!data_dict->GetString("price", price)) {
!data_dict->GetString("price", price) ||
!data_dict->GetString("totalSpend", total_price)) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions components/gemini/browser/gemini_json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GeminiJSONParser {
std::string *quantity,
std::string *fee,
std::string *price,
std::string *total_price,
std::string *error);
};

Expand Down
4 changes: 3 additions & 1 deletion components/gemini/browser/gemini_json_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ TEST_F(GeminiJSONParserTest, GetOrderQuoteInfoFromJSON) {
std::string quote_id;
std::string quantity;
std::string price;
std::string total_price;
std::string error;
ASSERT_TRUE(GeminiJSONParser::GetOrderQuoteInfoFromJSON(R"(
{
Expand All @@ -136,11 +137,12 @@ TEST_F(GeminiJSONParserTest, GetOrderQuoteInfoFromJSON) {
"totalSpend": "100",
"totalSpendCurrency": "USD"
}
})", &quote_id, &quantity, &fee, &price, &error));
})", &quote_id, &quantity, &fee, &price, &total_price, &error));
ASSERT_EQ(quote_id, "1328");
ASSERT_EQ(price, "6445.07");
ASSERT_EQ(fee, "2.9900309233");
ASSERT_EQ(quantity, "0.01505181");
ASSERT_EQ(total_price, "100");
}

} // namespace
7 changes: 5 additions & 2 deletions components/gemini/browser/gemini_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,15 @@ void GeminiService::OnGetOrderQuote(GetOrderQuoteCallback callback,
std::string quantity;
std::string price;
std::string error;
std::string total_price;
if (status >= 200 && status <= 299) {
const std::string json_body = "{\"data\": " + body + "}";
GeminiJSONParser::GetOrderQuoteInfoFromJSON(
json_body, &quote_id, &quantity, &fee, &price, &error);
json_body, &quote_id, &quantity,
&fee, &price, &total_price, &error);
}
std::move(callback).Run(quote_id, quantity, fee, price, error);
std::move(callback).Run(
quote_id, quantity, fee, price, total_price, error);
}

bool GeminiService::ExecuteOrder(const std::string& symbol,
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 @@ -65,6 +65,7 @@ class GeminiService : public KeyedService {
const std::string&,
const std::string&,
const std::string&,
const std::string&,
const std::string&)>;
using ExecuteOrderCallback = base::OnceCallback<void(bool)>;

Expand Down
17 changes: 11 additions & 6 deletions components/gemini/browser/gemini_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,23 @@ class GeminiAPIBrowserTest : public InProcessBrowserTest {

void OnGetOrderQuote(const std::string& quote_id,
const std::string& quantity, const std::string& fee,
const std::string& price, const std::string& error) {
const std::string& price, const std::string& total_price,
const std::string& error) {
if (wait_for_request_) {
wait_for_request_->Quit();
}
ASSERT_EQ(expected_quote_id_, quote_id);
ASSERT_EQ(expected_quantity_, quantity);
ASSERT_EQ(expected_total_fee_, fee);
ASSERT_EQ(expected_quote_price_, price);
ASSERT_EQ(expected_total_price_, total_price);
}

void WaitForGetOrderQuote(const std::string& expected_quote_id,
const std::string& expected_quantity,
const std::string& expected_total_fee,
const std::string& expected_quote_price) {
const std::string& expected_quote_price,
const std::string& expected_total_price) {
if (wait_for_request_) {
return;
}
Expand All @@ -206,6 +209,7 @@ class GeminiAPIBrowserTest : public InProcessBrowserTest {
expected_quote_price_ = expected_quote_price;
expected_total_fee_ = expected_total_fee;
expected_quantity_ = expected_quantity;
expected_total_price_ = expected_total_price;

wait_for_request_.reset(new base::RunLoop);
wait_for_request_->Run();
Expand Down Expand Up @@ -277,6 +281,7 @@ class GeminiAPIBrowserTest : public InProcessBrowserTest {
bool expected_success_;
std::string expected_quote_id_;
std::string expected_quote_price_;
std::string expected_total_price_;
std::string expected_total_fee_;
std::string expected_quantity_;
std::string expected_address_;
Expand Down Expand Up @@ -350,11 +355,11 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuote) {
ResetHTTPSServer(base::BindRepeating(&HandleRequest));
EXPECT_TRUE(NavigateToNewTabUntilLoadStop());
auto* service = GetGeminiService();
ASSERT_TRUE(service->GetOrderQuote("buy", "btcusd", "10",
ASSERT_TRUE(service->GetOrderQuote("buy", "btcusd", "100",
base::BindOnce(
&GeminiAPIBrowserTest::OnGetOrderQuote,
base::Unretained(this))));
WaitForGetOrderQuote("1328", "0.01505181", "2.9900309233", "6445.07");
WaitForGetOrderQuote("1328", "0.01505181", "2.9900309233", "6445.07", "100");
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteUnauthorized) {
Expand All @@ -365,7 +370,7 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteUnauthorized) {
base::BindOnce(
&GeminiAPIBrowserTest::OnGetOrderQuote,
base::Unretained(this))));
WaitForGetOrderQuote("", "", "", "");
WaitForGetOrderQuote("", "", "", "", "");
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteServerError) {
Expand All @@ -376,7 +381,7 @@ IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetOrderQuoteServerError) {
base::BindOnce(
&GeminiAPIBrowserTest::OnGetOrderQuote,
base::Unretained(this))));
WaitForGetOrderQuote("", "", "", "");
WaitForGetOrderQuote("", "", "", "", "");
}

IN_PROC_BROWSER_TEST_F(GeminiAPIBrowserTest, GetAccountBalances) {
Expand Down
4 changes: 3 additions & 1 deletion components/resources/brave_components_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,9 @@
<message name="IDS_GEMINI_WIDGET_SELLING" desc="">Selling</message>
<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_PRICE" desc="">Price</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>

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