Skip to content

Commit

Permalink
Implements new balance api
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jul 22, 2020
1 parent 7d0db1b commit 5aa7700
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ void RewardsBrowserTestResponse::Get(

if (URLMatches(
url,
"/wallet/",
PREFIX_V2,
ServerTypes::BALANCE)) {
"/wallet/uphold",
PREFIX_V3,
ServerTypes::kPromotion)) {
if (user_funds_balance_) {
*response = user_funds_balance_resp_;
} else {
Expand Down
35 changes: 5 additions & 30 deletions test/data/rewards-data/balance_resp.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
{
"altcurrency": "BAT",
"probi": "0",
"balance": "0.0000",
"unconfirmed": "0.0000",
"parameters": {
"adFree": {
"currency": "BAT",
"fee": {
"BAT": 20
},
"choices": {
"BAT": [
10,
15,
20,
30,
50,
100
]
},
"range": {
"BAT": [
10,
100
]
},
"days": 30
}
}
}
"total": 0,
"spendable": 0,
"confirmed": 0,
"unconfirmed": 0
}
35 changes: 4 additions & 31 deletions test/data/rewards-data/user_funds_balance_resp.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
{
"altcurrency": "BAT",
"probi": "0",
"balance": "20.0000",
"cardBalance": "20.0",
"probi": "20000000000000000000",
"unconfirmed": "0.0000",
"parameters": {
"adFree": {
"currency": "BAT",
"fee": {
"BAT": 0
},
"choices": {
"BAT": [
10,
15,
20,
30,
50,
100
]
},
"range": {
"BAT": [
10,
100
]
},
"days": 30
}
}
"total": 0,
"spendable": 0,
"confirmed": 20.0,
"unconfirmed": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ std::string GetClaimWalletURL(const std::string& payment_id) {
return BuildUrl(path, PREFIX_V3, ServerTypes::kPromotion);
}

std::string GetBalanceWalletURL(const std::string& payment_id) {
const std::string path = base::StringPrintf(
"/wallet/uphold/%s",
payment_id.c_str());
return BuildUrl(path, PREFIX_V3, ServerTypes::kPromotion);
}

} // namespace braveledger_request_util
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ std::string GetRecoverWalletURL(const std::string& public_key);

std::string GetClaimWalletURL(const std::string& payment_id);

std::string GetBalanceWalletURL(const std::string& payment_id);

} // namespace braveledger_request_util

#endif // BRAVELEDGER_COMMON_PROMOTION_REQUESTS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

#include "base/json/json_reader.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_number_conversions.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/request/request_promotion.h"
#include "bat/ledger/internal/static_values.h"
#include "bat/ledger/internal/state/state_util.h"
#include "net/http/http_status_code.h"
Expand Down Expand Up @@ -45,13 +46,9 @@ void WalletBalance::Fetch(ledger::FetchBalanceCallback callback) {

const std::string payment_id = braveledger_state::GetPaymentId(ledger_);

std::string path = base::StringPrintf(
"/wallet/%s/balance",
payment_id.c_str());
const std::string url = braveledger_request_util::BuildUrl(
path,
PREFIX_V2,
braveledger_request_util::ServerTypes::BALANCE);
const std::string url = braveledger_request_util::GetBalanceWalletURL(
braveledger_state::GetPaymentId(ledger_));

auto load_callback = std::bind(&WalletBalance::OnFetch,
this,
_1,
Expand Down Expand Up @@ -84,21 +81,14 @@ void WalletBalance::OnFetch(
return;
}

const auto* total = dictionary->FindStringKey("balance");
double total_anon = 0.0;
if (total) {
total_anon = std::stod(*total);
}
balance->total = total_anon;

const auto* funds = dictionary->FindStringKey("cardBalance");
std::string user_funds = "0";
if (funds) {
user_funds = *funds;
const auto confirmed = dictionary->FindDoubleKey("confirmed");
if (confirmed) {
balance->total = *confirmed;
}
balance->user_funds = user_funds;

balance->wallets.insert(std::make_pair(ledger::kWalletAnonymous, total_anon));
balance->user_funds = balance->total;
balance->wallets.insert(
std::make_pair(ledger::kWalletAnonymous, balance->total));

if (balance->total == 0.0) {
braveledger_state::SetFetchOldBalanceEnabled(ledger_, false);
Expand Down

0 comments on commit 5aa7700

Please sign in to comment.