Skip to content

Commit

Permalink
add a way to test durations
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Jan 14, 2025
1 parent 030ebee commit 042dde9
Show file tree
Hide file tree
Showing 28 changed files with 103 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/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": "ojW3Hm/UhUzUs35jVfoI9+xEsy0zoVLFzfZGZ3dgZeQ=",
"shasum": "IqWUM7iSg+nrJSYikr/aC3q3bSm6WlTcLQ45OTVkG9A=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/bip44/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": "kYRZmgdWbCe5U9mK1DBn4r1eh2VFI6TBolStHsYnwqQ=",
"shasum": "sOEC09390j3Swk29rbbSUC/xJdXcY+4J+yjWR027D1g=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "jyM2N8Y8sIxLacv1mwtPh41wGSt9lWsBNbFo9CSx7mM=",
"shasum": "0RVNrUD4z1+EaGY+/pYJff0D1QY8ZekIHzlppceGAz4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/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": "qHCkTRpYoHUFtt9Dd0nxQyw7Eblv0e5znQ39NszlJOo=",
"shasum": "Aw+w2Wiy54F9iBymGscjK8GSV9Km3eXEA8l85pt+/10=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "arWIxnocunv/5esPgYG068vzZzyBxg/FcbiDILeyLCs=",
"shasum": "Yfok0ZJsaOLPeYw/d7rr2A+q2YSTnFsH/lyuQkPOy4k=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "sPjBPTcb0g1o/xhWxWybs0NghAGflXxrv7JB7lRUxOc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
21 changes: 17 additions & 4 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 @@ -58,7 +59,8 @@ 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:
*
* - `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':
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
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/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": "6IGgmJV+Gc1jdmr4M+Gc0SHBfu2Ieuq3QFCIa5t7vYY=",
"shasum": "b/d4wvQNBpg1SFV4moeS0/J2wr+fIa4j9yHJtS1rAgs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "1QLc9RhVgpwxBu8FjPo5kZY61mpNoNAfkiAatq99Nc4=",
"shasum": "XalYVJppi0F1jj/Bsb+vcxFJnXNSXmYVDDFksjUbtlQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/ethers-js/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": "U4iQEpD4FcKBClMoFgESj8aVZy/io4bR3T3xC8D1J/c=",
"shasum": "MhoKeOSIHCiRkPw8y9I9qT2y4kJPkyF6pQnWcjiGMFc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/file-upload/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": "U5/rIXgTqaoXVudqfJyK1cglKFXyLKKal+hj4mk+i0g=",
"shasum": "Z7FH9MPjp8UnfSOppsONs9RRy8WODxWlJaVYpjsQljo=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-entropy/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": "vFbFG/f0wOk+xcAypUssW9LJST38Ul9Oq4fZRaHq50E=",
"shasum": "S7AUuVRrm0gfmrSvMKUw3kSYRn32L01jnoe7BfWe5sk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-file/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": "IkhZaFe3JhsDGcjO+/2CettMLck0yQUby8I13U2BUpU=",
"shasum": "gmw15KG0gq0X558gXzsTsW4VaCEHZ6sTPFNDZBQkOuE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/images/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": "qDnkVe1UEihxCX8jKtc5kU6Xsz2mszjC9abLo9gxyqQ=",
"shasum": "0Jazk7R5pgHAr/Bkjtyou9hKbU6on830QoPd/xUNTjE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "7hLctw66Qa64WB7Y1kdJnqQFIkPt5hYvdNj9l/00E78=",
"shasum": "s6tsVBfN4/TtsT7JbS1UmEWVPB7PI8NQIypShnd/QtQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "G6qDbbY6gfcNXbwavYeLOcX5dZRXGrzR4OVtIMWizwg=",
"shasum": "O/2zzk/KOs7TsWahCpCrvEFchWxZOoBJD/z15XIcwZ0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "UMY4+tvOkzhxFhBE7v1hIpSANISaTCM6BcjwniuxKUc=",
"shasum": "Lvc41Dl6s6WrUAFPzW+1zXPetJHl/VdjcbZUWz3a8yw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/json-rpc/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": "4i/96yKvEMlwjGd6BvcuYwwGtol1YZEZTZ2dOcGOtxs=",
"shasum": "iU677cU5HW7d/k/j7RpkHvICgZTQlxCEc/J9BSkJ/x4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/localization/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": "hE4IbH9CWs7QS8JnUWsReyG3TuZG9Z00/K2kCRsfID4=",
"shasum": "7rEm1NXOLRCnNpu7/C9YPb6W6vAi66y/VURliUF/0ek=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/manage-state/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": "9n3dGpYT/s9CwkwfGTkOgMA8jvrkLg9KVP2Y/KsrWkg=",
"shasum": "pO3d88b+ICS9lzGljWC2fI55J+3sOTqEQ6E4KfsSftQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "FLqA/O/vfM7Cbv07OMjLbf5wnJ6xiDVOicRc62DN6Dw=",
"shasum": "MtSGuVR6dxqOOlibd2vC6TWdgi5uh0hOjzVEFOcs/18=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "vgZf2fdTM9fTAFroi78267SHtaEeTJLTNa/hobO22s0=",
"shasum": "UqWI0OlG+BjAKDDtNXzx7IAUyTi25mKTgC19dBOz0ZU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/preinstalled/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": "i8jxIqiwe7OvwEP32oFIFKUu3EfnHnbFbyRXeqIKPiY=",
"shasum": "8LNrrjcUnqrNG4/+lFQJ17BXbVp0R9trWL1TcIa9hXE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "9kEcCvm7AAgm49TqYDjUdiqLlnNqjfXipUgtI2ndBwQ=",
"shasum": "z2muYIw+KbJS1DymfW/DLtE59zV01n6EvQT9WQIogdI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/wasm/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": "ZLNLyIw7vPg+geDBqmngSU6+92UjZPhG6on34Ij3+Nc=",
"shasum": "bXYFmE2ORLhf/s+DDVK9C8kA0Tbo9dOTtdZSF1u1vQQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
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": "l5ZrPChhTClz9FfuqCVCk95v7zwDzchZpWmHs/4Iji0=",
"shasum": "P3KkTe1VsmixCjeOIcxnqkQGN2WmqAe69e6Oy1IFqhc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
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

0 comments on commit 042dde9

Please sign in to comment.