|
| 1 | +import { |
| 2 | + ControllerGetStateAction, |
| 3 | + Messenger, |
| 4 | + RestrictedMessenger, |
| 5 | +} from '@metamask/base-controller'; |
| 6 | + |
| 7 | +import { |
| 8 | + AccountsControllerGetSelectedMultichainAccountAction, |
| 9 | + AccountsControllerListMultichainAccountsAction, |
| 10 | +} from '@metamask/accounts-controller'; |
| 11 | +import { |
| 12 | + AccountTreeControllerGetAccountsFromSelectedAccountGroupAction, |
| 13 | + AccountTreeControllerSelectedAccountGroupChangeEvent, |
| 14 | +} from '@metamask/account-tree-controller'; |
| 15 | +import { |
| 16 | + KeyringControllerSignPersonalMessageAction, |
| 17 | + KeyringControllerUnlockEvent, |
| 18 | +} from '@metamask/keyring-controller'; |
| 19 | +import { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller'; |
| 20 | +import type { HandleSnapRequest } from '@metamask/snaps-controllers'; |
| 21 | +import { PreferencesControllerGetStateAction } from '../../controllers/preferences-controller'; |
| 22 | +import { |
| 23 | + RewardsDataServiceGetOptInStatusAction, |
| 24 | + RewardsDataServiceEstimatePointsAction, |
| 25 | + RewardsDataServiceGetSeasonStatusAction, |
| 26 | + RewardsDataServiceLoginAction, |
| 27 | + RewardsDataServiceMobileJoinAction, |
| 28 | + RewardsDataServiceMobileOptinAction, |
| 29 | + RewardsDataServiceValidateReferralCodeAction, |
| 30 | + RewardsDataServiceFetchGeoLocationAction, |
| 31 | + RewardsDataServiceGetSeasonMetadataAction, |
| 32 | + RewardsDataServiceGetDiscoverSeasonsAction, |
| 33 | +} from '../../controllers/rewards/rewards-data-service-types'; |
| 34 | +import { |
| 35 | + RewardsControllerState, |
| 36 | + Patch, |
| 37 | + RewardsControllerAccountLinkedEvent, |
| 38 | + RewardsControllerOptInAction, |
| 39 | + RewardsControllerGetOptInStatusAction, |
| 40 | + RewardsControllerEstimatePointsAction, |
| 41 | + RewardsControllerIsRewardsFeatureEnabledAction, |
| 42 | + RewardsControllerValidateReferralCodeAction, |
| 43 | + RewardsControllerIsOptInSupportedAction, |
| 44 | + RewardsControllerLinkAccountToSubscriptionAction, |
| 45 | + RewardsControllerLinkAccountsToSubscriptionCandidateAction, |
| 46 | + RewardsControllerGetGeoRewardsMetadataAction, |
| 47 | + RewardsControllerGetCandidateSubscriptionIdAction, |
| 48 | + RewardsControllerGetHasAccountOptedInAction, |
| 49 | + RewardsControllerGetActualSubscriptionIdAction, |
| 50 | + RewardsControllerGetFirstSubscriptionIdAction, |
| 51 | + RewardsControllerGetSeasonMetadataAction, |
| 52 | + RewardsControllerGetSeasonStatusAction, |
| 53 | +} from '../../controllers/rewards/rewards-controller.types'; |
| 54 | + |
| 55 | +const name = 'RewardsController'; |
| 56 | + |
| 57 | +/** |
| 58 | + * Events that can be emitted by the RewardsController |
| 59 | + */ |
| 60 | +export type RewardsControllerEvents = |
| 61 | + | { |
| 62 | + type: 'RewardsController:stateChange'; |
| 63 | + payload: [RewardsControllerState, Patch[]]; |
| 64 | + } |
| 65 | + | RewardsControllerAccountLinkedEvent; |
| 66 | + |
| 67 | +/** |
| 68 | + * Actions that can be performed by the RewardsController |
| 69 | + */ |
| 70 | +export type RewardsControllerActions = |
| 71 | + | ControllerGetStateAction<'RewardsController', RewardsControllerState> |
| 72 | + | RewardsControllerGetOptInStatusAction |
| 73 | + | RewardsControllerEstimatePointsAction |
| 74 | + | RewardsControllerIsRewardsFeatureEnabledAction |
| 75 | + | RewardsControllerOptInAction |
| 76 | + | RewardsControllerGetGeoRewardsMetadataAction |
| 77 | + | RewardsControllerValidateReferralCodeAction |
| 78 | + | RewardsControllerIsOptInSupportedAction |
| 79 | + | RewardsControllerLinkAccountToSubscriptionAction |
| 80 | + | RewardsControllerLinkAccountsToSubscriptionCandidateAction |
| 81 | + | RewardsControllerGetCandidateSubscriptionIdAction |
| 82 | + | RewardsControllerGetHasAccountOptedInAction |
| 83 | + | RewardsControllerGetActualSubscriptionIdAction |
| 84 | + | RewardsControllerGetFirstSubscriptionIdAction |
| 85 | + | RewardsControllerGetSeasonMetadataAction |
| 86 | + | RewardsControllerGetSeasonStatusAction; |
| 87 | + |
| 88 | +// Don't reexport as per guidelines |
| 89 | +type AllowedActions = |
| 90 | + | AccountsControllerGetSelectedMultichainAccountAction |
| 91 | + | AccountsControllerListMultichainAccountsAction |
| 92 | + | KeyringControllerSignPersonalMessageAction |
| 93 | + | RewardsDataServiceLoginAction |
| 94 | + | RewardsDataServiceEstimatePointsAction |
| 95 | + | RewardsDataServiceGetSeasonStatusAction |
| 96 | + | RewardsDataServiceFetchGeoLocationAction |
| 97 | + | RewardsDataServiceMobileOptinAction |
| 98 | + | RewardsDataServiceValidateReferralCodeAction |
| 99 | + | RewardsDataServiceMobileJoinAction |
| 100 | + | RewardsDataServiceGetOptInStatusAction |
| 101 | + | RewardsDataServiceGetSeasonMetadataAction |
| 102 | + | RewardsDataServiceGetDiscoverSeasonsAction |
| 103 | + | AccountTreeControllerGetAccountsFromSelectedAccountGroupAction |
| 104 | + | HandleSnapRequest; |
| 105 | + |
| 106 | +type AllowedEvents = |
| 107 | + | KeyringControllerUnlockEvent |
| 108 | + | AccountTreeControllerSelectedAccountGroupChangeEvent; |
| 109 | + |
| 110 | +export type RewardsControllerMessenger = RestrictedMessenger< |
| 111 | + typeof name, |
| 112 | + RewardsControllerActions | AllowedActions, |
| 113 | + RewardsControllerEvents | AllowedEvents, |
| 114 | + AllowedActions['type'], |
| 115 | + AllowedEvents['type'] |
| 116 | +>; |
| 117 | + |
| 118 | +export function getRewardsControllerMessenger( |
| 119 | + messenger: Messenger< |
| 120 | + RewardsControllerActions | AllowedActions, |
| 121 | + RewardsControllerEvents | AllowedEvents |
| 122 | + >, |
| 123 | +): RewardsControllerMessenger { |
| 124 | + return messenger.getRestricted({ |
| 125 | + name, |
| 126 | + allowedActions: [ |
| 127 | + 'AccountsController:getSelectedMultichainAccount', |
| 128 | + 'AccountTreeController:getAccountsFromSelectedAccountGroup', |
| 129 | + 'AccountsController:listMultichainAccounts', |
| 130 | + 'KeyringController:signPersonalMessage', |
| 131 | + 'RewardsDataService:login', |
| 132 | + 'RewardsDataService:estimatePoints', |
| 133 | + 'RewardsDataService:getSeasonStatus', |
| 134 | + 'RewardsDataService:fetchGeoLocation', |
| 135 | + 'RewardsDataService:mobileOptin', |
| 136 | + 'RewardsDataService:validateReferralCode', |
| 137 | + 'RewardsDataService:mobileJoin', |
| 138 | + 'RewardsDataService:getOptInStatus', |
| 139 | + 'RewardsDataService:getSeasonMetadata', |
| 140 | + 'RewardsDataService:getDiscoverSeasons', |
| 141 | + 'SnapController:handleRequest', |
| 142 | + ], |
| 143 | + allowedEvents: [ |
| 144 | + 'AccountTreeController:selectedAccountGroupChange', |
| 145 | + 'KeyringController:unlock', |
| 146 | + ], |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +type AllowedInitializationActions = |
| 151 | + | RemoteFeatureFlagControllerGetStateAction |
| 152 | + | PreferencesControllerGetStateAction; |
| 153 | + |
| 154 | +export type RewardsControllerInitMessenger = ReturnType< |
| 155 | + typeof getRewardsControllerInitMessenger |
| 156 | +>; |
| 157 | + |
| 158 | +export function getRewardsControllerInitMessenger( |
| 159 | + messenger: Messenger<AllowedInitializationActions, never>, |
| 160 | +) { |
| 161 | + return messenger.getRestricted({ |
| 162 | + name: 'RewardsControllerInit', |
| 163 | + allowedActions: [ |
| 164 | + 'RemoteFeatureFlagController:getState', |
| 165 | + 'PreferencesController:getState', |
| 166 | + ], |
| 167 | + allowedEvents: [], |
| 168 | + }); |
| 169 | +} |
0 commit comments