@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
59 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBackgroundEventsHandler = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const endowments_1 = require("../endowments/index.cjs");
/**
* Get the scheduled background events for the Snap.
*
* @example
* ```ts
* const events = await snap.request({
* method: 'snap_getBackgroundEvents',
* });
* console.log(events);
* // [
* // {
* // id: 'event-1',
* // scheduledAt: 1672531200000,
* // snapId: 'npm:example-snap',
* // date: 1672531200000,
* // request: {
* // method: 'example_method',
* // params: { example: 'data' },
* // },
* // },
* // ...,
* // ]
* ```
*/
exports.getBackgroundEventsHandler = {
implementation: getGetBackgroundEventsImplementation,
actionNames: ['PermissionController:hasPermission', 'CronjobController:get'],
};
/**
* The `snap_getBackgroundEvents` method implementation.
*
* @param req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback.
* Not used by this function.
* @param end - The `json-rpc-engine` "end" callback.
* @param _hooks - The RPC method hooks. Not used by this function.
* @param messenger - The messenger used to call controller actions.
* @returns An array of background events.
*/
async function getGetBackgroundEventsImplementation(req, res, _next, end, _hooks, messenger) {
const { origin } = req;
if (!messenger.call('PermissionController:hasPermission', origin, endowments_1.SnapEndowments.Cronjob)) {
return end(rpc_errors_1.providerErrors.unauthorized());
}
try {
res.result = messenger.call('CronjobController:get', origin);
}
catch (error) {
return end(error);
}
return end();
}
//# sourceMappingURL=getBackgroundEvents.cjs.map