Skip to content

Commit

Permalink
Merge pull request #6442 from brave/user_funds_claim
Browse files Browse the repository at this point in the history
Fixes user funds claim
  • Loading branch information
NejcZdovc authored Aug 18, 2020
2 parents 8b70514 + a5bdc84 commit cf12bc9
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 22 deletions.
6 changes: 2 additions & 4 deletions components/brave_rewards/browser/rewards_p3a.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void RecordRewardsDisabledForSomeMetrics() {


double CalcWalletBalanceForP3A(base::flat_map<std::string, double> wallets,
std::string user_funds) {
double user_funds) {
double balance_minus_grant = 0.0;
for (const auto& wallet : wallets) {
// Skip anonymous wallet, since it can contain grants.
Expand All @@ -166,9 +166,7 @@ double CalcWalletBalanceForP3A(base::flat_map<std::string, double> wallets,

// `user_funds` is the amount of user-funded BAT
// in the anonymous wallet (ex: not grants).
double user_funds_value;
balance_minus_grant +=
base::StringToDouble(user_funds, &user_funds_value);
balance_minus_grant += user_funds;
return balance_minus_grant;
}

Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_p3a.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void RecordNoWalletCreatedForAllMetrics();
void RecordRewardsDisabledForSomeMetrics();

double CalcWalletBalanceForP3A(base::flat_map<std::string, double> wallets,
std::string user_funds);
double user_funds);

uint64_t RoundProbiToUint64(base::StringPiece probi);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ struct RewardsParameters {

struct Balance {
double total;
string user_funds;
double user_funds;
map<string, double> wallets;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/base64.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "bat/ledger/internal/common/security_helper.h"
#include "bat/ledger/internal/endpoint/promotion/promotions_util.h"
Expand Down Expand Up @@ -60,18 +61,16 @@ std::string PostClaimUphold::GetUrl() {
return GetServerUrl(path);
}

std::string PostClaimUphold::GeneratePayload(const std::string& user_funds) {
std::string PostClaimUphold::GeneratePayload(const double user_funds) {
auto wallets = ledger_->ledger_client()->GetExternalWallets();
auto wallet_ptr = braveledger_uphold::GetWallet(std::move(wallets));
if (!wallet_ptr) {
BLOG(0, "Wallet is null");
return "";
}

const std::string amount = user_funds.empty() ? "0" : user_funds;

base::Value denomination(base::Value::Type::DICTIONARY);
denomination.SetStringKey("amount", amount);
denomination.SetStringKey("amount", base::NumberToString(user_funds));
denomination.SetStringKey("currency", "BAT");

base::Value octets(base::Value::Type::DICTIONARY);
Expand Down Expand Up @@ -148,7 +147,7 @@ ledger::Result PostClaimUphold::CheckStatusCode(const int status_code) {
}

void PostClaimUphold::Request(
const std::string& user_funds,
const double user_funds,
PostClaimUpholdCallback callback) {
auto url_callback = std::bind(&PostClaimUphold::OnRequest,
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class PostClaimUphold {
~PostClaimUphold();

void Request(
const std::string& user_funds,
const double user_funds,
PostClaimUpholdCallback callback);

private:
std::string GetUrl();

std::string GeneratePayload(const std::string& user_funds);
std::string GeneratePayload(const double user_funds);

ledger::Result CheckStatusCode(const int status_code);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST_F(PostClaimUpholdTest, ServerOK) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::LEDGER_OK);
});
Expand All @@ -83,7 +83,7 @@ TEST_F(PostClaimUpholdTest, ServerError400) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::LEDGER_ERROR);
});
Expand All @@ -107,7 +107,7 @@ TEST_F(PostClaimUpholdTest, ServerError403) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::LEDGER_ERROR);
});
Expand All @@ -131,7 +131,7 @@ TEST_F(PostClaimUpholdTest, ServerError404) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::NOT_FOUND);
});
Expand All @@ -155,7 +155,7 @@ TEST_F(PostClaimUpholdTest, ServerError409) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::ALREADY_EXISTS);
});
Expand All @@ -179,7 +179,7 @@ TEST_F(PostClaimUpholdTest, ServerError500) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::LEDGER_ERROR);
});
Expand All @@ -203,7 +203,7 @@ TEST_F(PostClaimUpholdTest, ServerErrorRandom) {
}));

claim_->Request(
"30.0",
30.0,
[](const ledger::Result result) {
EXPECT_EQ(result, ledger::Result::LEDGER_ERROR);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void WalletBalance::Fetch(ledger::FetchBalanceCallback callback) {
// we can skip balance server ping
if (!ledger_->state()->GetFetchOldBalanceEnabled()) {
auto balance = ledger::Balance::New();
balance->user_funds = "0";
GetUnblindedTokens(std::move(balance), callback);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void WalletClaim::OnBalance(
}

if (ledger_->state()->GetAnonTransferChecked() &&
balance->user_funds == "0") {
balance->user_funds == 0) {
BLOG(1, "Second ping with zero balance");
callback(ledger::Result::LEDGER_OK);
return;
Expand Down

0 comments on commit cf12bc9

Please sign in to comment.