From 9b2a395b3d9ffe8f856454a08b93feecdbc038b5 Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Wed, 10 Oct 2018 11:59:36 -0700 Subject: [PATCH] Merge pull request #409 from brave/reports-probi Use probi for reports --- DEPS | 2 +- browser/ui/webui/brave_rewards_ui.cc | 17 ++-- common/extensions/api/brave_rewards.json | 50 +++++----- .../brave_rewards/browser/balance_report.cc | 18 ++-- .../brave_rewards/browser/balance_report.h | 16 ++-- .../browser/rewards_service_impl.cc | 22 ++--- .../extension/brave_rewards/BUILD.gn | 1 + .../brave_rewards/_locales/en/messages.json | 4 +- .../brave_rewards/background/storage.ts | 17 ++-- .../brave_rewards/components/panel.tsx | 71 +++++++-------- .../extension/brave_rewards/utils.ts | 34 +++++++ .../ui/components/contributeBox.tsx | 19 +++- .../ui/components/donationsBox.tsx | 2 +- .../brave_rewards/ui/components/grant.tsx | 6 +- .../ui/components/pageWallet.tsx | 91 +++++++------------ components/brave_rewards/ui/utils.ts | 36 +++++--- components/definitions/rewards.d.ts | 17 ++-- components/definitions/rewardsExtensions.d.ts | 17 ++-- .../extension/brave_rewards/utils_test.ts | 56 ++++++++++++ .../ui}/actions/rewards_actions_test.ts | 4 +- .../ui}/components/app_test.tsx | 6 +- .../ui}/reducers/index_test.ts | 4 +- .../ui}/reducers/wallet_reducer_test.ts | 8 +- .../test/brave_rewards/ui/utils_test.ts | 66 ++++++++++++++ package-lock.json | 6 +- package.json | 2 +- 26 files changed, 378 insertions(+), 214 deletions(-) create mode 100644 components/brave_rewards/extension/brave_rewards/utils.ts create mode 100644 components/test/brave_rewards/extension/brave_rewards/utils_test.ts rename components/test/{brave_rewards_ui => brave_rewards/ui}/actions/rewards_actions_test.ts (86%) rename components/test/{brave_rewards_ui => brave_rewards/ui}/components/app_test.tsx (91%) rename components/test/{brave_rewards_ui => brave_rewards/ui}/reducers/index_test.ts (84%) rename components/test/{brave_rewards_ui => brave_rewards/ui}/reducers/wallet_reducer_test.ts (86%) create mode 100644 components/test/brave_rewards/ui/utils_test.ts diff --git a/DEPS b/DEPS index cf01975e195d..a27dc04baffd 100644 --- a/DEPS +++ b/DEPS @@ -10,7 +10,7 @@ deps = { "vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b", "vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403", "vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0", - "vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@fa06a4b1153b5a1a87f1af8becfa0803e82bc6f3", + "vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@fe65bbb07d8e3ef237ada14b54dd9390f12fc2ee", "vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e", "vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d", "vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@0559543f458a949b83b58035273ef7f8f1a1b111", diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index b164a30e53d7..acd4c498f00c 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -153,13 +153,15 @@ void RewardsDOMHandler::GetAllBalanceReports() { for (auto const& report : reports) { const brave_rewards::BalanceReport oldReport = report.second; auto newReport = std::make_unique(); - newReport->SetDouble("opening", oldReport.opening_balance); - newReport->SetDouble("closing", oldReport.closing_balance); - newReport->SetDouble("grants", oldReport.grants); - newReport->SetDouble("ads", oldReport.earning_from_ads); - newReport->SetDouble("contribute", oldReport.auto_contribute); - newReport->SetDouble("donations", oldReport.recurring_donation); - newReport->SetDouble("oneTime", oldReport.one_time_donation); + newReport->SetString("opening", oldReport.opening_balance); + newReport->SetString("closing", oldReport.closing_balance); + newReport->SetString("grant", oldReport.grants); + newReport->SetString("deposit", oldReport.deposits); + newReport->SetString("ads", oldReport.earning_from_ads); + newReport->SetString("contribute", oldReport.auto_contribute); + newReport->SetString("donation", oldReport.recurring_donation); + newReport->SetString("tips", oldReport.one_time_donation); + newReport->SetString("total", oldReport.total); newReports.SetDictionary(report.first, std::move(newReport)); } @@ -395,6 +397,7 @@ void RewardsDOMHandler::SaveSetting(const base::ListValue* args) { if (key == "contributionMonthly") { rewards_service_->SetUserChangedContribution(); rewards_service_->SetContributionAmount(std::stod(value)); + GetAllBalanceReports(); } if (key == "contributionMinTime") { diff --git a/common/extensions/api/brave_rewards.json b/common/extensions/api/brave_rewards.json index d3fbd0299b3b..bbae4759816b 100644 --- a/common/extensions/api/brave_rewards.json +++ b/common/extensions/api/brave_rewards.json @@ -138,33 +138,41 @@ "name": "properties", "type": "object", "properties": { - "openingBalance": { - "type": "double", - "description": "balance defined in BAT" + "ads": { + "type": "string", + "description": "balance defined in probi" }, - "closingBalance": { - "type": "double", - "description": "balance defined in BAT" + "closing": { + "type": "string", + "description": "balance defined in probi" }, - "grants": { - "type": "double", - "description": "balance defined in BAT" + "contribute": { + "type": "string", + "description": "balance defined in probi" }, - "earningFromAds": { - "type": "double", - "description": "balance defined in BAT" + "deposit": { + "type": "string", + "description": "balance defined in probi" }, - "autoContribute": { - "type": "double", - "description": "balance defined in BAT" + "grant": { + "type": "string", + "description": "balance defined in probi" }, - "recurringDonation": { - "type": "double", - "description": "balance defined in BAT" + "opening": { + "type": "string", + "description": "balance defined in probi" }, - "oneTimeDonation": { - "type": "double", - "description": "balance defined in BAT" + "recurring": { + "type": "string", + "description": "balance defined in probi" + }, + "tips": { + "type": "string", + "description": "balance defined in probi" + }, + "total": { + "type": "string", + "description": "balance defined in probi" } } } diff --git a/components/brave_rewards/browser/balance_report.cc b/components/brave_rewards/browser/balance_report.cc index 3f7b6e81099e..09806b9ebba7 100644 --- a/components/brave_rewards/browser/balance_report.cc +++ b/components/brave_rewards/browser/balance_report.cc @@ -6,13 +6,15 @@ namespace brave_rewards { BalanceReport::BalanceReport() : - opening_balance(.0), - closing_balance(.0), - grants(.0), - earning_from_ads(.0), - auto_contribute(.0), - recurring_donation(.0), - one_time_donation(.0) { + opening_balance("0"), + closing_balance("0"), + deposits("0"), + grants("0"), + earning_from_ads("0"), + auto_contribute("0"), + recurring_donation("0"), + one_time_donation("0"), + total("0") { } BalanceReport::~BalanceReport() { } @@ -21,10 +23,12 @@ namespace brave_rewards { opening_balance = properties.opening_balance; closing_balance = properties.closing_balance; grants = properties.grants; + deposits = properties.deposits; earning_from_ads = properties.earning_from_ads; auto_contribute = properties.auto_contribute; recurring_donation = properties.recurring_donation; one_time_donation = properties.one_time_donation; + total = properties.total; } } // namespace brave_rewards diff --git a/components/brave_rewards/browser/balance_report.h b/components/brave_rewards/browser/balance_report.h index fcae4e5bf6f4..f876c90effe2 100644 --- a/components/brave_rewards/browser/balance_report.h +++ b/components/brave_rewards/browser/balance_report.h @@ -13,13 +13,15 @@ namespace brave_rewards { ~BalanceReport(); BalanceReport(const BalanceReport& properties); - double opening_balance = .0; - double closing_balance = .0; - double grants = .0; - double earning_from_ads = .0; - double auto_contribute = .0; - double recurring_donation = .0; - double one_time_donation = .0; + std::string opening_balance = "0"; + std::string closing_balance = "0"; + std::string deposits = "0"; + std::string grants = "0"; + std::string earning_from_ads = "0"; + std::string auto_contribute = "0"; + std::string recurring_donation = "0"; + std::string one_time_donation = "0"; + std::string total = "0"; }; } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 7408bde649e2..c8e4b6fa969c 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -489,9 +489,7 @@ void RewardsServiceImpl::OnGrantFinish(ledger::Result result, const ledger::Grant& grant) { ledger::BalanceReportInfo report_info; auto now = base::Time::Now(); - ledger_->GetBalanceReport(GetPublisherMonth(now), GetPublisherYear(now), &report_info); - report_info.grants_ += 10.0; // TODO NZ convert probi to - ledger_->SetBalanceReport(GetPublisherMonth(now), GetPublisherYear(now), report_info); + ledger_->SetBalanceReportCaptcha(GetPublisherMonth(now), GetPublisherYear(now), grant.probi); TriggerOnGrantFinish(result, grant); } @@ -971,7 +969,7 @@ void RewardsServiceImpl::SetPublisherAllowVideos(bool allow) const { } void RewardsServiceImpl::SetContributionAmount(double amount) const { - return ledger_->SetContributionAmount(amount); + ledger_->SetContributionAmount(amount); } void RewardsServiceImpl::SetUserChangedContribution() const { @@ -1083,13 +1081,15 @@ void RewardsServiceImpl::GetCurrentBalanceReport() { if (event_router) { extensions::api::brave_rewards::OnCurrentReport::Properties properties; - properties.opening_balance = report.opening_balance_; - properties.closing_balance = report.closing_balance_; - properties.grants = report.grants_; - properties.earning_from_ads = report.earning_from_ads_; - properties.auto_contribute = report.auto_contribute_; - properties.recurring_donation = report.recurring_donation_; - properties.one_time_donation = report.one_time_donation_; + properties.ads = report.earning_from_ads_; + properties.closing = report.closing_balance_; + properties.contribute = report.auto_contribute_; + properties.deposit = report.deposits_; + properties.grant = report.grants_; + properties.tips = report.one_time_donation_; + properties.opening = report.opening_balance_; + properties.total = report.total_; + properties.recurring = report.recurring_donation_; std::unique_ptr args( extensions::api::brave_rewards::OnCurrentReport::Create(properties) diff --git a/components/brave_rewards/extension/brave_rewards/BUILD.gn b/components/brave_rewards/extension/brave_rewards/BUILD.gn index c2cadcae6996..14bf320f19d9 100644 --- a/components/brave_rewards/extension/brave_rewards/BUILD.gn +++ b/components/brave_rewards/extension/brave_rewards/BUILD.gn @@ -24,6 +24,7 @@ transpile_web_ui("brave_rewards_panel") { "components/app.tsx", "components/panel.tsx", "constants/rewards_panel_types.ts", + "utils.ts", ] bundle_names = [ diff --git a/components/brave_rewards/extension/brave_rewards/_locales/en/messages.json b/components/brave_rewards/extension/brave_rewards/_locales/en/messages.json index 8419d69761db..7550767e4e4d 100644 --- a/components/brave_rewards/extension/brave_rewards/_locales/en/messages.json +++ b/components/brave_rewards/extension/brave_rewards/_locales/en/messages.json @@ -128,7 +128,7 @@ "description": "" }, "tokenGrant": { - "message": "New Token Grant", + "message": "Token Grant", "description": "" }, "earningsAds": { @@ -136,7 +136,7 @@ "description": "" }, "rewardsContribute": { - "message": "Brave Auto-Contribute", + "message": "Auto-Contribute", "description": "" }, "oneTimeDonation": { diff --git a/components/brave_rewards/extension/brave_rewards/background/storage.ts b/components/brave_rewards/extension/brave_rewards/background/storage.ts index ec69124ac6d3..e0e0e92e86a6 100644 --- a/components/brave_rewards/extension/brave_rewards/background/storage.ts +++ b/components/brave_rewards/extension/brave_rewards/background/storage.ts @@ -18,14 +18,15 @@ const defaultState: RewardsExtension.State = { rates: {} }, report: { - ads: 0, - closing: 0, - contribute: 0, - donations: 0, - grants: 0, - oneTime: 0, - opening: 0, - total: 0 + ads: '0', + closing: '0', + contribute: '0', + deposit: '0', + donation: '0', + grant: '0', + tips: '0', + opening: '0', + total: '0' } } diff --git a/components/brave_rewards/extension/brave_rewards/components/panel.tsx b/components/brave_rewards/extension/brave_rewards/components/panel.tsx index 9c4ffcb611d7..1193f02e0f82 100644 --- a/components/brave_rewards/extension/brave_rewards/components/panel.tsx +++ b/components/brave_rewards/extension/brave_rewards/components/panel.tsx @@ -8,11 +8,10 @@ import { connect } from 'react-redux' import { WalletAddIcon, BatColorIcon } from 'brave-ui/components/icons' import { WalletWrapper, WalletSummary, WalletSummarySlider, WalletPanel } from 'brave-ui/features/rewards' import { Provider } from 'brave-ui/features/rewards/profile' -import BigNumber from 'bignumber.js' // Utils import * as rewardsPanelActions from '../actions/rewards_panel_actions' -import * as utils from '../../../ui/utils' +import * as utils from '../utils' interface Props extends RewardsExtension.ComponentProps { windowId: number @@ -95,7 +94,7 @@ export class Panel extends React.Component { return grants.map((grant: RewardsExtension.Grant) => { return { - tokens: new BigNumber(grant.probi.toString()).dividedBy('1e18').toNumber(), + tokens: utils.convertProbiToFixed(grant.probi), expireDate: new Date(grant.expiryTime * 1000).toLocaleDateString() } }) @@ -104,42 +103,19 @@ export class Panel extends React.Component { getWalletSummary = () => { const { walletProperties, report } = this.props.rewardsPanelData const { rates } = walletProperties - const contributionMonthly = 10 // TODO NZ fix with new reports refactor https://github.com/brave/brave-core/pull/409 - const convertedMonthly = utils.convertBalance(contributionMonthly, rates) - let props = { - contribute: { - tokens: contributionMonthly, - converted: convertedMonthly - } - } + let props = {} if (report) { - if (report.ads) { - props['ads'] = { - tokens: report.ads, - converted: utils.convertBalance(report.ads, rates) - } - } - - if (report.donations) { - props['donation'] = { - tokens: report.donations, - converted: utils.convertBalance(report.donations, rates) - } - } - - if (report.grants) { - props['grant'] = { - tokens: report.grants, - converted: utils.convertBalance(report.grants, rates) - } - } - - if (report.oneTime) { - props['tips'] = { - tokens: report.oneTime, - converted: utils.convertBalance(report.oneTime, rates) + for (let key in report) { + const item = report[key] + + if (item.length > 1 && key !== 'total') { + const tokens = utils.convertProbiToFixed(item) + props[key] = { + tokens, + converted: utils.convertBalance(tokens, rates) + } } } } @@ -158,14 +134,14 @@ export class Panel extends React.Component { render () { const { balance, rates, grants } = this.props.rewardsPanelData.walletProperties const publisher: RewardsExtension.Publisher | undefined = this.getPublisher() - const converted = utils.convertBalance(balance, rates) + const converted = utils.convertBalance(balance.toString(), rates) return ( { includeInAuto={!publisher.excluded} attentionScore={(publisher.percentage || 0).toString()} donationAmounts={ - [5, 10, 15, 20, 30, 50, 100] + [ + { + tokens: '0.0', + converted: '0.00' + }, + { + tokens: '1.0', + converted: '0.50' + }, + { + tokens: '5.0', + converted: '2.50' + }, + { + tokens: '10.0', + converted: '5.00' + } + ] } onToggleTips={this.doNothing} donationAction={this.doNothing} diff --git a/components/brave_rewards/extension/brave_rewards/utils.ts b/components/brave_rewards/extension/brave_rewards/utils.ts new file mode 100644 index 000000000000..caf1153ca8ec --- /dev/null +++ b/components/brave_rewards/extension/brave_rewards/utils.ts @@ -0,0 +1,34 @@ +/* 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/. */ + +import BigNumber from 'bignumber.js' + +export const convertBalance = (tokens: string, rates: Record | undefined, currency: string = 'USD'): string => { + const tokensNum = parseFloat(tokens) + if (tokensNum === 0 || !rates || !rates[currency]) { + return '0.00' + } + + const converted = tokensNum * rates[currency] + + if (isNaN(converted)) { + return '0.00' + } + + return converted.toFixed(2) +} + +export const formatConverted = (converted: string, currency: string = 'USD'): string | null => { + return `${converted} ${currency}` +} + +export const convertProbiToFixed = (probi: string, places: number = 1) => { + const result = new BigNumber(probi).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN) + + if (result === 'NaN') { + return '0.0' + } + + return result +} diff --git a/components/brave_rewards/ui/components/contributeBox.tsx b/components/brave_rewards/ui/components/contributeBox.tsx index ab8772e79290..78f675ea0a25 100644 --- a/components/brave_rewards/ui/components/contributeBox.tsx +++ b/components/brave_rewards/ui/components/contributeBox.tsx @@ -24,8 +24,8 @@ interface State { } interface MonthlyChoice { - tokens: number - converted: number + tokens: string + converted: string } interface Props extends Rewards.ComponentProps { @@ -123,7 +123,10 @@ class ContributeBox extends React.Component { { monthlyList.map((choice: MonthlyChoice) => { return
- +
}) } @@ -216,7 +219,10 @@ class ContributeBox extends React.Component { { monthlyList.map((choice: MonthlyChoice) => { return
- +
}) } @@ -226,7 +232,10 @@ class ContributeBox extends React.Component { {new Date(reconcileStamp).toLocaleDateString()} - {getLocale('total')}   + {getLocale('total')}   { disabledContent={showDisabled ? this.disabledContent() : null} > - + { return null } - let tokens = 0 + let tokens = '0.0' if (grant.probi) { - tokens = new BigNumber(grant.probi.toString()).dividedBy('1e18').toNumber() + tokens = convertProbiToFixed(grant.probi) } return ( diff --git a/components/brave_rewards/ui/components/pageWallet.tsx b/components/brave_rewards/ui/components/pageWallet.tsx index ac9e03f1b0da..d580c3cc2f34 100644 --- a/components/brave_rewards/ui/components/pageWallet.tsx +++ b/components/brave_rewards/ui/components/pageWallet.tsx @@ -5,7 +5,6 @@ import * as React from 'react' import { bindActionCreators, Dispatch } from 'redux' import { connect } from 'react-redux' -import BigNumber from 'bignumber.js' // Components import { @@ -113,7 +112,7 @@ class PageWallet extends React.Component { getConversion = () => { const walletInfo = this.props.rewardsData.walletInfo - return utils.convertBalance(walletInfo.balance, walletInfo.rates) + return utils.convertBalance(walletInfo.balance.toString(), walletInfo.rates) } getGrants = () => { @@ -124,7 +123,7 @@ class PageWallet extends React.Component { return grants.map((grant: Rewards.Grant) => { return { - tokens: new BigNumber(grant.probi.toString()).dividedBy('1e18').toNumber(), + tokens: utils.convertProbiToFixed(grant.probi), expireDate: new Date(grant.expiryTime * 1000).toLocaleDateString() } }) @@ -177,46 +176,24 @@ class PageWallet extends React.Component { } getWalletSummary = () => { - const { contributionMonthly, walletInfo, reports } = this.props.rewardsData + const { walletInfo, reports } = this.props.rewardsData const { rates } = walletInfo - const convertedMonthly = utils.convertBalance(contributionMonthly, rates) - let props = { - contribute: { - tokens: contributionMonthly, - converted: convertedMonthly - } - } + let props = {} const currentTime = new Date() const reportKey = `${currentTime.getFullYear()}_${currentTime.getMonth() + 1}` const report: Rewards.Report = reports[reportKey] if (report) { - if (report.ads) { - props['ads'] = { - tokens: report.ads, - converted: utils.convertBalance(report.ads, rates) - } - } - - if (report.donations) { - props['donation'] = { - tokens: report.donations, - converted: utils.convertBalance(report.donations, rates) - } - } - - if (report.grants) { - props['grant'] = { - tokens: report.grants, - converted: utils.convertBalance(report.grants, rates) - } - } - - if (report.oneTime) { - props['tips'] = { - tokens: report.oneTime, - converted: utils.convertBalance(report.oneTime, rates) + for (let key in report) { + const item = report[key] + + if (item.length > 1 && key !== 'total') { + const tokens = utils.convertProbiToFixed(item) + props[key] = { + tokens, + converted: utils.convertBalance(tokens, rates) + } } } } @@ -235,7 +212,7 @@ class PageWallet extends React.Component { return ( <> { attention: 40, onRemove: this.onModalActivityRemove, token: { - value: 5, - converted: 5 + value: '5.0', + converted: '5.00' } } ]} @@ -315,8 +292,8 @@ class PageWallet extends React.Component { type: 'deposit', description: 'Brave Ads payment for May', amount: { - value: 5, - converted: 5 + value: '5.0', + converted: '5.00' } } ]} @@ -337,16 +314,16 @@ class PageWallet extends React.Component { text: 'Token Grant available', type: 'grant', token: { - value: 10, - converted: 5.20 + value: '10.0', + converted: '5.20' } }, { text: 'Earnings from Brave Ads', type: 'ads', token: { - value: 10, - converted: 5.20 + value: '10.0', + converted: '5.20' } }, { @@ -354,8 +331,8 @@ class PageWallet extends React.Component { type: 'contribute', notPaid: true, token: { - value: 10, - converted: 5.20, + value: '10.0', + converted: '5.20', isNegative: true } }, @@ -364,8 +341,8 @@ class PageWallet extends React.Component { type: 'recurring', notPaid: true, token: { - value: 2, - converted: 1.1, + value: '2.0', + converted: '1.1', isNegative: true } }, @@ -373,24 +350,24 @@ class PageWallet extends React.Component { text: 'One-time Donations/Tips', type: 'donations', token: { - value: 19, - converted: 10.10, + value: '19.0', + converted: '10.10', isNegative: true } } ]} total={{ - value: 1, - converted: 0.5 + value: '1.0', + converted: '0.5' }} paymentDay={12} openBalance={{ - value: 10, - converted: 5.20 + value: '10.0', + converted: '5.20' }} closingBalance={{ - value: 11, - converted: 5.30 + value: '11.0', + converted: '5.30' }} /> : null diff --git a/components/brave_rewards/ui/utils.ts b/components/brave_rewards/ui/utils.ts index 33dc35edb98f..0b15e311fed6 100644 --- a/components/brave_rewards/ui/utils.ts +++ b/components/brave_rewards/ui/utils.ts @@ -3,6 +3,7 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const qr = require('qr-image') +import BigNumber from 'bignumber.js' import { Address } from 'brave-ui/features/rewards/modalAddFunds' export let actions: any = null @@ -10,26 +11,23 @@ export let actions: any = null export const getActions = () => actions export const setActions = (newActions: any) => actions = newActions -export const convertBalance = (tokens: number, rates: Record | undefined, currency: string = 'USD'): number => { - if (tokens === 0 || !rates || !rates[currency]) { - return 0 +export const convertBalance = (tokens: string, rates: Record | undefined, currency: string = 'USD'): string => { + const tokensNum = parseFloat(tokens) + if (tokensNum === 0 || !rates || !rates[currency]) { + return '0.00' } - const converted = tokens * rates[currency] + const converted = tokensNum * rates[currency] if (isNaN(converted)) { - return 0 + return '0.00' } - return parseFloat(converted.toFixed(2)) + return converted.toFixed(2) } -export const formatConverted = (converted: number, currency: string = 'USD'): string | null => { - if (isNaN(converted) || converted < 0) { - return null - } - - return `${converted.toFixed(2)} ${currency}` +export const formatConverted = (converted: string, currency: string = 'USD'): string | null => { + return `${converted} ${currency}` } export const generateContributionMonthly = (list: number[], rates: Record | undefined) => { @@ -39,8 +37,8 @@ export const generateContributionMonthly = (list: number[], rates: Record { return { - tokens: item, - converted: convertBalance(item, rates) + tokens: item.toFixed(1), + converted: convertBalance(item.toString(), rates) } }) } @@ -108,3 +106,13 @@ export const getAddresses = (addresses?: Record { + const result = new BigNumber(probi).dividedBy('1e18').toFixed(places, BigNumber.ROUND_DOWN) + + if (result === 'NaN') { + return '0.0' + } + + return result +} diff --git a/components/definitions/rewards.d.ts b/components/definitions/rewards.d.ts index 4a1479cab672..953734b19d75 100644 --- a/components/definitions/rewards.d.ts +++ b/components/definitions/rewards.d.ts @@ -103,14 +103,15 @@ declare namespace Rewards { } export interface Report { - ads: number - closing: number - contribute: number - donations: number - grants: number - oneTime: number - opening: number - total?: number + ads: string + closing: string + contribute: string + deposit: string + donation: string + grant: string + tips: string + opening: string + total: string } export interface Captcha { diff --git a/components/definitions/rewardsExtensions.d.ts b/components/definitions/rewardsExtensions.d.ts index a7337c23d8a8..54ad931fc8d4 100644 --- a/components/definitions/rewardsExtensions.d.ts +++ b/components/definitions/rewardsExtensions.d.ts @@ -41,13 +41,14 @@ declare namespace RewardsExtension { } export interface Report { - ads: number - closing: number - contribute: number - donations: number - grants: number - oneTime: number - opening: number - total?: number + ads: string + closing: string + contribute: string + deposit: string + donation: string + grant: string + tips: string + opening: string + total: string } } diff --git a/components/test/brave_rewards/extension/brave_rewards/utils_test.ts b/components/test/brave_rewards/extension/brave_rewards/utils_test.ts new file mode 100644 index 000000000000..cab6ebfc3bf3 --- /dev/null +++ b/components/test/brave_rewards/extension/brave_rewards/utils_test.ts @@ -0,0 +1,56 @@ +/* global describe, it */ +/* 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/. */ + +import { convertBalance, convertProbiToFixed } from '../../../../brave_rewards/extension/brave_rewards/utils' + +describe('Rewards Panel extension - Utils', () => { + describe('convertBalance', () => { + it('token has letters', () => { + expect(convertBalance('test', { 'USD': 10 })).toBe('0.00') + }) + + it('rates are empty', () => { + expect(convertBalance('10', {})).toBe('0.00') + }) + + it('rate is missing', () => { + expect(convertBalance('10', { 'USD': 10 }, 'EUR')).toBe('0.00') + }) + + it('all good', () => { + expect(convertBalance('10', { 'USD': 10 })).toBe('100.00') + }) + + it('currency is provided', () => { + expect(convertBalance('10', { 'USD': 10, 'EUR': 4 }, 'EUR')).toBe('40.00') + }) + }) + + describe('convertProbiToFixed', () => { + it('probi is not in correct format', () => { + expect(convertProbiToFixed('sdfs')).toBe('0.0') + }) + + it('probi is not 10^18', () => { + expect(convertProbiToFixed('9')).toBe('0.0') + }) + + it('we should always round down', () => { + expect(convertProbiToFixed('0999999999999999999')).toBe('0.9') + }) + + it('regular convert', () => { + expect(convertProbiToFixed('1559999999999999990')).toBe('1.5') + }) + + it('regular convert two places', () => { + expect(convertProbiToFixed('1559999999999999990', 2)).toBe('1.55') + }) + + it('big convert', () => { + expect(convertProbiToFixed('150000000000000000000000000')).toBe('150000000.0') + }) + }) +}) diff --git a/components/test/brave_rewards_ui/actions/rewards_actions_test.ts b/components/test/brave_rewards/ui/actions/rewards_actions_test.ts similarity index 86% rename from components/test/brave_rewards_ui/actions/rewards_actions_test.ts rename to components/test/brave_rewards/ui/actions/rewards_actions_test.ts index c731e577128f..c8fdb0c85dd1 100644 --- a/components/test/brave_rewards_ui/actions/rewards_actions_test.ts +++ b/components/test/brave_rewards/ui/actions/rewards_actions_test.ts @@ -2,8 +2,8 @@ * 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/. */ -import { types } from '../../../brave_rewards/ui/constants/rewards_types' -import * as actions from '../../../brave_rewards/ui/actions/rewards_actions' +import { types } from '../../../../brave_rewards/ui/constants/rewards_types' +import * as actions from '../../../../brave_rewards/ui/actions/rewards_actions' describe('rewards_actions', () => { it('createWallet', () => { diff --git a/components/test/brave_rewards_ui/components/app_test.tsx b/components/test/brave_rewards/ui/components/app_test.tsx similarity index 91% rename from components/test/brave_rewards_ui/components/app_test.tsx rename to components/test/brave_rewards/ui/components/app_test.tsx index 99a7901ff68d..82c43819c04d 100644 --- a/components/test/brave_rewards_ui/components/app_test.tsx +++ b/components/test/brave_rewards/ui/components/app_test.tsx @@ -4,13 +4,13 @@ import * as React from 'react' import { shallow } from 'enzyme' -import { types } from '../../../brave_rewards/ui/constants/rewards_types' -import { rewardsInitialState } from '../../testData' +import { types } from '../../../../brave_rewards/ui/constants/rewards_types' +import { rewardsInitialState } from '../../../testData' import { App, mapStateToProps, mapDispatchToProps -} from '../../../brave_rewards/ui/components/app' +} from '../../../../brave_rewards/ui/components/app' describe('rewardsPage component', () => { describe('mapStateToProps', () => { diff --git a/components/test/brave_rewards_ui/reducers/index_test.ts b/components/test/brave_rewards/ui/reducers/index_test.ts similarity index 84% rename from components/test/brave_rewards_ui/reducers/index_test.ts rename to components/test/brave_rewards/ui/reducers/index_test.ts index e2bb6f20c270..ebb60214ec95 100644 --- a/components/test/brave_rewards_ui/reducers/index_test.ts +++ b/components/test/brave_rewards/ui/reducers/index_test.ts @@ -3,8 +3,8 @@ * 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/. */ -import reducers from '../../../brave_rewards/ui/reducers/index' -import { rewardsInitialState } from '../../testData' +import reducers from '../../../../brave_rewards/ui/reducers/index' +import { rewardsInitialState } from '../../../testData' describe('rewards reducers test', () => { it('reducers are a combined reducer function', () => { diff --git a/components/test/brave_rewards_ui/reducers/wallet_reducer_test.ts b/components/test/brave_rewards/ui/reducers/wallet_reducer_test.ts similarity index 86% rename from components/test/brave_rewards_ui/reducers/wallet_reducer_test.ts rename to components/test/brave_rewards/ui/reducers/wallet_reducer_test.ts index 5bd08d9db634..73cef62b88af 100644 --- a/components/test/brave_rewards_ui/reducers/wallet_reducer_test.ts +++ b/components/test/brave_rewards/ui/reducers/wallet_reducer_test.ts @@ -3,10 +3,10 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ /* global chrome */ -import reducers from '../../../brave_rewards/ui/reducers/index' -import * as actions from '../../../brave_rewards/ui/actions/rewards_actions' -import { types } from '../../../brave_rewards/ui/constants/rewards_types' -import { defaultState } from '../../../brave_rewards/ui/storage' +import reducers from '../../../../brave_rewards/ui/reducers/index' +import * as actions from '../../../../brave_rewards/ui/actions/rewards_actions' +import { types } from '../../../../brave_rewards/ui/constants/rewards_types' +import { defaultState } from '../../../../brave_rewards/ui/storage' describe('wallet reducer', () => { const constantDate = new Date('2018-01-01T12:00:00') diff --git a/components/test/brave_rewards/ui/utils_test.ts b/components/test/brave_rewards/ui/utils_test.ts new file mode 100644 index 000000000000..7d74f8fab864 --- /dev/null +++ b/components/test/brave_rewards/ui/utils_test.ts @@ -0,0 +1,66 @@ +/* global describe, it */ +/* 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/. */ + +import { convertBalance, formatConverted, convertProbiToFixed } from '../../../brave_rewards/ui/utils' + +describe('Rewards Settings - Utils', () => { + describe('convertBalance', () => { + it('token has letters', () => { + expect(convertBalance('test', { 'USD': 10 })).toBe('0.00') + }) + + it('rates are empty', () => { + expect(convertBalance('10', {})).toBe('0.00') + }) + + it('rate is missing', () => { + expect(convertBalance('10', { 'USD': 10 }, 'EUR')).toBe('0.00') + }) + + it('all good', () => { + expect(convertBalance('10', { 'USD': 10 })).toBe('100.00') + }) + + it('currency is provided', () => { + expect(convertBalance('10', { 'USD': 10, 'EUR': 4 }, 'EUR')).toBe('40.00') + }) + }) + + describe('formatConverted', () => { + it('currency is not provided', () => { + expect(formatConverted('10.00')).toBe('10.00 USD') + }) + + it('currency is provided', () => { + expect(formatConverted('10.00', 'EUR')).toBe('10.00 EUR') + }) + }) + + describe('convertProbiToFixed', () => { + it('probi is not in correct format', () => { + expect(convertProbiToFixed('sdfs')).toBe('0.0') + }) + + it('probi is not 10^18', () => { + expect(convertProbiToFixed('9')).toBe('0.0') + }) + + it('we should always round down', () => { + expect(convertProbiToFixed('0999999999999999999')).toBe('0.9') + }) + + it('regular convert', () => { + expect(convertProbiToFixed('1559999999999999990')).toBe('1.5') + }) + + it('regular convert two places', () => { + expect(convertProbiToFixed('1559999999999999990', 2)).toBe('1.55') + }) + + it('big convert', () => { + expect(convertProbiToFixed('150000000000000000000000000')).toBe('150000000.0') + }) + }) +}) diff --git a/package-lock.json b/package-lock.json index 9c40011188c3..dc564d015820 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2206,9 +2206,9 @@ } }, "brave-ui": { - "version": "0.25.1", - "resolved": "https://registry.npmjs.org/brave-ui/-/brave-ui-0.25.1.tgz", - "integrity": "sha512-QtWcWT5plh+2fz5/lfNIhjOiHqpbB2BXIibXzaruSQnAnct/Se+iPG8ddc1GGi1AKJFJ64M7lTAcrNM7SrbzkA==", + "version": "0.26.0", + "resolved": "https://registry.npmjs.org/brave-ui/-/brave-ui-0.26.0.tgz", + "integrity": "sha512-PMRU/HTAbOqK2r/AneA9KK/aj5f/qxUlTyL9Z7/Afu5OuwNvyvuJTPPfI0vY7kA28KMkBV8gj+XQFTVkX8qexg==", "dev": true, "requires": { "emptykit.css": "^1.0.1" diff --git a/package.json b/package.json index 16359a10cfd8..3e7bd2e0adcd 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "babel-preset-react": "^6.24.1", "babel-preset-react-optimize": "^1.0.1", "babel-preset-stage-0": "^6.24.1", - "brave-ui": "^0.25.1", + "brave-ui": "^0.26.0", "css-loader": "^0.28.9", "csstype": "^2.5.5", "emptykit.css": "^1.0.1",