@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
63 lines • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrencyRateHandler = void 0;
const rpc_errors_1 = require("@metamask/rpc-errors");
const snaps_utils_1 = require("@metamask/snaps-utils");
const superstruct_1 = require("@metamask/superstruct");
const hookNames = {
getCurrencyRate: true,
};
exports.getCurrencyRateHandler = {
methodNames: ['snap_getCurrencyRate'],
implementation: getGetCurrencyRateImplementation,
hookNames,
};
const GetCurrencyRateParametersStruct = (0, superstruct_1.object)({
currency: (0, superstruct_1.union)([(0, snaps_utils_1.currency)('btc')]),
});
/**
* The `snap_getCurrencyRate` 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.
* @param hooks.getCurrencyRate - The function to get the rate.
* @returns Nothing.
*/
function getGetCurrencyRateImplementation(req, res, _next, end, { getCurrencyRate }) {
const { params } = req;
try {
const validatedParams = getValidatedParams(params);
const { currency: selectedCurrency } = validatedParams;
res.result = getCurrencyRate(selectedCurrency) ?? null;
}
catch (error) {
return end(error);
}
return end();
}
/**
* Validate the getCurrencyRate method `params` and returns them cast to the correct
* type. Throws if validation fails.
*
* @param params - The unvalidated params object from the method request.
* @returns The validated getCurrencyRate method parameter object.
*/
function getValidatedParams(params) {
try {
return (0, superstruct_1.create)(params, GetCurrencyRateParametersStruct);
}
catch (error) {
if (error instanceof superstruct_1.StructError) {
throw rpc_errors_1.rpcErrors.invalidParams({
message: `Invalid params: ${error.message}.`,
});
}
/* istanbul ignore next */
throw rpc_errors_1.rpcErrors.internal();
}
}
//# sourceMappingURL=getCurrencyRate.cjs.map