From 613a6f1caa9045964f34ea2e73f03b46db039582 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 4 Jun 2020 09:44:48 +0200 Subject: [PATCH] Removes balance api calls for 0 balance Resolves https://github.com/brave/brave-browser/issues/9132 --- .../brave_rewards/browser/rewards_service.cc | 1 + components/brave_rewards/common/pref_names.cc | 2 ++ components/brave_rewards/common/pref_names.h | 1 + .../src/bat/ledger/internal/state/state_keys.h | 1 + .../bat/ledger/internal/state/state_util.cc | 10 ++++++++++ .../src/bat/ledger/internal/state/state_util.h | 4 ++++ .../src/bat/ledger/internal/wallet/balance.cc | 18 +++++++++++++++++- .../src/bat/ledger/internal/wallet/create.cc | 1 + .../src/bat/ledger/internal/wallet/recover.cc | 1 + 9 files changed, 38 insertions(+), 1 deletion(-) diff --git a/components/brave_rewards/browser/rewards_service.cc b/components/brave_rewards/browser/rewards_service.cc index 0245c4555193..3daf5fcf14e3 100644 --- a/components/brave_rewards/browser/rewards_service.cc +++ b/components/brave_rewards/browser/rewards_service.cc @@ -92,6 +92,7 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { ""); registry->RegisterStringPref(prefs::kStateParametersTipChoices, ""); registry->RegisterStringPref(prefs::kStateParametersMonthlyTipChoices, ""); + registry->RegisterBooleanPref(prefs::kStateFetchOldBalance, true); } } // namespace brave_rewards diff --git a/components/brave_rewards/common/pref_names.cc b/components/brave_rewards/common/pref_names.cc index 7c11d3ef0857..413fc7ec3ea9 100644 --- a/components/brave_rewards/common/pref_names.cc +++ b/components/brave_rewards/common/pref_names.cc @@ -62,5 +62,7 @@ const char kStateParametersTipChoices[] = "brave.rewards.parameters.tip.choices"; const char kStateParametersMonthlyTipChoices[] = "brave.rewards.parameters.tip.monthly_choices"; +const char kStateFetchOldBalance[] = + "brave.rewards.fetch_old_balance"; } // namespace prefs } // namespace brave_rewards diff --git a/components/brave_rewards/common/pref_names.h b/components/brave_rewards/common/pref_names.h index 9d9d0f54b502..d1a0918b0883 100644 --- a/components/brave_rewards/common/pref_names.h +++ b/components/brave_rewards/common/pref_names.h @@ -50,6 +50,7 @@ extern const char kStateParametersAutoContributeChoice[]; extern const char kStateParametersAutoContributeChoices[]; extern const char kStateParametersTipChoices[]; extern const char kStateParametersMonthlyTipChoices[]; +extern const char kStateFetchOldBalance[]; extern const char kUseRewardsStagingServer[]; } // namespace prefs diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h index e8c6642edd4e..a4ce0aa41e4b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h @@ -41,6 +41,7 @@ namespace ledger { const char kStateParametersTipChoices[] = "parameters.tip.choices"; const char kStateParametersMonthlyTipChoices[] = "parameters.tip.monthly_choices"; + const char kStateFetchOldBalance[] = "fetch_old_balance"; } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc index ae999006b1f3..f13cc2cb407e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc @@ -355,4 +355,14 @@ std::vector GetMonthlyTipChoices(bat_ledger::LedgerImpl* ledger) { return StringToVectorDouble(amounts_string); } +void SetFetchOldBalanceEnabled(bat_ledger::LedgerImpl* ledger, bool enabled) { + DCHECK(ledger); + ledger->SetBooleanState(ledger::kStateFetchOldBalance, enabled); +} + +bool GetFetchOldBalanceEnabled(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + return ledger->GetBooleanState(ledger::kStateFetchOldBalance); +} + } // namespace braveledger_state diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h index 4594e15616fb..0efe2c1af0c5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h @@ -119,6 +119,10 @@ std::vector GetTipChoices(bat_ledger::LedgerImpl* ledger); std::vector GetMonthlyTipChoices(bat_ledger::LedgerImpl* ledger); +void SetFetchOldBalanceEnabled(bat_ledger::LedgerImpl* ledger, bool enabled); + +bool GetFetchOldBalanceEnabled(bat_ledger::LedgerImpl* ledger); + } // namespace braveledger_state #endif // BRAVELEDGER_STATE_STATE_UTIL_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc index b3cbed0cfeae..0ebdea26dac4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/balance.cc @@ -11,11 +11,13 @@ #include #include "base/json/json_reader.h" +#include "base/strings/stringprintf.h" #include "bat/ledger/global_constants.h" #include "bat/ledger/internal/ledger_impl.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/request/request_util.h" #include "bat/ledger/internal/static_values.h" +#include "bat/ledger/internal/state/state_util.h" #include "net/http/http_status_code.h" using std::placeholders::_1; @@ -33,9 +35,19 @@ Balance::~Balance() { } void Balance::Fetch(ledger::FetchBalanceCallback callback) { + // if we don't have user funds in anon card anymore + // we can skip balance server ping + if (!braveledger_state::GetFetchOldBalanceEnabled(ledger_)) { + auto balance = ledger::Balance::New(); + GetUnBlindedTokens(std::move(balance), callback); + return; + } + std::string payment_id = ledger_->GetPaymentId(); - std::string path = "/wallet/" + payment_id + "/balance"; + std::string path = base::StringPrintf( + "/wallet/%s/balance", + payment_id.c_str()); const std::string url = braveledger_request_util::BuildUrl( path, PREFIX_V2, @@ -87,6 +99,9 @@ void Balance::OnFetch( balance->wallets.insert(std::make_pair(ledger::kWalletAnonymous, total_anon)); + if (balance->total == 0.0) { + braveledger_state::SetFetchOldBalanceEnabled(ledger_, false); + } GetUnBlindedTokens(std::move(balance), callback); } @@ -99,6 +114,7 @@ void Balance::GetUnBlindedTokens( callback(ledger::Result::LEDGER_ERROR, std::move(balance)); return; } + auto tokens_callback = std::bind(&Balance::OnGetUnBlindedTokens, this, *balance, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc index c9a22a025d24..0f64bdceb1f8 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc @@ -299,6 +299,7 @@ void Create::RegisterPersonaCallback( ledger_->SetAutoContributeEnabled(true); braveledger_state::SetPaymentId(ledger_, payment_id); braveledger_state::SetAnonymousCardId(ledger_, card_id); + braveledger_state::SetFetchOldBalanceEnabled(ledger_, false); ledger_->SetCreationStamp(braveledger_time_util::GetCurrentTimeStamp()); ledger_->ResetReconcileStamp(); braveledger_state::SetInlineTippingPlatformEnabled( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc index 37f854cefd91..9fe330dad73b 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc @@ -255,6 +255,7 @@ void Recover::RecoverWalletCallback( braveledger_state::SetRecoverySeed(ledger_, new_seed); braveledger_state::SetPaymentId(ledger_, recovery_id); braveledger_state::SetAnonymousCardId(ledger_, card_id); + braveledger_state::SetFetchOldBalanceEnabled(ledger_, true); ledger_->SetConfirmationsWalletInfo(); callback(ledger::Result::LEDGER_OK, balance);