Skip to content

Commit

Permalink
Removes balance api calls for 0 balance
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jun 4, 2020
1 parent 296ff95 commit 613a6f1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions components/brave_rewards/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,14 @@ std::vector<double> 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
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ std::vector<double> GetTipChoices(bat_ledger::LedgerImpl* ledger);

std::vector<double> 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_
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include <vector>

#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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 613a6f1

Please sign in to comment.