Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Added new method to cronjob example snap to test durations #3016

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/examples/packages/cronjobs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "B0eoXOpzU2Cts5d42w11AdvgClmS/une3zXQ9vHnJgU=",
"shasum": "UAyTtrfMx+qx9gS5oJzs9xHSUWHbftqIc0KbKq4JpQ0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
23 changes: 18 additions & 5 deletions packages/examples/packages/cronjobs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';

import type {
CancelNotificationParams,
ScheduleNotificationParams,
ScheduleNotificationParamsWithDate,
ScheduleNotificationParamsWithDuration,
} from './types';

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -70,11 +72,22 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'scheduleNotification':
case 'scheduleNotificationWithDate':
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
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',
},
Expand Down
13 changes: 11 additions & 2 deletions packages/examples/packages/cronjobs/src/types.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLInputElement>) => {
const handleDateChange = (event: ChangeEvent<HTMLInputElement>) => {
setDate(event.target.value);
};

const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
const handleDurationChange = (event: ChangeEvent<HTMLInputElement>) => {
setDuration(event.target.value);
};

const handleSubmitWithDate = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
invokeSnap({
snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT),
method: 'scheduleNotification',
method: 'scheduleNotificationWithDate',
params: {
date,
},
}).catch(logError);
};

const handleSubmitWithDuration = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
invokeSnap({
snapId: getSnapId(CRONJOBS_SNAP_ID, CRONJOBS_SNAP_PORT),
method: 'scheduleNotificationWithDuration',
params: {
duration,
},
}).catch(logError);
};

return (
<>
<Form onSubmit={handleSubmit} className="mb-3">
<Form onSubmit={handleSubmitWithDate} className="mb-3">
<Form.Group>
<Form.Label>Date (must be in IS8601 format)</Form.Label>
<Form.Label>Date (must be in ISO 8601 format)</Form.Label>
<Form.Control
type="text"
placeholder={new Date().toISOString()}
value={date}
onChange={handleChange}
onChange={handleDateChange}
id="backgroundEventDate"
className="mb-3"
/>
</Form.Group>

<Button type="submit" id="scheduleBackgroundEvent" disabled={isLoading}>
Schedule background event (notification)
<Button
type="submit"
id="scheduleBackgroundEventWithDate"
disabled={isLoading}
>
Schedule background event with date (notification)
</Button>
</Form>

<Form onSubmit={handleSubmitWithDuration} className="mb-3">
<Form.Group>
<Form.Label>Duration (must be in ISO 8601 format)</Form.Label>
<Form.Control
type="text"
placeholder="PT30S"
value={duration}
onChange={handleDurationChange}
id="backgroundEventDuration"
className="mb-3"
/>
</Form.Group>

<Button
type="submit"
id="scheduleBackgroundEventWithDuration"
disabled={isLoading}
>
Schedule background event with duration (notification)
</Button>
</Form>

Expand Down
Loading