Skip to content

Commit

Permalink
Moves external wallet out of contribution class
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 23, 2020
1 parent fdfbcf9 commit e7b4691
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 152 deletions.
2 changes: 2 additions & 0 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ source_set("ledger") {
"src/bat/ledger/internal/contribution/contribution.h",
"src/bat/ledger/internal/contribution/contribution_ac.cc",
"src/bat/ledger/internal/contribution/contribution_ac.h",
"src/bat/ledger/internal/contribution/contribution_external_wallet.cc",
"src/bat/ledger/internal/contribution/contribution_external_wallet.h",
"src/bat/ledger/internal/contribution/contribution_monthly.cc",
"src/bat/ledger/internal/contribution/contribution_monthly.h",
"src/bat/ledger/internal/contribution/contribution_monthly_util.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
#include "bat/ledger/internal/common/time_util.h"
#include "bat/ledger/internal/contribution/contribution.h"
#include "bat/ledger/internal/contribution/contribution_ac.h"
#include "bat/ledger/internal/contribution/contribution_external_wallet.h"
#include "bat/ledger/internal/contribution/contribution_monthly.h"
#include "bat/ledger/internal/contribution/contribution_tip.h"
#include "bat/ledger/internal/contribution/contribution_unblinded.h"
#include "bat/ledger/internal/contribution/contribution_util.h"
#include "bat/ledger/internal/contribution/unverified.h"
#include "bat/ledger/internal/uphold/uphold.h"
#include "bat/ledger/internal/uphold/uphold_util.h"
#include "bat/ledger/internal/wallet/balance.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "brave_base/random.h"
Expand Down Expand Up @@ -67,6 +67,11 @@ Contribution::Contribution(bat_ledger::LedgerImpl* ledger) :
tip_(std::make_unique<ContributionTip>(ledger, this)),
last_reconcile_timer_id_(0u),
queue_timer_id_(0u) {
DCHECK(ledger_);
external_wallet_ = std::make_unique<ContributionExternalWallet>(
ledger,
this,
uphold_.get());
}

Contribution::~Contribution() = default;
Expand Down Expand Up @@ -374,13 +379,7 @@ void Contribution::OnEntrySaved(
} else if (current_wallet_type == ledger::kWalletAnonymous) {
// TODO implement
} else if (current_wallet_type == ledger::kWalletUphold) {
auto wallets_callback = std::bind(&Contribution::OnExternalWallets,
this,
contribution_id,
_1);

// Check if we have token
ledger_->GetExternalWallets(wallets_callback);
external_wallet_->Process(contribution_id);
}

if (queue->amount > 0) {
Expand Down Expand Up @@ -426,123 +425,4 @@ void Contribution::Process(
queue->Clone());
}

void Contribution::OnExternalWallets(
const std::string& contribution_id,
std::map<std::string, ledger::ExternalWalletPtr> wallets) {
if (wallets.size() == 0) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "No external wallets";
ledger_->UpdateContributionInfoStepAndCount(
contribution_id,
ledger::ContributionStep::STEP_FAILED,
-1,
[](const ledger::Result _){});
return;
}

ledger::ExternalWalletPtr wallet =
braveledger_uphold::GetWallet(std::move(wallets));

ledger_->GetContributionInfo(contribution_id,
std::bind(&Contribution::ExternalWalletContributionInfo,
this,
_1,
*wallet));
}

void Contribution::ExternalWalletContributionInfo(
ledger::ContributionInfoPtr contribution,
const ledger::ExternalWallet& wallet) {
// In this phase we only support one wallet
// so we will just always pick uphold.
// In the future we will allow user to pick which wallet to use via UI
// and then we will extend this function
if (wallet.token.empty() ||
wallet.status != ledger::WalletStatus::VERIFIED) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR)
<< "Wallet token is empty/wallet is not verified " << wallet.status;
ledger_->ContributionCompleted(
ledger::Result::LEDGER_ERROR,
contribution->amount,
contribution->contribution_id,
contribution->type);
}

