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

Feat: Add background events #2941

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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": "SquG9JvLanG/gJwBw5H1AZBlsthmv21Ci4Vn+sMemjM=",
"shasum": "XMEM/j4XBZLY1nz8Ow7kFic+ReGDTkBwZrGmAnj5YJk=",
"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": "pCp96i558WHqHIUZyZGUFcxAfOQ0afBHJ59nJB5ma78=",
"shasum": "4Ld6BCuGlD4EQddRn2VHfW+NaDKU4SR54G5IW5bN3D8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
6 changes: 5 additions & 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": "z06MC0KYtySDOdGTKH+afyyu3GWBu4LnKc4FVvfq1Oo=",
"shasum": "+Y2G5jljVTKefA0vWgTQ+k4QLaC4uyLI6aMunBPFbOg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -17,6 +17,10 @@
}
},
"initialPermissions": {
"endowment:rpc": {
"dapps": true,
"snaps": false
},
"endowment:cronjob": {
"jobs": [
{
Expand Down
54 changes: 53 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,55 @@ 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: (request.params as Record<string, string>).date,
request: {
method: 'fireNotification',
},
},
});
case 'cancelNotification':
return snap.request({
method: 'snap_cancelBackgroundEvent',
params: {
id: (request.params as Record<string, string>).id,
},
});
case 'getBackgroundEvents':
return snap.request({
method: 'snap_getBackgroundEvents',
});
default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 92.89,
"functions": 96.71,
"lines": 98,
"statements": 97.71
"branches": 92.98,
"functions": 95.98,
"lines": 97.97,
"statements": 97.63
}
Loading
Loading