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

Add Ratios API for coingecko coin markets data #12510

Merged
merged 22 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion browser/android/preferences/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ source_set("preferences") {
java_cpp_strings("java_pref_names_srcjar") {
sources = [
"//brave/components/brave_rewards/common/pref_names.cc",
"//brave/components/brave_vpn/pref_names.cc",
"//brave/components/brave_shields/common/pref_names.cc",
"//brave/components/brave_vpn/pref_names.cc",
"//brave/components/constants/pref_names.cc",
"//brave/components/ntp_background_images/common/pref_names.cc",
"//brave/components/omnibox/browser/brave_omnibox_prefs.cc",
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ source_set("ui") {
"wallet_bubble_manager_delegate_impl.h",
"webui/brave_wallet/ledger/ledger_ui.cc",
"webui/brave_wallet/ledger/ledger_ui.h",
"webui/brave_wallet/market/market_ui.cc",
"webui/brave_wallet/market/market_ui.h",
"webui/brave_wallet/nft/nft_ui.cc",
"webui/brave_wallet/nft/nft_ui.h",
"webui/brave_wallet/page_handler/wallet_page_handler.cc",
Expand All @@ -673,6 +675,7 @@ source_set("ui") {
"//brave/components/brave_wallet/common:mojom",
"//brave/components/brave_wallet_ui:resources",
"//brave/components/brave_wallet_ui/ledger:ledger_bridge_generated",
"//brave/components/brave_wallet_ui/market:market_display_generated",
"//brave/components/brave_wallet_ui/nft:nft_display_generated",
"//brave/components/brave_wallet_ui/page:brave_wallet_page_generated",
"//brave/components/brave_wallet_ui/panel:brave_wallet_panel_generated",
Expand Down
82 changes: 82 additions & 0 deletions browser/ui/webui/brave_wallet/market/market_ui.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/webui/brave_wallet/market/market_ui.h"

#include <string>

#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/market_display/resources/grit/market_display_generated_map.h"
#include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "ui/resources/grit/webui_generated_resources.h"

namespace market {

UntrustedMarketUI::UntrustedMarketUI(content::WebUI* web_ui)
: ui::UntrustedWebUIController(web_ui) {
auto* untrusted_source =
content::WebUIDataSource::Create(kUntrustedMarketURL);

for (const auto& str : brave_wallet::kLocalizedStrings) {
std::u16string l10n_str =
brave_l10n::GetLocalizedResourceUTF16String(str.id);
untrusted_source->AddString(str.name, l10n_str);
}
untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML);
untrusted_source->AddResourcePaths(
base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
webui::SetupWebUIDataSource(
untrusted_source,
base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize),
IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML);

untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
std::string("script-src 'self' chrome-untrusted://resources "
"chrome-untrusted://brave-resources;"));
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::StyleSrc,
std::string("style-src 'self' 'unsafe-inline';"));
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::FontSrc,
std::string("font-src 'self' data:;"));
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ImgSrc,
std::string("img-src 'self' https://assets.cgproxy.brave.com;"));

untrusted_source->AddResourcePath("load_time_data.js",
IDR_WEBUI_JS_LOAD_TIME_DATA_JS);
untrusted_source->UseStringsJs();
untrusted_source->AddString("braveWalletTrezorBridgeUrl",
kUntrustedTrezorURL);
untrusted_source->AddString("braveWalletLedgerBridgeUrl",
kUntrustedLedgerURL);
untrusted_source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
untrusted_source->AddString("braveWalletMarketUiBridgeUrl",
kUntrustedMarketURL);
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context, untrusted_source);
}

UntrustedMarketUI::~UntrustedMarketUI() = default;

std::unique_ptr<content::WebUIController>
UntrustedMarketUIConfig::CreateWebUIController(content::WebUI* web_ui) {
return std::make_unique<UntrustedMarketUI>(web_ui);
}

UntrustedMarketUIConfig::UntrustedMarketUIConfig()
: WebUIConfig(content::kChromeUIUntrustedScheme, kUntrustedMarketHost) {}

} // namespace market
37 changes: 37 additions & 0 deletions browser/ui/webui/brave_wallet/market/market_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_WALLET_MARKET_MARKET_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_WALLET_MARKET_MARKET_UI_H_

#include <memory>

#include "content/public/browser/web_ui.h"
#include "content/public/browser/webui_config.h"
#include "content/public/common/url_constants.h"
#include "ui/webui/untrusted_web_ui_controller.h"

