Skip to content

Commit

Permalink
feat: ✨ update notification types and tests (#4818)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
matteoscurati authored Oct 18, 2024
1 parent 1251b86 commit 56efd1d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,6 @@ export function createMockNotificationLidoReadyToBeWithdrawn(): OnChainRawNotifi
export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.AAVE_V3_HEALTH_FACTOR,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -681,8 +679,6 @@ export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotificati
},
id: 'd73df14d-ce73-4f38-bad3-ab028154042b',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042b',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand All @@ -696,8 +692,6 @@ export function createMockNotificationAaveV3HealthFactor(): OnChainRawNotificati
export function createMockNotificationEnsExpiration(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.ENS_EXPIRATION,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -710,8 +704,6 @@ export function createMockNotificationEnsExpiration(): OnChainRawNotification {
},
id: 'f4ef0b7f-5612-537f-9144-0b5c63ae5391',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042c',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand All @@ -725,8 +717,6 @@ export function createMockNotificationEnsExpiration(): OnChainRawNotification {
export function createMockNotificationLidoStakingRewards(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.LIDO_STAKING_REWARDS,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -741,8 +731,6 @@ export function createMockNotificationLidoStakingRewards(): OnChainRawNotificati
},
id: 'd73df14d-ce73-4f38-bad3-ab028154042l',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042l',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand All @@ -756,8 +744,6 @@ export function createMockNotificationLidoStakingRewards(): OnChainRawNotificati
export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.NOTIONAL_LOAN_EXPIRATION,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -775,8 +761,6 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi
},
id: 'd73df14d-ce73-4f38-bad3-ab028154042n',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042n',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand All @@ -790,8 +774,6 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi
export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.ROCKETPOOL_STAKING_REWARDS,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -806,8 +788,6 @@ export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNoti
},
id: 'd73df14d-ce73-4f38-bad3-ab028154042r',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042r',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand All @@ -821,8 +801,6 @@ export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNoti
export function createMockNotificationSparkFiHealthFactor(): OnChainRawNotification {
const mockNotification: OnChainRawNotification = {
type: TRIGGER_TYPES.SPARK_FI_HEALTH_FACTOR,
block_number: 18378208,
block_timestamp: '1697643851',
chain_id: 1,
created_at: '2023-10-18T16:35:03.147606Z',
address: '0x881D40237659C251811CEC9c364ef91dC08D300C',
Expand All @@ -834,8 +812,6 @@ export function createMockNotificationSparkFiHealthFactor(): OnChainRawNotificat
},
id: 'd73df14d-ce73-4f38-bad3-ab028154042s',
trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042s',
tx_hash:
'0xe6d210d2e601ef3dd1075c48e71452cf35f2daae3886911e964e3babad8ac657',
unread: true,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export type Data_NotionalLoanExpiration =
export type Data_SparkFiHealthFactor =
components['schemas']['Data_SparkFiHealthFactor'];

type Notification = components['schemas']['Notification'];
type NotificationDataKinds = NonNullable<Notification['data']>['kind'];
type Notification =
| components['schemas']['WalletNotification']
| components['schemas']['Web3Notification'];
type ConvertToEnum<Kind> = {
[K in TRIGGER_TYPES]: Kind extends `${K}` ? K : never;
}[TRIGGER_TYPES];
Expand All @@ -49,15 +50,23 @@ type ConvertToEnum<Kind> = {
* 2. It ensures that the `data` field is the correct Notification data for this `type`
* - The `Compute` utility merges the intersections (`&`) for a prettier type.
*/
export type OnChainRawNotification = {
type NormalizeNotification<
N extends Notification,
NotificationDataKinds extends string = NonNullable<N['data']>['kind'],
> = {
[K in NotificationDataKinds]: Compute<
Omit<Notification, 'data'> & {
Omit<N, 'data'> & {
type: ConvertToEnum<K>;
data: Extract<Notification['data'], { kind: K }>;
data: Extract<N['data'], { kind: K }>;
}
>;
}[NotificationDataKinds];

export type OnChainRawNotification = Compute<
| NormalizeNotification<components['schemas']['WalletNotification']>
| NormalizeNotification<components['schemas']['Web3Notification']>
>;

export type UnprocessedOnChainRawNotification = Notification;

export type OnChainRawNotificationsWithNetworkFields = Extract<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export type paths = {
[name: string]: unknown;
};
content: {
'application/json': components['schemas']['Notification'][];
'application/json': (
| components['schemas']['WalletNotification']
| components['schemas']['Web3Notification']
)[];
};
};
};
Expand Down Expand Up @@ -372,21 +375,21 @@ export type components = {
/** Format: date-time */
created_at?: string;
};
Notification: {
WalletNotification: {
/** Format: uuid */
id: string;
/** Format: uuid */
trigger_id: string;
/** @example 1 */
chain_id: number;
/** @example 17485840 */
block_number?: number;
block_timestamp?: string;
block_number: number;
block_timestamp: string;
/**
* Format: address
* @example 0x881D40237659C251811CEC9c364ef91dC08D300C
*/
tx_hash?: string;
tx_hash: string;
/** @example false */
unread: boolean;
/** Format: date-time */
Expand All @@ -408,7 +411,22 @@ export type components = {
| components['schemas']['Data_ERC721Sent']
| components['schemas']['Data_ERC721Received']
| components['schemas']['Data_ERC1155Sent']
| components['schemas']['Data_ERC1155Received']
| components['schemas']['Data_ERC1155Received'];
};
Web3Notification: {
/** Format: uuid */
id: string;
/** Format: uuid */
trigger_id: string;
/** @example 1 */
chain_id: number;
/** @example false */
unread: boolean;
/** Format: date-time */
created_at: string;
/** Format: address */
address: string;
data?:
| components['schemas']['Data_AaveV3HealthFactor']
| components['schemas']['Data_EnsExpiration']
| components['schemas']['Data_LidoStakingRewards']
Expand Down Expand Up @@ -685,7 +703,5 @@ export type components = {
headers: never;
pathItems: never;
};

export type $defs = Record<string, never>;

export type operations = Record<string, never>;

0 comments on commit 56efd1d

Please sign in to comment.