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

Merged
merged 44 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e3f6fb9
add background event method files
hmalik88 Dec 3, 2024
c7f173d
Merge branch 'main' into hm/background-events
hmalik88 Dec 3, 2024
7442725
add background events feature
hmalik88 Dec 10, 2024
151d171
Merge branch 'main' into hm/background-events
hmalik88 Dec 10, 2024
d52a56c
update coverage and rebuild
hmalik88 Dec 10, 2024
0fe6ee9
fix type
hmalik88 Dec 10, 2024
deea633
fix spacing
hmalik88 Dec 10, 2024
4e41234
fix jsdoc
hmalik88 Dec 10, 2024
621d9f9
remove uuid in favor of nanoid
hmalik88 Dec 11, 2024
bceaa05
fix jsdocs
hmalik88 Dec 11, 2024
c22ce07
address PR comments
hmalik88 Dec 11, 2024
f4efd98
update example snap
hmalik88 Dec 11, 2024
48a543d
fix type and coverage
hmalik88 Dec 11, 2024
409b4aa
rebuild snap
hmalik88 Dec 11, 2024
663f4fb
update test-snaps
hmalik88 Dec 11, 2024
655d7b5
address PR comments
hmalik88 Dec 12, 2024
b1f07cd
Merge branch 'main' into hm/background-events
hmalik88 Dec 12, 2024
52cb0cf
rebuild
hmalik88 Dec 12, 2024
f49240a
update coverage
hmalik88 Dec 12, 2024
598eff6
update coverage again
hmalik88 Dec 12, 2024
e6cb6aa
update snaps controllers coverage
hmalik88 Dec 12, 2024
0e85a3d
fix logic
hmalik88 Dec 13, 2024
d9f44ce
fix test
hmalik88 Dec 13, 2024
c21e216
fix tests
hmalik88 Dec 13, 2024
757b5a2
remove unncecessary option
hmalik88 Dec 13, 2024
528d3f0
address PR comments
hmalik88 Dec 13, 2024
bfbaeb7
fix type
hmalik88 Dec 13, 2024
8bccf3a
update snap controllers coverage
hmalik88 Dec 13, 2024
7a54460
address PR comments
hmalik88 Dec 16, 2024
35cf3b5
fix tests, rebuild and update coverage
hmalik88 Dec 16, 2024
4702f8d
update validation logic
hmalik88 Dec 16, 2024
600902e
rebuild and update coverage
hmalik88 Dec 16, 2024
d33eca2
increase coverage
hmalik88 Dec 16, 2024
feb12e9
address PR comments
hmalik88 Dec 17, 2024
f248a01
Merge branch 'main' into hm/background-events
hmalik88 Dec 17, 2024
63c24e9
fix type issue, rebuild
hmalik88 Dec 17, 2024
68214c6
update snap controllers coverage
hmalik88 Dec 17, 2024
e68ce95
update allowed actions in controller-utils and document example snap …
hmalik88 Dec 17, 2024
385668c
add assert and remove cast
hmalik88 Dec 17, 2024
1554287
address nits
hmalik88 Dec 19, 2024
b92eb30
Merge branch 'main' into hm/background-events
hmalik88 Dec 19, 2024
c35611c
increase coverage
hmalik88 Dec 19, 2024
8543576
add assert
hmalik88 Dec 19, 2024
036e154
rebuild
hmalik88 Dec 19, 2024
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
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": "REliam7FRJGwKCI4NaC2G3mBSD5iYR7DCEhrXLcBDqA=",
"shasum": "9v14rgSPwHyW1Wxg94LVBTC3pya/0MNBiQiULxcjdAI=",
"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": "NItYOhAaWlS9q2r59/zlpoyVUyxojfsc5xMh65mLIwQ=",
"shasum": "w+jasFs5zm0dc8qZhW0rM+xOWJC4Y8kBsGj89kSEQhM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
9 changes: 7 additions & 2 deletions 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": "E8SzmLlC0rgCn0qOop9/zCLmrWLABS5LlnaQv/MYkUc=",
"shasum": "6RCKkCSH+tCAKsXIzAjpaZrWjvGDXbkMcuaCslVvwr4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -17,6 +17,10 @@
}
},
"initialPermissions": {
"endowment:rpc": {
"dapps": true,
"snaps": false
},
"endowment:cronjob": {
"jobs": [
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -27,7 +31,8 @@
}
]
},
"snap_dialog": {}
"snap_dialog": {},
"snap_notify": {}
},
"platformVersion": "6.14.0",
"manifestVersion": "0.1"
Expand Down
68 changes: 65 additions & 3 deletions packages/examples/packages/cronjobs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import type { OnCronjobHandler } from '@metamask/snaps-sdk';
import type {
OnCronjobHandler,
OnRpcRequestHandler,
} from '@metamask/snaps-sdk';
import { panel, text, heading, MethodNotFoundError } from '@metamask/snaps-sdk';

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

/**
* Handle cronjob execution requests from MetaMask. This handler handles one
* method:
* Handle cronjob execution requests from MetaMask. This handler handles two
* methods:
*
* - `execute`: The JSON-RPC method that is called by MetaMask when the cronjob
* is triggered. This method is specified in the snap manifest under the
* `endowment:cronjob` permission. If you want to support more methods (e.g.,
* with different times), you can add them to the manifest there.
* - `fireNotification`: The JSON-RPC method that is called by MetaMask when the
* background event is triggered. This method call is scheduled by the `scheduleNotification`
* method in the `onRpcRequest` handler.
*
* @param params - The request parameters.
* @param params.request - The JSON-RPC request object.
Expand All @@ -29,6 +40,57 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
]),
},
});
case 'fireNotification':
return snap.request({
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
method: 'snap_notify',
params: {
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
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 three methods:
hmalik88 marked this conversation as resolved.
Show resolved Hide resolved
*
* - `scheduleNotification`: Schedule a notification in the future.
* - `cancelNotification`: Cancel a notification.
* - `getBackgroundEvents`: Get the Snap's background events.
*
* @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 ScheduleNotificationParams).date,
request: {
method: 'fireNotification',
},
},
});
case 'cancelNotification':
return snap.request({
method: 'snap_cancelBackgroundEvent',
params: {
id: (request.params as CancelNotificationParams).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
7 changes: 7 additions & 0 deletions packages/examples/packages/cronjobs/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type ScheduleNotificationParams = {
date: string;
};

export type CancelNotificationParams = {
id: string;
};
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.96,
"functions": 96.71,
"lines": 98.01,
"statements": 97.71
"branches": 93.06,
"functions": 96.54,
"lines": 98.02,
"statements": 97.74
}
2 changes: 2 additions & 0 deletions packages/snaps-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"fast-deep-equal": "^3.1.3",
"get-npm-tarball-url": "^2.0.3",
"immer": "^9.0.6",
"luxon": "^3.5.0",
"nanoid": "^3.1.31",
"readable-stream": "^3.6.2",
"readable-web-to-node-stream": "^3.0.2",
Expand All @@ -124,6 +125,7 @@
"@types/concat-stream": "^2.0.0",
"@types/gunzip-maybe": "^1.4.0",
"@types/jest": "^27.5.1",
"@types/luxon": "^3",
"@types/mocha": "^10.0.1",
"@types/node": "18.14.2",
"@types/readable-stream": "^4.0.15",
Expand Down
Loading
Loading