diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 40e3ef39d149..7d9737661d9c 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -3307,22 +3307,27 @@ void RewardsServiceImpl::FetchBalance(FetchBalanceCallback callback) { void RewardsServiceImpl::SaveExternalWallet(const std::string& wallet_type, ledger::ExternalWalletPtr wallet) { - auto* perfs = + auto* wallets = profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets); - base::Value new_perfs(perfs->Clone()); + base::Value new_wallets(wallets->Clone()); - base::Value dict(base::Value::Type::DICTIONARY); - dict.SetStringKey("token", wallet->token); - dict.SetStringKey("address", wallet->address); - dict.SetIntKey("status", static_cast(wallet->status)); - dict.SetStringKey("one_time_string", wallet->one_time_string); - dict.SetStringKey("user_name", wallet->user_name); - dict.SetBoolKey("transferred", wallet->transferred); + auto* old_wallet = new_wallets.FindDictKey(wallet_type); + base::Value new_wallet(base::Value::Type::DICTIONARY); + if (old_wallet) { + new_wallet = old_wallet->Clone(); + } + + new_wallet.SetStringKey("token", wallet->token); + new_wallet.SetStringKey("address", wallet->address); + new_wallet.SetIntKey("status", static_cast(wallet->status)); + new_wallet.SetStringKey("one_time_string", wallet->one_time_string); + new_wallet.SetStringKey("user_name", wallet->user_name); + new_wallet.SetBoolKey("transferred", wallet->transferred); - new_perfs.SetKey(wallet_type, std::move(dict)); + new_wallets.SetKey(wallet_type, std::move(new_wallet)); - profile_->GetPrefs()->Set(prefs::kRewardsExternalWallets, new_perfs); + profile_->GetPrefs()->Set(prefs::kRewardsExternalWallets, new_wallets); } void RewardsServiceImpl::GetExternalWallets( @@ -3582,6 +3587,126 @@ void RewardsServiceImpl::OnGetServerPublisherInfo( callback(std::move(info)); } +void RewardsServiceImpl::SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) { + if (!transfer_fee) { + return; + } + + base::Value fee(base::Value::Type::DICTIONARY); + fee.SetStringKey("id", transfer_fee->id); + fee.SetDoubleKey("amount", transfer_fee->amount); + fee.SetIntKey("execution_timestamp", transfer_fee->execution_timestamp); + fee.SetIntKey("execution_id", transfer_fee->execution_id); + + auto* external_wallets = + profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets); + + base::Value new_external_wallets(external_wallets->Clone()); + + auto* wallet = new_external_wallets.FindDictKey(wallet_type); + base::Value new_wallet(base::Value::Type::DICTIONARY); + if (wallet) { + new_wallet = wallet->Clone(); + } + + auto* fees = wallet->FindListKey("transfer_fees"); + base::Value new_fees(base::Value::Type::DICTIONARY); + if (fees) { + new_fees = fees->Clone(); + } + new_fees.SetKey(transfer_fee->id, std::move(fee)); + + new_wallet.SetKey("transfer_fees", std::move(new_fees)); + new_external_wallets.SetKey(wallet_type, std::move(new_wallet)); + + profile_->GetPrefs()->Set( + prefs::kRewardsExternalWallets, + new_external_wallets); +} + +ledger::TransferFeeList RewardsServiceImpl::GetTransferFees( + const std::string& wallet_type) { + ledger::TransferFeeList fees; + + auto* external_wallets = + profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets); + + auto* wallet_key = external_wallets->FindKey(wallet_type); + + const base::DictionaryValue* wallet; + if (!wallet_key || !wallet_key->GetAsDictionary(&wallet)) { + return fees; + } + + auto* fees_key = wallet->FindKey("transfer_fees"); + const base::DictionaryValue* fee_dict; + if (!fees_key || !fees_key->GetAsDictionary(&fee_dict)) { + return fees; + } + + for (auto& item : *fee_dict) { + const base::DictionaryValue* fee_dict; + if (!item.second || !item.second->GetAsDictionary(&fee_dict)) { + continue; + } + + auto fee = ledger::TransferFee::New(); + + auto* id = fee_dict->FindKey("id"); + if (!id || !id->is_string()) { + continue; + } + fee->id = id->GetString(); + + auto* amount = fee_dict->FindKey("amount"); + if (!amount || !amount->is_double()) { + continue; + } + fee->amount = amount->GetDouble(); + + auto* timestamp = fee_dict->FindKey("execution_timestamp"); + if (!timestamp || !timestamp->is_int()) { + continue; + } + fee->execution_timestamp = timestamp->GetInt(); + + auto* execution_id = fee_dict->FindKey("execution_id"); + if (!execution_id || !execution_id->is_int()) { + continue; + } + fee->execution_id = execution_id->GetInt(); + + fees.insert(std::make_pair(fee->id, std::move(fee))); + } + + return fees; +} + +void RewardsServiceImpl::RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) { + auto* external_wallets = + profile_->GetPrefs()->GetDictionary(prefs::kRewardsExternalWallets); + + base::Value new_external_wallets(external_wallets->Clone()); + + const auto path = base::StringPrintf( + "%s.transfer_fees.%s", + wallet_type.c_str(), + id.c_str()); + + bool success = new_external_wallets.RemovePath(path); + if (!success) { + return; + } + + profile_->GetPrefs()->Set( + prefs::kRewardsExternalWallets, + new_external_wallets); +} + bool RewardsServiceImpl::OnlyAnonWallet() { const int32_t current_country = country_codes::GetCountryIDFromPrefs(profile_->GetPrefs()); diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index c4cba0ff3208..da620449f971 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -631,6 +631,17 @@ class RewardsServiceImpl : public RewardsService, const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback) override; + void SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) override; + + ledger::TransferFeeList GetTransferFees( + const std::string& wallet_type) override; + + void RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) override; + // end ledger::LedgerClient // Mojo Proxy methods diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc index 667bd40940a0..cfa3eac20185 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -906,4 +906,23 @@ void BatLedgerClientMojoProxy::GetServerPublisherInfo( base::BindOnce(&OnGetServerPublisherInfo, std::move(callback))); } +void BatLedgerClientMojoProxy::SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) { + bat_ledger_client_->SetTransferFee(wallet_type, std::move(transfer_fee)); +} + +ledger::TransferFeeList BatLedgerClientMojoProxy::GetTransferFees( + const std::string& wallet_type) { + base::flat_map list; + bat_ledger_client_->GetTransferFees(wallet_type, &list); + return mojo::FlatMapToMap(std::move(list)); +} + +void BatLedgerClientMojoProxy::RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) { + bat_ledger_client_->RemoveTransferFee(wallet_type, id); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h index 5d1f6fb66dc0..92d3f231678b 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -184,6 +184,18 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback) override; + + ledger::TransferFeeList GetTransferFees( + const std::string& wallet_type) override; + + void SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) override; + + void RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) override; + private: bool Connected() const; diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc index 1b8627e25051..60b161ca7873 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc @@ -945,4 +945,23 @@ void LedgerClientMojoProxy::GetServerPublisherInfo( _1)); } +void LedgerClientMojoProxy::GetTransferFees( + const std::string& wallet_type, + GetTransferFeesCallback callback) { + auto list = ledger_client_->GetTransferFees(wallet_type); + std::move(callback).Run(mojo::MapToFlatMap(std::move(list))); +} + +void LedgerClientMojoProxy::SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) { + ledger_client_->SetTransferFee(wallet_type, std::move(transfer_fee)); +} + +void LedgerClientMojoProxy::RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) { + ledger_client_->RemoveTransferFee(wallet_type, id); +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h index cf0bd8dd1033..8a87425afcdd 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h @@ -189,6 +189,18 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, const std::string& publisher_key, GetServerPublisherInfoCallback callback) override; + void SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) override; + + void GetTransferFees( + const std::string& wallet_type, + GetTransferFeesCallback callback) override; + + void RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) override; + private: // workaround to pass base::OnceCallback into std::bind // also serves as a wrapper for passing ledger::LedgerCallbackHandler* diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index 8d1da6d94592..be34cbae7527 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -273,4 +273,9 @@ interface BatLedgerClient { ClearAndInsertServerPublisherList(array list) => (ledger.mojom.Result result); GetServerPublisherInfo(string publisher_key) => (ledger.mojom.ServerPublisherInfo? info); + + [Sync] + GetTransferFees(string wallet_type) => (map list); + SetTransferFee(string wallet_type, ledger.mojom.TransferFee transfer_fee); + RemoveTransferFee(string wallet_type, string id); }; diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h index a7281b9fe29a..2265e36072d7 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h @@ -326,6 +326,17 @@ class MockConfirmationsClient : public ConfirmationsClient { MOCK_METHOD2(GetServerPublisherInfo, void( const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback)); + + MOCK_METHOD2(SetTransferFee, void( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee)); + + MOCK_METHOD1(GetTransferFees, ledger::TransferFeeList( + const std::string& wallet_type)); + + MOCK_METHOD2(RemoveTransferFee, void( + const std::string& wallet_type, + const std::string& id)); }; } // namespace confirmations diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index e395056547c5..e3ef9ca10f20 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -281,6 +281,17 @@ class LEDGER_EXPORT LedgerClient { virtual void GetServerPublisherInfo( const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback) = 0; + + virtual void SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) = 0; + + virtual ledger::TransferFeeList GetTransferFees( + const std::string& wallet_type) = 0; + + virtual void RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) = 0; }; } // namespace ledger diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom index 61ae1a479cd6..a8251fd3583d 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -239,3 +239,10 @@ struct ServerPublisherInfo { string address; PublisherBanner? banner; }; + +struct TransferFee { + string id; + double amount; + uint64 execution_timestamp; + uint32 execution_id; +}; diff --git a/vendor/bat-native-ledger/include/bat/ledger/wallet_properties.h b/vendor/bat-native-ledger/include/bat/ledger/wallet_properties.h index 43a88ee4417c..44da683979a0 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/wallet_properties.h +++ b/vendor/bat-native-ledger/include/bat/ledger/wallet_properties.h @@ -6,6 +6,8 @@ #ifndef BAT_LEDGER_WALLET_PROPERTIES_HANDLER_ #define BAT_LEDGER_WALLET_PROPERTIES_HANDLER_ +#include +#include #include "bat/ledger/public/interfaces/ledger.mojom.h" @@ -17,6 +19,10 @@ using WalletPropertiesPtr = ledger::mojom::WalletPropertiesPtr; using ExternalWallet = ledger::mojom::ExternalWallet; using ExternalWalletPtr = ledger::mojom::ExternalWalletPtr; +using TransferFee = ledger::mojom::TransferFee; +using TransferFeePtr = ledger::mojom::TransferFeePtr; +using TransferFeeList = std::map; + const char kWalletAnonymous[] = "anonymous"; const char kWalletUphold[] = "uphold"; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc index 7d4c0706e3c7..a550dfa39cd4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.cc @@ -41,10 +41,9 @@ Contribution::Contribution(bat_ledger::LedgerImpl* ledger) : Contribution::~Contribution() { } - -void Contribution::OnStartUp() { - // Check if we have some more pending ballots to go out - phase_two_->PrepareBallots(); +void Contribution::Initialize() { + phase_two_->Initialize(); + uphold_->Initialize(); // Resume in progress contributions braveledger_bat_helper::CurrentReconciles currentReconciles = @@ -395,6 +394,7 @@ void Contribution::InitReconcile( void Contribution::OnTimer(uint32_t timer_id) { phase_two_->OnTimer(timer_id); unverified_->OnTimer(timer_id); + uphold_->OnTimer(timer_id); if (timer_id == last_reconcile_timer_id_) { last_reconcile_timer_id_ = 0; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h index d72c83f882cf..577d011d5cf8 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h @@ -130,7 +130,7 @@ class Contribution { ~Contribution(); - void OnStartUp(); + void Initialize(); // Initial point for contribution // In this step we get balance from the server diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.cc index cabc5296112f..49d93e4a6817 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.cc @@ -36,6 +36,11 @@ PhaseTwo::PhaseTwo(bat_ledger::LedgerImpl* ledger, PhaseTwo::~PhaseTwo() { } +void PhaseTwo::Initialize() { + // Check if we have some more pending ballots to go out + PrepareBallots(); +} + void PhaseTwo::Start(const std::string& viewing_id) { unsigned int ballots_count = GetBallotsCount(viewing_id); const auto reconcile = ledger_->GetReconcileById(viewing_id); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.h index 3bb340ae73a3..d94d7f1b3e40 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two.h @@ -30,6 +30,8 @@ class PhaseTwo { ~PhaseTwo(); + void Initialize(); + void Start(const std::string& viewing_id); void PrepareBallots(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 818fbeae5440..5c051b11bea6 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -262,7 +262,6 @@ void LedgerImpl::OnLedgerStateLoaded(ledger::Result result, auto callback = std::bind( &LedgerImpl::OnPublisherStateLoaded, this, _1, _2); LoadPublisherState(std::move(callback)); - bat_contribution_->OnStartUp(); } } else { if (result != ledger::Result::NO_LEDGER_STATE) { @@ -343,6 +342,7 @@ void LedgerImpl::OnWalletInitialized(ledger::Result result) { bat_publisher_->SetPublisherServerListTimer(); bat_contribution_->SetReconcileTimer(); RefreshGrant(false); + bat_contribution_->Initialize(); } else { BLOG(this, ledger::LogLevel::LOG_ERROR) << "Failed to initialize wallet"; } @@ -1521,4 +1521,21 @@ void LedgerImpl::ClearState(const std::string& name) { ledger_client_->ClearState(name); } +void LedgerImpl::SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee) { + ledger_client_->SetTransferFee(wallet_type, std::move(transfer_fee)); +} + +ledger::TransferFeeList LedgerImpl::GetTransferFees( + const std::string& wallet_type) const { + return ledger_client_->GetTransferFees(wallet_type); +} + +void LedgerImpl::RemoveTransferFee( + const std::string& wallet_type, + const std::string& id) { + ledger_client_->RemoveTransferFee(wallet_type, id); +} + } // namespace bat_ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 026cd639c0aa..c82d1541ff99 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -542,6 +542,15 @@ class LedgerImpl : public ledger::Ledger, void ClearState(const std::string& name); + void SetTransferFee( + const std::string& wallet_type, + ledger::TransferFeePtr transfer_fee); + + ledger::TransferFeeList GetTransferFees(const std::string& wallet_type) const; + + void RemoveTransferFee( + const std::string& wallet_type, + const std::string& id); private: void OnLoad(ledger::VisitDataPtr visit_data, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc index 6649f1dd9866..d07fd9ffaaed 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.cc @@ -5,8 +5,10 @@ #include +#include "base/guid.h" #include "base/json/json_reader.h" #include "base/strings/stringprintf.h" +#include "base/strings/string_number_conversions.h" #include "bat/ledger/internal/uphold/uphold.h" #include "bat/ledger/internal/uphold/uphold_authorization.h" #include "bat/ledger/internal/uphold/uphold_card.h" @@ -14,6 +16,7 @@ #include "bat/ledger/internal/uphold/uphold_transfer.h" #include "bat/ledger/internal/uphold/uphold_wallet.h" #include "bat/ledger/internal/ledger_impl.h" +#include "brave_base/random.h" #include "net/http/http_status_code.h" using std::placeholders::_1; @@ -34,6 +37,17 @@ Uphold::Uphold(bat_ledger::LedgerImpl* ledger) : Uphold::~Uphold() { } +void Uphold::Initialize() { + auto fees = ledger_->GetTransferFees(ledger::kWalletUphold); + for (auto const& value : fees) { + if (!value.second) { + continue; + } + + SaveTransferFee(value.second->Clone()); + } +} + void Uphold::StartContribution( const std::string& viewing_id, const std::string& address, @@ -71,17 +85,13 @@ void Uphold::ContributionCompleted( const auto amount = ConvertToProbi(std::to_string(reconcile.fee_)); if (result == ledger::Result::LEDGER_OK) { - // 5% fee - auto fee_callback = std::bind(&Uphold::OnFeeCompleted, - this, - _1, - _2, - viewing_id); - - transfer_->Start(fee, - GetFeeAddress(), - ledger::ExternalWallet::New(wallet), - fee_callback); + const auto current_time_seconds = base::Time::Now().ToDoubleT(); + auto transfer_fee = ledger::TransferFee::New(); + transfer_fee->id = viewing_id; + transfer_fee->amount = fee; + transfer_fee->execution_timestamp = + current_time_seconds + brave_base::random::Geometric(60); + SaveTransferFee(std::move(transfer_fee)); } ledger_->OnReconcileComplete(result, @@ -97,11 +107,6 @@ void Uphold::ContributionCompleted( } } -void Uphold::OnFeeCompleted(ledger::Result result, - bool created, - const std::string &viewing_id) { -} - void Uphold::FetchBalance( std::map wallets, FetchBalanceCallback callback) { @@ -276,4 +281,88 @@ void Uphold::CreateAnonAddressIfNecessary( card_->CreateAnonAddressIfNecessary(std::move(wallet), callback); } +void Uphold::SaveTransferFee(ledger::TransferFeePtr transfer_fee) { + if (!transfer_fee) { + return; + } + + auto timer_id = 0u; + SetTimer(&timer_id); + transfer_fee->execution_id = timer_id; + ledger_->SetTransferFee(ledger::kWalletUphold, std::move(transfer_fee)); +} + +void Uphold::OnTransferFeeCompleted( + const ledger::Result result, + const bool created, + const ledger::TransferFee& transfer_fee) { + if (result == ledger::Result::LEDGER_OK) { + ledger_->RemoveTransferFee(ledger::kWalletUphold, transfer_fee.id); + return; + } + + SaveTransferFee(ledger::TransferFee::New(transfer_fee)); +} + +void Uphold::TransferFee( + const ledger::Result result, + ledger::ExternalWalletPtr wallet, + const ledger::TransferFee& transfer_fee) { + + if (result != ledger::Result::LEDGER_OK) { + SaveTransferFee(ledger::TransferFee::New(transfer_fee)); + return; + } + + auto callback = std::bind(&Uphold::OnTransferFeeCompleted, + this, + _1, + _2, + transfer_fee); + + transfer_->Start( + transfer_fee.amount, + GetFeeAddress(), + std::move(wallet), + callback); +} + +void Uphold::TransferFeeOnTimer(const uint32_t timer_id) { + const auto fees = ledger_->GetTransferFees(ledger::kWalletUphold); + + for (auto const& value : fees) { + const auto fee = *value.second; + if (fee.execution_id == timer_id) { + auto callback = std::bind(&Uphold::TransferFee, + this, + _1, + _2, + fee); + + ledger_->GetExternalWallet(ledger::kWalletUphold, callback); + return; + } + } +} + +void Uphold::OnTimer(uint32_t timer_id) { + BLOG(ledger_, ledger::LogLevel::LOG_INFO) + << "OnTimer Uphold: " + << timer_id; + + TransferFeeOnTimer(timer_id); +} + +void Uphold::SetTimer(uint32_t* timer_id, uint64_t start_timer_in) { + if (start_timer_in == 0) { + start_timer_in = brave_base::random::Geometric(45); + } + + BLOG(ledger_, ledger::LogLevel::LOG_INFO) + << "Starts Uphold timer in " + << start_timer_in; + + ledger_->SetTimer(start_timer_in, timer_id); +} + } // namespace braveledger_uphold diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h index ae1ebf4964a3..463813f8a980 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold.h @@ -6,6 +6,8 @@ #ifndef BRAVELEDGER_UPHOLD_UPHOLD_H_ #define BRAVELEDGER_UPHOLD_UPHOLD_H_ +#include + #include #include #include @@ -37,6 +39,8 @@ class Uphold { ~Uphold(); + void Initialize(); + void StartContribution( const std::string& viewing_id, const std::string& address, @@ -79,6 +83,8 @@ class Uphold { ledger::ExternalWalletPtr wallet, CreateAnonAddressCallback callback); + void OnTimer(uint32_t timer_id); + private: void ContributionCompleted( const ledger::Result result, @@ -106,6 +112,22 @@ class Uphold { ledger::Result result, ledger::ExternalWalletPtr wallet); + void SaveTransferFee(ledger::TransferFeePtr transfer_fee); + + void OnTransferFeeCompleted( + const ledger::Result result, + const bool created, + const ledger::TransferFee& transfer_fee); + + void TransferFee( + const ledger::Result result, + ledger::ExternalWalletPtr wallet, + const ledger::TransferFee& transfer_fee); + + void TransferFeeOnTimer(const uint32_t timer_id); + + void SetTimer(uint32_t* timer_id, uint64_t start_timer_in = 0); + std::unique_ptr transfer_; std::unique_ptr card_; std::unique_ptr user_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc index 08db99fc97e9..d9f03a2f7d18 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_transfer.cc @@ -154,7 +154,6 @@ void UpholdTransfer::OnCommitTransaction( } if (response_status_code != net::HTTP_OK) { - // TODO(nejczdovc): add retry logic to all errors in this function callback(ledger::Result::LEDGER_ERROR, true); return; } diff --git a/vendor/brave-ios/Ledger/BATBraveLedger.mm b/vendor/brave-ios/Ledger/BATBraveLedger.mm index f466531a30e0..6c0e432ae7cd 100644 --- a/vendor/brave-ios/Ledger/BATBraveLedger.mm +++ b/vendor/brave-ios/Ledger/BATBraveLedger.mm @@ -1733,6 +1733,23 @@ - (void)clearAndInsertServerPublisherList:(ledger::ServerPublisherInfoList)list // FIXME: Add implementation } +- (void)setTransferFee:(const std::string &)wallet_type transfer_fee:(ledger::TransferFeePtr)transfer_fee +{ + // FIXME: Add implementation +} + +- (ledger::TransferFeeList)getTransferFees:(const std::string &)wallet_type +{ + // FIXME: Add implementation + ledger::TransferFeeList list; + return list; +} + +- (void)removeTransferFee:(const std::string &)wallet_type id:(const std::string &)id +{ + // FIXME: Add implementation +} + @end // FIXME: This is a patch, need to use the actual verified state diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h index b60f2ccd02e8..bff556e5e694 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h @@ -79,4 +79,7 @@ class NativeLedgerClient : public ledger::LedgerClient { void DeleteActivityInfo(const std::string& publisher_key, ledger::DeleteActivityInfoCallback callback) override; void ClearAndInsertServerPublisherList(ledger::ServerPublisherInfoList list, ledger::ClearAndInsertServerPublisherListCallback callback) override; void GetServerPublisherInfo(const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback) override; + void SetTransferFee(const std::string& wallet_type, ledger::TransferFeePtr transfer_fee) override; + ledger::TransferFeeList GetTransferFees(const std::string& wallet_type) override; + void RemoveTransferFee(const std::string& wallet_type, const std::string& id) override; }; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm index 0ad2ba732125..3aab28fa7ad6 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm @@ -203,3 +203,12 @@ void NativeLedgerClient::GetServerPublisherInfo(const std::string& publisher_key, ledger::GetServerPublisherInfoCallback callback) { [bridge_ getServerPublisherInfo:publisher_key callback:callback]; } +void NativeLedgerClient::SetTransferFee(const std::string& wallet_type, ledger::TransferFeePtr transfer_fee) { + [bridge_ setTransferFee:wallet_type transfer_fee:std::move(transfer_fee)]; +} +ledger::TransferFeeList NativeLedgerClient::GetTransferFees(const std::string& wallet_type) { + return [bridge_ getTransferFees:wallet_type]; +} +void NativeLedgerClient::RemoveTransferFee(const std::string& wallet_type, const std::string& id) { + [bridge_ removeTransferFee:wallet_type id:id]; +} \ No newline at end of file diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h index dd12a6cb9a2a..e7f44966fd69 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h @@ -72,5 +72,8 @@ - (void)deleteActivityInfo:(const std::string&)publisher_key callback:(ledger::DeleteActivityInfoCallback)callback; - (void)clearAndInsertServerPublisherList:(ledger::ServerPublisherInfoList)list callback:(ledger::ClearAndInsertServerPublisherListCallback)callback; - (void)getServerPublisherInfo:(const std::string&)publisher_key callback:(ledger::GetServerPublisherInfoCallback) callback; +- (void)setTransferFee:(const std::string&)wallet_type transfer_fee:(ledger::TransferFeePtr)transfer_fee; +- (void)removeTransferFee:(const std::string&)wallet_type id:(const std::string&)id; +- (ledger::TransferFeeList)getTransferFees:(const std::string&)wallet_type; @end