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EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';\nimport type { PermittedHandlerExport } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type {\n  AvailableCurrency,\n  CurrencyRate,\n  GetCurrencyRateParams,\n  GetCurrencyRateResult,\n  JsonRpcRequest,\n} from '@metamask/snaps-sdk';\nimport { currency, type InferMatching } from '@metamask/snaps-utils';\nimport { StructError, create, object, union } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetCurrencyRateMethodHooks> = {\n  getCurrencyRate: true,\n};\n\nexport type GetCurrencyRateMethodHooks = {\n  /**\n   * @param currency - The currency symbol.\n   * Currently only 'btc' is supported.\n   * @returns The {@link CurrencyRate} object.\n   */\n  getCurrencyRate: (currency: AvailableCurrency) => CurrencyRate | undefined;\n};\n\nexport const getCurrencyRateHandler: PermittedHandlerExport<\n  GetCurrencyRateMethodHooks,\n  GetCurrencyRateParameters,\n  GetCurrencyRateResult\n> = {\n  methodNames: ['snap_getCurrencyRate'],\n  implementation: getGetCurrencyRateImplementation,\n  hookNames,\n};\n\nconst GetCurrencyRateParametersStruct = object({\n  currency: union([currency('btc')]),\n});\n\nexport type GetCurrencyRateParameters = InferMatching<\n  typeof GetCurrencyRateParametersStruct,\n  GetCurrencyRateParams\n>;\n\n/**\n * The `snap_getCurrencyRate` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getCurrencyRate - The function to get the rate.\n * @returns Nothing.\n */\nfunction getGetCurrencyRateImplementation(\n  req: JsonRpcRequest<GetCurrencyRateParameters>,\n  res: PendingJsonRpcResponse<GetCurrencyRateResult>,\n  _next: unknown,\n  end: JsonRpcEngineEndCallback,\n  { getCurrencyRate }: GetCurrencyRateMethodHooks,\n): void {\n  const { params } = req;\n\n  try {\n    const validatedParams = getValidatedParams(params);\n\n    const { currency: selectedCurrency } = validatedParams;\n\n    res.result = getCurrencyRate(selectedCurrency) ?? null;\n  } catch (error) {\n    return end(error);\n  }\n\n  return end();\n}\n\n/**\n * Validate the getCurrencyRate method `params` and returns them cast to the correct\n * type. Throws if validation fails.\n *\n * @param params - The unvalidated params object from the method request.\n * @returns The validated getCurrencyRate method parameter object.\n */\nfunction getValidatedParams(params: unknown): GetCurrencyRateParameters {\n  try {\n    return create(params, GetCurrencyRateParametersStruct);\n  } catch (error) {\n    if (error instanceof StructError) {\n      throw rpcErrors.invalidParams({\n        message: `Invalid params: ${error.message}.`,\n      });\n    }\n    /* istanbul ignore next */\n    throw rpcErrors.internal();\n  }\n}\n"]}