namespace market {

class UntrustedMarketUI : public ui::UntrustedWebUIController {
public:
explicit UntrustedMarketUI(content::WebUI* web_ui);
UntrustedMarketUI(const UntrustedMarketUI&) = delete;
UntrustedMarketUI& operator=(const UntrustedMarketUI&) = delete;
~UntrustedMarketUI() override;
};

class UntrustedMarketUIConfig : public content::WebUIConfig {
public:
UntrustedMarketUIConfig();
~UntrustedMarketUIConfig() override = default;

std::unique_ptr<content::WebUIController> CreateWebUIController(
content::WebUI* web_ui) override;
};

} // namespace market

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_WALLET_MARKET_MARKET_UI_H_
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_wallet/nft/nft_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ UntrustedNftUI::UntrustedNftUI(content::WebUI* web_ui)
kUntrustedTrezorURL);
untrusted_source->AddString("braveWalletLedgerBridgeUrl",
kUntrustedLedgerURL);
untrusted_source->AddString("braveWalletMarketUiBridgeUrl",
kUntrustedMarketURL);
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ImgSrc,
std::string("img-src 'self' https: data:;"));
Expand Down
4 changes: 3 additions & 1 deletion browser/ui/webui/brave_wallet/wallet_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ WalletPageUI::WalletPageUI(content::WebUI* web_ui)
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::FrameSrc,
std::string("frame-src ") + kUntrustedTrezorURL + " " +
kUntrustedLedgerURL + " " + kUntrustedNftURL + ";");
kUntrustedLedgerURL + " " + kUntrustedNftURL + " " +
kUntrustedMarketURL + ";");
source->AddString("braveWalletTrezorBridgeUrl", kUntrustedTrezorURL);
source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
source->AddString("braveWalletMarketUiBridgeUrl", kUntrustedMarketURL);
auto* profile = Profile::FromWebUI(web_ui);
content::WebUIDataSource::Add(profile, source);
content::URLDataSource::Add(profile,
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ WalletPanelUI::WalletPanelUI(content::WebUI* web_ui)
kUntrustedLedgerURL + ";");
source->AddString("braveWalletTrezorBridgeUrl", kUntrustedTrezorURL);
source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
source->AddString("braveWalletMarketUiBridgeUrl", kUntrustedMarketURL);

if (ShouldDisableCSPForTesting()) {
source->DisableContentSecurityPolicy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "chrome/browser/ui/webui/chrome_untrusted_web_ui_configs.h"

#include "brave/browser/ui/webui/brave_wallet/ledger/ledger_ui.h"
#include "brave/browser/ui/webui/brave_wallet/market/market_ui.h"
#include "brave/browser/ui/webui/brave_wallet/nft/nft_ui.h"
#include "brave/browser/ui/webui/brave_wallet/trezor/trezor_ui.h"
#include "brave/components/brave_vpn/buildflags/buildflags.h"
Expand All @@ -29,6 +30,8 @@ void RegisterChromeUntrustedWebUIConfigs() {
#if !BUILDFLAG(IS_ANDROID)
content::WebUIConfigMap::GetInstance().AddUntrustedWebUIConfig(
std::make_unique<ledger::UntrustedLedgerUIConfig>());
content::WebUIConfigMap::GetInstance().AddUntrustedWebUIConfig(
std::make_unique<market::UntrustedMarketUIConfig>());
content::WebUIConfigMap::GetInstance().AddUntrustedWebUIConfig(
std::make_unique<trezor::UntrustedTrezorUIConfig>());
content::WebUIConfigMap::GetInstance().AddUntrustedWebUIConfig(
Expand Down
130 changes: 130 additions & 0 deletions components/brave_wallet/browser/asset_ratio_response_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,134 @@ mojom::BlockchainTokenPtr ParseTokenInfo(const std::string& json,
*symbol, decimals, true, "", "", chain_id, coin);
}

bool ParseCoinMarkets(const std::string& json,
std::vector<mojom::CoinMarketPtr>* values) {
DCHECK(values);
// {
// "payload": [
// {
// "id": "bitcoin",
// "symbol": "btc",
// "name": "Bitcoin",
// "image":
// "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
kdenhartog marked this conversation as resolved.
Show resolved Hide resolved
muliswilliam marked this conversation as resolved.
Show resolved Hide resolved
// "market_cap": 727960800075,
// "market_cap_rank": 1,
// "current_price": 38357,
// "price_change_24h": -1229.64683216549,
// "price_change_percentage_24h": -3.10625,
// "total_volume": 17160995925
// },
// {
// "id": "ethereum",
// "symbol": "eth",
// "name": "Ethereum",
// "image":
// "https://assets.coingecko.com/coins/images/279/large/ethereum.png?1595348880",
muliswilliam marked this conversation as resolved.
Show resolved Hide resolved
// "market_cap": 304535808667,
// "market_cap_rank": 2,
// "current_price": 2539.82,
// "price_change_24h": -136.841895278459,
// "price_change_percentage_24h": -5.11242,
// "total_volume": 9583014937
// }
// ],
// "lastUpdated": "2022-03-07T00:25:12.259823452Z"
// }
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
diracdeltas marked this conversation as resolved.
Show resolved Hide resolved
json, base::JSON_PARSE_CHROMIUM_EXTENSIONS |
base::JSONParserOptions::JSON_PARSE_RFC);
absl::optional<base::Value>& records_v = value_with_error.value;
if (!records_v) {
VLOG(0) << "Invalid response, could not parse JSON, JSON is: " << json;
return false;
}

if (!records_v->is_dict()) {
return false;
}

auto* payload = records_v->FindListKey("payload");
if (!payload) {
return false;
}

for (const auto& coin_market_list_it : payload->GetList()) {
if (!coin_market_list_it.is_dict()) {
return false;
}
auto coin_market = mojom::CoinMarket::New();
auto* id = coin_market_list_it.FindStringKey("id");
if (!id) {
return false;
}
coin_market->id = *id;

auto* symbol = coin_market_list_it.FindStringKey("symbol");
if (!symbol) {
return false;
}
coin_market->symbol = *symbol;

auto* name = coin_market_list_it.FindStringKey("name");
if (!name) {
return false;
}
coin_market->name = *name;

auto* image = coin_market_list_it.FindStringKey("image");
if (!image) {
return false;
}
coin_market->image = *image;

absl::optional<double> market_cap =
coin_market_list_it.FindDoubleKey("market_cap");
if (!market_cap) {
return false;
}
coin_market->market_cap = *market_cap;

absl::optional<uint32_t> market_cap_rank =
coin_market_list_it.FindIntKey("market_cap_rank");
if (!market_cap_rank) {
return false;
}
coin_market->market_cap_rank = *market_cap_rank;

absl::optional<double> current_price =
coin_market_list_it.FindDoubleKey("current_price");
if (!current_price) {
return false;
}
coin_market->current_price = *current_price;

absl::optional<double> price_change_24h =
coin_market_list_it.FindDoubleKey("price_change_24h");
if (!price_change_24h) {
return false;
}
coin_market->price_change_24h = *price_change_24h;

absl::optional<double> price_change_percentage_24h =
coin_market_list_it.FindDoubleKey("price_change_percentage_24h");
if (!price_change_percentage_24h) {
return false;
}
coin_market->price_change_percentage_24h = *price_change_percentage_24h;

absl::optional<double> total_volume =
coin_market_list_it.FindDoubleKey("total_volume");
if (!total_volume) {
return false;
}
coin_market->total_volume = *total_volume;

values->push_back(std::move(coin_market));
}

return true;
}

} // namespace brave_wallet
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ bool ParseAssetPrice(const std::string& json,
std::vector<mojom::AssetPricePtr>* values);
bool ParseAssetPriceHistory(const std::string& json,
std::vector<mojom::AssetTimePricePtr>* values);
bool ParseCoinMarkets(const std::string& json,
std::vector<mojom::CoinMarketPtr>* values);

