Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Fixes brave/brave-browser#4773 - adds BAT token by default to accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Jun 11, 2019
1 parent acc9e1e commit 93ab9b1
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class PreferencesController {
completedUiMigration: true,
metaMetricsId: null,
metaMetricsSendCount: 0,

// Brave
batTokenAdded: false
}, opts.initState)

this.diagnostics = opts.diagnostics
Expand Down Expand Up @@ -621,6 +624,18 @@ class PreferencesController {
return Promise.resolve(true)
}

//
// Brave
//

/**
* Sets the {@code batTokenAdded} state to {@code true}, indicating that the default addition of the BAT token has completed
*/
setBatTokenAdded () {
this.store.updateState({ batTokenAdded: true })
return Promise.resolve(true)
}

//
// PRIVATE METHODS
//
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ module.exports = class MetamaskController extends EventEmitter {
completeUiMigration: nodeify(preferencesController.completeUiMigration, preferencesController),
completeOnboarding: nodeify(preferencesController.completeOnboarding, preferencesController),
addKnownMethodData: nodeify(preferencesController.addKnownMethodData, preferencesController),
setBatTokenAdded: nodeify(preferencesController.setBatTokenAdded, preferencesController),

// BlacklistController
whitelistPhishingDomain: this.whitelistPhishingDomain.bind(this),
Expand Down
43 changes: 43 additions & 0 deletions ui/app/components/app/dropdowns/components/brave/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const inherits = require('util').inherits
const Component = require('react').Component
const h = require('react-hyperscript')

inherits(Menu, Component)
function Menu () { Component.call(this) }

Menu.prototype.render = function () {
const { className = '', children, isShowing } = this.props
return isShowing
? h('div', { className: `menu ${className}` }, children)
: h('noscript')
}

inherits(Item, Component)
function Item () { Component.call(this) }

Item.prototype.render = function () {
const {
icon,
children,
text,
className = '',
onClick,
isShowing
} = this.props

if (isShowing === false) {
return h('noscript')
}

const itemClassName = `menu__item ${className} ${onClick ? 'menu__item--clickable' : ''}`
const iconComponent = icon ? h('div.menu__item__icon', [icon]) : null
const textComponent = text ? h('div.menu__item__text', text) : null

return children
? h('div', { className: itemClassName, onClick }, children)
: h('div.menu__item', { className: itemClassName, onClick }, [ iconComponent, textComponent ]
.filter(d => Boolean(d))
)
}

module.exports = { Menu, Item }
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const Component = require('react').Component
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const inherits = require('util').inherits
const connect = require('react-redux').connect
const actions = require('../../../../../store/actions')
const genAccountLink = require('etherscan-link').createAccountLink
const { CloseArea } = require('../menu')
const { Menu, Item } = require('../brave/menu')
TokenMenuDropdown.contextTypes = {
t: PropTypes.func,
}

module.exports = connect(mapStateToProps, mapDispatchToProps)(TokenMenuDropdown)

function mapStateToProps (state) {
return {
network: state.metamask.network,
}
}

function mapDispatchToProps (dispatch) {
return {
showHideTokenConfirmationModal: (token) => {
dispatch(actions.showModal({ name: 'HIDE_TOKEN_CONFIRMATION', token }))
},
}
}


inherits(TokenMenuDropdown, Component)
function TokenMenuDropdown () {
Component.call(this)

this.onClose = this.onClose.bind(this)
}

TokenMenuDropdown.prototype.onClose = function (e) {
e.stopPropagation()
this.props.onClose()
}

TokenMenuDropdown.prototype.render = function () {
const { showHideTokenConfirmationModal } = this.props

return h(Menu, { className: 'token-menu-dropdown', isShowing: true }, [
h(CloseArea, {
onClick: this.onClose,
}),
h(Item, {
onClick: (e) => {
e.stopPropagation()
showHideTokenConfirmationModal(this.props.token)
this.props.onClose()
},
isShowing: (this.props.token.symbol !== 'BAT'),
text: this.context.t('hideToken'),
}),
h(Item, {
onClick: (e) => {
e.stopPropagation()
const url = genAccountLink(this.props.token.address, this.props.network)
global.platform.openWindow({ url })
this.props.onClose()
},
text: this.context.t('viewOnEtherscan'),
}),
])
}
1 change: 1 addition & 0 deletions ui/app/components/app/dropdowns/components/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Item.prototype.render = function () {
className = '',
onClick,
} = this.props

const itemClassName = `menu__item ${className} ${onClick ? 'menu__item--clickable' : ''}`
const iconComponent = icon ? h('div.menu__item__icon', [icon]) : null
const textComponent = text ? h('div.menu__item__text', text) : null
Expand Down
2 changes: 1 addition & 1 deletion ui/app/components/app/token-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const selectors = require('../../selectors/selectors')
const actions = require('../../store/actions')
const { conversionUtil, multiplyCurrencies } = require('../../helpers/utils/conversion-util')

const TokenMenuDropdown = require('./dropdowns/token-menu-dropdown.js')
const TokenMenuDropdown = require('./dropdowns/components/brave/token-menu-dropdown')

function mapStateToProps (state) {
return {
Expand Down
10 changes: 10 additions & 0 deletions ui/app/ducks/metamask/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ function reduceMetamask (state, action) {
knownMethodData: {},
participateInMetaMetrics: null,
metaMetricsSendCount: 0,

// Brave
batTokenAdded: false
}, state.metamask)

switch (action.type) {
Expand Down Expand Up @@ -415,6 +418,13 @@ function reduceMetamask (state, action) {
})
}

// Brave
case actions.SET_BAT_TOKEN_ADDED: {
return extend(metamaskState, {
batTokenAdded: action.value
})
}

default:
return metamaskState

Expand Down
8 changes: 8 additions & 0 deletions ui/app/pages/home/home.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Redirect } from 'react-router-dom'
import WalletView from '../../components/app/wallet-view'
import TransactionView from '../../components/app/transaction-view'
import ProviderApproval from '../provider-approval'
import actions from '../../store/actions'
import batToken from '../../store/brave/bat-token'

