Skip to content

Commit 56efd1d

Browse files
feat: ✨ update notification types and tests (#4818)
## Explanation This PR introduces a better definition of the notification types described by the `NotificationServiceController`. The new types allow for the description of the new `Web3Notification` while maintaining compatibility with the current `WalletNotification`.​ ## References Related to https://consensyssoftware.atlassian.net/browse/NOTIFY-1230?atlOrigin=eyJpIjoiMzYwOGEwNTFhN2IxNGRiMWEwZGU0N2IyNzI0MTExNzgiLCJwIjoiaiJ9 ## Changelog ### `@metamask/notification-services-controller` - **CHANGED**: Updated the types described in `types/on-chain-notification/schema` and `types/on-chain-notification/on-chain-notification` - **CHANGED**: Updated the mock data according to the new types in `mock-raw-notifications` ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 1251b86 commit 56efd1d

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mock-raw-notifications.ts

-24
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,6 @@ export function createMockNotificationLidoReadyToBeWithdrawn(): OnChainRawNotifi
668668
export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotification {
669669
const mockNotification: OnChainRawNotification = {
670670
type: TRIGGER_TYPES.AAVE_V3_HEALTH_FACTOR,
671-
block_number: 18378208,
672-
block_timestamp: '1697643851',
673671
chain_id: 1,
674672
created_at: '2023-10-18T16:35:03.147606Z',
675673
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -681,8 +679,6 @@ export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotificati
681679
},
682680
id: 'd73df14d-ce73-4f38-bad3-ab028154042b',
683681
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042b',
684-
tx_hash:
685-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
686682
unread: true,
687683
};
688684

@@ -696,8 +692,6 @@ export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotificati
696692
export function createMockNotificationEnsExpiration(): OnChainRawNotification {
697693
const mockNotification: OnChainRawNotification = {
698694
type: TRIGGER_TYPES.ENS_EXPIRATION,
699-
block_number: 18378208,
700-
block_timestamp: '1697643851',
701695
chain_id: 1,
702696
created_at: '2023-10-18T16:35:03.147606Z',
703697
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -710,8 +704,6 @@ export function createMockNotificationEnsExpiration(): OnChainRawNotification {
710704
},
711705
id: 'f4ef0b7f-5612-537f-9144-0b5c63ae5391',
712706
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042c',
713-
tx_hash:
714-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
715707
unread: true,
716708
};
717709

@@ -725,8 +717,6 @@ export function createMockNotificationEnsExpiration(): OnChainRawNotification {
725717
export function createMockNotificationLidoStakingRewards(): OnChainRawNotification {
726718
const mockNotification: OnChainRawNotification = {
727719
type: TRIGGER_TYPES.LIDO_STAKING_REWARDS,
728-
block_number: 18378208,
729-
block_timestamp: '1697643851',
730720
chain_id: 1,
731721
created_at: '2023-10-18T16:35:03.147606Z',
732722
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -741,8 +731,6 @@ export function createMockNotificationLidoStakingRewards(): OnChainRawNotificati
741731
},
742732
id: 'd73df14d-ce73-4f38-bad3-ab028154042l',
743733
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042l',
744-
tx_hash:
745-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
746734
unread: true,
747735
};
748736

@@ -756,8 +744,6 @@ export function createMockNotificationLidoStakingRewards(): OnChainRawNotificati
756744
export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotification {
757745
const mockNotification: OnChainRawNotification = {
758746
type: TRIGGER_TYPES.NOTIONAL_LOAN_EXPIRATION,
759-
block_number: 18378208,
760-
block_timestamp: '1697643851',
761747
chain_id: 1,
762748
created_at: '2023-10-18T16:35:03.147606Z',
763749
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -775,8 +761,6 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi
775761
},
776762
id: 'd73df14d-ce73-4f38-bad3-ab028154042n',
777763
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042n',
778-
tx_hash:
779-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
780764
unread: true,
781765
};
782766

@@ -790,8 +774,6 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi
790774
export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNotification {
791775
const mockNotification: OnChainRawNotification = {
792776
type: TRIGGER_TYPES.ROCKETPOOL_STAKING_REWARDS,
793-
block_number: 18378208,
794-
block_timestamp: '1697643851',
795777
chain_id: 1,
796778
created_at: '2023-10-18T16:35:03.147606Z',
797779
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -806,8 +788,6 @@ export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNoti
806788
},
807789
id: 'd73df14d-ce73-4f38-bad3-ab028154042r',
808790
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042r',
809-
tx_hash:
810-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
811791
unread: true,
812792
};
813793

@@ -821,8 +801,6 @@ export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNoti
821801
export function createMockNotificationSparkFiHealthFactor(): OnChainRawNotification {
822802
const mockNotification: OnChainRawNotification = {
823803
type: TRIGGER_TYPES.SPARK_FI_HEALTH_FACTOR,
824-
block_number: 18378208,
825-
block_timestamp: '1697643851',
826804
chain_id: 1,
827805
created_at: '2023-10-18T16:35:03.147606Z',
828806
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
@@ -834,8 +812,6 @@ export function createMockNotificationSparkFiHealthFactor(): OnChainRawNotificat
834812
},
835813
id: 'd73df14d-ce73-4f38-bad3-ab028154042s',
836814
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042s',
837-
tx_hash:
838-
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
839815
unread: true,
840816
};
841817

packages/notification-services-controller/src/NotificationServicesController/types/on-chain-notification/on-chain-notification.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ export type Data_NotionalLoanExpiration =
3737
export type Data_SparkFiHealthFactor =
3838
components['schemas']['Data_SparkFiHealthFactor'];
3939

40-
type Notification = components['schemas']['Notification'];
41-
type NotificationDataKinds = NonNullable<Notification['data']>['kind'];
40+
type Notification =
41+
| components['schemas']['WalletNotification']
42+
| components['schemas']['Web3Notification'];
4243
type ConvertToEnum<Kind> = {
4344
[K in TRIGGER_TYPES]: Kind extends `${K}` ? K : never;
4445
}[TRIGGER_TYPES];
@@ -49,15 +50,23 @@ type ConvertToEnum<Kind> = {
4950
* 2. It ensures that the `data` field is the correct Notification data for this `type`
5051
* - The `Compute` utility merges the intersections (`&`) for a prettier type.
5152
*/
52-
export type OnChainRawNotification = {
53+
type NormalizeNotification<
54+
N extends Notification,
55+
NotificationDataKinds extends string = NonNullable<N['data']>['kind'],
56+
> = {
5357
[K in NotificationDataKinds]: Compute<
54-
Omit<Notification, 'data'> & {
58+
Omit<N, 'data'> & {
5559
type: ConvertToEnum<K>;
56-
data: Extract<Notification['data'], { kind: K }>;
60+
data: Extract<N['data'], { kind: K }>;
5761
}
5862
>;
5963
}[NotificationDataKinds];
6064

65+
export type OnChainRawNotification = Compute<
66+
| NormalizeNotification<components['schemas']['WalletNotification']>
67+
| NormalizeNotification<components['schemas']['Web3Notification']>
68+
>;
69+
6170
export type UnprocessedOnChainRawNotification = Notification;
6271

6372
export type OnChainRawNotificationsWithNetworkFields = Extract<

packages/notification-services-controller/src/NotificationServicesController/types/on-chain-notification/schema.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export type paths = {
5050
[name: string]: unknown;
5151
};
5252
content: {
53-
'application/json': components['schemas']['Notification'][];
53+
'application/json': (
54+
| components['schemas']['WalletNotification']
55+
| components['schemas']['Web3Notification']
56+
)[];
5457
};
5558
};
5659
};
@@ -372,21 +375,21 @@ export type components = {
372375
/** Format: date-time */
373376
created_at?: string;
374377
};
375-
Notification: {
378+
WalletNotification: {
376379
/** Format: uuid */
377380
id: string;
378381
/** Format: uuid */
379382
trigger_id: string;
380383
/** @example 1 */
381384
chain_id: number;
382385
/** @example 17485840 */
383-
block_number?: number;
384-
block_timestamp?: string;
386+
block_number: number;
387+
block_timestamp: string;
385388
/**
386389
* Format: address
387390
* @example 0x881D40237659C251811CEC9c364ef91dC08D300C
388391
*/
389-
tx_hash?: string;
392+
tx_hash: string;
390393
/** @example false */
391394
unread: boolean;
392395
/** Format: date-time */
@@ -408,7 +411,22 @@ export type components = {
408411
| components['schemas']['Data_ERC721Sent']
409412
| components['schemas']['Data_ERC721Received']
410413
| components['schemas']['Data_ERC1155Sent']
411-
| components['schemas']['Data_ERC1155Received']
414+
| components['schemas']['Data_ERC1155Received'];
415+
};
416+
Web3Notification: {
417+
/** Format: uuid */
418+
id: string;
419+
/** Format: uuid */
420+
trigger_id: string;
421+
/** @example 1 */
422+
chain_id: number;
423+
/** @example false */
424+
unread: boolean;
425+
/** Format: date-time */
426+
created_at: string;
427+
/** Format: address */
428+
address: string;
429+
data?:
412430
| components['schemas']['Data_AaveV3HealthFactor']
413431
| components['schemas']['Data_EnsExpiration']
414432
| components['schemas']['Data_LidoStakingRewards']
@@ -685,7 +703,5 @@ export type components = {
685703
headers: never;
686704
pathItems: never;
687705
};
688-
689706
export type $defs = Record<string, never>;
690-
691707
export type operations = Record<string, never>;

0 commit comments

Comments
 (0)