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

212 lines
6.1 KiB
TypeScript
Raw Normal View History

import log from 'loglevel';
import { ThunkAction } from 'redux-thunk';
import { AnyAction } from 'redux';
import {
forceUpdateMetamaskState,
displayWarning,
hideLoadingIndication,
showLoadingIndication,
} from '../actions';
import {
callBackgroundMethod,
submitRequestToBackground,
} from '../action-queue';
import { MetaMaskReduxState } from '../store';
import { isErrorWithMessage } from '../../../shared/modules/error';
export function showInteractiveReplacementTokenBanner(
url: string,
oldRefreshToken: string,
) {
return () => {
callBackgroundMethod(
'showInteractiveReplacementTokenBanner',
[url, oldRefreshToken],
(err) => {
if (isErrorWithMessage(err)) {
throw new Error(err.message);
}
},
);
};
}
/**
* A factory that contains all MMI actions ready to use
* Example usage:
* const mmiActions = mmiActionsFactory();
* mmiActions.connectCustodyAddresses(...)
*/
export function mmiActionsFactory() {
function createAsyncAction(
name: string,
params: any,
useForceUpdateMetamaskState?: any,
loadingText?: string,
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
log.debug(`background.${name}`);
return async (dispatch) => {
if (loadingText) {
dispatch(showLoadingIndication(loadingText));
}
let result;
try {
result = await submitRequestToBackground(name, [...params]);
} catch (error) {
dispatch(displayWarning(error));
if (isErrorWithMessage(error)) {
throw new Error(error.message);
} else {
throw error;
}
}
if (loadingText) {
dispatch(hideLoadingIndication());
}
if (useForceUpdateMetamaskState) {
await forceUpdateMetamaskState(dispatch);
}
return result;
};
}
function createAction(name: string, payload: any) {
return () => {
callBackgroundMethod(name, [payload], (err) => {
if (isErrorWithMessage(err)) {
throw new Error(err.message);
}
});
};
}
return {
connectCustodyAddresses: (
custodianType: string,
custodianName: string,
newAccounts: string[],
) =>
createAsyncAction(
'connectCustodyAddresses',
[custodianType, custodianName, newAccounts],
forceUpdateMetamaskState,
'Looking for your custodian account...',
),
getCustodianAccounts: (
token: string,
apiUrl: string,
custody: string,
getNonImportedAccounts: boolean,
) =>
createAsyncAction(
'getCustodianAccounts',
[token, apiUrl, custody, getNonImportedAccounts],
forceUpdateMetamaskState,
'Getting custodian accounts...',
),
getCustodianAccountsByAddress: (
jwt: string,
apiUrl: string,
address: string,
custody: string,
) =>
createAsyncAction(
'getCustodianAccountsByAddress',
[jwt, apiUrl, address, custody],
forceUpdateMetamaskState,
'Getting custodian accounts...',
),
getCustodianTransactionDeepLink: (address: string, txId: string) =>
createAsyncAction(
'getCustodianTransactionDeepLink',
[address, txId],
forceUpdateMetamaskState,
),
getCustodianConfirmDeepLink: (txId: string) =>
createAsyncAction(
'getCustodianConfirmDeepLink',
[txId],
forceUpdateMetamaskState,
),
getCustodianSignMessageDeepLink: (from: string, custodyTxId: string) =>
createAsyncAction(
'getCustodianSignMessageDeepLink',
[from, custodyTxId],
forceUpdateMetamaskState,
),
getCustodianToken: (custody: string) =>
createAsyncAction(
'getCustodianToken',
[custody],
forceUpdateMetamaskState,
),
getCustodianJWTList: (custody: string) =>
createAsyncAction(
'getCustodianJWTList',
[custody],
forceUpdateMetamaskState,
),
setWaitForConfirmDeepLinkDialog: (waitForConfirmDeepLinkDialog: boolean) =>
createAction(
'setWaitForConfirmDeepLinkDialog',
waitForConfirmDeepLinkDialog,
),
setComplianceAuthData: (clientId: string, projectId: string) =>
createAsyncAction('setComplianceAuthData', [{ clientId, projectId }]),
deleteComplianceAuthData: () =>
createAsyncAction('deleteComplianceAuthData', []),
generateComplianceReport: (address: string) =>
createAction('generateComplianceReport', address),
getComplianceHistoricalReportsByAddress: (
address: string,
projectId: string,
) =>
createAsyncAction('getComplianceHistoricalReportsByAddress', [
address,
projectId,
]),
syncReportsInProgress: (address: string, historicalReports: []) =>
createAction('syncReportsInProgress', { address, historicalReports }),
removeConnectInstitutionalFeature: (origin: string, projectId: string) =>
createAction('removeConnectInstitutionalFeature', { origin, projectId }),
removeAddTokenConnectRequest: (
origin: string,
apiUrl: string,
token: string,
) =>
createAction('removeAddTokenConnectRequest', { origin, apiUrl, token }),
setCustodianConnectRequest: (
token: string,
apiUrl: string,
custodianType: string,
custodianName: string,
) =>
createAsyncAction('setCustodianConnectRequest', [
{ token, apiUrl, custodianType, custodianName },
]),
getCustodianConnectRequest: () =>
createAsyncAction('getCustodianConnectRequest', []),
getMmiConfiguration: () => createAsyncAction('getMmiConfiguration', []),
getAllCustodianAccountsWithToken: (custodyType: string, token: string) =>
createAsyncAction('getAllCustodianAccountsWithToken', [
custodyType,
token,
]),
setCustodianNewRefreshToken: (
address: string,
oldAuthDetails: string,
oldApiUrl: string,
newAuthDetails: string,
newApiUrl: string,
) =>
createAsyncAction('setCustodianNewRefreshToken', [
address,
oldAuthDetails,
oldApiUrl,
newAuthDetails,
newApiUrl,
]),
};
}