diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index f6ce64cd862a..608a283cb163 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -105,13 +105,15 @@ class RewardsDOMHandler : public WebUIMessageHandler, void OnContentSiteUpdated( brave_rewards::RewardsService* rewards_service) override; void GetAddressesForPaymentId(const base::ListValue* args); - void GetConfirmationsHistory(const base::ListValue* args); + void GetTransactionHistoryForThisCycle(const base::ListValue* args); void GetRewardsMainEnabled(const base::ListValue* args); void OnGetRewardsMainEnabled(bool enabled); void GetExcludedPublishersNumber(const base::ListValue* args); - void OnConfirmationsHistory(int total_viewed, double estimated_earnings); + void OnTransactionHistoryForThisCycle( + int ads_notifications_received, + double estimated_earnings); void OnGetRecurringTips( std::unique_ptr list); @@ -160,7 +162,7 @@ class RewardsDOMHandler : public WebUIMessageHandler, brave_rewards::RewardsService* rewards_service, brave_rewards::ContentSiteList list) override; - void OnConfirmationsHistoryChanged( + void OnTransactionHistoryForThisCycleChanged( brave_rewards::RewardsService* rewards_service) override; void OnRecurringTipSaved(brave_rewards::RewardsService* rewards_service, @@ -286,8 +288,9 @@ void RewardsDOMHandler::RegisterMessages() { web_ui()->RegisterMessageCallback("brave_rewards.getAddressesForPaymentId", base::BindRepeating(&RewardsDOMHandler::GetAddressesForPaymentId, base::Unretained(this))); - web_ui()->RegisterMessageCallback("brave_rewards.getConfirmationsHistory", - base::BindRepeating(&RewardsDOMHandler::GetConfirmationsHistory, + web_ui()->RegisterMessageCallback( + "brave_rewards.getTransactionHistoryForThisCycle", + base::BindRepeating(&RewardsDOMHandler::GetTransactionHistoryForThisCycle, base::Unretained(this))); web_ui()->RegisterMessageCallback("brave_rewards.getRewardsMainEnabled", base::BindRepeating(&RewardsDOMHandler::GetRewardsMainEnabled, @@ -997,32 +1000,32 @@ void RewardsDOMHandler::GetAddressesForPaymentId( } } -void RewardsDOMHandler::GetConfirmationsHistory( +void RewardsDOMHandler::GetTransactionHistoryForThisCycle( const base::ListValue* args) { - rewards_service_->GetConfirmationsHistory(base::Bind( - &RewardsDOMHandler::OnConfirmationsHistory, - weak_factory_.GetWeakPtr())); + rewards_service_->GetTransactionHistoryForThisCycle(base::Bind( + &RewardsDOMHandler::OnTransactionHistoryForThisCycle, + weak_factory_.GetWeakPtr())); } -void RewardsDOMHandler::OnConfirmationsHistory( - int total_viewed, +void RewardsDOMHandler::OnTransactionHistoryForThisCycle( + int ads_notifications_received, double estimated_earnings) { if (web_ui()->CanCallJavascript()) { base::DictionaryValue history; - history.SetInteger("adsTotalPages", total_viewed); + history.SetInteger("adsNotificationsReceived", ads_notifications_received); history.SetDouble("adsEstimatedEarnings", estimated_earnings); web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards.confirmationsHistory", history); + "brave_rewards.transactionHistoryForThisCycle", history); } } -void RewardsDOMHandler::OnConfirmationsHistoryChanged( +void RewardsDOMHandler::OnTransactionHistoryForThisCycleChanged( brave_rewards::RewardsService* rewards_service) { if (web_ui()->CanCallJavascript()) { web_ui()->CallJavascriptFunctionUnsafe( - "brave_rewards.confirmationsHistoryChanged"); + "brave_rewards.transactionHistoryForThisCycleChanged"); } } diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index b6096fecc3ef..85722b341d29 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -68,7 +68,8 @@ using GetReconcileStampCallback = base::Callback; using IsWalletCreatedCallback = base::Callback; using GetPendingContributionsTotalCallback = base::Callback; using GetRewardsMainEnabledCallback = base::Callback; -using ConfirmationsHistoryCallback = base::Callback; +using GetTransactionHistoryForThisCycleCallback = + base::Callback; using GetRewardsInternalsInfoCallback = base::OnceCallback)>; using GetRecurringTipsCallback = base::OnceCallbackSetCatalogIssuers(json); } -std::pair RewardsServiceImpl::GetEarningsRange() { - auto now = base::Time::Now(); - base::Time::Exploded exploded; - now.LocalExplode(&exploded); - - if (exploded.day_of_month < 5) { - exploded.month--; - if (exploded.month < 1) { - exploded.month = 12; - - exploded.year--; - } - } - - exploded.day_of_month = 1; - - exploded.hour = 0; - exploded.minute = 0; - exploded.second = 0; - exploded.millisecond = 0; - - base::Time from_timestamp; - auto success = base::Time::FromLocalExploded(exploded, &from_timestamp); - DCHECK(success); - - uint64_t from_timestamp_in_seconds = - (from_timestamp - base::Time()).InSeconds(); - - uint64_t to_timestamp_in_seconds = - (now - base::Time()).InSeconds(); - - return std::make_pair(from_timestamp_in_seconds, to_timestamp_in_seconds); -} - void RewardsServiceImpl::ConfirmAd(const std::string& json) { if (!Connected()) { return; @@ -1568,51 +1534,44 @@ void RewardsServiceImpl::SetConfirmationsIsReady(const bool is_ready) { void RewardsServiceImpl::ConfirmationsTransactionHistoryDidChange() { for (auto& observer : observers_) - observer.OnConfirmationsHistoryChanged(this); + observer.OnTransactionHistoryForThisCycleChanged(this); } -void RewardsServiceImpl::GetConfirmationsHistory( - brave_rewards::ConfirmationsHistoryCallback callback) { +void RewardsServiceImpl::GetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback) { if (!Connected()) { return; } - auto earnings_range = GetEarningsRange(); - - bat_ledger_->GetConfirmationsHistory( - earnings_range.first, - earnings_range.second, - base::BindOnce(&RewardsServiceImpl::OnGetConfirmationsHistory, - AsWeakPtr(), - std::move(callback))); + bat_ledger_->GetTransactionHistoryForThisCycle( + base::BindOnce(&RewardsServiceImpl::OnGetTransactionHistoryForThisCycle, + AsWeakPtr(), std::move(callback))); } -void RewardsServiceImpl::OnGetConfirmationsHistory( - brave_rewards::ConfirmationsHistoryCallback callback, +void RewardsServiceImpl::OnGetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback, const std::string& transactions) { - std::unique_ptr info; - if (!transactions.empty()) { - info.reset(new ledger::TransactionsInfo()); - info->FromJson(transactions); - } - - if (!info) { + if (transactions.empty()) { callback.Run(0, 0.0); + return; } + ledger::TransactionsInfo info; + info.FromJson(transactions); + + int ads_notifications_received = 0; double estimated_earnings = 0.0; - int total_viewed = 0; - for (const auto& transaction : info->transactions) { + for (const auto& transaction : info.transactions) { if (transaction.estimated_redemption_value == 0.0) { continue; } + ads_notifications_received++; estimated_earnings += transaction.estimated_redemption_value; - total_viewed++; } - callback.Run(total_viewed, estimated_earnings); + callback.Run(ads_notifications_received, estimated_earnings); } void RewardsServiceImpl::SaveState(const std::string& name, diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 1e61fcb8e35b..f8aee947be13 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -198,7 +198,6 @@ class RewardsServiceImpl : public RewardsService, const GetRewardsMainEnabledCallback& callback) const override; void GetAddressesForPaymentId(const GetAddressesCallback& callback) override; - std::pair GetEarningsRange(); void GetOneTimeTipsUI(GetOneTimeTipsCallback callback) override; void RefreshPublisher( @@ -364,8 +363,8 @@ class RewardsServiceImpl : public RewardsService, void SetCatalogIssuers(const std::string& json) override; void ConfirmAd(const std::string& json) override; void SetConfirmationsIsReady(const bool is_ready) override; - void GetConfirmationsHistory( - brave_rewards::ConfirmationsHistoryCallback callback) override; + void GetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback) override; void ConfirmationsTransactionHistoryDidChange() override; void OnExcludedSitesChanged(const std::string& publisher_id, @@ -448,8 +447,8 @@ class RewardsServiceImpl : public RewardsService, void ShowNotificationTipsPaid(bool ac_enabled); // Mojo Proxy methods - void OnGetConfirmationsHistory( - brave_rewards::ConfirmationsHistoryCallback callback, + void OnGetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback, const std::string& transactions); void OnGetAllBalanceReports( const GetAllBalanceReportsCallback& callback, diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index ecfdc5024690..0092dea786fe 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -70,7 +70,7 @@ class RewardsServiceObserver : public base::CheckedObserver { virtual void OnPublisherListNormalized( RewardsService* rewards_service, brave_rewards::ContentSiteList list) {} - virtual void OnConfirmationsHistoryChanged( + virtual void OnTransactionHistoryForThisCycleChanged( brave_rewards::RewardsService* rewards_service) {} virtual void OnRecurringTipSaved( brave_rewards::RewardsService* rewards_service, diff --git a/components/brave_rewards/resources/ui/actions/rewards_actions.ts b/components/brave_rewards/resources/ui/actions/rewards_actions.ts index ff5c9bef1534..d52f507a9c4a 100644 --- a/components/brave_rewards/resources/ui/actions/rewards_actions.ts +++ b/components/brave_rewards/resources/ui/actions/rewards_actions.ts @@ -175,14 +175,14 @@ export const onAddressesForPaymentId = (addresses: Record - action(types.ON_CONFIRMATIONS_HISTORY, { +export const onTransactionHistoryForThisCycle = (data: {adsNotificationsReceived: number, adsEstimatedEarnings: number}) => + action(types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE, { data }) -export const getConfirmationsHistory = () => action(types.GET_CONFIRMATIONS_HISTORY) +export const getTransactionHistoryForThisCycle = () => action(types.GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE) -export const onConfirmationsHistoryChanged = () => action(types.ON_CONFIRMATIONS_HISTORY_CHANGED) +export const onTransactionHistoryForThisCycleChanged = () => action(types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED) export const getExcludedPublishersNumber = () => action(types.GET_EXCLUDED_PUBLISHERS_NUMBER) diff --git a/components/brave_rewards/resources/ui/brave_rewards.tsx b/components/brave_rewards/resources/ui/brave_rewards.tsx index 2b597e54e7f5..b8df0b29f4e8 100644 --- a/components/brave_rewards/resources/ui/brave_rewards.tsx +++ b/components/brave_rewards/resources/ui/brave_rewards.tsx @@ -147,12 +147,12 @@ window.cr.define('brave_rewards', function () { getActions().onAddressesForPaymentId(addresses) } - function confirmationsHistory (totalPages: number, estimatedEarnings: number) { - getActions().onConfirmationsHistory(totalPages, estimatedEarnings) + function transactionHistoryForThisCycle (totalPages: number, estimatedEarnings: number) { + getActions().onTransactionHistoryForThisCycle(totalPages, estimatedEarnings) } - function confirmationsHistoryChanged () { - getActions().onConfirmationsHistoryChanged() + function transactionHistoryForThisCycleChanged () { + getActions().onTransactionHistoryForThisCycleChanged() } function recurringTipSaved (success: boolean) { @@ -193,8 +193,8 @@ window.cr.define('brave_rewards', function () { onPendingContributionSaved, rewardsEnabled, addressesForPaymentId, - confirmationsHistory, - confirmationsHistoryChanged, + transactionHistoryForThisCycle, + transactionHistoryForThisCycleChanged, recurringTipSaved, recurringTipRemoved, onContributionSaved diff --git a/components/brave_rewards/resources/ui/components/settingsPage.tsx b/components/brave_rewards/resources/ui/components/settingsPage.tsx index 1b5d4a196f92..3cedee41c6bd 100644 --- a/components/brave_rewards/resources/ui/components/settingsPage.tsx +++ b/components/brave_rewards/resources/ui/components/settingsPage.tsx @@ -38,7 +38,7 @@ class SettingsPage extends React.Component { this.actions.getContributeList() this.actions.getPendingContributionsTotal() this.actions.getReconcileStamp() - this.actions.getConfirmationsHistory() + this.actions.getTransactionHistoryForThisCycle() this.actions.getExcludedPublishersNumber() this.actions.getAdsData() } diff --git a/components/brave_rewards/resources/ui/constants/rewards_types.ts b/components/brave_rewards/resources/ui/constants/rewards_types.ts index 193439c56c57..f904457f85ab 100644 --- a/components/brave_rewards/resources/ui/constants/rewards_types.ts +++ b/components/brave_rewards/resources/ui/constants/rewards_types.ts @@ -54,9 +54,9 @@ export const enum types { ON_REWARDS_ENABLED = '@@rewards/ON_REWARDS_ENABLED', GET_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/GET_ADDRESSES_FOR_PAYMENT_ID', ON_ADDRESSES_FOR_PAYMENT_ID = '@@rewards/ON_ADDRESSES_FOR_PAYMENT_ID', - GET_CONFIRMATIONS_HISTORY = '@@rewards/GET_CONFIRMATIONS_HISTORY', - ON_CONFIRMATIONS_HISTORY = '@@rewards/ON_CONFIRMATIONS_HISTORY', - ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED', + GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE = '@@rewards/GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE', + ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE = '@@rewards/ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE', + ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED = '@@rewards/ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED', GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER', GET_REWARDS_MAIN_ENABLED = '@@rewards/GET_REWARDS_MAIN_ENABLED', ON_RECURRING_TIP_SAVED = '@@rewards/ON_RECURRING_TIP_SAVED', diff --git a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts index a12c8bd14fb3..e786e912ba4e 100644 --- a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts +++ b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts @@ -139,19 +139,19 @@ const rewardsReducer: Reducer = (state: Rewards.State state.enabledMain = action.payload.enabled break } - case types.GET_CONFIRMATIONS_HISTORY: - case types.ON_CONFIRMATIONS_HISTORY_CHANGED: { - chrome.send('brave_rewards.getConfirmationsHistory', []) + case types.GET_TRANSACTION_HISTORY_FOR_THIS_CYCLE: + case types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE_CHANGED: { + chrome.send('brave_rewards.getTransactionHistoryForThisCycle', []) break } - case types.ON_CONFIRMATIONS_HISTORY: { + case types.ON_TRANSACTION_HISTORY_FOR_THIS_CYCLE: { if (!action.payload.data) { break } state = { ...state } const data = action.payload.data - state.adsData.adsNotificationsReceived = data.adsTotalPages + state.adsData.adsNotificationsReceived = data.adsNotificationsReceived state.adsData.adsEstimatedEarnings = data.adsEstimatedEarnings break } diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index 946cb12d776a..bd4e816f9ca7 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -197,7 +197,7 @@ Earn tokens by viewing ads in Brave. Ads presented are based on your interests, as inferred from your browsing behavior. No personal data or browsing history ever leaves your browser. Auto-Contribute An automatic way to support publishers and content creators. Set a monthly payment and browse normally. The sites you visit receive your contributions automatically, based on your attention as measured by Brave. - Current earnings this month (Estimated) + Estimated earnings this cycle Payment date Ad notifications received Sorry! Ads are not yet available in your region. diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index e6095d7e777a..f787d7fb7136 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -398,8 +398,8 @@ void BatLedgerImpl::ConfirmAd(const std::string& info) { } // static -void BatLedgerImpl::OnGetConfirmationsHistory( - CallbackHolder* holder, +void BatLedgerImpl::OnGetTransactionHistoryForThisCycle( + CallbackHolder* holder, std::unique_ptr history) { std::string json_transactions = history.get() ? history->ToJson() : ""; if (holder->is_valid()) @@ -407,16 +407,14 @@ void BatLedgerImpl::OnGetConfirmationsHistory( delete holder; } -void BatLedgerImpl::GetConfirmationsHistory( - const uint64_t from_timestamp_seconds, - const uint64_t to_timestamp_seconds, - GetConfirmationsHistoryCallback callback) { - auto* holder = new CallbackHolder( +void BatLedgerImpl::GetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback) { + auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - ledger_->GetConfirmationsHistory(from_timestamp_seconds, - to_timestamp_seconds, std::bind( - BatLedgerImpl::OnGetConfirmationsHistory, holder, _1)); + ledger_->GetTransactionHistoryForThisCycle( + std::bind(BatLedgerImpl::OnGetTransactionHistoryForThisCycle, + holder, _1)); } void BatLedgerImpl::GetRewardsInternalsInfo( diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index b27130a07082..409789214859 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -129,10 +129,8 @@ class BatLedgerImpl : public mojom::BatLedger, void GetAddressesForPaymentId( GetAddressesForPaymentIdCallback callback) override; - void GetConfirmationsHistory( - const uint64_t from_timestamp_seconds, - const uint64_t to_timestamp_seconds, - GetConfirmationsHistoryCallback callback) override; + void GetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback) override; void GetRewardsInternalsInfo( GetRewardsInternalsInfoCallback callback) override; void RefreshPublisher( @@ -188,8 +186,8 @@ class BatLedgerImpl : public mojom::BatLedger, CallbackHolder* holder, std::map addresses); - static void OnGetConfirmationsHistory( - CallbackHolder* holder, + static void OnGetTransactionHistoryForThisCycle( + CallbackHolder* holder, std::unique_ptr history); static void OnGetExcludedPublishersNumber( diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index ae12fc4c849f..949d33993b0d 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -100,8 +100,7 @@ interface BatLedger { SetCatalogIssuers(string info); ConfirmAd(string info); - GetConfirmationsHistory(uint64 from_timestamp_seconds, - uint64 to_timestamp_seconds) => (string transactions); + GetTransactionHistoryForThisCycle() => (string transactions); GetRewardsInternalsInfo() => (string info); GetRecurringTips() => (array list); diff --git a/vendor/bat-native-confirmations/README.md b/vendor/bat-native-confirmations/README.md index 297ca2608c07..1e6bbcb6c78f 100644 --- a/vendor/bat-native-confirmations/README.md +++ b/vendor/bat-native-confirmations/README.md @@ -22,12 +22,10 @@ void SetCatalogIssuers( std::unique_ptr info) ``` -`GetTransactionHistory` should be called to get transaction history +`GetTransactionHistoryForThisCycle` should be called to get transaction history for this cycle ``` void GetTransactionHistory( - const uint64_t from_timestamp_in_seconds, - const uint64_t to_timestamp_in_seconds, - OnGetTransactionHistoryCallback callback) + OnGetTransactionHistoryForThisCycle callback) ``` `ConfirmAd` should be called by Ads when clicking, viewing, dismissing or landing an Ad diff --git a/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h b/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h index b7c93ce991ce..0511d77caee2 100644 --- a/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h +++ b/vendor/bat-native-confirmations/include/bat/confirmations/confirmations.h @@ -31,8 +31,8 @@ extern const char _confirmations_name[]; using TransactionInfo = ::ledger::TransactionInfo; using TransactionsInfo = ::ledger::TransactionsInfo; -using OnGetTransactionHistoryCallback = - ::ledger::ConfirmationsHistoryCallback; +using OnGetTransactionHistoryForThisCycle = + ::ledger::GetTransactionHistoryForThisCycleCallback; class CONFIRMATIONS_EXPORT Confirmations { public: @@ -51,11 +51,9 @@ class CONFIRMATIONS_EXPORT Confirmations { // Should be called when a new catalog has been downloaded in Ads virtual void SetCatalogIssuers(std::unique_ptr info) = 0; - // Should be called to get transaction history - virtual void GetTransactionHistory( - const uint64_t from_timestamp_in_seconds, - const uint64_t to_timestamp_in_seconds, - OnGetTransactionHistoryCallback callback) = 0; + // Should be called to get transaction history for this cycle + virtual void GetTransactionHistoryForThisCycle( + OnGetTransactionHistoryForThisCycle callback) = 0; // Should be called when an ad is sustained in Ads virtual void ConfirmAd(std::unique_ptr info) = 0; diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc index e2177c3be7c8..565637c74a6e 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc @@ -788,10 +788,61 @@ void ConfirmationsImpl::RemoveConfirmationFromQueue( SaveState(); } -void ConfirmationsImpl::GetTransactionHistory( +uint64_t ConfirmationsImpl::GetEstimatedEarningsStartTimestampInSeconds() { + auto now = base::Time::Now(); + base::Time::Exploded exploded; + now.LocalExplode(&exploded); + + if (exploded.day_of_month < 5) { + exploded.month--; + if (exploded.month < 1) { + exploded.month = 12; + + exploded.year--; + } + } + + exploded.day_of_month = 1; + + exploded.hour = 0; + exploded.minute = 0; + exploded.second = 0; + exploded.millisecond = 0; + + base::Time from_timestamp; + auto success = base::Time::FromLocalExploded(exploded, &from_timestamp); + DCHECK(success); + + return (from_timestamp - base::Time()).InSeconds(); +} + +void ConfirmationsImpl::GetTransactionHistoryForThisCycle( + OnGetTransactionHistoryForThisCycle callback) { + auto transactions_info = std::make_unique(); + + auto from_timestamp_in_seconds = + GetEstimatedEarningsStartTimestampInSeconds(); + + auto to_timestamp_in_seconds = Time::NowInSeconds(); + + auto transactions = GetTransactionHistory(from_timestamp_in_seconds, + to_timestamp_in_seconds); + + auto unredeemed_transactions_for_previous_cycles = + GetUnredeemedTransactionsForPreviousCycles(from_timestamp_in_seconds); + + transactions.insert(transactions.end(), + unredeemed_transactions_for_previous_cycles.begin(), + unredeemed_transactions_for_previous_cycles.end()); + + transactions_info->transactions = transactions; + + callback(std::move(transactions_info)); +} + +std::vector ConfirmationsImpl::GetTransactionHistory( const uint64_t from_timestamp_in_seconds, - const uint64_t to_timestamp_in_seconds, - OnGetTransactionHistoryCallback callback) { + const uint64_t to_timestamp_in_seconds) { std::vector transactions(transaction_history_.size()); auto it = std::copy_if(transaction_history_.begin(), @@ -803,10 +854,34 @@ void ConfirmationsImpl::GetTransactionHistory( transactions.resize(std::distance(transactions.begin(), it)); - auto transactions_info = std::make_unique(); - transactions_info->transactions = transactions; + return transactions; +} - callback(std::move(transactions_info)); +std::vector +ConfirmationsImpl::GetUnredeemedTransactionsForPreviousCycles( + const uint64_t before_timestamp_in_seconds) { + auto unredeemed_transactions_count = unblinded_payment_tokens_->Count(); + if (unredeemed_transactions_count == 0) { + // There are no outstanding unblinded payment tokens to redeem + return {}; + } + + // Unredeemed transactions are always at the end of the history + std::vector transactions(transaction_history_.end() + - unredeemed_transactions_count, transaction_history_.end()); + + // Filter transactions which occurred for previous cycles + std::vector transactions_for_previous_cycles; + + for (const auto& transaction : transactions) { + if (transaction.timestamp_in_seconds >= before_timestamp_in_seconds) { + continue; + } + + transactions_for_previous_cycles.push_back(transaction); + } + + return transactions_for_previous_cycles; } double ConfirmationsImpl::GetEstimatedRedemptionValue( diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h index e78ee9160af8..6ab9f6d9fbe4 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h @@ -48,11 +48,18 @@ class ConfirmationsImpl : public Confirmations { void RemoveConfirmationFromQueue(const ConfirmationInfo& confirmation_info); void StartRetryingFailedConfirmations(const uint64_t start_timer_in); + // Estimated earnings + uint64_t GetEstimatedEarningsStartTimestampInSeconds(); + + void GetTransactionHistoryForThisCycle( + OnGetTransactionHistoryForThisCycle callback) override; + // Transaction history - void GetTransactionHistory( + std::vector GetTransactionHistory( const uint64_t from_timestamp_in_seconds, - const uint64_t to_timestamp_in_seconds, - OnGetTransactionHistoryCallback callback) override; + const uint64_t to_timestamp_in_seconds); + std::vector GetUnredeemedTransactionsForPreviousCycles( + const uint64_t before_timestamp_in_seconds); void AppendTransactionToHistory( const double estimated_redemption_value, const ConfirmationType confirmation_type); diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 235b71b2263c..79732fcfbb86 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -57,8 +57,8 @@ using PublisherBannerCallback = std::function banner)>; using WalletAddressesCallback = std::function addresses)>; -using ConfirmationsHistoryCallback = std::function info)>; +using GetTransactionHistoryForThisCycleCallback = + std::function info)>; using GetExcludedPublishersNumberDBCallback = std::function; using OnWalletPropertiesCallback = std::function)>; @@ -279,10 +279,8 @@ class LEDGER_EXPORT Ledger { virtual void SetCatalogIssuers(const std::string& info) = 0; virtual void ConfirmAd(const std::string& info) = 0; - virtual void GetConfirmationsHistory( - const uint64_t from_timestamp_seconds, - const uint64_t to_timestamp_seconds, - ledger::ConfirmationsHistoryCallback callback) = 0; + virtual void GetTransactionHistoryForThisCycle( + GetTransactionHistoryForThisCycleCallback callback) = 0; virtual void GetRewardsInternalsInfo(ledger::RewardsInternalsInfo* info) = 0; virtual void GetRecurringTips(ledger::PublisherInfoListCallback callback) = 0; 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 858ccab4f112..7ddd57391d74 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 @@ -1474,12 +1474,9 @@ void LedgerImpl::ConfirmAd(const std::string& info) { bat_confirmations_->ConfirmAd(std::move(notification_info)); } -void LedgerImpl::GetConfirmationsHistory( - const uint64_t from_timestamp_seconds, - const uint64_t to_timestamp_seconds, - ledger::ConfirmationsHistoryCallback callback) { - bat_confirmations_->GetTransactionHistory(from_timestamp_seconds, - to_timestamp_seconds, callback); +void LedgerImpl::GetTransactionHistoryForThisCycle( + ledger::GetTransactionHistoryForThisCycleCallback callback) { + bat_confirmations_->GetTransactionHistoryForThisCycle(callback); } void LedgerImpl::RefreshPublisher( 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 5aeff21f782c..d6ab4153b5b0 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 @@ -427,10 +427,8 @@ class LedgerImpl : public ledger::Ledger, void SetCatalogIssuers(const std::string& info) override; void ConfirmAd(const std::string& info) override; - void GetConfirmationsHistory( - const uint64_t from_timestamp_seconds, - const uint64_t to_timestamp_seconds, - ledger::ConfirmationsHistoryCallback callback) override; + void GetTransactionHistoryForThisCycle( + ledger::GetTransactionHistoryForThisCycleCallback callback) override; std::unique_ptr Log( const char* file,