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S,CAAC;IAEd,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,oBAAoB,EAAE,CAAC;AAClE,CAAC;AA3BD,8BA2BC","sourcesContent":["import type { GasFeeEstimates } from '@metamask/gas-fee-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getNativeToken, getTokenFiatRate } from './token';\nimport type { TransactionPayControllerMessenger } from '..';\nimport { projectLogger } from '../logger';\nimport type { FiatValue } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'gas');\n\n/**\n *\n * Calculate the estimated gas cost for a given transaction in fiat.\n *\n * @param transaction - Transaction to calculate gas cost for\n * @param messenger - Controller messenger.\n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateGasCost(\n  transaction: TransactionMeta,\n  messenger: TransactionPayControllerMessenger,\n): FiatValue {\n  const { chainId, gasUsed, gasLimitNoBuffer, txParams } = transaction;\n  const { gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;\n\n  const estimatedGasLimit = gasUsed || gasLimitNoBuffer || gas || '0x0';\n  const { estimatedBaseFee } = getGasFee(chainId, messenger);\n\n  const gasFeeValue = estimatedBaseFee\n    ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas ?? '0x0')\n    : new BigNumber(maxFeePerGas ?? '0x0');\n\n  const gasCost = gasFeeValue.multipliedBy(estimatedGasLimit).shiftedBy(-18);\n\n  const fiatRate = getTokenFiatRate(\n    messenger,\n    getNativeToken(chainId),\n    chainId,\n  );\n\n  if (!fiatRate) {\n    throw new Error('Could not fetch fiat rate for native token');\n  }\n\n  const usd = gasCost.multipliedBy(fiatRate.usdRate).toString(10);\n  const fiat = gasCost.multipliedBy(fiatRate.fiatRate).toString(10);\n\n  log('Calculated gas cost', {\n    transactionId: transaction.id,\n    chainId,\n    usd,\n    fiat,\n  });\n\n  return {\n    usd,\n    fiat,\n  };\n}\n\n/**\n * Get gas fee estimates for a given chain.\n *\n * @param chainId - Chain ID.\n * @param messenger - Controller messenger.\n * @returns Gas fee estimates for the chain.\n */\nexport function getGasFee(\n  chainId: Hex,\n  messenger: TransactionPayControllerMessenger,\n) {\n  const gasFeeControllerState = messenger.call('GasFeeController:getState');\n\n  const chainState = gasFeeControllerState?.gasFeeEstimatesByChainId?.[chainId];\n\n  const { estimatedBaseFee: estimatedBaseFeeGwei, medium } =\n    (chainState?.gasFeeEstimates as GasFeeEstimates | undefined) ?? {};\n\n  const maxFeePerGasGwei = medium?.suggestedMaxFeePerGas;\n  const maxPriorityFeePerGasGwei = medium?.suggestedMaxPriorityFeePerGas;\n\n  const estimatedBaseFee = estimatedBaseFeeGwei\n    ? new BigNumber(estimatedBaseFeeGwei).shiftedBy(9).toString(10)\n    : undefined;\n\n  const maxFeePerGas = maxFeePerGasGwei\n    ? new BigNumber(maxFeePerGasGwei).shiftedBy(9).toString(10)\n    : undefined;\n\n  const maxPriorityFeePerGas = maxPriorityFeePerGasGwei\n    ? new BigNumber(maxPriorityFeePerGasGwei).shiftedBy(9).toString(10)\n    : undefined;\n\n  return { estimatedBaseFee, maxFeePerGas, maxPriorityFeePerGas };\n}\n"]}