2016-04-13 15:28:44 -07:00
|
|
|
const extend = require('xtend')
|
|
|
|
const actions = require('../actions')
|
2017-08-18 04:11:26 -07:00
|
|
|
const MetamascaraPlatform = require('../../../app/scripts/platforms/window')
|
2016-04-13 15:28:44 -07:00
|
|
|
|
|
|
|
module.exports = reduceMetamask
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
function reduceMetamask (state, action) {
|
2016-06-21 13:56:04 -07:00
|
|
|
let newState
|
|
|
|
|
2016-04-13 15:28:44 -07:00
|
|
|
// clone + defaults
|
|
|
|
var metamaskState = extend({
|
|
|
|
isInitialized: false,
|
|
|
|
isUnlocked: false,
|
2017-10-15 22:28:25 -07:00
|
|
|
isAccountMenuOpen: false,
|
2017-08-18 04:11:26 -07:00
|
|
|
isMascara: window.platform instanceof MetamascaraPlatform,
|
2016-04-13 15:28:44 -07:00
|
|
|
rpcTarget: 'https://rawtestrpc.metamask.io/',
|
|
|
|
identities: {},
|
2017-01-27 16:11:59 -08:00
|
|
|
unapprovedTxs: {},
|
2016-12-07 14:34:15 -08:00
|
|
|
noActiveNotices: true,
|
|
|
|
lastUnreadNotice: undefined,
|
2017-03-07 16:01:51 -08:00
|
|
|
frequentRpcList: [],
|
2017-03-09 13:58:42 -08:00
|
|
|
addressBook: [],
|
2017-09-06 03:17:49 -07:00
|
|
|
selectedTokenAddress: null,
|
2017-09-13 01:25:39 -07:00
|
|
|
tokenExchangeRates: {},
|
2017-10-03 10:25:52 -07:00
|
|
|
tokens: [],
|
2017-10-12 09:42:14 -07:00
|
|
|
send: {
|
|
|
|
gasLimit: null,
|
|
|
|
gasPrice: null,
|
2017-10-17 10:22:23 -07:00
|
|
|
gasTotal: null,
|
|
|
|
from: '',
|
|
|
|
to: '',
|
|
|
|
amount: '0x0',
|
|
|
|
memo: '',
|
2017-10-17 13:13:20 -07:00
|
|
|
errors: {},
|
2017-10-12 09:42:14 -07:00
|
|
|
},
|
2017-09-25 14:51:49 -07:00
|
|
|
coinOptions: {},
|
2016-04-13 15:28:44 -07:00
|
|
|
}, state.metamask)
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
case actions.SHOW_ACCOUNTS_PAGE:
|
2016-06-21 13:56:04 -07:00
|
|
|
newState = extend(metamaskState)
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
2016-06-21 13:18:32 -07:00
|
|
|
|
2016-12-07 14:34:15 -08:00
|
|
|
case actions.SHOW_NOTICE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
noActiveNotices: false,
|
|
|
|
lastUnreadNotice: action.value,
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.CLEAR_NOTICES:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
noActiveNotices: true,
|
|
|
|
})
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
case actions.UPDATE_METAMASK_STATE:
|
|
|
|
return extend(metamaskState, action.value)
|
|
|
|
|
|
|
|
case actions.UNLOCK_METAMASK:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-30 15:08:31 -08:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 13:18:32 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
case actions.LOCK_METAMASK:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: false,
|
|
|
|
})
|
|
|
|
|
2017-03-07 10:25:50 -08:00
|
|
|
case actions.SET_RPC_LIST:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
frequentRpcList: action.value,
|
|
|
|
})
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
case actions.SET_RPC_TARGET:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
provider: {
|
|
|
|
type: 'rpc',
|
2017-03-07 10:25:50 -08:00
|
|
|
rpcTarget: action.value,
|
2016-06-21 13:18:32 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.SET_PROVIDER_TYPE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
provider: {
|
|
|
|
type: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.COMPLETED_TX:
|
|
|
|
var stringId = String(action.id)
|
2016-06-21 13:56:04 -07:00
|
|
|
newState = extend(metamaskState, {
|
2017-01-27 16:11:59 -08:00
|
|
|
unapprovedTxs: {},
|
|
|
|
unapprovedMsgs: {},
|
2016-06-21 13:18:32 -07:00
|
|
|
})
|
2017-01-27 16:11:59 -08:00
|
|
|
for (const id in metamaskState.unapprovedTxs) {
|
2016-06-21 13:18:32 -07:00
|
|
|
if (id !== stringId) {
|
2017-01-27 16:11:59 -08:00
|
|
|
newState.unapprovedTxs[id] = metamaskState.unapprovedTxs[id]
|
2016-06-21 13:18:32 -07:00
|
|
|
}
|
2016-04-13 15:28:44 -07:00
|
|
|
}
|
2017-01-27 16:11:59 -08:00
|
|
|
for (const id in metamaskState.unapprovedMsgs) {
|
2016-06-21 13:18:32 -07:00
|
|
|
if (id !== stringId) {
|
2017-01-27 16:11:59 -08:00
|
|
|
newState.unapprovedMsgs[id] = metamaskState.unapprovedMsgs[id]
|
2016-06-21 13:18:32 -07:00
|
|
|
}
|
2016-05-03 14:32:22 -07:00
|
|
|
}
|
2016-06-21 13:18:32 -07:00
|
|
|
return newState
|
|
|
|
|
|
|
|
case actions.SHOW_NEW_VAULT_SEED:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: false,
|
2017-03-28 08:23:25 -07:00
|
|
|
seedWords: action.value,
|
2016-06-21 13:18:32 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
case actions.CLEAR_SEED_WORD_CACHE:
|
2016-06-21 13:56:04 -07:00
|
|
|
newState = extend(metamaskState, {
|
2016-06-21 13:18:32 -07:00
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-30 15:08:31 -08:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 13:18:32 -07:00
|
|
|
})
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
|
|
|
|
|
|
|
case actions.SHOW_ACCOUNT_DETAIL:
|
2016-06-21 13:56:04 -07:00
|
|
|
newState = extend(metamaskState, {
|
2016-06-21 13:18:32 -07:00
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-30 15:08:31 -08:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 13:18:32 -07:00
|
|
|
})
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
|
|
|
|
2017-09-06 03:17:49 -07:00
|
|
|
case actions.SET_SELECTED_TOKEN:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
selectedTokenAddress: action.value,
|
|
|
|
})
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
case actions.SAVE_ACCOUNT_LABEL:
|
|
|
|
const account = action.value.account
|
|
|
|
const name = action.value.label
|
|
|
|
var id = {}
|
|
|
|
id[account] = extend(metamaskState.identities[account], { name })
|
|
|
|
var identities = extend(metamaskState.identities, id)
|
|
|
|
return extend(metamaskState, { identities })
|
|
|
|
|
2016-07-21 10:15:34 -07:00
|
|
|
case actions.SET_CURRENT_FIAT:
|
2016-07-22 10:15:39 -07:00
|
|
|
return extend(metamaskState, {
|
2017-03-14 17:06:16 -07:00
|
|
|
currentCurrency: action.value.currentCurrency,
|
2016-07-21 10:15:34 -07:00
|
|
|
conversionRate: action.value.conversionRate,
|
2016-07-21 14:08:26 -07:00
|
|
|
conversionDate: action.value.conversionDate,
|
2016-07-21 10:15:34 -07:00
|
|
|
})
|
|
|
|
|
2017-09-13 01:25:39 -07:00
|
|
|
case actions.UPDATE_TOKEN_EXCHANGE_RATE:
|
2017-10-24 16:50:25 -07:00
|
|
|
const { payload: { pair, marketinfo } } = action
|
2017-09-13 01:25:39 -07:00
|
|
|
return extend(metamaskState, {
|
|
|
|
tokenExchangeRates: {
|
|
|
|
...metamaskState.tokenExchangeRates,
|
|
|
|
[pair]: marketinfo,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-03 10:25:52 -07:00
|
|
|
case actions.UPDATE_TOKENS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
tokens: action.newTokens,
|
|
|
|
})
|
|
|
|
|
2017-10-17 10:22:23 -07:00
|
|
|
// metamask.send
|
2017-10-12 09:42:14 -07:00
|
|
|
case actions.UPDATE_GAS_LIMIT:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasLimit: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_GAS_PRICE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasPrice: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-15 22:28:25 -07:00
|
|
|
case actions.TOGGLE_ACCOUNT_MENU:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
|
|
|
})
|
|
|
|
|
2017-10-17 10:22:23 -07:00
|
|
|
case actions.UPDATE_GAS_TOTAL:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasTotal: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_FROM:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
from: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_TO:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
to: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_AMOUNT:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
amount: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_MEMO:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
memo: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-17 13:13:20 -07:00
|
|
|
case actions.UPDATE_SEND_ERRORS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
errors: {
|
|
|
|
...metamaskState.send.errors,
|
|
|
|
...action.value,
|
2017-11-02 05:15:59 -07:00
|
|
|
},
|
2017-10-17 13:13:20 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-20 19:05:22 -07:00
|
|
|
case actions.CLEAR_SEND:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
gasLimit: null,
|
|
|
|
gasPrice: null,
|
|
|
|
gasTotal: null,
|
|
|
|
from: '',
|
|
|
|
to: '',
|
|
|
|
amount: '0x0',
|
|
|
|
memo: '',
|
|
|
|
errors: {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-09-25 14:51:49 -07:00
|
|
|
case actions.PAIR_UPDATE:
|
|
|
|
const { value: { marketinfo: pairMarketInfo } } = action
|
|
|
|
return extend(metamaskState, {
|
|
|
|
tokenExchangeRates: {
|
|
|
|
...metamaskState.tokenExchangeRates,
|
|
|
|
[pairMarketInfo.pair]: pairMarketInfo,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.SHAPESHIFT_SUBVIEW:
|
2017-10-24 16:50:25 -07:00
|
|
|
const { value: { marketinfo: ssMarketInfo, coinOptions } } = action
|
2017-09-25 14:51:49 -07:00
|
|
|
return extend(metamaskState, {
|
|
|
|
tokenExchangeRates: {
|
|
|
|
...metamaskState.tokenExchangeRates,
|
2017-10-24 16:50:25 -07:00
|
|
|
[marketinfo.pair]: ssMarketInfo,
|
2017-09-25 14:51:49 -07:00
|
|
|
},
|
|
|
|
coinOptions,
|
|
|
|
})
|
|
|
|
|
2016-06-21 13:18:32 -07:00
|
|
|
default:
|
|
|
|
return metamaskState
|
2016-04-13 15:28:44 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|