if (contribution->type == ledger::RewardsType::AUTO_CONTRIBUTE) {
auto callback = std::bind(&Contribution::OnUpholdAC,
this,
_1,
_2,
contribution->contribution_id);
uphold_->TransferFunds(
contribution->amount,
ledger_->GetCardIdAddress(),
ledger::ExternalWallet::New(wallet),
callback);
return;
}

for (const auto& publisher : contribution->publishers) {
auto callback =
std::bind(&Contribution::OnExternalWalletServerPublisherInfo,
this,
_1,
contribution->contribution_id,
publisher->total_amount,
wallet,
contribution->type);

ledger_->GetServerPublisherInfo(publisher->publisher_key, callback);
}
}

void Contribution::OnExternalWalletServerPublisherInfo(
ledger::ServerPublisherInfoPtr info,
const std::string& contribution_id,
double amount,
const ledger::ExternalWallet& wallet,
const ledger::RewardsType type) {
if (!info || info->status != ledger::PublisherStatus::VERIFIED) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Publisher not found";
ledger_->ContributionCompleted(
ledger::Result::LEDGER_ERROR,
amount,
contribution_id,
type);
return;
}

auto completed_callback = std::bind(&Contribution::ExternalWalletCompleted,
this,
_1,
amount,
contribution_id,
type);

uphold_->StartContribution(
contribution_id,
std::move(info),
amount,
ledger::ExternalWallet::New(wallet),
completed_callback);
}

void Contribution::ExternalWalletCompleted(
const ledger::Result result,
const double amount,
const std::string& contribution_id,
const ledger::RewardsType type) {
ledger_->ContributionCompleted(result, amount, contribution_id, type);
}

void Contribution::OnUpholdAC(ledger::Result result,
bool created,
const std::string& contribution_id) {
if (result != ledger::Result::LEDGER_OK) {
// TODO(nejczdovc): add retries
return;
}

// TODO implement
}

} // namespace braveledger_contribution
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Uphold;
namespace braveledger_contribution {

class ContributionAC;
class ContributionExternalWallet;
class ContributionMonthly;
class ContributionTip;
class Unverified;
Expand Down Expand Up @@ -116,40 +117,16 @@ class Contribution {

void DeleteContributionQueue(const uint64_t id);

void OnExternalWallets(
const std::string& contribution_id,
std::map<std::string, ledger::ExternalWalletPtr> wallets);

void ExternalWalletContributionInfo(
ledger::ContributionInfoPtr contribution,
const ledger::ExternalWallet& wallet);

void OnExternalWalletServerPublisherInfo(
ledger::ServerPublisherInfoPtr info,
const std::string& contribution_id,
double amount,
const ledger::ExternalWallet& wallet,
const ledger::RewardsType type);

void OnUpholdAC(ledger::Result result,
bool created,
const std::string& contribution_id);

void OnDeleteContributionQueue(const ledger::Result result);

void ExternalWalletCompleted(
const ledger::Result result,
const double amount,
const std::string& contribution_id,
const ledger::RewardsType type);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
std::unique_ptr<Unverified> unverified_;
std::unique_ptr<Unblinded> unblinded_;
std::unique_ptr<braveledger_uphold::Uphold> uphold_;
std::unique_ptr<ContributionMonthly> monthly_;
std::unique_ptr<ContributionAC> ac_;
std::unique_ptr<ContributionTip> tip_;
std::unique_ptr<ContributionExternalWallet> external_wallet_;
uint32_t last_reconcile_timer_id_;
std::map<std::string, uint32_t> retry_timers_;
uint32_t queue_timer_id_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <utility>

#include "bat/ledger/internal/contribution/contribution_external_wallet.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/uphold/uphold_util.h"

using std::placeholders::_1;
using std::placeholders::_2;

namespace braveledger_contribution {

ContributionExternalWallet::ContributionExternalWallet(
bat_ledger::LedgerImpl* ledger,
Contribution* contribution,
braveledger_uphold::Uphold* uphold) :
ledger_(ledger),
contribution_(contribution),
uphold_(uphold) {
DCHECK(ledger_ && contribution_ && uphold_);
}

ContributionExternalWallet::~ContributionExternalWallet() = default;

void ContributionExternalWallet::Process(const std::string& contribution_id) {
auto wallets_callback = std::bind(
&ContributionExternalWallet::OnExternalWallets,
this,
contribution_id,
_1);

// Check if we have token
ledger_->GetExternalWallets(wallets_callback);
}

void ContributionExternalWallet::OnExternalWallets(
const std::string& contribution_id,
std::map<std::string, ledger::ExternalWalletPtr> wallets) {
if (wallets.size() == 0) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "No external wallets";
ledger_->UpdateContributionInfoStepAndCount(
contribution_id,
ledger::ContributionStep::STEP_FAILED,
-1,
[](const ledger::Result _){});
return;
}

ledger::ExternalWalletPtr wallet =
braveledger_uphold::GetWallet(std::move(wallets));

ledger_->GetContributionInfo(
contribution_id,
std::bind(&ContributionExternalWallet::ContributionInfo,
this,
_1,
*wallet));
}

void ContributionExternalWallet::ContributionInfo(
ledger::ContributionInfoPtr contribution,
const ledger::ExternalWallet& wallet) {
if (!contribution) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Contribution is null";
return;
}

// In this phase we only support one wallet
// so we will just always pick uphold.
// In the future we will allow user to pick which wallet to use via UI
// and then we will extend this function
if (wallet.token.empty() ||
wallet.status != ledger::WalletStatus::VERIFIED) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR)
<< "Wallet token is empty/wallet is not verified " << wallet.status;
ledger_->ContributionCompleted(
ledger::Result::LEDGER_ERROR,
contribution->amount,
contribution->contribution_id,
contribution->type);
}

