Skip to content

Commit

Permalink
rename to snap_getCurrencyRate
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Sep 26, 2024
1 parent 53d0a35 commit c59bee0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -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<GetRateParameters>,
response as PendingJsonRpcResponse<GetRateResult>,
request as JsonRpcRequest<GetCurrencyRateParameters>,
response as PendingJsonRpcResponse<GetCurrencyRateResult>,
next,
end,
hooks,
Expand All @@ -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',
},
});

Expand All @@ -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<GetRateParameters>,
response as PendingJsonRpcResponse<GetRateResult>,
request as JsonRpcRequest<GetCurrencyRateParameters>,
response as PendingJsonRpcResponse<GetCurrencyRateResult>,
next,
end,
hooks,
Expand All @@ -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',
},
});

Expand All @@ -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<GetRateParameters>,
response as PendingJsonRpcResponse<GetRateResult>,
request as JsonRpcRequest<GetCurrencyRateParameters>,
response as PendingJsonRpcResponse<GetCurrencyRateResult>,
next,
end,
hooks,
Expand All @@ -135,17 +135,17 @@ describe('snap_getRate', () => {
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_getRate',
method: 'snap_getCurrencyRate',
params: {
cryptocurrency: 'eth',
currency: 'eth',
},
});

expect(response).toStrictEqual({
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,65 +20,65 @@ import type { PendingJsonRpcResponse } from '@metamask/utils';

import type { MethodHooksObject } from '../utils';

const hookNames: MethodHooksObject<GetRateMethodHooks> = {
getRate: true,
const hookNames: MethodHooksObject<GetCurrencyRateMethodHooks> = {
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.
* @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.getRate - The function to get the rate.
* @param hooks.getCurrencyRate - The function to get the rate.
* @returns Nothing.
*/
function getGetRateImplementation(
req: JsonRpcRequest<GetRateParameters>,
res: PendingJsonRpcResponse<GetRateResult>,
function getGetCurrencyRateImplementation(
req: JsonRpcRequest<GetCurrencyRateParameters>,
res: PendingJsonRpcResponse<GetCurrencyRateResult>,
_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);
}
Expand All @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-rpc-methods/src/permitted/handlers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 */

Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-rpc-methods/src/permitted/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +16,7 @@ export type PermittedRpcMethodHooks = GetAllSnapsHooks &
UpdateInterfaceMethodHooks &
GetInterfaceStateMethodHooks &
ResolveInterfaceMethodHooks &
GetRateMethodHooks;
GetCurrencyRateMethodHooks;

export * from './handlers';
export * from './middleware';
29 changes: 29 additions & 0 deletions packages/snaps-sdk/src/types/methods/get-currency-rate.ts
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 0 additions & 29 deletions packages/snaps-sdk/src/types/methods/get-rate.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/snaps-sdk/src/types/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit c59bee0

Please sign in to comment.