2021-02-04 19:15:23 +01:00
|
|
|
import * as actionConstants from '../../store/actionConstants';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { ALERT_TYPES } from '../../../shared/constants/alerts';
|
|
|
|
import { NETWORK_TYPE_RPC } from '../../../shared/constants/network';
|
2021-06-08 18:03:59 +02:00
|
|
|
import {
|
|
|
|
accountsWithSendEtherInfoSelector,
|
|
|
|
getAddressBook,
|
|
|
|
} from '../../selectors';
|
2016-04-14 00:28:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function reduceMetamask(state = {}, action) {
|
2020-08-19 18:27:05 +02:00
|
|
|
const metamaskState = {
|
2016-04-14 00:28:44 +02:00
|
|
|
isInitialized: false,
|
|
|
|
isUnlocked: false,
|
2017-10-16 07:28:25 +02:00
|
|
|
isAccountMenuOpen: false,
|
2016-04-14 00:28:44 +02:00
|
|
|
identities: {},
|
2017-01-28 01:11:59 +01:00
|
|
|
unapprovedTxs: {},
|
2017-03-08 01:01:51 +01:00
|
|
|
frequentRpcList: [],
|
2017-03-09 22:58:42 +01:00
|
|
|
addressBook: [],
|
2018-04-16 05:37:42 +02:00
|
|
|
contractExchangeRates: {},
|
2017-10-03 19:25:52 +02:00
|
|
|
tokens: [],
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: {},
|
2019-09-27 06:30:36 +02:00
|
|
|
customNonceValue: '',
|
2017-11-24 02:33:44 +01:00
|
|
|
useBlockie: false,
|
2017-11-14 17:04:55 +01:00
|
|
|
featureFlags: {},
|
2018-03-08 18:31:21 +01:00
|
|
|
welcomeScreenSeen: false,
|
2018-03-23 17:59:59 +01:00
|
|
|
currentLocale: '',
|
2021-06-08 18:03:59 +02:00
|
|
|
currentBlockGasLimit: '',
|
2018-10-17 01:03:29 +02:00
|
|
|
preferences: {
|
2020-05-13 16:53:00 +02:00
|
|
|
autoLockTimeLimit: undefined,
|
2019-02-26 19:30:41 +01:00
|
|
|
showFiatInTestnets: false,
|
2020-05-13 16:53:00 +02:00
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
2018-10-17 01:03:29 +02:00
|
|
|
},
|
2019-03-05 16:45:01 +01:00
|
|
|
firstTimeFlowType: null,
|
2019-01-23 16:25:34 +01:00
|
|
|
completedOnboarding: false,
|
2019-01-22 19:22:56 +01:00
|
|
|
knownMethodData: {},
|
2019-03-05 16:45:01 +01:00
|
|
|
participateInMetaMetrics: null,
|
|
|
|
metaMetricsSendCount: 0,
|
2019-09-27 06:30:36 +02:00
|
|
|
nextNonce: null,
|
2021-06-08 18:03:59 +02:00
|
|
|
conversionRate: null,
|
|
|
|
nativeCurrency: 'ETH',
|
2020-08-19 18:27:05 +02:00
|
|
|
...state,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
switch (action.type) {
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_METAMASK_STATE:
|
2021-02-04 19:15:23 +01:00
|
|
|
return { ...metamaskState, ...action.value };
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.LOCK_METAMASK:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2016-06-21 22:18:32 +02:00
|
|
|
isUnlocked: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_RPC_TARGET:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2016-06-21 22:18:32 +02:00
|
|
|
provider: {
|
2021-01-20 17:49:05 +01:00
|
|
|
type: NETWORK_TYPE_RPC,
|
2020-10-06 19:57:02 +02:00
|
|
|
rpcUrl: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_PROVIDER_TYPE:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2016-06-21 22:18:32 +02:00
|
|
|
provider: {
|
|
|
|
type: action.value,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SHOW_ACCOUNT_DETAIL:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2016-06-21 22:18:32 +02:00
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-31 00:08:31 +01:00
|
|
|
selectedAddress: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-08-12 18:41:27 +02:00
|
|
|
case actionConstants.SET_ACCOUNT_LABEL: {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { account } = action.value;
|
|
|
|
const name = action.value.label;
|
|
|
|
const id = {};
|
|
|
|
id[account] = { ...metamaskState.identities[account], name };
|
|
|
|
const identities = { ...metamaskState.identities, ...id };
|
|
|
|
return Object.assign(metamaskState, { identities });
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_TOKENS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2017-10-03 19:25:52 +02:00
|
|
|
tokens: action.newTokens,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-10-03 19:25:52 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_CUSTOM_NONCE:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-09-27 06:30:36 +02:00
|
|
|
customNonceValue: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-10-12 18:42:14 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.TOGGLE_ACCOUNT_MENU:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2017-10-16 07:28:25 +02:00
|
|
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-10-16 07:28:25 +02:00
|
|
|
|
2020-08-12 18:41:27 +02:00
|
|
|
case actionConstants.UPDATE_TRANSACTION_PARAMS: {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { id: txId, value } = action;
|
|
|
|
let { currentNetworkTxList } = metamaskState;
|
2020-03-06 22:34:56 +01:00
|
|
|
currentNetworkTxList = currentNetworkTxList.map((tx) => {
|
2017-11-08 20:18:27 +01:00
|
|
|
if (tx.id === txId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const newTx = { ...tx };
|
|
|
|
newTx.txParams = value;
|
|
|
|
return newTx;
|
2017-11-08 17:44:48 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return tx;
|
|
|
|
});
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2020-03-06 22:34:56 +01:00
|
|
|
currentNetworkTxList,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_PARTICIPATE_IN_METAMETRICS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-03-05 16:45:01 +01:00
|
|
|
participateInMetaMetrics: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_METAMETRICS_SEND_COUNT:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-03-05 16:45:01 +01:00
|
|
|
metaMetricsSendCount: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_USE_BLOCKIE:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-10-10 10:12:25 +02:00
|
|
|
useBlockie: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-11-24 02:33:44 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_FEATURE_FLAGS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2017-11-14 17:04:55 +01:00
|
|
|
featureFlags: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-11-14 17:04:55 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.CLOSE_WELCOME_SCREEN:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-03-08 18:31:21 +01:00
|
|
|
welcomeScreenSeen: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-03-08 18:31:21 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_CURRENT_LOCALE:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-09-06 15:47:07 +02:00
|
|
|
currentLocale: action.value.locale,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_PENDING_TOKENS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: { ...action.payload },
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.CLEAR_PENDING_TOKENS: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_PREFERENCES: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-02-26 19:30:41 +01:00
|
|
|
preferences: {
|
|
|
|
...metamaskState.preferences,
|
|
|
|
...action.payload,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-17 01:03:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.COMPLETE_ONBOARDING: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-01-23 16:25:34 +01:00
|
|
|
completedOnboarding: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_FIRST_TIME_FLOW_TYPE: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-03-05 16:45:01 +01:00
|
|
|
firstTimeFlowType: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_NEXT_NONCE: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-09-27 06:30:36 +02:00
|
|
|
nextNonce: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-27 06:30:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
default:
|
2021-02-04 19:15:23 +01:00
|
|
|
return metamaskState;
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 18:07:05 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getCurrentLocale = (state) => state.metamask.currentLocale;
|
2020-05-08 21:45:52 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getAlertEnabledness = (state) => state.metamask.alertEnabledness;
|
2020-05-08 21:45:52 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getUnconnectedAccountAlertEnabledness = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
getAlertEnabledness(state)[ALERT_TYPES.unconnectedAccount];
|
2020-05-12 15:01:52 +02:00
|
|
|
|
2020-12-11 00:40:29 +01:00
|
|
|
export const getWeb3ShimUsageAlertEnabledness = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
getAlertEnabledness(state)[ALERT_TYPES.web3ShimUsage];
|
2020-12-11 00:40:29 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getUnconnectedAccountAlertShown = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unconnectedAccountAlertShownOrigins;
|
2020-05-29 02:43:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getTokens = (state) => state.metamask.tokens;
|
2021-06-08 18:03:59 +02:00
|
|
|
|
|
|
|
export function getBlockGasLimit(state) {
|
|
|
|
return state.metamask.currentBlockGasLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getConversionRate(state) {
|
|
|
|
return state.metamask.conversionRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNativeCurrency(state) {
|
|
|
|
return state.metamask.nativeCurrency;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSendHexDataFeatureFlagState(state) {
|
|
|
|
return state.metamask.featureFlags.sendHexData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSendToAccounts(state) {
|
|
|
|
const fromAccounts = accountsWithSendEtherInfoSelector(state);
|
|
|
|
const addressBookAccounts = getAddressBook(state);
|
|
|
|
return [...fromAccounts, ...addressBookAccounts];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUnapprovedTxs(state) {
|
|
|
|
return state.metamask.unapprovedTxs;
|
|
|
|
}
|