Skip to content

Commit 7a32cd6

Browse files
hmalik88PatrykLucka
authored andcommitted
Feat: Add background events (#2941)
This PR adds a new feature: background events, per [SIP-28](https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-28.md). A summary of the changes made: 1. The `CronjobController` was updated to now include logic for handling and storing background events. 2. `snap_scheduleBackgroundEvent` RPC method was added to schedule an event. 3. `snap_cancelBackgroundEvent` RPC method was added to cancel an event. 4. `snap_getBackgroundEvents` RPC method was added to get a snap's background events (not outlined in the SIP, but will be added as an addendum soon).
1 parent cd593b0 commit 7a32cd6

35 files changed

+2353
-62
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "82KbG3cf0wtxooJpWzHeM1g4FhO8O7zSYCAAGNPshfM=",
10+
"shasum": "hy0TMeQeqznNQRX2j7DnxRt1Nn5Z+v0rjaWNpe1fEWE=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "5LsB950haZGnl0q5K7M4XgSh5J2e0p5O1Ptl/e6kpSQ=",
10+
"shasum": "VR3Zwjo0yqKLkuKHGDfS9AmuyW3KMbXSmi9Nh9JgCMw=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/cronjobs/snap.manifest.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "E8SzmLlC0rgCn0qOop9/zCLmrWLABS5LlnaQv/MYkUc=",
10+
"shasum": "6RCKkCSH+tCAKsXIzAjpaZrWjvGDXbkMcuaCslVvwr4=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",
@@ -17,6 +17,10 @@
1717
}
1818
},
1919
"initialPermissions": {
20+
"endowment:rpc": {
21+
"dapps": true,
22+
"snaps": false
23+
},
2024
"endowment:cronjob": {
2125
"jobs": [
2226
{
@@ -27,7 +31,8 @@
2731
}
2832
]
2933
},
30-
"snap_dialog": {}
34+
"snap_dialog": {},
35+
"snap_notify": {}
3136
},
3237
"platformVersion": "6.14.0",
3338
"manifestVersion": "0.1"

packages/examples/packages/cronjobs/src/index.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
import type { OnCronjobHandler } from '@metamask/snaps-sdk';
1+
import type {
2+
OnCronjobHandler,
3+
OnRpcRequestHandler,
4+
} from '@metamask/snaps-sdk';
25
import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';
36

7+
import type {
8+
CancelNotificationParams,
9+
ScheduleNotificationParams,
10+
} from './types';
11+
412
/**
5-
* Handle cronjob execution requests from MetaMask. This handler handles one
6-
* method:
13+
* Handle cronjob execution requests from MetaMask. This handler handles two
14+
* methods:
715
*
816
* - `execute`: The JSON-RPC method that is called by MetaMask when the cronjob
917
* is triggered. This method is specified in the snap manifest under the
1018
* `endowment:cronjob` permission. If you want to support more methods (e.g.,
1119
* with different times), you can add them to the manifest there.
20+
* - `fireNotification`: The JSON-RPC method that is called by MetaMask when the
21+
* background event is triggered. This method call is scheduled by the `scheduleNotification`
22+
* method in the `onRpcRequest` handler.
1223
*
1324
* @param params - The request parameters.
1425
* @param params.request - The JSON-RPC request object.
@@ -29,6 +40,57 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
2940
]),
3041
},
3142
});
43+
case 'fireNotification':
44+
return snap.request({
45+
method: 'snap_notify',
46+
params: {
47+
type: 'inApp',
48+
message: 'Hello world!',
49+
},
50+
});
51+
default:
52+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
53+
throw new MethodNotFoundError({ method: request.method });
54+
}
55+
};
56+
57+
/**
58+
* Handle incoming JSON-RPC requests from the dapp, sent through the
59+
* `wallet_invokeSnap` method. This handler handles three methods:
60+
*
61+
* - `scheduleNotification`: Schedule a notification in the future.
62+
* - `cancelNotification`: Cancel a notification.
63+
* - `getBackgroundEvents`: Get the Snap's background events.
64+
*
65+
* @param params - The request parameters.
66+
* @param params.request - The JSON-RPC request object.
67+
* @returns The JSON-RPC response.
68+
* @see https://docs.metamask.io/snaps/reference/exports/#onrpcrequest
69+
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
70+
*/
71+
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
72+
switch (request.method) {
73+
case 'scheduleNotification':
74+
return snap.request({
75+
method: 'snap_scheduleBackgroundEvent',
76+
params: {
77+
date: (request.params as ScheduleNotificationParams).date,
78+
request: {
79+
method: 'fireNotification',
80+
},
81+
},
82+
});
83+
case 'cancelNotification':
84+
return snap.request({
85+
method: 'snap_cancelBackgroundEvent',
86+
params: {
87+
id: (request.params as CancelNotificationParams).id,
88+
},
89+
});
90+
case 'getBackgroundEvents':
91+
return snap.request({
92+
method: 'snap_getBackgroundEvents',
93+
});
3294
default:
3395
// eslint-disable-next-line @typescript-eslint/no-throw-literal
3496
throw new MethodNotFoundError({ method: request.method });
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* The parameters for calling the `scheduleNotification` JSON-RPC method.
3+
*
4+
* @property date - The ISO 8601 date of when the notification should be scheduled.
5+
*/
6+
export type ScheduleNotificationParams = {
7+
date: string;
8+
};
9+
10+
/**
11+
* The parameters for calling the `cancelNotification` JSON-RPC method.
12+
*
13+
* @property id - The id of the notification event to cancel.
14+
*/
15+
export type CancelNotificationParams = {
16+
id: string;
17+
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 92.96,
3-
"functions": 96.71,
4-
"lines": 98.01,
5-
"statements": 97.71
2+
"branches": 93.06,
3+
"functions": 96.54,
4+
"lines": 98.02,
5+
"statements": 97.74
66
}

packages/snaps-controllers/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"fast-deep-equal": "^3.1.3",
101101
"get-npm-tarball-url": "^2.0.3",
102102
"immer": "^9.0.6",
103+
"luxon": "^3.5.0",
103104
"nanoid": "^3.1.31",
104105
"readable-stream": "^3.6.2",
105106
"readable-web-to-node-stream": "^3.0.2",
@@ -124,6 +125,7 @@
124125
"@types/concat-stream": "^2.0.0",
125126
"@types/gunzip-maybe": "^1.4.0",
126127
"@types/jest": "^27.5.1",
128+
"@types/luxon": "^3",
127129
"@types/mocha": "^10.0.1",
128130
"@types/node": "18.14.2",
129131
"@types/readable-stream": "^4.0.15",

0 commit comments

Comments
 (0)