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

(Desktop) FTX for NTP #8629

Merged
merged 8 commits into from
May 14, 2021
Merged

(Desktop) FTX for NTP #8629

merged 8 commits into from
May 14, 2021

Conversation

petemill
Copy link
Member

@petemill petemill commented Apr 23, 2021

  • Performs oauth pattern
    • Receives auth token (at ftx_protocol_handler.cc)
    • Requests authentication token and stores encrypted in regular settings using OSCrypt.
  • Implements FTX API calls and JSON parsing (ftx_service.cc, ftx_json_parser.cc)
  • Adds an extension API to call service methods from the New Tab Page WebUI (courtesy of @ryanml) (see note below re: mojom).
  • Adds UI for FTX on the New Tab Page (courtesy of @AlanBreck).

Resolves brave/brave-browser#15790

Review instructions

  • For building, you must have the correct API key and secret environment variables in your config, see internal wiki.
  • Please contact me for test FTX credentials.

Note on WebUI API

Like all other NTP crypto widgets, this creates an Extension API even though it is not called from an Extension and only called from the NTP WebUI. These should all be converted to mojom interfaces. Created follow-up here: brave/brave-browser#15770

Test Plan:

All intended UI states are on storybook via npm run storybook:

Not yet initialized

image

Not connected

image

Not connected (View markets)

image

Connected: summary

image

Connected: markets

image

Connected: markets, asset detail (loading)

image

Connected: markets, asset detail

image

Connected: markets, asset detail (fetch error)

image

Convert

image

Convert: no balance

image

Convert: quote loading

image

Convert: quote ready

image

Convert: quote submitting

image

Convert: complete

image

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

@petemill petemill force-pushed the ntp-ftx branch 6 times, most recently from 170e870 to 6a72d26 Compare May 11, 2021 04:06
@petemill petemill changed the title FTX for NTP (Desktop) FTX for NTP May 11, 2021
@petemill petemill force-pushed the ntp-ftx branch 4 times, most recently from b58aa94 to a4a310d Compare May 11, 2021 07:11
@petemill petemill self-assigned this May 11, 2021
@petemill petemill force-pushed the ntp-ftx branch 2 times, most recently from cf90ec1 to 56aed23 Compare May 12, 2021 06:03
@petemill petemill marked this pull request as ready for review May 12, 2021 17:14
@petemill petemill requested a review from a team as a code owner May 12, 2021 17:14
Copy link
Collaborator

@mkarolin mkarolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chromium_src changes LGTM

