From c59bee068e9f5926acf1574407bdf5235c37a2cd Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 25 Sep 2024 19:14:05 +0200 Subject: [PATCH] rename to `snap_getCurrencyRate` --- ...etRate.test.ts => getCurrencyRate.test.ts} | 62 +++++++++---------- .../{getRate.ts => getCurrencyRate.ts} | 62 +++++++++---------- .../src/permitted/handlers.ts | 4 +- .../snaps-rpc-methods/src/permitted/index.ts | 4 +- .../src/types/methods/get-currency-rate.ts | 29 +++++++++ .../snaps-sdk/src/types/methods/get-rate.ts | 29 --------- packages/snaps-sdk/src/types/methods/index.ts | 2 +- 7 files changed, 96 insertions(+), 96 deletions(-) rename packages/snaps-rpc-methods/src/permitted/{getRate.test.ts => getCurrencyRate.test.ts} (59%) rename packages/snaps-rpc-methods/src/permitted/{getRate.ts => getCurrencyRate.ts} (53%) create mode 100644 packages/snaps-sdk/src/types/methods/get-currency-rate.ts delete mode 100644 packages/snaps-sdk/src/types/methods/get-rate.ts diff --git a/packages/snaps-rpc-methods/src/permitted/getRate.test.ts b/packages/snaps-rpc-methods/src/permitted/getCurrencyRate.test.ts similarity index 59% rename from packages/snaps-rpc-methods/src/permitted/getRate.test.ts rename to packages/snaps-rpc-methods/src/permitted/getCurrencyRate.test.ts index 0e42725173..c15cdbd593 100644 --- a/packages/snaps-rpc-methods/src/permitted/getRate.test.ts +++ b/packages/snaps-rpc-methods/src/permitted/getCurrencyRate.test.ts @@ -1,43 +1,43 @@ import { JsonRpcEngine } from '@metamask/json-rpc-engine'; -import { type GetRateResult } from '@metamask/snaps-sdk'; +import { type GetCurrencyRateResult } from '@metamask/snaps-sdk'; import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; -import type { GetRateParameters } from './getRate'; -import { getRateHandler } from './getRate'; +import type { GetCurrencyRateParameters } from './getCurrencyRate'; +import { getCurrencyRateHandler } from './getCurrencyRate'; -describe('snap_getRate', () => { - describe('getRateHandler', () => { +describe('snap_getCurrencyRate', () => { + describe('getCurrencyRateHandler', () => { it('has the expected shape', () => { - expect(getRateHandler).toMatchObject({ - methodNames: ['snap_getRate'], + expect(getCurrencyRateHandler).toMatchObject({ + methodNames: ['snap_getCurrencyRate'], implementation: expect.any(Function), hookNames: { - getRate: true, + getCurrencyRate: true, }, }); }); }); describe('implementation', () => { - it('returns the result from the `getRate` hook', async () => { - const { implementation } = getRateHandler; + it('returns the result from the `getCurrencyRate` hook', async () => { + const { implementation } = getCurrencyRateHandler; - const getRate = jest.fn().mockReturnValue({ + const getCurrencyRate = jest.fn().mockReturnValue({ conversionRate: '1', conversionDate: 1, usdConversionRate: '1', }); const hooks = { - getRate, + getCurrencyRate, }; const engine = new JsonRpcEngine(); engine.push((request, response, next, end) => { const result = implementation( - request as JsonRpcRequest, - response as PendingJsonRpcResponse, + request as JsonRpcRequest, + response as PendingJsonRpcResponse, next, end, hooks, @@ -49,9 +49,9 @@ describe('snap_getRate', () => { const response = await engine.handle({ jsonrpc: '2.0', id: 1, - method: 'snap_getRate', + method: 'snap_getCurrencyRate', params: { - cryptocurrency: 'btc', + currency: 'btc', }, }); @@ -67,20 +67,20 @@ describe('snap_getRate', () => { }); it('returns null if there is no rate available', async () => { - const { implementation } = getRateHandler; + const { implementation } = getCurrencyRateHandler; - const getRate = jest.fn().mockReturnValue(undefined); + const getCurrencyRate = jest.fn().mockReturnValue(undefined); const hooks = { - getRate, + getCurrencyRate, }; const engine = new JsonRpcEngine(); engine.push((request, response, next, end) => { const result = implementation( - request as JsonRpcRequest, - response as PendingJsonRpcResponse, + request as JsonRpcRequest, + response as PendingJsonRpcResponse, next, end, hooks, @@ -92,9 +92,9 @@ describe('snap_getRate', () => { const response = await engine.handle({ jsonrpc: '2.0', id: 1, - method: 'snap_getRate', + method: 'snap_getCurrencyRate', params: { - cryptocurrency: 'btc', + currency: 'btc', }, }); @@ -106,24 +106,24 @@ describe('snap_getRate', () => { }); it('throws on invalid params', async () => { - const { implementation } = getRateHandler; + const { implementation } = getCurrencyRateHandler; - const getRate = jest.fn().mockReturnValue({ + const getCurrencyRate = jest.fn().mockReturnValue({ conversionRate: '1', conversionDate: 1, usdConversionRate: '1', }); const hooks = { - getRate, + getCurrencyRate, }; const engine = new JsonRpcEngine(); engine.push((request, response, next, end) => { const result = implementation( - request as JsonRpcRequest, - response as PendingJsonRpcResponse, + request as JsonRpcRequest, + response as PendingJsonRpcResponse, next, end, hooks, @@ -135,9 +135,9 @@ describe('snap_getRate', () => { const response = await engine.handle({ jsonrpc: '2.0', id: 1, - method: 'snap_getRate', + method: 'snap_getCurrencyRate', params: { - cryptocurrency: 'eth', + currency: 'eth', }, }); @@ -145,7 +145,7 @@ describe('snap_getRate', () => { error: { code: -32602, message: - 'Invalid params: At path: cryptocurrency -- Expected the value to satisfy a union of `literal`, but received: "eth".', + 'Invalid params: At path: currency -- Expected the value to satisfy a union of `literal`, but received: "eth".', stack: expect.any(String), }, id: 1, diff --git a/packages/snaps-rpc-methods/src/permitted/getRate.ts b/packages/snaps-rpc-methods/src/permitted/getCurrencyRate.ts similarity index 53% rename from packages/snaps-rpc-methods/src/permitted/getRate.ts rename to packages/snaps-rpc-methods/src/permitted/getCurrencyRate.ts index 5434b2b8b1..cfc80b9087 100644 --- a/packages/snaps-rpc-methods/src/permitted/getRate.ts +++ b/packages/snaps-rpc-methods/src/permitted/getCurrencyRate.ts @@ -2,9 +2,9 @@ import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine'; import type { PermittedHandlerExport } from '@metamask/permission-controller'; import { rpcErrors } from '@metamask/rpc-errors'; import type { - Cryptocurrency, - GetRateParams, - GetRateResult, + Currency, + GetCurrencyRateParams, + GetCurrencyRateResult, JsonRpcRequest, Rate, } from '@metamask/snaps-sdk'; @@ -20,40 +20,40 @@ import type { PendingJsonRpcResponse } from '@metamask/utils'; import type { MethodHooksObject } from '../utils'; -const hookNames: MethodHooksObject = { - getRate: true, +const hookNames: MethodHooksObject = { + getCurrencyRate: true, }; -export type GetRateMethodHooks = { +export type GetCurrencyRateMethodHooks = { /** - * @param cryptocurrency - The cryptocurrency symbol. + * @param currency - The currency symbol. * Currently only 'btc' is supported. * @returns The {@link Rate} object. */ - getRate: (cryptocurrency: Cryptocurrency) => Rate | undefined; + getCurrencyRate: (currency: Currency) => Rate | undefined; }; -export const getRateHandler: PermittedHandlerExport< - GetRateMethodHooks, - GetRateParameters, - GetRateResult +export const getCurrencyRateHandler: PermittedHandlerExport< + GetCurrencyRateMethodHooks, + GetCurrencyRateParameters, + GetCurrencyRateResult > = { - methodNames: ['snap_getRate'], - implementation: getGetRateImplementation, + methodNames: ['snap_getCurrencyRate'], + implementation: getGetCurrencyRateImplementation, hookNames, }; -const GetRateParametersStruct = object({ - cryptocurrency: union([literal('btc')]), +const GetCurrencyRateParametersStruct = object({ + currency: union([literal('btc')]), }); -export type GetRateParameters = InferMatching< - typeof GetRateParametersStruct, - GetRateParams +export type GetCurrencyRateParameters = InferMatching< + typeof GetCurrencyRateParametersStruct, + GetCurrencyRateParams >; /** - * The `snap_getRate` method implementation. + * The `snap_getCurrencyRate` method implementation. * * @param req - The JSON-RPC request object. * @param res - The JSON-RPC response object. @@ -61,24 +61,24 @@ export type GetRateParameters = InferMatching< * function. * @param end - The `json-rpc-engine` "end" callback. * @param hooks - The RPC method hooks. - * @param hooks.getRate - The function to get the rate. + * @param hooks.getCurrencyRate - The function to get the rate. * @returns Nothing. */ -function getGetRateImplementation( - req: JsonRpcRequest, - res: PendingJsonRpcResponse, +function getGetCurrencyRateImplementation( + req: JsonRpcRequest, + res: PendingJsonRpcResponse, _next: unknown, end: JsonRpcEngineEndCallback, - { getRate }: GetRateMethodHooks, + { getCurrencyRate }: GetCurrencyRateMethodHooks, ): void { const { params } = req; try { const validatedParams = getValidatedParams(params); - const { cryptocurrency } = validatedParams; + const { currency } = validatedParams; - res.result = getRate(cryptocurrency) ?? null; + res.result = getCurrencyRate(currency) ?? null; } catch (error) { return end(error); } @@ -87,15 +87,15 @@ function getGetRateImplementation( } /** - * Validate the getRate method `params` and returns them cast to the correct + * 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 getRate method parameter object. + * @returns The validated getCurrencyRate method parameter object. */ -function getValidatedParams(params: unknown): GetRateParameters { +function getValidatedParams(params: unknown): GetCurrencyRateParameters { try { - return create(params, GetRateParametersStruct); + return create(params, GetCurrencyRateParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ diff --git a/packages/snaps-rpc-methods/src/permitted/handlers.ts b/packages/snaps-rpc-methods/src/permitted/handlers.ts index 6a89578566..294192aa13 100644 --- a/packages/snaps-rpc-methods/src/permitted/handlers.ts +++ b/packages/snaps-rpc-methods/src/permitted/handlers.ts @@ -1,9 +1,9 @@ import { createInterfaceHandler } from './createInterface'; import { getAllSnapsHandler } from './getAllSnaps'; import { getClientStatusHandler } from './getClientStatus'; +import { getCurrencyRateHandler } from './getCurrencyRate'; import { getFileHandler } from './getFile'; import { getInterfaceStateHandler } from './getInterfaceState'; -import { getRateHandler } from './getRate'; import { getSnapsHandler } from './getSnaps'; import { invokeKeyringHandler } from './invokeKeyring'; import { invokeSnapSugarHandler } from './invokeSnapSugar'; @@ -24,7 +24,7 @@ export const methodHandlers = { snap_updateInterface: updateInterfaceHandler, snap_getInterfaceState: getInterfaceStateHandler, snap_resolveInterface: resolveInterfaceHandler, - snap_getRate: getRateHandler, + snap_getCurrencyRate: getCurrencyRateHandler, }; /* eslint-enable @typescript-eslint/naming-convention */ diff --git a/packages/snaps-rpc-methods/src/permitted/index.ts b/packages/snaps-rpc-methods/src/permitted/index.ts index 7aeca00eca..604493ee55 100644 --- a/packages/snaps-rpc-methods/src/permitted/index.ts +++ b/packages/snaps-rpc-methods/src/permitted/index.ts @@ -1,8 +1,8 @@ import type { CreateInterfaceMethodHooks } from './createInterface'; import type { GetAllSnapsHooks } from './getAllSnaps'; import type { GetClientStatusHooks } from './getClientStatus'; +import type { GetCurrencyRateMethodHooks } from './getCurrencyRate'; import type { GetInterfaceStateMethodHooks } from './getInterfaceState'; -import type { GetRateMethodHooks } from './getRate'; import type { GetSnapsHooks } from './getSnaps'; import type { RequestSnapsHooks } from './requestSnaps'; import type { ResolveInterfaceMethodHooks } from './resolveInterface'; @@ -16,7 +16,7 @@ export type PermittedRpcMethodHooks = GetAllSnapsHooks & UpdateInterfaceMethodHooks & GetInterfaceStateMethodHooks & ResolveInterfaceMethodHooks & - GetRateMethodHooks; + GetCurrencyRateMethodHooks; export * from './handlers'; export * from './middleware'; diff --git a/packages/snaps-sdk/src/types/methods/get-currency-rate.ts b/packages/snaps-sdk/src/types/methods/get-currency-rate.ts new file mode 100644 index 0000000000..5abc86325f --- /dev/null +++ b/packages/snaps-sdk/src/types/methods/get-currency-rate.ts @@ -0,0 +1,29 @@ +export type Currency = 'btc'; + +/** + * The rate object. + * + * @property conversionRate - The conversion rate. It maps a currency code (e.g. "btc") to its + * conversion rate + * @property conversionDate - The date of the conversion rate as a UNIX timestamp. + * @property usdConversionRate - The conversion rate to USD. + */ +export type Rate = { + conversionRate: string; + conversionDate: number; + usdConversionRate?: string; +}; + +/** + * The request parameters for the `snap_getCurrencyRate` method. + * + * @property currency - The currency symbol. + */ +export type GetCurrencyRateParams = { + currency: Currency; +}; + +/** + * The result returned by the `snap_getCurrencyRate` method, which is the {@link Rate} object. + */ +export type GetCurrencyRateResult = Rate | null; diff --git a/packages/snaps-sdk/src/types/methods/get-rate.ts b/packages/snaps-sdk/src/types/methods/get-rate.ts deleted file mode 100644 index d609db9bac..0000000000 --- a/packages/snaps-sdk/src/types/methods/get-rate.ts +++ /dev/null @@ -1,29 +0,0 @@ -export type Cryptocurrency = 'btc'; - -/** - * The rate object. - * - * @property conversionRate - The conversion rate. It maps a cryptocurrency code (e.g., "BTC") to its - * conversion rate - * @property conversionDate - The date of the conversion rate as a UNIX timestamp. - * @property usdConversionRate - The conversion rate to USD. - */ -export type Rate = { - conversionRate: string; - conversionDate: number; - usdConversionRate?: string; -}; - -/** - * The request parameters for the `snap_getRate` method. - * - * @property cryptocurrency - The cryptocurrency symbol. - */ -export type GetRateParams = { - cryptocurrency: Cryptocurrency; -}; - -/** - * The result returned by the `snap_getRate` method, which is the {@link Rate} object. - */ -export type GetRateResult = Rate | null; diff --git a/packages/snaps-sdk/src/types/methods/index.ts b/packages/snaps-sdk/src/types/methods/index.ts index 627983743f..8ab24b16c7 100644 --- a/packages/snaps-sdk/src/types/methods/index.ts +++ b/packages/snaps-sdk/src/types/methods/index.ts @@ -19,4 +19,4 @@ export * from './notify'; export * from './request-snaps'; export * from './update-interface'; export * from './resolve-interface'; -export * from './get-rate'; +export * from './get-currency-rate';