1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Add toggle for incoming transactions (#7049)

This commit is contained in:
Whymarrh Whitby 2019-08-21 16:12:14 -02:30 committed by GitHub
parent f9c0bdda32
commit ef3859ff77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 108 additions and 5 deletions

View File

@ -1,4 +1,10 @@
{ {
"showIncomingTransactions": {
"message": "Show Incoming Transactions"
},
"showIncomingTransactionsDescription": {
"message": "Select this to use Etherscan to show incoming transactions in the transactions list"
},
"exposeAccounts": { "exposeAccounts": {
"message": "Expose Accounts" "message": "Expose Accounts"
}, },

View File

@ -56,6 +56,35 @@ class IncomingTransactionsController {
}, opts.initState) }, opts.initState)
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
this.preferencesController.store.subscribe(pairwise((prevState, currState) => {
const { featureFlags: { showIncomingTransactions: prevShowIncomingTransactions } = {} } = prevState
const { featureFlags: { showIncomingTransactions: currShowIncomingTransactions } = {} } = currState
if (currShowIncomingTransactions === prevShowIncomingTransactions) {
return
}
if (prevShowIncomingTransactions && !currShowIncomingTransactions) {
this.stop()
return
}
this.start()
}))
this.preferencesController.store.subscribe(pairwise(async (prevState, currState) => {
const { selectedAddress: prevSelectedAddress } = prevState
const { selectedAddress: currSelectedAddress } = currState
if (currSelectedAddress === prevSelectedAddress) {
return
}
await this._update({
address: currSelectedAddress,
})
}))
this.networkController.on('networkDidChange', async (newType) => { this.networkController.on('networkDidChange', async (newType) => {
const address = this.preferencesController.getSelectedAddress() const address = this.preferencesController.getSelectedAddress()
await this._update({ await this._update({
@ -63,14 +92,16 @@ class IncomingTransactionsController {
networkType: newType, networkType: newType,
}) })
}) })
this.preferencesController.store.subscribe(async ({ selectedAddress }) => {
await this._update({
address: selectedAddress,
})
})
} }
start () { start () {
const { featureFlags = {} } = this.preferencesController.store.getState()
const { showIncomingTransactions } = featureFlags
if (!showIncomingTransactions) {
return
}
this.blockTracker.removeListener('latest', this._onLatestBlock) this.blockTracker.removeListener('latest', this._onLatestBlock)
this.blockTracker.addListener('latest', this._onLatestBlock) this.blockTracker.addListener('latest', this._onLatestBlock)
} }
@ -234,3 +265,20 @@ class IncomingTransactionsController {
} }
module.exports = IncomingTransactionsController module.exports = IncomingTransactionsController
function pairwise (fn) {
let first = true
let cache
return (value) => {
try {
if (first) {
first = false
return fn(value, value)
} else {
return fn(cache, value)
}
} finally {
cache = value
}
}
}

View File

