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

BAT integration (BAT Mercury) #11231

Merged
merged 14 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
427 changes: 250 additions & 177 deletions app/browser/api/ledger.js

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ const ledgerReducer = (state, action, immutableAction) => {
state = ledgerApi.recoverKeys(
state,
action.get('useRecoveryKeyFile'),
action.get('firstRecoveryKey'),
action.get('secondRecoveryKey')
action.get('recoveryKey')
)
break
}
Expand All @@ -62,6 +61,7 @@ const ledgerReducer = (state, action, immutableAction) => {
const clearData = defaults ? defaults.merge(temp) : temp
if (clearData.get('browserHistory') && !getSetting(settings.PAYMENTS_ENABLED)) {
state = ledgerState.resetSynopsis(state)
ledgerApi.deleteSynopsis()
}
break
}
Expand Down Expand Up @@ -130,7 +130,7 @@ const ledgerReducer = (state, action, immutableAction) => {
case 'ledgerPaymentsShown':
{
if (action.get('value') === false) {
ledgerApi.deleteSynopsis(publisherKey)
ledgerApi.deleteSynopsisPublisher(publisherKey)
state = ledgerState.deletePublishers(state, publisherKey)
state = ledgerApi.updatePublisherInfo(state)
}
Expand Down Expand Up @@ -246,11 +246,6 @@ const ledgerReducer = (state, action, immutableAction) => {
state = ledgerApi.onBootStateFile(state)
break
}
case appConstants.APP_ON_LEDGER_BALANCE_RECEIVED:
{
state = ledgerApi.balanceReceived(state, action.get('unconfirmed'))
break
}
case appConstants.APP_ON_WALLET_PROPERTIES:
{
state = ledgerApi.onWalletProperties(state, action.get('body'))
Expand All @@ -266,6 +261,11 @@ const ledgerReducer = (state, action, immutableAction) => {
ledgerApi.addFoundClosed(state)
break
}
case appConstants.APP_ON_CHANGE_ADD_FUNDS_DIALOG_STEP:
{
state = ledgerState.saveWizardData(state, action.get('page'), action.get('currency'))
break
}
case appConstants.APP_ON_WALLET_RECOVERY:
{
state = ledgerApi.onWalletRecovery(state, action.get('error'), action.get('result'))
Expand Down Expand Up @@ -304,6 +304,7 @@ const ledgerReducer = (state, action, immutableAction) => {
case appConstants.APP_ON_RESET_RECOVERY_STATUS:
{
state = ledgerState.setRecoveryStatus(state, null)
state = ledgerState.setInfoProp(state, 'error', null)
break
}
case appConstants.APP_ON_LEDGER_INIT_READ:
Expand All @@ -316,6 +317,16 @@ const ledgerReducer = (state, action, immutableAction) => {
state = state.setIn(['migrations', 'btcToBatNotifiedTimestamp'], new Date().getTime())
break
}
case appConstants.APP_ON_BTC_TO_BAT_TRANSITIONED:
{
state = state.setIn(['migrations', 'btcToBatTimestamp'], new Date().getTime())
break
}
case appConstants.APP_ON_LEDGER_QR_GENERATED:
{
state = ledgerState.saveQRCode(state, action.get('currency'), action.get('image'))
break
}
}
return state
}
Expand Down
8 changes: 6 additions & 2 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ const updateAboutDetails = (tab, tabValue) => {

// TODO(bridiver) - refactor these to use state helpers
const ledgerInfo = ledgerState.getInfoProps(appState)
const synopsis = appState.getIn(['ledger', 'about'])
const synopsis = ledgerState.getAboutData(appState)
const wizardData = ledgerState.geWizardData(appState)
const preferencesData = appState.getIn(['about', 'preferences'])
const appSettings = appState.get('settings')
let allSiteSettings = appState.get('siteSettings')
Expand All @@ -180,20 +181,23 @@ 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
.merge(synopsis)
.merge(preferencesData)
.set('wizardData', wizardData)
tab.send(messages.LEDGER_UPDATED, ledgerData.toJS())
tab.send(messages.SETTINGS_UPDATED, appSettings.toJS())
tab.send(messages.SITE_SETTINGS_UPDATED, allSiteSettings.toJS())
Expand Down
50 changes: 28 additions & 22 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const Immutable = require('immutable')
const moment = require('moment')
const BigNumber = require('bignumber.js')

// State
const siteSettingsState = require('../state/siteSettingsState')
Expand Down Expand Up @@ -62,32 +63,36 @@ const shouldTrackView = (view, responseList) => {
return false
}

const btcToCurrencyString = (btc, ledgerData) => {
const balance = Number(btc || 0)
const batToCurrencyString = (bat, ledgerData) => {
const balance = Number(bat || 0)
const currency = (ledgerData && ledgerData.get('currency')) || 'USD'

if (balance === 0) {
if (balance === 0 || ledgerData == null) {
return `0 ${currency}`
}

if (ledgerData && ledgerData.get('btc') && typeof ledgerData.get('amount') === 'number') {
const btcValue = ledgerData.get('btc') / ledgerData.get('amount')
const fiatValue = (balance / btcValue).toFixed(2)
let roundedValue = Math.floor(fiatValue)
const diff = fiatValue - roundedValue

if (diff > 0.74) {
roundedValue += 0.75
} else if (diff > 0.49) {
roundedValue += 0.50
} else if (diff > 0.24) {
roundedValue += 0.25
}
const rate = ledgerData.get('currentRate') || 0
const converted = new BigNumber(new BigNumber(rate.toString())).times(balance).toFixed(2)
return `${converted} ${currency}`
}

return `${roundedValue.toFixed(2)} ${currency}`
const formatCurrentBalance = (ledgerData) => {
let currency = 'USD'
let balance = 0
let converted = 0
let hasRate = false

if (ledgerData != null) {
currency = ledgerData.get('currency') || 'USD'
balance = Number(ledgerData.get('balance') || 0)
converted = Number.parseFloat(ledgerData.get('converted')) || 0
hasRate = ledgerData.has('currentRate')
}

return `${balance} BTC`
balance = balance.toFixed(2)
converted = converted.toFixed(2)

return `${balance} BAT${hasRate ? ` (${converted} ${currency})` : ''}`
}

const formattedTimeFromNow = (timestamp) => {
Expand All @@ -110,11 +115,11 @@ const walletStatus = (ledgerData) => {
const pendingFunds = Number(ledgerData.get('unconfirmed') || 0)

if (pendingFunds + Number(ledgerData.get('balance') || 0) <
0.9 * Number(ledgerData.get('btc') || 0)) {
0.9 * Number(ledgerData.get('bat') || 0)) {
status.id = 'insufficientFundsStatus'
} else if (pendingFunds > 0) {
status.id = 'pendingFundsStatus'
status.args = {funds: btcToCurrencyString(pendingFunds, ledgerData)}
status.args = {funds: batToCurrencyString(pendingFunds, ledgerData)}
} else if (transactions && transactions.size > 0) {
status.id = 'defaultWalletStatus'
} else {
Expand Down Expand Up @@ -206,13 +211,14 @@ const stickyP = (state, publisherKey) => {

module.exports = {
shouldTrackView,
btcToCurrencyString,
batToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus,
blockedP,
contributeP,
visibleP,
eligibleP,
stickyP
stickyP,
formatCurrentBalance
}
34 changes: 31 additions & 3 deletions app/common/state/ledgerState.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ const ledgerState = {
return state.setIn(['ledger', 'info'], Immutable.Map())
},

saveQRCode: (state, currency, image) => {
state = validateState(state)
if (currency == null) {
return state
}

return state.setIn(['ledger', 'info', 'walletQR', currency], image)
},

/**
* OTHERS
*/
Expand All @@ -275,9 +284,10 @@ const ledgerState = {
return state.setIn(['ledger', 'info', 'error'], null)
}

return state
.setIn(['ledger', 'info', 'error', 'caller'], caller)
.setIn(['ledger', 'info', 'error', 'error'], error)
return state.setIn(['ledger', 'info', 'error'], Immutable.fromJS({
caller: caller,
error: error
}))
},

changePinnedValues: (state, publishers) => {
Expand All @@ -298,6 +308,7 @@ const ledgerState = {
return state
},

// About page
// TODO (optimization) don't have two almost identical object in state (synopsi->publishers and about->synopsis)
saveAboutSynopsis: (state, publishers) => {
state = validateState(state)
Expand All @@ -310,6 +321,23 @@ const ledgerState = {
state = validateState(state)
return state
.setIn(['ledger', 'about', 'synopsisOptions'], ledgerState.getSynopsisOptions(state))
},

getAboutData: (state) => {
return state.getIn(['ledger', 'about']) || Immutable.Map()
},

saveWizardData: (state, page, currency) => {
state = validateState(state)
return state.mergeIn(['ledger', 'wizardData'], {
currentPage: page,
currency: currency
})
},

geWizardData: (state) => {
state = validateState(state)
return state.getIn(['ledger', 'wizardData']) || Immutable.Map()
}
}

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
22 changes: 22 additions & 0 deletions app/extensions/brave/img/ledger/cryptoIcons/BAT_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions app/extensions/brave/img/ledger/cryptoIcons/BTC_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions app/extensions/brave/img/ledger/cryptoIcons/ETH_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions app/extensions/brave/img/ledger/cryptoIcons/LTC_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/extensions/brave/img/ledger/fakeQRcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions app/extensions/brave/img/ledger/wallet_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions app/extensions/brave/img/preferences/browser_prefs_payments.svg

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading