Skip to content

Commit f2fa96c

Browse files
lwin-kyawmetamaskbotchaitanyapotti
authored
feat: link rewards to shield subscription (#38489)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> This PR allows rewards opted in users to claim points from shield subscription. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/38489?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: link/claims rewards points from shield subscription ## **Related issues** Fixes: #38038 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [x] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Integrates rewards with Shield subscriptions and shifts crypto-approval post-transaction handling into `SubscriptionService`, adding post-tx cohort assignment, UI hooks, and tests. > > - **Subscription Service**: > - Adds rewards integration: resolves primary `rewardAccountId` (CAIP), links rewards to existing Shield (`linkRewardToExistingSubscription`). > - Handles Shield crypto approval post-tx in `handlePostTransaction` (incl. gas sponsorship check) and tracks events. > - Assigns post-tx Shield cohort via `AppStateController:setPendingShieldCohort` and emits metrics. > - New helpers: primary CAIP account lookup, season/opt-in checks. > - **Controllers/Messaging**: > - `metamask-controller`: delegate `TransactionController:transactionSubmitted` to `subscriptionService.handlePostTransaction`; remove legacy internal approval handler; expose `linkRewardToShieldSubscription`. > - `AppStateController`: add `setPendingShieldCohort` action and handler. > - Subscription service messenger/types: add actions `submitShieldSubscriptionCryptoApproval`, `linkRewards`, rewards getters, and new AppState action. > - **UI**: > - Remove `startSubscriptionWithCrypto` thunk. > - Add `linkRewardToShieldSubscription` thunk. > - **Tests**: > - Unit tests for reward ID inclusion on card flow, post-tx handling, and reward linking conditions. > - **Dependencies/Config**: > - Bump `@metamask/subscription-controller` to `^5.3.1` (and related lockfile updates). > - Privacy: allow `rewards.uat-api.cx.metamask.io`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 5b9bc4e. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: MetaMask Bot <[email protected]> Co-authored-by: Chaitanya Potti <[email protected]>
1 parent 6fdfd8a commit f2fa96c

File tree

10 files changed

+619
-102
lines changed

10 files changed

+619
-102
lines changed

app/scripts/controller-init/messengers/subscription/subscription-service-messenger.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ export function getSubscriptionServiceMessenger(
4040
'SubscriptionController:getBillingPortalUrl',
4141
'SubscriptionController:submitSponsorshipIntents',
4242
'SubscriptionController:getState',
43+
'SubscriptionController:submitShieldSubscriptionCryptoApproval',
44+
'SubscriptionController:linkRewards',
4345
'AppStateController:getState',
46+
'AppStateController:setPendingShieldCohort',
4447
'AuthenticationController:getBearerToken',
4548
'TransactionController:getTransactions',
4649
'AccountsController:getState',
@@ -50,6 +53,10 @@ export function getSubscriptionServiceMessenger(
5053
'SwapsController:getState',
5154
'MetaMetricsController:trackEvent',
5255
'KeyringController:getState',
56+
// Rewards Integration
57+
'RewardsController:getSeasonStatus',
58+
'RewardsController:getSeasonMetadata',
59+
'RewardsController:getHasAccountOptedIn',
5360
],
5461
});
5562
return serviceMessenger;

app/scripts/controllers/app-state-controller.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,20 @@ export type AppStateControllerSetCanTrackWalletFundsObtainedAction = {
192192
handler: AppStateController['setCanTrackWalletFundsObtained'];
193193
};
194194

195+
export type AppStateControllerSetPendingShieldCohortAction = {
196+
type: 'AppStateController:setPendingShieldCohort';
197+
handler: AppStateController['setPendingShieldCohort'];
198+
};
199+
195200
/**
196201
* Actions exposed by the {@link AppStateController}.
197202
*/
198203
export type AppStateControllerActions =
199204
| AppStateControllerGetStateAction
200205
| AppStateControllerGetUnlockPromiseAction
201206
| AppStateControllerRequestQrCodeScanAction
202-
| AppStateControllerSetCanTrackWalletFundsObtainedAction;
207+
| AppStateControllerSetCanTrackWalletFundsObtainedAction
208+
| AppStateControllerSetPendingShieldCohortAction;
203209