@ -41,6 +41,7 @@ class PreferencesController {
// for convenient testing of pre-release features, and should never // for convenient testing of pre-release features, and should never
// perform sensitive operations. // perform sensitive operations.
featureFlags: { featureFlags: {
showIncomingTransactions: true,
}, },
knownMethodData: {}, knownMethodData: {},
participateInMetaMetrics: null, participateInMetaMetrics: null,

View File

@ -1,5 +1,8 @@
{ {
"metamask": { "metamask": {
"featureFlags": {
"showIncomingTransactions": true
},
"network": "4", "network": "4",
"identities": { "identities": {
"0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc": { "0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc": {

View File

@ -63,6 +63,11 @@ describe('IncomingTransactionsController', () => {
const MOCK_PREFERENCES_CONTROLLER = { const MOCK_PREFERENCES_CONTROLLER = {
getSelectedAddress: sinon.stub().returns('0x0101'), getSelectedAddress: sinon.stub().returns('0x0101'),
store: { store: {
getState: sinon.stub().returns({
featureFlags: {
showIncomingTransactions: true,
},
}),
subscribe: sinon.spy(), subscribe: sinon.spy(),
}, },
} }

View File

@ -18,6 +18,8 @@ export default class SecurityTab extends PureComponent {
mobileSync: PropTypes.bool, mobileSync: PropTypes.bool,
participateInMetaMetrics: PropTypes.bool, participateInMetaMetrics: PropTypes.bool,
setParticipateInMetaMetrics: PropTypes.func, setParticipateInMetaMetrics: PropTypes.func,
showIncomingTransactions: PropTypes.bool,
setShowIncomingTransactionsFeatureFlag: PropTypes.func,
} }
renderSeedWords () { renderSeedWords () {
@ -80,6 +82,32 @@ export default class SecurityTab extends PureComponent {
) )
} }
renderIncomingTransactionsOptIn () {
const { t } = this.context
const { showIncomingTransactions, setShowIncomingTransactionsFeatureFlag } = this.props
return (
<div className="settings-page__content-row">
<div className="settings-page__content-item">
<span>{ t('showIncomingTransactions') }</span>
<div className="settings-page__content-description">
{ t('showIncomingTransactionsDescription') }
</div>
</div>
<div className="settings-page__content-item">
<div className="settings-page__content-item-col">
<ToggleButton
value={showIncomingTransactions}
onToggle={value => setShowIncomingTransactionsFeatureFlag(!value)}
offLabel={t('off')}
onLabel={t('on')}
/>
</div>
</div>
</div>
)
}
renderContent () { renderContent () {
const { warning } = this.props const { warning } = this.props
@ -87,6 +115,7 @@ export default class SecurityTab extends PureComponent {
<div className="settings-page__body"> <div className="settings-page__body">
{ warning && <div className="settings-tab__error">{ warning }</div> } { warning && <div className="settings-tab__error">{ warning }</div> }
{ this.renderSeedWords() } { this.renderSeedWords() }
{ this.renderIncomingTransactionsOptIn() }
{ this.renderMetaMetricsOptIn() } { this.renderMetaMetricsOptIn() }
</div> </div>
) )

View File

@ -5,17 +5,22 @@ import { withRouter } from 'react-router-dom'
import { import {
displayWarning, displayWarning,
revealSeedConfirmation, revealSeedConfirmation,
setFeatureFlag,
setParticipateInMetaMetrics, setParticipateInMetaMetrics,
} from '../../../store/actions' } from '../../../store/actions'
const mapStateToProps = state => { const mapStateToProps = state => {
const { appState: { warning }, metamask } = state const { appState: { warning }, metamask } = state
const { const {
featureFlags: {
showIncomingTransactions,
} = {},
participateInMetaMetrics, participateInMetaMetrics,
} = metamask } = metamask
return { return {
warning, warning,
showIncomingTransactions,
participateInMetaMetrics, participateInMetaMetrics,
} }
} }
@ -25,6 +30,7 @@ const mapDispatchToProps = dispatch => {
displayWarning: warning => dispatch(displayWarning(warning)), displayWarning: warning => dispatch(displayWarning(warning)),
revealSeedConfirmation: () => dispatch(revealSeedConfirmation()), revealSeedConfirmation: () => dispatch(revealSeedConfirmation()),
setParticipateInMetaMetrics: (val) => dispatch(setParticipateInMetaMetrics(val)), setParticipateInMetaMetrics: (val) => dispatch(setParticipateInMetaMetrics(val)),
setShowIncomingTransactionsFeatureFlag: shouldShow => dispatch(setFeatureFlag('showIncomingTransactions', shouldShow)),
} }
} }

View File

@ -16,6 +16,11 @@ import txHelper from '../../lib/tx-helper'
export const shapeShiftTxListSelector = state => state.metamask.shapeShiftTxList export const shapeShiftTxListSelector = state => state.metamask.shapeShiftTxList
export const incomingTxListSelector = state => { export const incomingTxListSelector = state => {
const { showIncomingTransactions } = state.metamask.featureFlags
if (!showIncomingTransactions) {
return []
}
const network = state.metamask.network const network = state.metamask.network
const selectedAddress = state.metamask.selectedAddress const selectedAddress = state.metamask.selectedAddress
return Object.values(state.metamask.incomingTransactions) return Object.values(state.metamask.incomingTransactions)