import {
INITIALIZE_SEED_PHRASE_ROUTE,
Expand All @@ -21,6 +23,7 @@ export default class Home extends PureComponent {
suggestedTokens: PropTypes.object,
unconfirmedTransactionsCount: PropTypes.number,
providerRequests: PropTypes.array,
batTokenAdded: PropTypes.bool
}

componentWillMount () {
Expand All @@ -37,13 +40,18 @@ export default class Home extends PureComponent {
componentDidMount () {
const {
history,
batTokenAdded,
suggestedTokens = {},
} = this.props

// suggested new tokens
if (Object.keys(suggestedTokens).length > 0) {
history.push(CONFIRM_ADD_SUGGESTED_TOKEN_ROUTE)
}

if (!batTokenAdded) {
this.props.dispatch(actions.addTokens(batToken))
}
}

render () {
Expand Down
2 changes: 2 additions & 0 deletions ui/app/pages/home/home.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mapStateToProps = state => {
seedWords,
suggestedTokens,
providerRequests,
batTokenAdded
} = metamask
const { forgottenPassword } = appState

Expand All @@ -21,6 +22,7 @@ const mapStateToProps = state => {
suggestedTokens,
unconfirmedTransactionsCount: unconfirmedTransactionsCountSelector(state),
providerRequests,
batTokenAdded
}
}

Expand Down
3 changes: 3 additions & 0 deletions ui/app/pages/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ Routes.propTypes = {
providerId: PropTypes.string,
providerRequests: PropTypes.array,
autoLogoutTimeLimit: PropTypes.number,
batTokenAdded: PropTypes.bool
}

function mapStateToProps (state) {
Expand Down Expand Up @@ -387,6 +388,7 @@ function mapStateToProps (state) {
unapprovedPersonalMsgCount,
unapprovedTypedMessagesCount,
providerRequests,
batTokenAdded
} = metamask
const selected = address || Object.keys(accounts)[0]

Expand Down Expand Up @@ -432,6 +434,7 @@ function mapStateToProps (state) {
selected,
keyrings,
providerRequests,
batTokenAdded
}
}

Expand Down
21 changes: 21 additions & 0 deletions ui/app/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ var actions = {
// AppStateController-related actions
SET_LAST_ACTIVE_TIME: 'SET_LAST_ACTIVE_TIME',
setLastActiveTime,

// Brave
setBatTokenAdded,
SET_BAT_TOKEN_ADDED: 'SET_BAT_TOKEN_ADDED'
}

module.exports = actions
Expand Down Expand Up @@ -1768,6 +1772,8 @@ function addToken (address, symbol, decimals, image) {
if (err) {
dispatch(actions.displayWarning(err.message))
reject(err)
} else if (symbol === 'BAT') {
dispatch(actions.setBatTokenAdded())
}
dispatch(actions.updateTokens(tokens))
resolve(tokens)
Expand Down Expand Up @@ -2774,3 +2780,18 @@ function setLastActiveTime () {
})
}
}

// Brave
function setBatTokenAdded () {
return (dispatch) => {
background.setBatTokenAdded((err) => {
if (err) {
return dispatch(actions.displayWarning(err.message))
}
})
dispatch({
type: actions.SET_BAT_TOKEN_ADDED,
value: true
})
}
}
10 changes: 10 additions & 0 deletions ui/app/store/brave/bat-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
'0x0D8775F648430679A709E98d2b0Cb6250d2887EF': {
'name': 'Basic Attention Token',
'logo': 'BAT_icon.svg',
'erc20': true,
'symbol': 'BAT',
'decimals': 18,
'address' :'0x0D8775F648430679A709E98d2b0Cb6250d2887EF'
}
}

0 comments on commit 93ab9b1

Please sign in to comment.