You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
420 lines
10 KiB
420 lines
10 KiB
9 years ago
|
const extend = require('xtend')
|
||
6 years ago
|
const actions = require('../../store/actions')
|
||
|
const { getEnvironmentType } = require('../../../../app/scripts/lib/util')
|
||
|
const { ENVIRONMENT_TYPE_POPUP } = require('../../../../app/scripts/lib/enums')
|
||
|
const { OLD_UI_NETWORK_TYPE } = require('../../../../app/scripts/controllers/network/enums')
|
||
9 years ago
|
|
||
|
module.exports = reduceMetamask
|
||
|
|
||
9 years ago
|
function reduceMetamask (state, action) {
|
||
9 years ago
|
let newState
|
||
|
|
||
9 years ago
|
// clone + defaults
|
||
|
var metamaskState = extend({
|
||
|
isInitialized: false,
|
||
|
isUnlocked: false,
|
||
7 years ago
|
isAccountMenuOpen: false,
|
||
7 years ago
|
isPopup: getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_POPUP,
|
||
9 years ago
|
rpcTarget: 'https://rawtestrpc.metamask.io/',
|
||
|
identities: {},
|
||
8 years ago
|
unapprovedTxs: {},
|
||
8 years ago
|
noActiveNotices: true,
|
||
7 years ago
|
nextUnreadNotice: undefined,
|
||
8 years ago
|
frequentRpcList: [],
|
||
8 years ago
|
addressBook: [],
|
||
7 years ago
|
selectedTokenAddress: null,
|
||
7 years ago
|
contractExchangeRates: {},
|
||
7 years ago
|
tokenExchangeRates: {},
|
||
7 years ago
|
tokens: [],
|
||
7 years ago
|
pendingTokens: {},
|
||
7 years ago
|
send: {
|
||
|
gasLimit: null,
|
||
|
gasPrice: null,
|
||
7 years ago
|
gasTotal: null,
|
||
6 years ago
|
tokenBalance: '0x0',
|
||
7 years ago
|
from: '',
|
||
|
to: '',
|
||
6 years ago
|
amount: '0',
|
||
7 years ago
|
memo: '',
|
||
7 years ago
|
errors: {},
|
||
7 years ago
|
maxModeOn: false,
|
||
7 years ago
|
editingTransactionId: null,
|
||
7 years ago
|
forceGasMin: null,
|
||
7 years ago
|
toNickname: '',
|
||
7 years ago
|
},
|
||
7 years ago
|
coinOptions: {},
|
||
7 years ago
|
useBlockie: false,
|
||
7 years ago
|
featureFlags: {},
|
||
7 years ago
|
networkEndpointType: OLD_UI_NETWORK_TYPE,
|
||
7 years ago
|
isRevealingSeedWords: false,
|
||
7 years ago
|
welcomeScreenSeen: false,
|
||
7 years ago
|
currentLocale: '',
|
||
6 years ago
|
preferences: {
|
||
6 years ago
|
useNativeCurrencyAsPrimaryCurrency: true,
|
||
6 years ago
|
showFiatInTestnets: false,
|
||
6 years ago
|
},
|
||
6 years ago
|
firstTimeFlowType: null,
|
||
6 years ago
|
completedOnboarding: false,
|
||
6 years ago
|
knownMethodData: {},
|
||
6 years ago
|
participateInMetaMetrics: null,
|
||
|
metaMetricsSendCount: 0,
|
||
9 years ago
|
}, state.metamask)
|
||
|
|
||
|
switch (action.type) {
|
||
|
|
||
9 years ago
|
case actions.SHOW_ACCOUNTS_PAGE:
|
||
7 years ago
|
newState = extend(metamaskState, {
|
||
|
isRevealingSeedWords: false,
|
||
|
})
|
||
9 years ago
|
delete newState.seedWords
|
||
|
return newState
|
||
9 years ago
|
|
||
8 years ago
|
case actions.SHOW_NOTICE:
|
||
|
return extend(metamaskState, {
|
||
|
noActiveNotices: false,
|
||
7 years ago
|
nextUnreadNotice: action.value,
|
||
8 years ago
|
})
|
||
|
|
||
|
case actions.CLEAR_NOTICES:
|
||
|
return extend(metamaskState, {
|
||
|
noActiveNotices: true,
|
||
6 years ago
|
nextUnreadNotice: undefined,
|
||
8 years ago
|
})
|
||
|
|
||
9 years ago
|
case actions.UPDATE_METAMASK_STATE:
|
||
|
return extend(metamaskState, action.value)
|
||
|
|
||
|
case actions.UNLOCK_METAMASK:
|
||
|
return extend(metamaskState, {
|
||
|
isUnlocked: true,
|
||
|
isInitialized: true,
|
||
8 years ago
|
selectedAddress: action.value,
|
||
9 years ago
|
})
|
||
|
|
||
|
case actions.LOCK_METAMASK:
|
||
|
return extend(metamaskState, {
|
||
|
isUnlocked: false,
|
||
|
})
|
||
|
|
||
8 years ago
|
case actions.SET_RPC_LIST:
|
||
|
return extend(metamaskState, {
|
||
|
frequentRpcList: action.value,
|
||
|
})
|
||
|
|
||
9 years ago
|
case actions.SET_RPC_TARGET:
|
||
|
return extend(metamaskState, {
|
||
|
provider: {
|
||
|
type: 'rpc',
|
||
8 years ago
|
rpcTarget: action.value,
|
||
9 years ago
|
},
|
||
|
})
|
||
|
|
||
|
case actions.SET_PROVIDER_TYPE:
|
||
|
return extend(metamaskState, {
|
||
|
provider: {
|
||
|
type: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
case actions.COMPLETED_TX:
|
||
|
var stringId = String(action.id)
|
||
9 years ago
|
newState = extend(metamaskState, {
|
||
8 years ago
|
unapprovedTxs: {},
|
||
|
unapprovedMsgs: {},
|
||
9 years ago
|
})
|
||
8 years ago
|
for (const id in metamaskState.unapprovedTxs) {
|
||
9 years ago
|
if (id !== stringId) {
|
||
8 years ago
|
newState.unapprovedTxs[id] = metamaskState.unapprovedTxs[id]
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
for (const id in metamaskState.unapprovedMsgs) {
|
||
9 years ago
|
if (id !== stringId) {
|
||
8 years ago
|
newState.unapprovedMsgs[id] = metamaskState.unapprovedMsgs[id]
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
return newState
|
||
|
|
||
7 years ago
|
case actions.EDIT_TX:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
editingTransactionId: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
|
||
9 years ago
|
case actions.SHOW_NEW_VAULT_SEED:
|
||
|
return extend(metamaskState, {
|
||
7 years ago
|
isRevealingSeedWords: true,
|
||
8 years ago
|
seedWords: action.value,
|
||
9 years ago
|
})
|
||
|
|
||
|
case actions.CLEAR_SEED_WORD_CACHE:
|
||
9 years ago
|
newState = extend(metamaskState, {
|
||
9 years ago
|
isUnlocked: true,
|
||
|
isInitialized: true,
|
||
8 years ago
|
selectedAddress: action.value,
|
||
9 years ago
|
})
|
||
|
delete newState.seedWords
|
||
|
return newState
|
||
|
|
||
|
case actions.SHOW_ACCOUNT_DETAIL:
|
||
9 years ago
|
newState = extend(metamaskState, {
|
||
9 years ago
|
isUnlocked: true,
|
||
|
isInitialized: true,
|
||
8 years ago
|
selectedAddress: action.value,
|
||
9 years ago
|
})
|
||
|
delete newState.seedWords
|
||
|
return newState
|
||
|
|
||
7 years ago
|
case actions.SET_SELECTED_TOKEN:
|
||
|
return extend(metamaskState, {
|
||
|
selectedTokenAddress: action.value,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.SET_ACCOUNT_LABEL:
|
||
9 years ago
|
const account = action.value.account
|
||
|
const name = action.value.label
|
||
7 years ago
|
const id = {}
|
||
9 years ago
|
id[account] = extend(metamaskState.identities[account], { name })
|
||
7 years ago
|
const identities = extend(metamaskState.identities, id)
|
||
9 years ago
|
return extend(metamaskState, { identities })
|
||
|
|
||
8 years ago
|
case actions.SET_CURRENT_FIAT:
|
||
8 years ago
|
return extend(metamaskState, {
|
||
8 years ago
|
currentCurrency: action.value.currentCurrency,
|
||
8 years ago
|
conversionRate: action.value.conversionRate,
|
||
8 years ago
|
conversionDate: action.value.conversionDate,
|
||
8 years ago
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_TOKENS:
|
||
|
return extend(metamaskState, {
|
||
|
tokens: action.newTokens,
|
||
|
})
|
||
|
|
||
7 years ago
|
// metamask.send
|
||
7 years ago
|
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,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.TOGGLE_ACCOUNT_MENU:
|
||
|
return extend(metamaskState, {
|
||
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_GAS_TOTAL:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
gasTotal: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_SEND_TOKEN_BALANCE:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
tokenBalance: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
6 years ago
|
case actions.UPDATE_SEND_HEX_DATA:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
data: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
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,
|
||
7 years ago
|
to: action.value.to,
|
||
|
toNickname: action.value.nickname,
|
||
7 years ago
|
},
|
||
|
})
|
||
|
|
||
|
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,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_MAX_MODE:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
maxModeOn: action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_SEND:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
...metamaskState.send,
|
||
|
...action.value,
|
||
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.CLEAR_SEND:
|
||
|
return extend(metamaskState, {
|
||
|
send: {
|
||
|
gasLimit: null,
|
||
|
gasPrice: null,
|
||
|
gasTotal: null,
|
||
7 years ago
|
tokenBalance: null,
|
||
7 years ago
|
from: '',
|
||
|
to: '',
|
||
|
amount: '0x0',
|
||
|
memo: '',
|
||
|
errors: {},
|
||
6 years ago
|
maxModeOn: false,
|
||
7 years ago
|
editingTransactionId: null,
|
||
7 years ago
|
forceGasMin: null,
|
||
6 years ago
|
toNickname: '',
|
||
7 years ago
|
},
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_TRANSACTION_PARAMS:
|
||
7 years ago
|
const { id: txId, value } = action
|
||
7 years ago
|
let { selectedAddressTxList } = metamaskState
|
||
|
selectedAddressTxList = selectedAddressTxList.map(tx => {
|
||
7 years ago
|
if (tx.id === txId) {
|
||
7 years ago
|
tx.txParams = value
|
||
|
}
|
||
|
return tx
|
||
|
})
|
||
|
|
||
|
return extend(metamaskState, {
|
||
|
selectedAddressTxList,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.PAIR_UPDATE:
|
||
|
const { value: { marketinfo: pairMarketInfo } } = action
|
||
|
return extend(metamaskState, {
|
||
|
tokenExchangeRates: {
|
||
|
...metamaskState.tokenExchangeRates,
|
||
|
[pairMarketInfo.pair]: pairMarketInfo,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
case actions.SHAPESHIFT_SUBVIEW:
|
||
7 years ago
|
const { value: { marketinfo: ssMarketInfo, coinOptions } } = action
|
||
7 years ago
|
return extend(metamaskState, {
|
||
|
tokenExchangeRates: {
|
||
|
...metamaskState.tokenExchangeRates,
|
||
7 years ago
|
[ssMarketInfo.pair]: ssMarketInfo,
|
||
7 years ago
|
},
|
||
|
coinOptions,
|
||
|
})
|
||
|
|
||
6 years ago
|
case actions.SET_PARTICIPATE_IN_METAMETRICS:
|
||
|
return extend(metamaskState, {
|
||
|
participateInMetaMetrics: action.value,
|
||
|
})
|
||
|
|
||
|
case actions.SET_METAMETRICS_SEND_COUNT:
|
||
|
return extend(metamaskState, {
|
||
|
metaMetricsSendCount: action.value,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.SET_USE_BLOCKIE:
|
||
6 years ago
|
return extend(metamaskState, {
|
||
|
useBlockie: action.value,
|
||
|
})
|
||
7 years ago
|
|
||
7 years ago
|
case actions.UPDATE_FEATURE_FLAGS:
|
||
|
return extend(metamaskState, {
|
||
|
featureFlags: action.value,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.UPDATE_NETWORK_ENDPOINT_TYPE:
|
||
|
return extend(metamaskState, {
|
||
|
networkEndpointType: action.value,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.CLOSE_WELCOME_SCREEN:
|
||
|
return extend(metamaskState, {
|
||
|
welcomeScreenSeen: true,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.SET_CURRENT_LOCALE:
|
||
7 years ago
|
return extend(metamaskState, {
|
||
|
currentLocale: action.value,
|
||
|
})
|
||
|
|
||
7 years ago
|
case actions.SET_PENDING_TOKENS:
|
||
|
return extend(metamaskState, {
|
||
|
pendingTokens: { ...action.payload },
|
||
|
})
|
||
|
|
||
|
case actions.CLEAR_PENDING_TOKENS: {
|
||
|
return extend(metamaskState, {
|
||
|
pendingTokens: {},
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
case actions.UPDATE_PREFERENCES: {
|
||
|
return extend(metamaskState, {
|
||
6 years ago
|
preferences: {
|
||
|
...metamaskState.preferences,
|
||
|
...action.payload,
|
||
|
},
|
||
6 years ago
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
case actions.COMPLETE_ONBOARDING: {
|
||
|
return extend(metamaskState, {
|
||
|
completedOnboarding: true,
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
case actions.COMPLETE_UI_MIGRATION: {
|
||
|
return extend(metamaskState, {
|
||
|
completedUiMigration: true,
|
||
|
})
|
||
|
}
|
||
|
|
||
6 years ago
|
case actions.SET_FIRST_TIME_FLOW_TYPE: {
|
||
|
return extend(metamaskState, {
|
||
|
firstTimeFlowType: action.value,
|
||
|
})
|
||
|
}
|
||
|
|
||
9 years ago
|
default:
|
||
|
return metamaskState
|
||
9 years ago
|
|
||
|
}
|
||
|
}
|