@petemill petemill force-pushed the ntp-ftx branch 2 times, most recently from 06a4dff to 84801d2 Compare May 12, 2021 18:48
json, base::JSONParserOptions::JSON_PARSE_RFC);
base::Optional<base::Value>& records_v = value_with_error.value;
if (!records_v) {
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this json potentially contain sensitive data or PII?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of them not, some of them yes. I'll remove the JSON output for all of them. Thanks, I didn't notice these.

Copy link
Member Author

@petemill petemill May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base::Optional<base::Value>& records_v = value_with_error.value;

if (!records_v) {
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Member Author

@petemill petemill May 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json, base::JSONParserOptions::JSON_PARSE_RFC);
base::Optional<base::Value>& records_v = value_with_error.value;
if (!records_v) {
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petemill petemill force-pushed the ntp-ftx branch 2 times, most recently from a88598a to 799c791 Compare May 13, 2021 02:52
Copy link
Member

@simonhong simonhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits and after fixing percent and infinite throbbing issue shared in DM.

browser/brave_profile_prefs.cc Outdated Show resolved Hide resolved
components/ftx/browser/ftx_service.h Outdated Show resolved Hide resolved
browser/ftx/ftx_protocol_handler.cc Outdated Show resolved Hide resolved
browser/ftx/ftx_service_factory.h Outdated Show resolved Hide resolved
browser/ftx/ftx_service_factory.h Outdated Show resolved Hide resolved
browser/ftx/ftx_protocol_handler.cc Outdated Show resolved Hide resolved
browser/extensions/api/ftx_api.h Outdated Show resolved Hide resolved
browser/extensions/api/ftx_api.cc Outdated Show resolved Hide resolved
browser/ui/BUILD.gn Show resolved Hide resolved
components/ftx/common/pref_names.h Outdated Show resolved Hide resolved
@petemill petemill force-pushed the ntp-ftx branch 3 times, most recently from f84617c to 6f4c503 Compare May 14, 2021 04:51
@petemill petemill requested a review from bridiver as a code owner May 14, 2021 04:51
brave_browser_ftx_deps = []

if (ftx_enabled) {
brave_browser_ftx_sources += [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a circular dependency here? If not it should just be added as a normal dep

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include <string>

#include "chrome/browser/external_protocol/external_protocol_handler.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header is not used here and you are missing headers for GURL, WebContents Optional and Origin. See https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhyIWYU.md

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -235,6 +235,8 @@ source_set("ui") {
"//brave/components/brave_wayback_machine:buildflags",
"//brave/components/cosmetic_filters/resources/data:generated_resources",
"//brave/components/crypto_dot_com/browser/buildflags:buildflags",
"//brave/components/ftx/browser/buildflags:buildflags",
"//brave/components/ftx/common",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildflag_header("buildflags") {
header = "buildflags.h"
flags = [
"FTX_ENABLED=$ftx_enabled",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the convention is enable_feature

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


assert(ftx_enabled)

source_set("common") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these are only used in browser this component doesn't need browser and common
"A component used only by the browser process contains code directly in its directory." - https://www.chromium.org/developers/design-documents/cookbook

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this header is not used in this file and others (like base/callback.h) are missing. See https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/WhyIWYU.md

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typedef std::vector<TokenPriceData> FTXFuturesData;
typedef std::map<std::string, double> FTXAccountBalances;

class FTXService : public KeyedService {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have some comments explaining what this is since FTX is not exactly intuitive? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kRetriesCountOnNetworkChange,
network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);

auto iter = url_loaders_.insert(url_loaders_.begin(), std::move(url_loader));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url_loaders_ should be cleared in KeyedService::Shutdown

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include "base/strings/string_number_conversions.h"
#include "base/values.h"

bool FTXJSONParser::GetFuturesDataFromJSON(const std::string& json,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't necessarily need to happen in this PR, but we should be using JSONValueConverter for this kind of thing

const std::map<std::string, std::string>& headers) {
FTXFuturesData data;
if (status >= 200 && status <= 299) {
FTXJSONParser::GetFuturesDataFromJSON(body, &data, futures_filter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value is ignored

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty data will be returned. If we handle false return value then we can show error UI. Follow-up here brave/brave-browser#15852

json, base::JSONParserOptions::JSON_PARSE_RFC);
base::Optional<base::Value>& records_v = value_with_error.value;
if (!records_v) {
LOG(ERROR) << "FTX GetFuturesDataFromJSON: did not understand json";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a VLOG and also doesn't provide any useful information

result.Append(std::move(point));
}

Respond(OneArgument(std::move(result)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just going to respond with an empty ListValue if there was an error. Seems like we should respond with an error here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty result handed on the frontend, but no error is shown even though it should. Follow-up here brave/brave-browser#15852


KeyedService* FTXServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new FTXService(Profile::FromBrowserContext(context));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTXService takes BrowserContext so no reason for Profile::FromBrowserContext

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


} // namespace

FTXService::FTXService(content::BrowserContext* context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing in BrowserContext should be avoided because it makes this impossible to use for ios and is generally considered an anti-pattern. It is better to pass in the url_loader_factory and user prefs to avoid any content dependencies

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Profile::FromBrowserContext(context));
}

bool IsFTXAPIAvailable(content::BrowserContext* context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think IsFTXAvailable should be defined somewhere else so the implementation stays in sync with https://github.com/brave/brave-core/pull/8629/files#diff-05d82cf839b090feae4f8623d86beebe9c71b528eca857c3a235c19d65a7fbfeR21

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually there's really not much reason for this because GetFTXService will return null in that case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string auth_token = parts["code"];
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
FTXServiceFactory::GetInstance()->GetForProfile(profile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to crash if it's called from Tor or Guest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@bridiver bridiver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEPS are ok and approved based on making critical changes discussed in DM

@petemill
Copy link
Member Author

SonarCloud is complaining about duplicated crypto assets. We have a plan to use a shared set, contained in this PR, as a follow-up.

@petemill
Copy link
Member Author

petemill commented May 14, 2021

macOS test failure is unrelated

[2021-05-14T08:37:30.822Z] 1 test crashed:

[2021-05-14T08:37:30.822Z]     AdBlockServiceTest.CnameCloakedRequestsGetBlocked (../../brave/browser/brave_shields/ad_block_service_browsertest.cc:586)

linux test failure is unrelated


[2021-05-14T07:21:35.059Z]     BraveAdsBrowserTest/BraveAdsUpgradeBrowserTest.UpgradePath/PreferencesForVersion062WithRewardsEnabled_ForSupportedLocale_ForNewlySupportedLocale_RewardsShouldBeEnabled_AdsShouldBeDisabled (../../brave/components/brave_ads/browser/ads_service_browsertest.cc:1044)

[2021-05-14T07:21:35.059Z]     BraveAdsBrowserTest/BraveAdsUpgradeBrowserTest.UpgradePath/PreferencesForVersion062WithRewardsEnabled_ForUnsupportedLocale_ForUnsupportedLocale_RewardsShouldBeEnabled_AdsShouldBeDisabled (../../brave/components/brave_ads/browser/ads_service_browsertest.cc:1044)

[2021-05-14T07:21:35.059Z]     BraveAdsBrowserTest/BraveAdsUpgradeBrowserTest.UpgradePath/PreferencesForVersion070WithRewardsAndAdsEnabled_ForSupportedLocale_ForUnsupportedLocale_RewardsShouldBeEnabled_AdsShouldBeEnabled (../../brave/components/brave_ads/browser/ads_service_browsertest.cc:1044)

[2021-05-14T07:21:35.059Z]     BraveAdsBrowserTest/BraveAdsUpgradeBrowserTest.UpgradePath/PreferencesForVersion072WithRewardsAndAdsEnabled_ForSupportedLocale_ForUnsupportedLocale_RewardsShouldBeEnabled_AdsShouldBeEnabled (../../brave/components/brave_ads/browser/ads_service_browsertest.cc:1044)

[2021-05-14T07:21:35.059Z] null

post-init failure is due to audit-deps which is fixed on master now.

Everything else passed. Merging...

@petemill petemill merged commit 68bb77d into master May 14, 2021
@petemill petemill deleted the ntp-ftx branch May 14, 2021 17:37
@petemill petemill added this to the 1.26.x - Nightly milestone May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants