diff --git a/packages/examples/packages/cronjobs/snap.manifest.json b/packages/examples/packages/cronjobs/snap.manifest.json index bc19026b22..e2d37d178a 100644 --- a/packages/examples/packages/cronjobs/snap.manifest.json +++ b/packages/examples/packages/cronjobs/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps.git" }, "source": { - "shasum": "B0eoXOpzU2Cts5d42w11AdvgClmS/une3zXQ9vHnJgU=", + "shasum": "UAyTtrfMx+qx9gS5oJzs9xHSUWHbftqIc0KbKq4JpQ0=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/examples/packages/cronjobs/src/index.ts b/packages/examples/packages/cronjobs/src/index.ts index e22d989580..1aa24ad4d0 100644 --- a/packages/examples/packages/cronjobs/src/index.ts +++ b/packages/examples/packages/cronjobs/src/index.ts @@ -6,7 +6,8 @@ import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk'; import type { CancelNotificationParams, - ScheduleNotificationParams, + ScheduleNotificationParamsWithDate, + ScheduleNotificationParamsWithDuration, } from './types'; /** @@ -56,9 +57,10 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => { /** * Handle incoming JSON-RPC requests from the dapp, sent through the - * `wallet_invokeSnap` method. This handler handles three methods: + * `wallet_invokeSnap` method. This handler handles four methods: * - * - `scheduleNotification`: Schedule a notification in the future. + * - `scheduleNotificationWithDate`: Schedule a notification in the future with the `date` param. + * - `scheduleNotificationWithDuration`: Schedule a notification in the future with the `duration` param. * - `cancelNotification`: Cancel a notification. * - `getBackgroundEvents`: Get the Snap's background events. * @@ -70,11 +72,22 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => { */ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => { switch (request.method) { - case 'scheduleNotification': + case 'scheduleNotificationWithDate': return snap.request({ method: 'snap_scheduleBackgroundEvent', params: { - date: (request.params as ScheduleNotificationParams).date, + date: (request.params as ScheduleNotificationParamsWithDate).date, + request: { + method: 'fireNotification', + }, + }, + }); + case 'scheduleNotificationWithDuration': + return snap.request({ + method: 'snap_scheduleBackgroundEvent', + params: { + duration: (request.params as ScheduleNotificationParamsWithDuration) + .duration, request: { method: 'fireNotification', }, diff --git a/packages/examples/packages/cronjobs/src/types.ts b/packages/examples/packages/cronjobs/src/types.ts index c379c3fe4b..9a9ac7360f 100644 --- a/packages/examples/packages/cronjobs/src/types.ts +++ b/packages/examples/packages/cronjobs/src/types.ts @@ -1,12 +1,21 @@ /** - * The parameters for calling the `scheduleNotification` JSON-RPC method. + * The parameters for calling the `scheduleNotificationWithDate` JSON-RPC method. * * @property date - The ISO 8601 date of when the notification should be scheduled. */ -export type ScheduleNotificationParams = { +export type ScheduleNotificationParamsWithDate = { date: string; }; +/** + * The parameters for calling the `scheduleNotificationWithDuration` JSON-RPC method. + * + * @property duration - The ISO 8601 duration of when the notification should be scheduled. + */ +export type ScheduleNotificationParamsWithDuration = { + duration: string; +}; + /** * The parameters for calling the `cancelNotification` JSON-RPC method. * diff --git a/packages/test-snaps/src/features/snaps/cronjobs/components/ScheduleBackgroundEvent.tsx b/packages/test-snaps/src/features/snaps/cronjobs/components/ScheduleBackgroundEvent.tsx index 8eb11d7c81..8e34bbcb18 100644 --- a/packages/test-snaps/src/features/snaps/cronjobs/components/ScheduleBackgroundEvent.tsx +++ b/packages/test-snaps/src/features/snaps/cronjobs/components/ScheduleBackgroundEvent.tsx @@ -10,40 +10,82 @@ import { CRONJOBS_SNAP_PORT, CRONJOBS_SNAP_ID } from '../constants'; export const ScheduleBackgroundEvent: FunctionComponent = () => { const [date, setDate] = useState(''); + const [duration, setDuration] = useState(''); const [invokeSnap, { isLoading, data, error }] = useInvokeMutation(); - const handleChange = (event: ChangeEvent) => { + const handleDateChange = (event: ChangeEvent) => { setDate(event.target.value); }; - const handleSubmit = (event: FormEvent) => { + const handleDurationChange = (event: ChangeEvent) => { + setDuration(event.target.value); + }; + + const handleSubmitWithDate = (event: FormEvent) => { event.preventDefault(); invokeSnap({ snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT), - method: 'scheduleNotification', + method: 'scheduleNotificationWithDate', params: { date, }, }).catch(logError); }; + const handleSubmitWithDuration = (event: FormEvent) => { + event.preventDefault(); + invokeSnap({ + snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT), + method: 'scheduleNotificationWithDuration', + params: { + duration, + }, + }).catch(logError); + }; + return ( <> -
+ - Date (must be in IS8601 format) + Date (must be in ISO 8601 format) - +
+ +
+ + Duration (must be in ISO 8601 format) + + + +