std::string ParseEstimatedTime(const std::string& json);
mojom::BlockchainTokenPtr ParseTokenInfo(const std::string& json,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,54 @@ TEST(AssetRatioResponseParserUnitTest, ParseGetTokenInfo) {
EXPECT_FALSE(ParseTokenInfo(R"({"payload":{})", "0x1", mojom::CoinType::ETH));
}

TEST(AssetRatioResponseParserUnitTest, ParseCoinMarkets) {
// https://ratios.rewards.brave.software/v2/market/provider/coingecko\?vsCurrency\=usd\&limit\=2
std::string json(R"(
{
"payload": [
{
"id": "bitcoin",
"symbol": "btc",
"name": "Bitcoin",
"image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"market_cap": 727960800075,
"market_cap_rank": 1,
"current_price": 38357,
"price_change_24h": -1229.64683216549,
"price_change_percentage_24h": -3.10625,
"total_volume": 17160995925
}
]
}
)");

std::vector<brave_wallet::mojom::CoinMarketPtr> values;
ASSERT_TRUE(ParseCoinMarkets(json, &values));
ASSERT_EQ(values.size(), 1UL);
EXPECT_EQ(values[0]->id, "bitcoin");
EXPECT_EQ(values[0]->symbol, "btc");
EXPECT_EQ(values[0]->name, "Bitcoin");
EXPECT_EQ(values[0]->image,
"https://assets.coingecko.com/coins/images/1/large/"
"bitcoin.png?1547033579");
EXPECT_EQ(values[0]->market_cap, 727960800075);
EXPECT_EQ(values[0]->market_cap_rank, uint32_t(1));
EXPECT_EQ(values[0]->current_price, 38357);
EXPECT_EQ(values[0]->price_change_24h, -1229.64683216549);
EXPECT_EQ(values[0]->price_change_percentage_24h, -3.10625);
EXPECT_EQ(values[0]->total_volume, 17160995925);

// Invalid input
json = R"({"id": [])";
EXPECT_FALSE(ParseCoinMarkets(json, &values));
json = R"({"id": []})";
EXPECT_FALSE(ParseCoinMarkets(json, &values));
json = "3";
EXPECT_FALSE(ParseCoinMarkets(json, &values));
json = "[3]";
EXPECT_FALSE(ParseCoinMarkets(json, &values));
json = "";
EXPECT_FALSE(ParseCoinMarkets(json, &values));
}

} // namespace brave_wallet
Loading