Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Adds new balance breakdown
Browse files Browse the repository at this point in the history
Resolves #14229

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed May 25, 2018
1 parent 8e9be72 commit b3d7dad
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 89 deletions.
31 changes: 25 additions & 6 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1882,15 +1882,34 @@ const onWalletProperties = (state, body) => {
state = ledgerState.setInfoProp(state, 'currentRate', rate)
}

// Grants
let probi = parseFloat(body.get('probi'))
let userFunded = null
if (!isNaN(probi)) {
userFunded = probi
let grants = body.get('grants') || Immutable.List()
if (!grants.isEmpty()) {
let grantTotal = 0
grants = grants.map(grant => {
const amount = new BigNumber(grant.get('probi').toString()).dividedBy('1e18').toNumber()
grantTotal += parseFloat(grant.get('probi'))
return {
amount: amount,
expirationDate: grant.get('expiryTime')
}
})
state = ledgerState.setInfoProp(state, 'grants', grants)
userFunded = probi - grantTotal
}

state = ledgerState.setInfoProp(state, 'userFunded', new BigNumber(userFunded.toString()).dividedBy('1e18').toNumber())
}

// Probi
const probi = parseFloat(body.get('probi'))
if (probi >= 0) {
state = ledgerState.setInfoProp(state, 'probi', probi)

const amount = info.get('balance')

if (amount != null && rate) {
const bigProbi = new BigNumber(probi.toString()).dividedBy('1e18')
if (userFunded != null && rate) {
const bigProbi = new BigNumber(userFunded.toString()).dividedBy('1e18')
const bigRate = new BigNumber(rate.toString())
const converted = bigProbi.times(bigRate).toNumber()

Expand Down
6 changes: 3 additions & 3 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ const batToCurrencyString = (bat, ledgerData) => {
return `${converted} ${currency}`
}

const formatCurrentBalance = (ledgerData) => {
const formatCurrentBalance = (ledgerData, amount, showAlt = true) => {
let currency = 'USD'
let balance = 0
let converted = 0
let hasRate = false

if (ledgerData != null) {
balance = Number(ledgerData.get('balance') || 0)
balance = Number(amount || 0)
converted = Number.parseFloat(ledgerData.get('converted')) || 0
hasRate = ledgerData.has('currentRate') && ledgerData.hasIn(['rates', 'BTC'])
hasRate = showAlt ? ledgerData.has('currentRate') && ledgerData.hasIn(['rates', 'BTC']) : false
}

balance = balance.toFixed(2)
Expand Down
2 changes: 2 additions & 0 deletions app/extensions/brave/locales/en-US/preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ engineGoKey=Engine Go Key (Type First)
engineGoKey=Engine Go Key (type first)
enpass=Enpass® (requires application)
extensions=Extensions
expires=expires
flash=Run Adobe Flash Player
flashAllowAlways=Allow until {{time}}
flashTroubleshooting=Flash not working? Try the troubleshooting tips on our
Expand Down Expand Up @@ -385,6 +386,7 @@ tabsSettings=Tabs Settings
termsOfService=Terms of Service
timeSpent=Time Spent
toolbarUserInterfaceScale=Toolbar and UI elements scale
total=total
totalAmount=Total Amount
update=Update
updateToPreviewReleases=Update to preview releases *
Expand Down
68 changes: 47 additions & 21 deletions app/renderer/components/preferences/payment/enabledContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const Immutable = require('immutable')
// Components
const ImmutableComponent = require('../../immutableComponent')
const BrowserButton = require('../../common/browserButton')
const {FormTextbox} = require('../../common/textbox')
const {FormDropdown} = require('../../common/dropdown')
const LedgerTable = require('./ledgerTable')
const Captcha = require('./captcha')
Expand Down Expand Up @@ -143,15 +142,28 @@ class EnabledContent extends ImmutableComponent {

fundsAmount () {
const ledgerData = this.props.ledgerData
const val = formatCurrentBalance(ledgerData) || ''
const big = val.length > 23

return <FormTextbox
readOnly
data-test-id='fundsAmount'
value={val}
customClass={big && styles.width_input}
/>
if (!ledgerData) {
return
}

const total = formatCurrentBalance(ledgerData, ledgerData.get('balance'), false) || ''
const userFunded = formatCurrentBalance(ledgerData, ledgerData.get('userFunded')) || ''
const grants = ledgerData.get('grants') || Immutable.List()

return <div className={css(styles.fundsAmount)}>
<div className={css(styles.fundsAmount__item)}>{userFunded}</div>
{
grants.map(grant => {
return <div className={css(styles.fundsAmount__item)}>
{formatCurrentBalance(ledgerData, grant.get('amount'), false)}
<span> (<span data-l10n-id='expires' /> {new Date(grant.get('expirationDate') * 1000).toLocaleDateString()})</span>
</div>
})
}
<div className={css(styles.fundsAmount__item, styles.fundsAmount__total)}>
{total} (<span data-l10n-id='total' />)
</div>
</div>
}

lastReconcileMessage () {
Expand Down Expand Up @@ -179,8 +191,12 @@ class EnabledContent extends ImmutableComponent {
}

return <section>
<div data-l10n-id='lastContribution' />
<div data-l10n-id={text} data-l10n-args={JSON.stringify(l10nDataArgs)} />
{
prevReconcileDateValue
? <span data-l10n-id='lastContribution' />
: null
}
<span data-l10n-id={text} data-l10n-args={JSON.stringify(l10nDataArgs)} />
</section>
}

Expand Down Expand Up @@ -220,8 +236,7 @@ class EnabledContent extends ImmutableComponent {
}

return <section>
<div data-l10n-id='nextContribution' />
<div data-l10n-args={JSON.stringify(l10nDataArgs)} data-l10n-id={l10nDataId} />
<span data-l10n-id='nextContribution' /> <span data-l10n-args={JSON.stringify(l10nDataArgs)} data-l10n-id={l10nDataId} />
</section>
}

Expand Down Expand Up @@ -419,7 +434,7 @@ class EnabledContent extends ImmutableComponent {
}
</FormDropdown>
</div>
<div className={css(gridStyles.row2col2)}>
<div className={css(gridStyles.row2col2, gridStyles.mergeRow23Col2)}>
{
ledgerData.get('error') && ledgerData.get('error').get('caller') === 'getWalletProperties'
? <div data-l10n-id='accountBalanceConnectionError' />
Expand All @@ -431,8 +446,6 @@ class EnabledContent extends ImmutableComponent {
</div>
<div className={css(gridStyles.row3col1, styles.enabledContent__walletBar__message)}>
{this.lastReconcileMessage()}
</div>
<div className={css(gridStyles.row3col2, styles.enabledContent__walletBar__message)}>
{
ledgerData.get('error') && ledgerData.get('error').get('caller') === 'getWalletProperties'
? <div data-l10n-id={this.ledgerDataErrorText()} />
Expand Down Expand Up @@ -524,6 +537,10 @@ const gridStyles = StyleSheet.create({
gridColumn: 3,
marginRight: globalStyles.spacing.panelPadding,
marginBottom: globalStyles.spacing.panelPadding
},

mergeRow23Col2: {
gridRow: '2 / span 2'
}
})

Expand All @@ -532,10 +549,6 @@ const styles = StyleSheet.create({
marginTop: '10px'
},

width_input: {
width: '195px'
},

iconLink: {
color: globalStyles.color.mediumGray,
fontSize: globalStyles.payments.fontSize.regular,
Expand Down Expand Up @@ -662,6 +675,19 @@ const styles = StyleSheet.create({
fontSize: globalStyles.payments.fontSize.regular,
lineHeight: 1.5,
marginTop: globalStyles.spacing.panelPadding
},

fundsAmount__item: {
marginBottom: '4px',
width: '215px',
fontSize: '14.5px'
},

fundsAmount__total: {
marginTop: '10px',
paddingTop: '12px',
borderTop: '1px solid #999',
fontSize: '15px'
}
})

Expand Down
5 changes: 5 additions & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ AppStore
created, boolean, // wallet is created
creating: boolean, // wallet is being created
currentRate: number,
grants: [{
amount: number,
expirationDate: number
}]
hasBitcoinHandler: boolean, // brave browser has a `bitcoin:` URI handler
monthlyAmounts: Array<float> // list of all monthly amounts for the contribution
passphrase: string, // the BAT wallet passphrase
Expand Down Expand Up @@ -245,6 +249,7 @@ AppStore
viewingId: string, // UUIDv4 for this contribution
}],
unconfirmed: string, // unconfirmed balance in BAT.toFixed(2)
userFunded: number, // amount funded by the user
userHasFunded: boolean // permanently true once user funds wallet
},
locations: {
Expand Down
21 changes: 18 additions & 3 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1504,19 +1504,18 @@ describe('ledger api unit tests', function () {
const expectedState = state
.setIn(['ledger', 'info', 'probi'], probi)
.setIn(['ledger', 'info', 'balance'], 25)
.setIn(['ledger', 'info', 'userFunded'], 25)
.setIn(['ledger', 'info', 'userHasFunded'], true)
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('amount is null', function () {
const result = ledgerApi.onWalletProperties(state, Immutable.fromJS({
probi: probi,
rates: rates
}))
const expectedState = state
.setIn(['ledger', 'info', 'rates'], Immutable.fromJS(rates))
.setIn(['ledger', 'info', 'currentRate'], rate)
.setIn(['ledger', 'info', 'probi'], probi)
assert.deepEqual(result.toJS(), expectedState.toJS())
})

Expand All @@ -1531,13 +1530,14 @@ describe('ledger api unit tests', function () {
.setIn(['ledger', 'info', 'currentRate'], rate)
.setIn(['ledger', 'info', 'converted'], 3.5836474125)
.setIn(['ledger', 'info', 'balance'], 25)
.setIn(['ledger', 'info', 'userFunded'], 25)
.setIn(['ledger', 'info', 'probi'], probi)
.setIn(['ledger', 'info', 'userHasFunded'], true)
assert.deepEqual(result.toJS(), expectedState.toJS())
})

it('big probi', function () {
const bigProbi = '7.309622404968674704085e+21'
const bigProbi = 7.309622404968674704085e+21
const result = ledgerApi.onWalletProperties(state, Immutable.fromJS({
probi: bigProbi,
balance: '7309.6224',
Expand All @@ -1548,6 +1548,7 @@ describe('ledger api unit tests', function () {
.setIn(['ledger', 'info', 'currentRate'], rate)
.setIn(['ledger', 'info', 'converted'], 1047.8043767167208)
.setIn(['ledger', 'info', 'balance'], 7309.6224)
.setIn(['ledger', 'info', 'userFunded'], 7309.622404968675)
.setIn(['ledger', 'info', 'probi'], bigProbi)
.setIn(['ledger', 'info', 'userHasFunded'], true)
assert.deepEqual(result.toJS(), expectedState.toJS())
Expand Down Expand Up @@ -1642,6 +1643,20 @@ describe('ledger api unit tests', function () {
contributionAmount = 10
})
})

describe('grants', function () {
it('probi is missing', function () {

})

it('grants is missing', function () {

})

it('grant is saved', function () {

})
})
})

describe('claimPromotion', function () {
Expand Down
17 changes: 11 additions & 6 deletions test/unit/app/common/lib/ledgerUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ describe('ledgerUtil unit test', function () {

it('defaults to 0 as balance when rate is not present', function () {
const data = ledgerData.delete('rates')
const result = ledgerUtil.formatCurrentBalance(data)
const result = ledgerUtil.formatCurrentBalance(data, ledgerData.get('balance'))
assert.equal(result, '5.00 BAT')
})

it('formats `balance` and `converted` values to two decimal places', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData)
const result = ledgerUtil.formatCurrentBalance(ledgerData, ledgerData.get('balance'))
assert.equal(result, '5.00 BAT (1.12 USD)')
})

Expand All @@ -238,24 +238,29 @@ describe('ledgerUtil unit test', function () {
})

it('defaults `converted` to 0 if not found', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData.delete('converted'))
const result = ledgerUtil.formatCurrentBalance(ledgerData.delete('converted'), ledgerData.get('balance'))
assert.equal(result, '5.00 BAT (0.00 USD)')
})

it('handles `balance` being a string', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData.set('balance', '5'))
const result = ledgerUtil.formatCurrentBalance(ledgerData, 5)
assert.equal(result, '5.00 BAT (1.12 USD)')
})

it('handles `converted` being a string', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData.set('converted', '1.1234'))
const result = ledgerUtil.formatCurrentBalance(ledgerData.set('converted', '1.1234'), ledgerData.get('balance'))
assert.equal(result, '5.00 BAT (1.12 USD)')
})

it('custom format for amount lower then 0.01', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData.set('converted', '0.004'))
const result = ledgerUtil.formatCurrentBalance(ledgerData.set('converted', '0.004'), ledgerData.get('balance'))
assert.equal(result, '5.00 BAT (< 0.01 USD)')
})

it('formats only `balance` when alt is excluded', function () {
const result = ledgerUtil.formatCurrentBalance(ledgerData, ledgerData.get('balance'), false)
assert.equal(result, '5.00 BAT')
})
})

describe('formattedTimeFromNow', function () {
Expand Down
Loading

0 comments on commit b3d7dad

Please sign in to comment.