if (contribution->type == ledger::RewardsType::AUTO_CONTRIBUTE) {
auto callback = std::bind(&ContributionExternalWallet::OnAC,
this,
_1,
_2,
contribution->contribution_id);

uphold_->TransferFunds(
contribution->amount,
ledger_->GetCardIdAddress(),
ledger::ExternalWallet::New(wallet),
callback);
return;
}

for (const auto& publisher : contribution->publishers) {
auto callback =
std::bind(&ContributionExternalWallet::OnServerPublisherInfo,
this,
_1,
contribution->contribution_id,
publisher->total_amount,
wallet,
contribution->type);

ledger_->GetServerPublisherInfo(publisher->publisher_key, callback);
}
}

void ContributionExternalWallet::OnAC(
const ledger::Result result,
const bool created,
const std::string& contribution_id) {
if (result != ledger::Result::LEDGER_OK) {
// TODO(nejczdovc): add retries
return;
}

// TODO implement
}

void ContributionExternalWallet::OnServerPublisherInfo(
ledger::ServerPublisherInfoPtr info,
const std::string& contribution_id,
const double amount,
const ledger::ExternalWallet& wallet,
const ledger::RewardsType type) {
if (!info || info->status != ledger::PublisherStatus::VERIFIED) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Publisher not found";
ledger_->ContributionCompleted(
ledger::Result::LEDGER_ERROR,
amount,
contribution_id,
type);
return;
}

auto completed_callback =
std::bind(&ContributionExternalWallet::Completed,
this,
_1,
amount,
contribution_id,
type);

uphold_->StartContribution(
contribution_id,
std::move(info),
amount,
ledger::ExternalWallet::New(wallet),
completed_callback);
}

void ContributionExternalWallet::Completed(
const ledger::Result result,
const double amount,
const std::string& contribution_id,
const ledger::RewardsType type) {
ledger_->ContributionCompleted(result, amount, contribution_id, type);
}

} // namespace braveledger_contribution
Loading

0 comments on commit e7b4691

Please sign in to comment.