Skip to content

Commit

Permalink
update example snap
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Dec 11, 2024
1 parent c22ce07 commit f4efd98
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/examples/packages/cronjobs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
}
},
"initialPermissions": {
"endowment:rpc": {
"dapps": true,
"snaps": false
},
"endowment:cronjob": {
"jobs": [
{
Expand Down
50 changes: 49 additions & 1 deletion packages/examples/packages/cronjobs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { OnCronjobHandler } from '@metamask/snaps-sdk';
import type {
OnCronjobHandler,
OnRpcRequestHandler,
} from '@metamask/snaps-sdk';
import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';

/**
Expand Down Expand Up @@ -29,6 +32,51 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
]),
},
});
case 'fireNotification':
return snap.request({
method: 'snap_notify',
params: {
type: 'inApp',
message: 'Hello world!',
},
});
default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
* `wallet_invokeSnap` method. This handler handles two methods:
*
* - `scheduleNotification`: Schedule a notification in the future.
*
* @param params - The request parameters.
* @param params.request - The JSON-RPC request object.
* @returns The JSON-RPC response.
* @see https://docs.metamask.io/snaps/reference/exports/#onrpcrequest
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'scheduleNotification':
return snap.request({
method: 'snap_scheduleBackgroundEvent',
params: {
date: new Date().toISOString(),
request: {
method: 'fireNotification',
},
},
});
case 'cancelNotification':
return snap.request({
method: 'snap_cancelBackgroundEvent',
params: {
id: request.params?.id,
},
});
default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
Expand Down
24 changes: 24 additions & 0 deletions packages/snaps-sdk/src/types/methods/methods.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { Method } from '../../internals';
import type {
CancelBackgroundEventParams,
CancelBackgroundEventResult,
} from './cancel-background-event';
import type {
CreateInterfaceParams,
CreateInterfaceResult,
} from './create-interface';
import type { DialogParams, DialogResult } from './dialog';
import type {
GetBackgroundEventsParams,
GetBackgroundEventsResult,
} from './get-background-events';
import type {
GetBip32EntropyParams,
GetBip32EntropyResult,
Expand Down Expand Up @@ -56,6 +64,10 @@ import type {
ResolveInterfaceParams,
ResolveInterfaceResult,
} from './resolve-interface';
import type {
ScheduleBackgroundEventParams,
ScheduleBackgroundEventResult,
} from './schedule-background-event';
import type {
UpdateInterfaceParams,
UpdateInterfaceResult,
Expand All @@ -80,6 +92,18 @@ export type SnapMethods = {
snap_manageAccounts: [ManageAccountsParams, ManageAccountsResult];
snap_manageState: [ManageStateParams, ManageStateResult];
snap_notify: [NotifyParams, NotifyResult];
snap_scheduleBackgroundEvent: [
ScheduleBackgroundEventParams,
ScheduleBackgroundEventResult,
];
snap_cancelBackgroundEvent: [
CancelBackgroundEventParams,
CancelBackgroundEventResult,
];
snap_getBackgroundEvents: [
GetBackgroundEventsParams,
GetBackgroundEventsResult,
];
snap_createInterface: [CreateInterfaceParams, CreateInterfaceResult];
snap_updateInterface: [UpdateInterfaceParams, UpdateInterfaceResult];
snap_getInterfaceState: [GetInterfaceStateParams, GetInterfaceStateResult];
Expand Down

0 comments on commit f4efd98

Please sign in to comment.