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

Commit

Permalink
WIP balance data
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Oct 4, 2017
1 parent 4e2508a commit fa3ea3e
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 366 deletions.
75 changes: 44 additions & 31 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ const onWalletRecovery = (state, error, result) => {
// we reset ledgerInfo.error to what it was before (likely null)
// if ledgerInfo.error is not null, the wallet info will not display in UI
// logError sets ledgerInfo.error, so we must we clear it or UI will show an error
state = logError(error, 'recoveryWallet')
state = logError(state, error, 'recoveryWallet')
state = ledgerState.setInfoProp(state, 'error', existingLedgerError)
state = ledgerState.setRecoveryStatus(state, false)
} else {
Expand Down Expand Up @@ -1526,6 +1526,8 @@ const generatePaymentData = (state) => {
case 'LTC':
url = `litecoin:${address}`
break
default:
return
}

try {
Expand All @@ -1539,7 +1541,7 @@ const generatePaymentData = (state) => {
appActions.onLedgerQRGenerated(index, paymentIMG)
})
} catch (ex) {
console.error('qr.imageSync error: ' + ex.toString())
console.error('qr.imageSync (for url ' + url + ') error: ' + ex.toString())
}
})

Expand All @@ -1563,7 +1565,6 @@ const getPaymentInfo = (state) => {

client.getWalletProperties(amount, currency, function (err, body) {
if (err) {
logError(err, 'getWalletProperties')
return
}

Expand All @@ -1579,30 +1580,7 @@ const getPaymentInfo = (state) => {
}

const onWalletProperties = (state, body) => {
let newInfo = {
probi: body.get('probi'),
addresses: body.get('addresses')
}

state = ledgerState.mergeInfoProp(state, newInfo)

const info = ledgerState.getInfoProps(state)

const amount = info.get('balance')
const currency = info.getIn(['bravery', 'fee', 'currency'])

if (currency) {
const rate = body.getIn(['rates', currency])
state = ledgerState.setInfoProp(state, 'currentRate', rate)

if (amount) {
if (rate) {
const converted = new BigNumber(newInfo.probi.toString()).times(new BigNumber(rate.toString())).toFixed(2)
state = ledgerState.setInfoProp(state, 'converted', converted)
}
}
}

state = ledgerState.setInfoProp(state, 'addresses', body.get('addresses'))
state = generatePaymentData(state)

return state
Expand Down Expand Up @@ -1654,6 +1632,7 @@ const onBraveryProperties = (state, error, result) => {
}

const getBalance = (state) => {
console.log('getBalance')
if (!client) return

const addresses = ledgerState.getInfoProp(state, 'addresses')
Expand All @@ -1670,13 +1649,47 @@ const getBalance = (state) => {
if (err) {
return console.warn('ledger balance warning: ' + JSON.stringify(err, null, 2))
}
appActions.onLedgerBalanceReceived(result.balance, result.unconfirmed)
appActions.onLedgerBalanceReceived(result)
})
}

const balanceReceived = (state, balance, unconfirmed) => {
state = ledgerState.setInfoProp(state, 'balance', balance)
// TODO we are still not getting this data from bat-client
const balanceReceived = (state, result) => {
// Balance
const balance = parseFloat(result.get('balance'))
if (balance > 0) {
state = ledgerState.setInfoProp(state, 'balance', balance)
}

// Rates
const rates = result.get('rates')
if (rates != null) {
state = ledgerState.setInfoProp(state, 'rates', rates)
}

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

const info = ledgerState.getInfoProps(state)
const amount = info.get('balance')
const infoRates = info.get('rates')
const currency = info.getIn(['bravery', 'fee', 'currency'])

if (currency) {
const rate = infoRates.get(currency)
state = ledgerState.setInfoProp(state, 'currentRate', rate)

if (amount && rate) {
const converted = new BigNumber(probi.toString()).times(new BigNumber(rate.toString())).toFixed(2)
state = ledgerState.setInfoProp(state, 'converted', converted)
}
}
}

// unconfirmed amount
const unconfirmed = parseFloat(result.get('unconfirmed'))
if (unconfirmed > 0) {
const result = (unconfirmed / 1e8).toFixed(4)
if (ledgerState.getInfoProp(state, 'unconfirmed') === result) {
Expand Down Expand Up @@ -1917,7 +1930,7 @@ const onInitRead = (state, parsedData) => {
}

const onTimeUntilReconcile = (state, stateResult) => {
state = getStateInfo(stateResult)
state = getStateInfo(state, stateResult.toJS()) // TODO optimize
muonWriter(stateResult)

return state
Expand Down
2 changes: 1 addition & 1 deletion app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_ON_LEDGER_BALANCE_RECEIVED:
{
state = ledgerApi.balanceReceived(state, action.get('balance'), action.get('unconfirmed'))
state = ledgerApi.balanceReceived(state, action.get('result'))
break
}
case appConstants.APP_ON_WALLET_PROPERTIES:
Expand Down
4 changes: 3 additions & 1 deletion app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,16 @@ const updateAboutDetails = (tab, tabValue) => {
// TODO save this into values into the sate so that we don't call this app action on every state change
// this should be saved in app state when windows will be refactored #11151

/*
if (url === 'about:preferences#payments') {
tab.on('destroyed', () => {
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
})
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), true)
} else {
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
}
*/

if (location === 'about:preferences' || location === 'about:contributions' || location === aboutUrls.get('about:contributions')) {
const ledgerData = ledgerInfo
Expand Down
2 changes: 1 addition & 1 deletion app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const formatCurrentBalance = (ledgerData) => {
converted = Number.parseFloat(ledgerData.get('converted')) || 0
}

balance = Math.floor(balance)
balance = balance.toFixed(2)
converted = converted.toFixed(2)

return `${balance} BAT (${converted} ${currency})`
Expand Down
2 changes: 1 addition & 1 deletion app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ let generateBraveManifest = () => {
'form-action': '\'none\'',
'style-src': '\'self\' \'unsafe-inline\'',
'img-src': '* data: file://*',
'frame-src': '\'self\' https://buy.coinbase.com https://brave.com'
'frame-src': '\'self\' https://brave.com'
}

if (process.env.NODE_ENV === 'development') {
Expand Down
5 changes: 2 additions & 3 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1606,11 +1606,10 @@ const appActions = {
})
},

onLedgerBalanceReceived: function (balance, unconfirmed) {
onLedgerBalanceReceived: function (result) {
dispatch({
actionType: appConstants.APP_ON_LEDGER_BALANCE_RECEIVED,
balance,
unconfirmed
result
})
},

Expand Down
Loading

0 comments on commit fa3ea3e

Please sign in to comment.