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.
381 lines
9.1 KiB
381 lines
9.1 KiB
4 years ago
|
import * as actionConstants from '../../store/actionConstants';
|
||
4 years ago
|
import { ALERT_TYPES } from '../../../shared/constants/alerts';
|
||
|
import { NETWORK_TYPE_RPC } from '../../../shared/constants/network';
|
||
9 years ago
|
|
||
4 years ago
|
export default function reduceMetamask(state = {}, action) {
|
||
4 years ago
|
const metamaskState = {
|
||
9 years ago
|
isInitialized: false,
|
||
|
isUnlocked: false,
|
||
7 years ago
|
isAccountMenuOpen: false,
|
||
9 years ago
|
identities: {},
|
||
8 years ago
|
unapprovedTxs: {},
|
||
8 years ago
|
frequentRpcList: [],
|
||
8 years ago
|
addressBook: [],
|
||
7 years ago
|
contractExchangeRates: {},
|
||
7 years ago
|
tokens: [],
|
||
7 years ago
|
pendingTokens: {},
|
||
5 years ago
|
customNonceValue: '',
|
||
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
|
toNickname: '',
|
||
5 years ago
|
ensResolution: null,
|
||
|
ensResolutionError: '',
|
||
7 years ago
|
},
|
||
7 years ago
|
useBlockie: false,
|
||
7 years ago
|
featureFlags: {},
|
||
7 years ago
|
welcomeScreenSeen: false,
|
||
7 years ago
|
currentLocale: '',
|
||
6 years ago
|
preferences: {
|
||
5 years ago
|
autoLockTimeLimit: undefined,
|
||
6 years ago
|
showFiatInTestnets: false,
|
||
5 years ago
|
useNativeCurrencyAsPrimaryCurrency: true,
|
||
6 years ago
|
},
|
||
6 years ago
|
firstTimeFlowType: null,
|
||
6 years ago
|
completedOnboarding: false,
|
||
6 years ago
|
knownMethodData: {},
|
||
6 years ago
|
participateInMetaMetrics: null,
|
||
|
metaMetricsSendCount: 0,
|
||
5 years ago
|
nextNonce: null,
|
||
4 years ago
|
...state,
|
||
4 years ago
|
};
|
||
9 years ago
|
|
||
|
switch (action.type) {
|
||
5 years ago
|
case actionConstants.UPDATE_METAMASK_STATE:
|
||
4 years ago
|
return { ...metamaskState, ...action.value };
|
||
9 years ago
|
|
||
5 years ago
|
case actionConstants.LOCK_METAMASK:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
9 years ago
|
isUnlocked: false,
|
||
4 years ago
|
};
|
||
9 years ago
|
|
||
5 years ago
|
case actionConstants.SET_RPC_TARGET:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
9 years ago
|
provider: {
|
||
4 years ago
|
type: NETWORK_TYPE_RPC,
|
||
4 years ago
|
rpcUrl: action.value,
|
||
9 years ago
|
},
|
||
4 years ago
|
};
|
||
9 years ago
|
|
||
5 years ago
|
case actionConstants.SET_PROVIDER_TYPE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
9 years ago
|
provider: {
|
||
|
type: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
9 years ago
|
|
||
5 years ago
|
case actionConstants.SHOW_ACCOUNT_DETAIL:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
9 years ago
|
isUnlocked: true,
|
||
|
isInitialized: true,
|
||
8 years ago
|
selectedAddress: action.value,
|
||
4 years ago
|
};
|
||
9 years ago
|
|
||
4 years ago
|
case actionConstants.SET_ACCOUNT_LABEL: {
|
||
4 years ago
|
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 });
|
||
4 years ago
|
}
|
||
9 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_TOKENS:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
tokens: action.newTokens,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
7 years ago
|
// metamask.send
|
||
5 years ago
|
case actionConstants.UPDATE_GAS_LIMIT:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
gasLimit: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
case actionConstants.UPDATE_CUSTOM_NONCE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
customNonceValue: action.value,
|
||
4 years ago
|
};
|
||
5 years ago
|
case actionConstants.UPDATE_GAS_PRICE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
gasPrice: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.TOGGLE_ACCOUNT_MENU:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_GAS_TOTAL:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
gasTotal: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_TOKEN_BALANCE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
tokenBalance: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_HEX_DATA:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
data: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_TO:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
7 years ago
|
to: action.value.to,
|
||
|
toNickname: action.value.nickname,
|
||
7 years ago
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_AMOUNT:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
amount: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_MAX_MODE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
maxModeOn: action.value,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND:
|
||
5 years ago
|
return Object.assign(metamaskState, {
|
||
7 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
...action.value,
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
case actionConstants.UPDATE_SEND_TOKEN: {
|
||
5 years ago
|
const newSend = {
|
||
|
...metamaskState.send,
|
||
|
token: action.value,
|
||
4 years ago
|
};
|
||
5 years ago
|
// erase token-related state when switching back to native currency
|
||
|
if (newSend.editingTransactionId && !newSend.token) {
|
||
4 years ago
|
const unapprovedTx =
|
||
4 years ago
|
newSend?.unapprovedTxs?.[newSend.editingTransactionId] || {};
|
||
|
const txParams = unapprovedTx.txParams || {};
|
||
5 years ago
|
Object.assign(newSend, {
|
||
|
tokenBalance: null,
|
||
|
balance: '0',
|
||
|
from: unapprovedTx.from || '',
|
||
|
unapprovedTxs: {
|
||
|
...newSend.unapprovedTxs,
|
||
|
[newSend.editingTransactionId]: {
|
||
|
...unapprovedTx,
|
||
|
txParams: {
|
||
|
...txParams,
|
||
|
data: '',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
}
|
||
|
return Object.assign(metamaskState, {
|
||
|
send: newSend,
|
||
4 years ago
|
});
|
||
4 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_ENS_RESOLUTION:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
ensResolution: action.payload,
|
||
|
ensResolutionError: '',
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_SEND_ENS_RESOLUTION_ERROR:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
send: {
|
||
|
...metamaskState.send,
|
||
|
ensResolution: null,
|
||
|
ensResolutionError: action.payload,
|
||
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.CLEAR_SEND:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
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,
|
||
6 years ago
|
toNickname: '',
|
||
7 years ago
|
},
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
case actionConstants.UPDATE_TRANSACTION_PARAMS: {
|
||
4 years ago
|
const { id: txId, value } = action;
|
||
|
let { currentNetworkTxList } = metamaskState;
|
||
5 years ago
|
currentNetworkTxList = currentNetworkTxList.map((tx) => {
|
||
7 years ago
|
if (tx.id === txId) {
|
||
4 years ago
|
const newTx = { ...tx };
|
||
|
newTx.txParams = value;
|
||
|
return newTx;
|
||
7 years ago
|
}
|
||
4 years ago
|
return tx;
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
currentNetworkTxList,
|
||
4 years ago
|
};
|
||
4 years ago
|
}
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.SET_PARTICIPATE_IN_METAMETRICS:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
participateInMetaMetrics: action.value,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
5 years ago
|
case actionConstants.SET_METAMETRICS_SEND_COUNT:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
metaMetricsSendCount: action.value,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
5 years ago
|
case actionConstants.SET_USE_BLOCKIE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
useBlockie: action.value,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.UPDATE_FEATURE_FLAGS:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
featureFlags: action.value,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.CLOSE_WELCOME_SCREEN:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
welcomeScreenSeen: true,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.SET_CURRENT_LOCALE:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
currentLocale: action.value.locale,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.SET_PENDING_TOKENS:
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
pendingTokens: { ...action.payload },
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
case actionConstants.CLEAR_PENDING_TOKENS: {
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
7 years ago
|
pendingTokens: {},
|
||
4 years ago
|
};
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
case actionConstants.UPDATE_PREFERENCES: {
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
preferences: {
|
||
|
...metamaskState.preferences,
|
||
|
...action.payload,
|
||
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
case actionConstants.COMPLETE_ONBOARDING: {
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
completedOnboarding: true,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
case actionConstants.SET_FIRST_TIME_FLOW_TYPE: {
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
6 years ago
|
firstTimeFlowType: action.value,
|
||
4 years ago
|
};
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
case actionConstants.SET_NEXT_NONCE: {
|
||
5 years ago
|
return {
|
||
|
...metamaskState,
|
||
5 years ago
|
nextNonce: action.value,
|
||
4 years ago
|
};
|
||
5 years ago
|
}
|
||
|
|
||
9 years ago
|
default:
|
||
4 years ago
|
return metamaskState;
|
||
9 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export const getCurrentLocale = (state) => state.metamask.currentLocale;
|
||
5 years ago
|
|
||
4 years ago
|
export const getAlertEnabledness = (state) => state.metamask.alertEnabledness;
|
||
5 years ago
|
|
||
4 years ago
|
export const getUnconnectedAccountAlertEnabledness = (state) =>
|
||
4 years ago
|
getAlertEnabledness(state)[ALERT_TYPES.unconnectedAccount];
|
||
5 years ago
|
|
||
4 years ago
|
export const getWeb3ShimUsageAlertEnabledness = (state) =>
|
||
4 years ago
|
getAlertEnabledness(state)[ALERT_TYPES.web3ShimUsage];
|
||
4 years ago
|
|
||
4 years ago
|
export const getUnconnectedAccountAlertShown = (state) =>
|
||
4 years ago
|
state.metamask.unconnectedAccountAlertShownOrigins;
|
||
5 years ago
|
|
||
4 years ago
|
export const getTokens = (state) => state.metamask.tokens;
|