204210
/**
205211
* Actions that this controller is allowed to call.
@@ -778,6 +784,11 @@ export class AppStateController extends BaseController<
778784
this.setCanTrackWalletFundsObtained.bind(this),
779785
);
780786

787+
this.messenger.registerActionHandler(
788+
'AppStateController:setPendingShieldCohort',
789+
this.setPendingShieldCohort.bind(this),
790+
);
791+
781792
this.#approvalRequestId = null;
782793
}
783794

app/scripts/metamask-controller.js

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ import {
129129
SecretType,
130130
RecoveryError,
131131
} from '@metamask/seedless-onboarding-controller';
132-
import { COHORT_NAMES, PRODUCT_TYPES } from '@metamask/subscription-controller';
132+
import { PRODUCT_TYPES } from '@metamask/subscription-controller';
133133
import { isSnapId } from '@metamask/snaps-utils';
134134
import {
135135
findAtomicBatchSupportForChain,
@@ -916,11 +916,11 @@ export default class MetamaskController extends EventEmitter {
916916
this.controllerMessenger.subscribe(
917917
'TransactionController:transactionSubmitted',
918918
({ transactionMeta }) => {
919-
this._onShieldSubscriptionApprovalTransaction(transactionMeta).catch(
920-
(err) => {
919+
this.subscriptionService
920+
.handlePostTransaction(transactionMeta)
921+
.catch((err) => {
921922
console.error('Error onShieldSubscriptionApprovalTransaction', err);
922-
},
923-
);
923+
});
924924
},
925925
);
926926

@@ -2618,10 +2618,6 @@ export default class MetamaskController extends EventEmitter {
26182618
this.subscriptionService.updateSubscriptionCardPaymentMethod.bind(
26192619
this.subscriptionService,
26202620
),
2621-
startSubscriptionWithCrypto:
2622-
this.subscriptionController.startSubscriptionWithCrypto.bind(
2623-
this.subscriptionController,
2624-
),
26252621
updateSubscriptionCryptoPaymentMethod:
26262622
this.subscriptionService.updateSubscriptionCryptoPaymentMethod.bind(
26272623
this.subscriptionService,
@@ -2630,6 +2626,10 @@ export default class MetamaskController extends EventEmitter {
26302626
this.subscriptionController.submitUserEvent.bind(
26312627
this.subscriptionController,
26322628
),
2629+
linkRewardToShieldSubscription:
2630+
this.subscriptionService.linkRewardToExistingSubscription.bind(
2631+
this.subscriptionService,
2632+
),
26332633

26342634
// rewards
26352635
getRewardsCandidateSubscriptionId:
@@ -8658,66 +8658,6 @@ export default class MetamaskController extends EventEmitter {
86588658
});
86598659
}
86608660

8661-
/**
8662-
* Handles the shield subscription approval transaction after confirm
8663-
* NOTE: This doesn't subscribe to messenger internally inside controller because we need more info from the client as params
8664-
*
8665-
* @param transactionMeta - The transaction metadata.
8666-
*/
8667-
async _onShieldSubscriptionApprovalTransaction(transactionMeta) {
8668-
const { isGasFeeSponsored, chainId } = transactionMeta;
8669-
const bundlerSupported = await isSendBundleSupported(chainId);
8670-
const isSponsored = isGasFeeSponsored && bundlerSupported;
8671-
8672-
try {
8673-
this.subscriptionService.trackSubscriptionRequestEvent(
8674-
'started',
8675-
transactionMeta,
8676-
{
8677-
has_sufficient_crypto_balance: true,
8678-
},
8679-
);
8680-
8681-
await this.subscriptionController.submitShieldSubscriptionCryptoApproval(
8682-
transactionMeta,
8683-
isSponsored,
8684-
);
8685-
8686-
// Mark send/transfer/swap transactions for Shield post_tx cohort evaluation
8687-
const isPostTxTransaction = [
8688-
TransactionType.simpleSend,
8689-
TransactionType.tokenMethodTransfer,
8690-
TransactionType.swap,
8691-
TransactionType.swapAndSend,
8692-
].includes(transactionMeta.type);
8693-
8694-
const { pendingShieldCohort } = this.appStateController.state;
8695-
if (isPostTxTransaction && !pendingShieldCohort) {
8696-
this.appStateController.setPendingShieldCohort(
8697-
COHORT_NAMES.POST_TX,
8698-
transactionMeta.type,
8699-
);
8700-
}
8701-
this.subscriptionService.trackSubscriptionRequestEvent(
8702-
'completed',
8703-
transactionMeta,
8704-
{
8705-
gas_sponsored: isSponsored,
8706-
},
8707-
);
8708-
} catch (error) {
8709-
log.error('Error on Shield subscription approval transaction', error);
8710-
this.subscriptionService.trackSubscriptionRequestEvent(
8711-
'failed',
8712-
transactionMeta,
8713-
{
8714-
error_message: error.message,
8715-
},
8716-
);
8717-
throw error;
8718-
}
8719-
}
8720-
87218661
/**
87228662
* @deprecated
87238663
* Controllers should subscribe to messenger events internally rather than relying on the client.

0 commit comments

Comments
 (0)