Skip to content

Commit

Permalink
feat: notifications on cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
vkabc committed Aug 10, 2023
1 parent 6b1e059 commit 25b9d1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
},
"source": {
"shasum": "KL959iQJVvwp0xM1PUNFrSDwgcfA2qdNrhSS/eeA80Q=",
"shasum": "Kb4jQY5ygvWh4OQ+MOltTrvZ5KcoW9o7+fiANG1oiTw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -24,6 +24,16 @@
"dapps": true,
"snaps": false
},
"endowment:cronjob": {
"jobs": [
{
"expression": "* * * * *",
"request": {
"method": "execute"
}
}
]
},
"endowment:transaction-insight": {
"allowTransactionOrigin": true
}
Expand Down
37 changes: 36 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { OnRpcRequestHandler } from '@metamask/snaps-types';
import { panel, text } from '@metamask/snaps-ui';
import { panel, text, heading } from '@metamask/snaps-ui';

Check failure on line 2 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

'text' is defined but never used

Check failure on line 2 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

'heading' is defined but never used

import { rpcErrors } from '@metamask/rpc-errors';

Check failure on line 4 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

'@metamask/rpc-errors' should be listed in the project's dependencies. Run 'npm i -S @metamask/rpc-errors' to add it

Check failure on line 4 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

'rpcErrors' is defined but never used
import type { OnCronjobHandler } from '@metamask/snaps-types';
/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
*
Expand All @@ -26,3 +28,36 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
throw new Error('Method not found.');
}
};

Check failure on line 30 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

Delete `⏎`


/**
* Handle cronjob execution requests from MetaMask. This handler handles one
* method:
*
* - `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.
*
* @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/#oncronjob
*/

Check failure on line 46 in packages/snap/src/index.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, and Test (16.x)

Delete `⏎`


export const onCronjob: OnCronjobHandler = async ({ request }) => {
switch (request.method) {
case 'execute':
return snap.request({
method: 'snap_notify',
params: {
type: 'native',
message: `Hello, world!`,
},
});

default:
throw new Error('Method not found.');
}
};

0 comments on commit 25b9d1c

Please sign in to comment.