Skip to content

Commit f4efd98

Browse files
committed
update example snap
1 parent c22ce07 commit f4efd98

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
}
1818
},
1919
"initialPermissions": {
20+
"endowment:rpc": {
21+
"dapps": true,
22+
"snaps": false
23+
},
2024
"endowment:cronjob": {
2125
"jobs": [
2226
{

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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

47
/**
@@ -29,6 +32,51 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
2932
]),
3033
},
3134
});
35+
case 'fireNotification':
36+
return snap.request({
37+
method: 'snap_notify',
38+
params: {
39+
type: 'inApp',
40+
message: 'Hello world!',
41+
},
42+
});
43+
default:
44+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
45+
throw new MethodNotFoundError({ method: request.method });
46+
}
47+
};
48+
49+
/**
50+
* Handle incoming JSON-RPC requests from the dapp, sent through the
51+
* `wallet_invokeSnap` method. This handler handles two methods:
52+
*
53+
* - `scheduleNotification`: Schedule a notification in the future.
54+
*
55+
* @param params - The request parameters.
56+
* @param params.request - The JSON-RPC request object.
57+
* @returns The JSON-RPC response.
58+
* @see https://docs.metamask.io/snaps/reference/exports/#onrpcrequest
59+
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
60+
*/
61+
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
62+
switch (request.method) {
63+
case 'scheduleNotification':
64+
return snap.request({
65+
method: 'snap_scheduleBackgroundEvent',
66+
params: {
67+
date: new Date().toISOString(),
68+
request: {
69+
method: 'fireNotification',
70+
},
71+
},
72+
});
73+
case 'cancelNotification':
74+
return snap.request({
75+
method: 'snap_cancelBackgroundEvent',
76+
params: {
77+
id: request.params?.id,
78+
},
79+
});
3280
default:
3381
// eslint-disable-next-line @typescript-eslint/no-throw-literal
3482
throw new MethodNotFoundError({ method: request.method });

packages/snaps-sdk/src/types/methods/methods.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import type { Method } from '../../internals';
2+
import type {
3+
CancelBackgroundEventParams,
4+
CancelBackgroundEventResult,
5+
} from './cancel-background-event';
26
import type {
37
CreateInterfaceParams,
48
CreateInterfaceResult,
59
} from './create-interface';
610
import type { DialogParams, DialogResult } from './dialog';
11+
import type {
12+
GetBackgroundEventsParams,
13+
GetBackgroundEventsResult,
14+
} from './get-background-events';
715
import type {
816
GetBip32EntropyParams,
917
GetBip32EntropyResult,
@@ -56,6 +64,10 @@ import type {
5664
ResolveInterfaceParams,
5765
ResolveInterfaceResult,
5866
} from './resolve-interface';
67+
import type {
68+
ScheduleBackgroundEventParams,
69+
ScheduleBackgroundEventResult,
70+
} from './schedule-background-event';
5971
import type {
6072
UpdateInterfaceParams,
6173
UpdateInterfaceResult,
@@ -80,6 +92,18 @@ export type SnapMethods = {
8092
snap_manageAccounts: [ManageAccountsParams, ManageAccountsResult];
8193
snap_manageState: [ManageStateParams, ManageStateResult];
8294
snap_notify: [NotifyParams, NotifyResult];
95+
snap_scheduleBackgroundEvent: [
96+
ScheduleBackgroundEventParams,
97+
ScheduleBackgroundEventResult,
98+
];
99+
snap_cancelBackgroundEvent: [
100+
CancelBackgroundEventParams,
101+
CancelBackgroundEventResult,
102+
];
103+
snap_getBackgroundEvents: [
104+
GetBackgroundEventsParams,
105+
GetBackgroundEventsResult,
106+
];
83107
snap_createInterface: [CreateInterfaceParams, CreateInterfaceResult];
84108
snap_updateInterface: [UpdateInterfaceParams, UpdateInterfaceResult];
85109
snap_getInterfaceState: [GetInterfaceStateParams, GetInterfaceStateResult];

0 commit comments

Comments
 (0)