Skip to content

Commit 7ac333a

Browse files
authored
BREAKING: Added new method to cronjob example snap to test durations (#3016)
Updated the cronjob example snap with a new method to test durations and updated `test-snaps` accordingly. Note: This PR breaks the `scheduleNotification` method in the example snap, it is now `scheduleNotificationWithDate`.
1 parent ba4fd7f commit 7ac333a

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

packages/examples/packages/cronjobs/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": "B0eoXOpzU2Cts5d42w11AdvgClmS/une3zXQ9vHnJgU=",
10+
"shasum": "UAyTtrfMx+qx9gS5oJzs9xHSUWHbftqIc0KbKq4JpQ0=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';
66

77
import type {
88
CancelNotificationParams,
9-
ScheduleNotificationParams,
9+
ScheduleNotificationParamsWithDate,
10+
ScheduleNotificationParamsWithDuration,
1011
} from './types';
1112

1213
/**
@@ -56,9 +57,10 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
5657

5758
/**
5859
* Handle incoming JSON-RPC requests from the dapp, sent through the
59-
* `wallet_invokeSnap` method. This handler handles three methods:
60+
* `wallet_invokeSnap` method. This handler handles four methods:
6061
*
61-
* - `scheduleNotification`: Schedule a notification in the future.
62+
* - `scheduleNotificationWithDate`: Schedule a notification in the future with the `date` param.
63+
* - `scheduleNotificationWithDuration`: Schedule a notification in the future with the `duration` param.
6264
* - `cancelNotification`: Cancel a notification.
6365
* - `getBackgroundEvents`: Get the Snap's background events.
6466
*
@@ -70,11 +72,22 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
7072
*/
7173
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
7274
switch (request.method) {
73-
case 'scheduleNotification':
75+
case 'scheduleNotificationWithDate':
7476
return snap.request({
7577
method: 'snap_scheduleBackgroundEvent',
7678
params: {
77-
date: (request.params as ScheduleNotificationParams).date,
79+
date: (request.params as ScheduleNotificationParamsWithDate).date,
80+
request: {
81+
method: 'fireNotification',
82+
},
83+
},
84+
});
85+
case 'scheduleNotificationWithDuration':
86+
return snap.request({
87+
method: 'snap_scheduleBackgroundEvent',
88+
params: {
89+
duration: (request.params as ScheduleNotificationParamsWithDuration)
90+
.duration,
7891
request: {
7992
method: 'fireNotification',
8093
},

packages/examples/packages/cronjobs/src/types.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
/**
2-
* The parameters for calling the `scheduleNotification` JSON-RPC method.
2+
* The parameters for calling the `scheduleNotificationWithDate` JSON-RPC method.
33
*
44
* @property date - The ISO 8601 date of when the notification should be scheduled.
55
*/
6-
export type ScheduleNotificationParams = {
6+
export type ScheduleNotificationParamsWithDate = {
77
date: string;
88
};
99

10+
/**
11+
* The parameters for calling the `scheduleNotificationWithDuration` JSON-RPC method.
12+
*
13+
* @property duration - The ISO 8601 duration of when the notification should be scheduled.
14+
*/
15+
export type ScheduleNotificationParamsWithDuration = {
16+
duration: string;
17+
};
18+
1019
/**
1120
* The parameters for calling the `cancelNotification` JSON-RPC method.
1221
*

packages/test-snaps/src/features/snaps/cronjobs/components/ScheduleBackgroundEvent.tsx

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,82 @@ import { CRONJOBS_SNAP_PORT, CRONJOBS_SNAP_ID } from '../constants';
1010

1111
export const ScheduleBackgroundEvent: FunctionComponent = () => {
1212
const [date, setDate] = useState('');
13+
const [duration, setDuration] = useState('');
1314
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
1415

15-
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
16+
const handleDateChange = (event: ChangeEvent<HTMLInputElement>) => {
1617
setDate(event.target.value);
1718
};
1819

19-
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
20+
const handleDurationChange = (event: ChangeEvent<HTMLInputElement>) => {
21+
setDuration(event.target.value);
22+
};
23+
24+
const handleSubmitWithDate = (event: FormEvent<HTMLFormElement>) => {
2025
event.preventDefault();
2126
invokeSnap({
2227
snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT),
23-
method: 'scheduleNotification',
28+
method: 'scheduleNotificationWithDate',
2429
params: {
2530
date,
2631
},
2732
}).catch(logError);
2833
};
2934

35+
const handleSubmitWithDuration = (event: FormEvent<HTMLFormElement>) => {
36+
event.preventDefault();
37+
invokeSnap({
38+
snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT),
39+
method: 'scheduleNotificationWithDuration',
40+
params: {
41+
duration,
42+
},
43+
}).catch(logError);
44+
};
45+
3046
return (
3147
<>
32-
<Form onSubmit={handleSubmit} className="mb-3">
48+
<Form onSubmit={handleSubmitWithDate} className="mb-3">
3349
<Form.Group>
34-
<Form.Label>Date (must be in IS8601 format)</Form.Label>
50+
<Form.Label>Date (must be in ISO 8601 format)</Form.Label>
3551
<Form.Control
3652
type="text"
3753
placeholder={new Date().toISOString()}
3854
value={date}
39-
onChange={handleChange}
55+
onChange={handleDateChange}
4056
id="backgroundEventDate"
4157
className="mb-3"
4258
/>
4359
</Form.Group>
4460

45-
<Button type="submit" id="scheduleBackgroundEvent" disabled={isLoading}>
46-
Schedule background event (notification)
61+
<Button
62+
type="submit"
63+
id="scheduleBackgroundEventWithDate"
64+
disabled={isLoading}
65+
>
66+
Schedule background event with date (notification)
67+
</Button>
68+
</Form>
69+
70+
<Form onSubmit={handleSubmitWithDuration} className="mb-3">
71+
<Form.Group>
72+
<Form.Label>Duration (must be in ISO 8601 format)</Form.Label>
73+
<Form.Control
74+
type="text"
75+
placeholder="PT30S"
76+
value={duration}
77+
onChange={handleDurationChange}
78+
id="backgroundEventDuration"
79+
className="mb-3"
80+
/>
81+
</Form.Group>
82+
83+
<Button
84+
type="submit"
85+
id="scheduleBackgroundEventWithDuration"
86+
disabled={isLoading}
87+
>
88+
Schedule background event with duration (notification)
4789
</Button>
4890
</Form>
4991

0 commit comments

Comments
 (0)