Skip to content

Commit

Permalink
Get the latest balance from the server before doing contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 27, 2019
1 parent 3266871 commit cba09ed
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void BatContribution::ReconcilePublisherList(
new_list.push_back(new_publisher);
}

StartReconcile(viewing_id, category, new_list, {}, budget);
InitReconcile(viewing_id, category, new_list, {}, budget);
}

void BatContribution::ResetReconcileStamp() {
Expand Down Expand Up @@ -259,12 +259,56 @@ void BatContribution::StartAutoContribute() {
_2));
}

void BatContribution::StartReconcile(
void BatContribution::OnWalletPropertiesForReconcile(
const std::string& viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions,
double budget,
const ledger::Result result,
std::unique_ptr<ledger::WalletInfo> info) {
if (result != ledger::Result::LEDGER_OK || !info) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"We couldn't get balance from the server.";
OnReconcileComplete(ledger::Result::LEDGER_ERROR,
viewing_id,
category);
return;
}

StartReconcile(viewing_id,
category,
list,
directions,
budget,
info->balance_);
}

void BatContribution::InitReconcile(
const std::string& viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions,
double budget) {
ledger_->FetchWalletProperties(
std::bind(&BatContribution::OnWalletPropertiesForReconcile,
this,
viewing_id,
category,
list,
directions,
budget,
_1,
_2));
}

void BatContribution::StartReconcile(
const std::string& viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions,
double budget,
double balance) {
if (ledger_->ReconcileExists(viewing_id)) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Unable to reconcile with the same viewing id: " << viewing_id;
Expand All @@ -274,7 +318,6 @@ void BatContribution::StartReconcile(

auto reconcile = braveledger_bat_helper::CURRENT_RECONCILE();
double fee = .0;
double balance = ledger_->GetBalance();

if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) {
if (list.size() == 0 || budget > balance || budget == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

// Contribution process

// PHASE 0
// 1. InitReconcile

// PHASE 1 (reconcile)
// 1. StartReconcile
// 2. Reconcile
Expand Down Expand Up @@ -110,15 +113,24 @@ class BatContribution {

void OnStartUp();

// Starting point for contribution
// We determinate which contribution we want to do and do appropriate actions
void StartReconcile(
// Initial point for contribution
// In this step we get balance from the server
void InitReconcile(
const std::string &viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions = {},
double budget = 0);

// We determinate which contribution we want to do and do appropriate actions
void StartReconcile(
const std::string &viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions,
double budget,
double balance);

// Called when timer is triggered
void OnTimer(uint32_t timer_id);

Expand Down Expand Up @@ -170,6 +182,15 @@ class BatContribution {
// Triggers contribution process for auto contribute table
void StartAutoContribute();

void OnWalletPropertiesForReconcile(
const std::string& viewing_id,
const ledger::REWARDS_CATEGORY category,
const braveledger_bat_helper::PublisherList& list,
const braveledger_bat_helper::Directions& directions,
double budget,
const ledger::Result result,
std::unique_ptr<ledger::WalletInfo> info);

void Reconcile(const std::string &viewing_id);

void ReconcileCallback(const std::string& viewing_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ void LedgerImpl::DoDirectDonation(const ledger::PublisherInfo& publisher,
auto direction_list =
std::vector<braveledger_bat_helper::RECONCILE_DIRECTION> { direction };
braveledger_bat_helper::PublisherList list;
bat_contribution_->StartReconcile(GenerateGUID(),
bat_contribution_->InitReconcile(GenerateGUID(),
ledger::REWARDS_CATEGORY::DIRECT_DONATION,
list,
direction_list);
Expand Down

0 comments on commit cba09ed

Please sign in to comment.