@metamask/transaction-pay-controller 5.0.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/TransactionPayController.cjs +1 -1
  3. package/dist/TransactionPayController.cjs.map +1 -1
  4. package/dist/TransactionPayController.mjs +1 -1
  5. package/dist/TransactionPayController.mjs.map +1 -1
  6. package/dist/constants.cjs +5 -1
  7. package/dist/constants.cjs.map +1 -1
  8. package/dist/constants.d.cts +4 -0
  9. package/dist/constants.d.cts.map +1 -1
  10. package/dist/constants.d.mts +4 -0
  11. package/dist/constants.d.mts.map +1 -1
  12. package/dist/constants.mjs +4 -0
  13. package/dist/constants.mjs.map +1 -1
  14. package/dist/helpers/TransactionPayPublishHook.cjs +1 -1
  15. package/dist/helpers/TransactionPayPublishHook.cjs.map +1 -1
  16. package/dist/helpers/TransactionPayPublishHook.mjs +1 -1
  17. package/dist/helpers/TransactionPayPublishHook.mjs.map +1 -1
  18. package/dist/strategy/bridge/bridge-quotes.cjs +37 -22
  19. package/dist/strategy/bridge/bridge-quotes.cjs.map +1 -1
  20. package/dist/strategy/bridge/bridge-quotes.mjs +37 -22
  21. package/dist/strategy/bridge/bridge-quotes.mjs.map +1 -1
  22. package/dist/strategy/relay/constants.cjs +1 -4
  23. package/dist/strategy/relay/constants.cjs.map +1 -1
  24. package/dist/strategy/relay/constants.d.cts +0 -3
  25. package/dist/strategy/relay/constants.d.cts.map +1 -1
  26. package/dist/strategy/relay/constants.d.mts +0 -3
  27. package/dist/strategy/relay/constants.d.mts.map +1 -1
  28. package/dist/strategy/relay/constants.mjs +0 -3
  29. package/dist/strategy/relay/constants.mjs.map +1 -1
  30. package/dist/strategy/relay/relay-quotes.cjs +34 -8
  31. package/dist/strategy/relay/relay-quotes.cjs.map +1 -1
  32. package/dist/strategy/relay/relay-quotes.d.cts.map +1 -1
  33. package/dist/strategy/relay/relay-quotes.d.mts.map +1 -1
  34. package/dist/strategy/relay/relay-quotes.mjs +32 -6
  35. package/dist/strategy/relay/relay-quotes.mjs.map +1 -1
  36. package/dist/strategy/relay/types.cjs.map +1 -1
  37. package/dist/strategy/relay/types.d.cts +1 -0
  38. package/dist/strategy/relay/types.d.cts.map +1 -1
  39. package/dist/strategy/relay/types.d.mts +1 -0
  40. package/dist/strategy/relay/types.d.mts.map +1 -1
  41. package/dist/strategy/relay/types.mjs.map +1 -1
  42. package/dist/strategy/test/TestStrategy.cjs +24 -2
  43. package/dist/strategy/test/TestStrategy.cjs.map +1 -1
  44. package/dist/strategy/test/TestStrategy.d.cts.map +1 -1
  45. package/dist/strategy/test/TestStrategy.d.mts.map +1 -1
  46. package/dist/strategy/test/TestStrategy.mjs +24 -2
  47. package/dist/strategy/test/TestStrategy.mjs.map +1 -1
  48. package/dist/tests/messenger-mock.d.cts +1 -1
  49. package/dist/tests/messenger-mock.d.mts +1 -1
  50. package/dist/types.cjs.map +1 -1
  51. package/dist/types.d.cts +19 -3
  52. package/dist/types.d.cts.map +1 -1
  53. package/dist/types.d.mts +19 -3
  54. package/dist/types.d.mts.map +1 -1
  55. package/dist/types.mjs.map +1 -1
  56. package/dist/utils/gas.cjs +85 -12
  57. package/dist/utils/gas.cjs.map +1 -1
  58. package/dist/utils/gas.d.cts +12 -3
  59. package/dist/utils/gas.d.cts.map +1 -1
  60. package/dist/utils/gas.d.mts +12 -3
  61. package/dist/utils/gas.d.mts.map +1 -1
  62. package/dist/utils/gas.mjs +86 -13
  63. package/dist/utils/gas.mjs.map +1 -1
  64. package/dist/utils/quotes.cjs +2 -2
  65. package/dist/utils/quotes.cjs.map +1 -1
  66. package/dist/utils/quotes.mjs +2 -2
  67. package/dist/utils/quotes.mjs.map +1 -1
  68. package/dist/utils/source-amounts.cjs +34 -5
  69. package/dist/utils/source-amounts.cjs.map +1 -1
  70. package/dist/utils/source-amounts.d.cts.map +1 -1
  71. package/dist/utils/source-amounts.d.mts.map +1 -1
  72. package/dist/utils/source-amounts.mjs +34 -5
  73. package/dist/utils/source-amounts.mjs.map +1 -1
  74. package/dist/utils/strategy.cjs +2 -2
  75. package/dist/utils/strategy.cjs.map +1 -1
  76. package/dist/utils/strategy.d.cts +1 -1
  77. package/dist/utils/strategy.d.cts.map +1 -1
  78. package/dist/utils/strategy.d.mts +1 -1
  79. package/dist/utils/strategy.d.mts.map +1 -1
  80. package/dist/utils/strategy.mjs +2 -2
  81. package/dist/utils/strategy.mjs.map +1 -1
  82. package/dist/utils/token.cjs +11 -4
  83. package/dist/utils/token.cjs.map +1 -1
  84. package/dist/utils/token.d.cts +1 -1
  85. package/dist/utils/token.d.cts.map +1 -1
  86. package/dist/utils/token.d.mts +1 -1
  87. package/dist/utils/token.d.mts.map +1 -1
  88. package/dist/utils/token.mjs +12 -5
  89. package/dist/utils/token.mjs.map +1 -1
  90. package/dist/utils/totals.cjs +51 -26
  91. package/dist/utils/totals.cjs.map +1 -1
  92. package/dist/utils/totals.d.cts.map +1 -1
  93. package/dist/utils/totals.d.mts.map +1 -1
  94. package/dist/utils/totals.mjs +51 -26
  95. package/dist/utils/totals.mjs.map +1 -1
  96. package/package.json +4 -4
@@ -1 +1 @@
1
- {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n CurrencyRateControllerActions,\n TokenBalancesControllerGetStateAction,\n} from '@metamask/assets-controllers';\nimport type { TokenListControllerActions } from '@metamask/assets-controllers';\nimport type { TokenRatesControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { TokensControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { AccountTrackerControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { ControllerStateChangeEvent } from '@metamask/base-controller';\nimport type { ControllerGetStateAction } from '@metamask/base-controller';\nimport type { BridgeControllerActions } from '@metamask/bridge-controller';\nimport type { BridgeStatusControllerStateChangeEvent } from '@metamask/bridge-status-controller';\nimport type { BridgeStatusControllerActions } from '@metamask/bridge-status-controller';\nimport type { GasFeeControllerActions } from '@metamask/gas-fee-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport type { NetworkControllerFindNetworkClientIdByChainIdAction } from '@metamask/network-controller';\nimport type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\nimport type {\n AuthorizationList,\n TransactionControllerAddTransactionBatchAction,\n TransactionControllerUnapprovedTransactionAddedEvent,\n} from '@metamask/transaction-controller';\nimport type { TransactionControllerGetStateAction } from '@metamask/transaction-controller';\nimport type { TransactionControllerStateChangeEvent } from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { TransactionControllerAddTransactionAction } from '@metamask/transaction-controller';\nimport type { TransactionControllerUpdateTransactionAction } from '@metamask/transaction-controller';\nimport type { BatchTransaction } from '@metamask/transaction-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport type { Draft } from 'immer';\n\nimport type { CONTROLLER_NAME, TransactionPayStrategy } from './constants';\n\nexport type AllowedActions =\n | AccountTrackerControllerGetStateAction\n | BridgeControllerActions\n | BridgeStatusControllerActions\n | CurrencyRateControllerActions\n | GasFeeControllerActions\n | NetworkControllerFindNetworkClientIdByChainIdAction\n | NetworkControllerGetNetworkClientByIdAction\n | RemoteFeatureFlagControllerGetStateAction\n | TokenBalancesControllerGetStateAction\n | TokenListControllerActions\n | TokenRatesControllerGetStateAction\n | TokensControllerGetStateAction\n | TransactionControllerAddTransactionAction\n | TransactionControllerAddTransactionBatchAction\n | TransactionControllerGetStateAction\n | TransactionControllerUpdateTransactionAction;\n\nexport type AllowedEvents =\n | BridgeStatusControllerStateChangeEvent\n | TransactionControllerStateChangeEvent\n | TransactionControllerUnapprovedTransactionAddedEvent;\n\nexport type TransactionPayControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState\n>;\n\nexport type TransactionPayControllerGetDelegationTransactionAction = {\n type: `${typeof CONTROLLER_NAME}:getDelegationTransaction`;\n handler: GetDelegationTransactionCallback;\n};\n\n/** Action to get the pay strategy type used for a transaction. */\nexport type TransactionPayControllerGetStrategyAction = {\n type: `${typeof CONTROLLER_NAME}:getStrategy`;\n handler: (transaction: TransactionMeta) => Promise<TransactionPayStrategy>;\n};\n\n/** Action to update the payment token for a transaction. */\nexport type TransactionPayControllerUpdatePaymentTokenAction = {\n type: `${typeof CONTROLLER_NAME}:updatePaymentToken`;\n handler: (request: UpdatePaymentTokenRequest) => void;\n};\n\nexport type TransactionPayControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState\n >;\n\nexport type TransactionPayControllerActions =\n | TransactionPayControllerGetDelegationTransactionAction\n | TransactionPayControllerGetStateAction\n | TransactionPayControllerGetStrategyAction\n | TransactionPayControllerUpdatePaymentTokenAction;\n\nexport type TransactionPayControllerEvents =\n TransactionPayControllerStateChangeEvent;\n\nexport type TransactionPayControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n TransactionPayControllerActions | AllowedActions,\n TransactionPayControllerEvents | AllowedEvents\n>;\n\n/** Options for the TransactionPayController. */\nexport type TransactionPayControllerOptions = {\n /** Callback to convert a transaction into a redeem delegation. */\n getDelegationTransaction: GetDelegationTransactionCallback;\n\n /** Callback to select the PayStrategy for a transaction. */\n getStrategy?: (\n transaction: TransactionMeta,\n ) => Promise<TransactionPayStrategy>;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Initial state of the controller. */\n state?: Partial<TransactionPayControllerState>;\n};\n\n/** State of the TransactionPayController. */\nexport type TransactionPayControllerState = {\n /** State relating to each transaction, keyed by transaction ID. */\n transactionData: Record<string, TransactionData>;\n};\n\n/** State relating to a single transaction. */\nexport type TransactionData = {\n /** Whether quotes are currently being retrieved. */\n isLoading: boolean;\n\n /** Source token selected for the transaction. */\n paymentToken?: TransactionPaymentToken;\n\n /** Quotes retrieved for the transaction. */\n quotes?: TransactionPayQuote<Json>[];\n\n /** Timestamp of when quotes were last updated. */\n quotesLastUpdated?: number;\n\n /** Amounts of payment token required for each required token. */\n sourceAmounts?: TransactionPaySourceAmount[];\n\n /** Tokens required by the transaction. */\n tokens: TransactionPayRequiredToken[];\n\n /** Calculated totals for the transaction. */\n totals?: TransactionPayTotals;\n};\n\n/** A token required by a transaction. */\nexport type TransactionPayRequiredToken = {\n /** Address of the required token. */\n address: Hex;\n\n /** Whether to allow quotes that return less than the minimum amount requested. */\n allowUnderMinimum: boolean;\n\n /** Amount required in the selected currency. */\n amountFiat: string;\n\n /** Amount required in a human-readable format factoring token decimals. */\n amountHuman: string;\n\n /** Amount required in atomic format without factoring token decimals. */\n amountRaw: string;\n\n /** Amount required in USD. */\n amountUsd: string;\n\n /** Balance of the required token in the selected currency. */\n balanceFiat: string;\n\n /** Balance of the required token in a human-readable format factoring token decimals. */\n balanceHuman: string;\n\n /** Balance of the required token in atomic format without factoring token decimals. */\n balanceRaw: string;\n\n /** Balance of the required token in USD. */\n balanceUsd: string;\n\n /** Chain ID of the required token. */\n chainId: Hex;\n\n /** Decimals of the required token. */\n decimals: number;\n\n /** Whether to skip transfer of this token if balance is already sufficient. */\n skipIfBalance: boolean;\n\n /** Symbol of the required token. */\n symbol: string;\n};\n\n/** Amount of payment token required by a required token. */\nexport type TransactionPaySourceAmount = {\n /** Amount of payment token required in the selected currency. */\n sourceAmountHuman: string;\n\n /** Amount of payment token required in atomic format without factoring token decimals. */\n sourceAmountRaw: string;\n\n /** Address of the required token. */\n targetTokenAddress: Hex;\n};\n\n/** Source token used to pay for required tokens. */\nexport type TransactionPaymentToken = {\n /** Address of the payment token. */\n address: Hex;\n\n /** Balance of the payment token in the selected currency. */\n balanceFiat: string;\n\n /** Balance of the payment token in a human-readable format factoring token decimals. */\n balanceHuman: string;\n\n /** Balance of the payment token in atomic format without factoring token decimals. */\n balanceRaw: string;\n\n /** Balance of the payment token in USD. */\n balanceUsd: string;\n\n /** Chain ID of the payment token. */\n chainId: Hex;\n\n /** Decimals of the payment token. */\n decimals: number;\n\n /** Symbol of the payment token. */\n symbol: string;\n};\n\n/** Callback to update state for a single transaction. */\nexport type UpdateTransactionDataCallback = (\n /** ID of the transaction to update. */\n transactionId: string,\n /** Function that receives a draft of the transaction data to update. */\n fn: (data: Draft<TransactionData>) => void,\n) => void;\n\n/** Conversion rates from the native currency to other currencies. */\nexport type FiatRates = {\n /** Conversion rate for the native currency to the selected fiat currency. */\n fiatRate: string;\n\n /** Conversion rate for the native currency to USD. */\n usdRate: string;\n};\n\n/** Request for a quote to retrieve a required token. */\nexport type QuoteRequest = {\n /** Address of the user's account. */\n from: Hex;\n\n /** Balance of the source token in atomic format without factoring token decimals. */\n sourceBalanceRaw: string;\n\n /** Chain ID of the source token. */\n sourceChainId: Hex;\n\n /** Address of the source token. */\n sourceTokenAddress: Hex;\n\n /** Amount of the required token in atomic format without factoring token decimals. */\n sourceTokenAmount: string;\n\n /** Minimum amount required of the target token in atomic format without factoring token decimals. */\n targetAmountMinimum: string;\n\n /** Chain ID of the target token. */\n targetChainId: Hex;\n\n /** Address of the target token. */\n targetTokenAddress: Hex;\n};\n\n/** Fees associated with a transaction pay quote. */\nexport type TransactionPayFees = {\n /** Fee charged by the quote provider. */\n provider: FiatValue;\n\n /** Network fee for transactions on the source network. */\n sourceNetwork: FiatValue;\n\n /** Network fee for transactions on the target network. */\n targetNetwork: FiatValue;\n};\n\n/** Quote returned to retrieve a required token using the payment token. */\nexport type TransactionPayQuote<OriginalQuote> = {\n /** Additional amount provided by the quote beyond the minimum requested. */\n dust: FiatValue;\n\n /** Duration estimated for the transaction to complete in seconds. */\n estimatedDuration: number;\n\n /** Fees associated with the transaction pay quote. */\n fees: TransactionPayFees;\n\n /** Raw quote data returned by the provider. */\n original: OriginalQuote;\n\n /** Associated quote request. */\n request: QuoteRequest;\n\n /** Name of the strategy used to retrieve the quote. */\n strategy: TransactionPayStrategy;\n};\n\n/** Request to get quotes for a transaction. */\nexport type PayStrategyGetQuotesRequest = {\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quote requests for required tokens. */\n requests: QuoteRequest[];\n\n /** Metadata of the original target transaction. */\n transaction: TransactionMeta;\n};\n\n/** Request to submit quotes for a transaction. */\nexport type PayStrategyExecuteRequest<OriginalRequest> = {\n /** Callback to determine if the transaction is a smart transaction. */\n isSmartTransaction: (chainId: Hex) => boolean;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quotes to be submitted. */\n quotes: TransactionPayQuote<OriginalRequest>[];\n\n /** Metadata of the original target transaction. */\n transaction: TransactionMeta;\n};\n\n/** Request to get batch transactions for quotes. */\nexport type PayStrategyGetBatchRequest<OriginalQuote> = {\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quotes for required tokens. */\n quotes: TransactionPayQuote<OriginalQuote>[];\n};\n\n/** Request to get refresh interval for a specific strategy. */\nexport type PayStrategyGetRefreshIntervalRequest = {\n /** Chain ID of the source or payment token. */\n chainId: Hex;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n};\n\n/** Strategy used to obtain required tokens for a transaction. */\nexport type PayStrategy<OriginalQuote> = {\n /** Retrieve quotes for required tokens. */\n getQuotes: (\n request: PayStrategyGetQuotesRequest,\n ) => Promise<TransactionPayQuote<OriginalQuote>[]>;\n\n /** Retrieve batch transactions for quotes, if supported by the strategy. */\n getBatchTransactions?: (\n request: PayStrategyGetBatchRequest<OriginalQuote>,\n ) => Promise<BatchTransaction[]>;\n\n /**\n * Retrieve refresh interval for the strategy, if applicable.\n * Defaults to 30 seconds.\n */\n getRefreshInterval?: (\n request: PayStrategyGetRefreshIntervalRequest,\n ) => Promise<number | undefined>;\n\n /** Execute or submit the quotes to obtain required tokens. */\n execute: (request: PayStrategyExecuteRequest<OriginalQuote>) => Promise<{\n transactionHash?: Hex;\n }>;\n};\n\n/** Single fiat value in alternate currencies. */\nexport type FiatValue = {\n /** Value in the selected fiat currency. */\n fiat: string;\n\n /** Value in USD. */\n usd: string;\n};\n\n/** Calculated totals for a target transaction and all quotes. */\nexport type TransactionPayTotals = {\n /** Total estimated duration for the target transaction and all quotes. */\n estimatedDuration: number;\n\n /** Total fees for the target transaction and all quotes. */\n fees: TransactionPayFees;\n\n /** Overall total cost for the target transaction and all quotes. */\n total: FiatValue;\n};\n\n/** Request to update the payment token for a transaction. */\nexport type UpdatePaymentTokenRequest = {\n /** ID of the transaction to update. */\n transactionId: string;\n\n /** Address of the new payment token. */\n tokenAddress: Hex;\n\n /** Chain ID of the new payment token. */\n chainId: Hex;\n};\n\n/** Callback to convert a transaction to a redeem delegation. */\nexport type GetDelegationTransactionCallback = ({\n transaction,\n}: {\n transaction: TransactionMeta;\n}) => Promise<{\n authorizationList?: AuthorizationList;\n data: Hex;\n to: Hex;\n value: Hex;\n}>;\n"]}
1
+ {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n CurrencyRateControllerActions,\n TokenBalancesControllerGetStateAction,\n} from '@metamask/assets-controllers';\nimport type { TokenListControllerActions } from '@metamask/assets-controllers';\nimport type { TokenRatesControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { TokensControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { AccountTrackerControllerGetStateAction } from '@metamask/assets-controllers';\nimport type { ControllerStateChangeEvent } from '@metamask/base-controller';\nimport type { ControllerGetStateAction } from '@metamask/base-controller';\nimport type { BridgeControllerActions } from '@metamask/bridge-controller';\nimport type { BridgeStatusControllerStateChangeEvent } from '@metamask/bridge-status-controller';\nimport type { BridgeStatusControllerActions } from '@metamask/bridge-status-controller';\nimport type { GasFeeControllerActions } from '@metamask/gas-fee-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport type { NetworkControllerFindNetworkClientIdByChainIdAction } from '@metamask/network-controller';\nimport type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\nimport type {\n AuthorizationList,\n TransactionControllerAddTransactionBatchAction,\n TransactionControllerUnapprovedTransactionAddedEvent,\n} from '@metamask/transaction-controller';\nimport type { TransactionControllerGetStateAction } from '@metamask/transaction-controller';\nimport type { TransactionControllerStateChangeEvent } from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { TransactionControllerAddTransactionAction } from '@metamask/transaction-controller';\nimport type { TransactionControllerUpdateTransactionAction } from '@metamask/transaction-controller';\nimport type { BatchTransaction } from '@metamask/transaction-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport type { Draft } from 'immer';\n\nimport type { CONTROLLER_NAME, TransactionPayStrategy } from './constants';\n\nexport type AllowedActions =\n | AccountTrackerControllerGetStateAction\n | BridgeControllerActions\n | BridgeStatusControllerActions\n | CurrencyRateControllerActions\n | GasFeeControllerActions\n | NetworkControllerFindNetworkClientIdByChainIdAction\n | NetworkControllerGetNetworkClientByIdAction\n | RemoteFeatureFlagControllerGetStateAction\n | TokenBalancesControllerGetStateAction\n | TokenListControllerActions\n | TokenRatesControllerGetStateAction\n | TokensControllerGetStateAction\n | TransactionControllerAddTransactionAction\n | TransactionControllerAddTransactionBatchAction\n | TransactionControllerGetStateAction\n | TransactionControllerUpdateTransactionAction;\n\nexport type AllowedEvents =\n | BridgeStatusControllerStateChangeEvent\n | TransactionControllerStateChangeEvent\n | TransactionControllerUnapprovedTransactionAddedEvent;\n\nexport type TransactionPayControllerGetStateAction = ControllerGetStateAction<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState\n>;\n\nexport type TransactionPayControllerGetDelegationTransactionAction = {\n type: `${typeof CONTROLLER_NAME}:getDelegationTransaction`;\n handler: GetDelegationTransactionCallback;\n};\n\n/** Action to get the pay strategy type used for a transaction. */\nexport type TransactionPayControllerGetStrategyAction = {\n type: `${typeof CONTROLLER_NAME}:getStrategy`;\n handler: (transaction: TransactionMeta) => TransactionPayStrategy;\n};\n\n/** Action to update the payment token for a transaction. */\nexport type TransactionPayControllerUpdatePaymentTokenAction = {\n type: `${typeof CONTROLLER_NAME}:updatePaymentToken`;\n handler: (request: UpdatePaymentTokenRequest) => void;\n};\n\nexport type TransactionPayControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState\n >;\n\nexport type TransactionPayControllerActions =\n | TransactionPayControllerGetDelegationTransactionAction\n | TransactionPayControllerGetStateAction\n | TransactionPayControllerGetStrategyAction\n | TransactionPayControllerUpdatePaymentTokenAction;\n\nexport type TransactionPayControllerEvents =\n TransactionPayControllerStateChangeEvent;\n\nexport type TransactionPayControllerMessenger = Messenger<\n typeof CONTROLLER_NAME,\n TransactionPayControllerActions | AllowedActions,\n TransactionPayControllerEvents | AllowedEvents\n>;\n\n/** Options for the TransactionPayController. */\nexport type TransactionPayControllerOptions = {\n /** Callback to convert a transaction into a redeem delegation. */\n getDelegationTransaction: GetDelegationTransactionCallback;\n\n /** Callback to select the PayStrategy for a transaction. */\n getStrategy?: (transaction: TransactionMeta) => TransactionPayStrategy;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Initial state of the controller. */\n state?: Partial<TransactionPayControllerState>;\n};\n\n/** State of the TransactionPayController. */\nexport type TransactionPayControllerState = {\n /** State relating to each transaction, keyed by transaction ID. */\n transactionData: Record<string, TransactionData>;\n};\n\n/** State relating to a single transaction. */\nexport type TransactionData = {\n /** Whether quotes are currently being retrieved. */\n isLoading: boolean;\n\n /** Source token selected for the transaction. */\n paymentToken?: TransactionPaymentToken;\n\n /** Quotes retrieved for the transaction. */\n quotes?: TransactionPayQuote<Json>[];\n\n /** Timestamp of when quotes were last updated. */\n quotesLastUpdated?: number;\n\n /** Amounts of payment token required for each required token. */\n sourceAmounts?: TransactionPaySourceAmount[];\n\n /** Tokens required by the transaction. */\n tokens: TransactionPayRequiredToken[];\n\n /** Calculated totals for the transaction. */\n totals?: TransactionPayTotals;\n};\n\n/** A token required by a transaction. */\nexport type TransactionPayRequiredToken = {\n /** Address of the required token. */\n address: Hex;\n\n /** Whether to allow quotes that return less than the minimum amount requested. */\n allowUnderMinimum: boolean;\n\n /** Amount required in the selected currency. */\n amountFiat: string;\n\n /** Amount required in a human-readable format factoring token decimals. */\n amountHuman: string;\n\n /** Amount required in atomic format without factoring token decimals. */\n amountRaw: string;\n\n /** Amount required in USD. */\n amountUsd: string;\n\n /** Balance of the required token in the selected currency. */\n balanceFiat: string;\n\n /** Balance of the required token in a human-readable format factoring token decimals. */\n balanceHuman: string;\n\n /** Balance of the required token in atomic format without factoring token decimals. */\n balanceRaw: string;\n\n /** Balance of the required token in USD. */\n balanceUsd: string;\n\n /** Chain ID of the required token. */\n chainId: Hex;\n\n /** Decimals of the required token. */\n decimals: number;\n\n /** Whether to skip transfer of this token if balance is already sufficient. */\n skipIfBalance: boolean;\n\n /** Symbol of the required token. */\n symbol: string;\n};\n\n/** Amount of payment token required by a required token. */\nexport type TransactionPaySourceAmount = {\n /** Amount of payment token required in the selected currency. */\n sourceAmountHuman: string;\n\n /** Amount of payment token required in atomic format without factoring token decimals. */\n sourceAmountRaw: string;\n\n /** Address of the required token. */\n targetTokenAddress: Hex;\n};\n\n/** Source token used to pay for required tokens. */\nexport type TransactionPaymentToken = {\n /** Address of the payment token. */\n address: Hex;\n\n /** Balance of the payment token in the selected currency. */\n balanceFiat: string;\n\n /** Balance of the payment token in a human-readable format factoring token decimals. */\n balanceHuman: string;\n\n /** Balance of the payment token in atomic format without factoring token decimals. */\n balanceRaw: string;\n\n /** Balance of the payment token in USD. */\n balanceUsd: string;\n\n /** Chain ID of the payment token. */\n chainId: Hex;\n\n /** Decimals of the payment token. */\n decimals: number;\n\n /** Symbol of the payment token. */\n symbol: string;\n};\n\n/** Callback to update state for a single transaction. */\nexport type UpdateTransactionDataCallback = (\n /** ID of the transaction to update. */\n transactionId: string,\n /** Function that receives a draft of the transaction data to update. */\n fn: (data: Draft<TransactionData>) => void,\n) => void;\n\n/** Conversion rates from the native currency to other currencies. */\nexport type FiatRates = {\n /** Conversion rate for the native currency to the selected fiat currency. */\n fiatRate: string;\n\n /** Conversion rate for the native currency to USD. */\n usdRate: string;\n};\n\n/** Request for a quote to retrieve a required token. */\nexport type QuoteRequest = {\n /** Address of the user's account. */\n from: Hex;\n\n /** Balance of the source token in atomic format without factoring token decimals. */\n sourceBalanceRaw: string;\n\n /** Chain ID of the source token. */\n sourceChainId: Hex;\n\n /** Address of the source token. */\n sourceTokenAddress: Hex;\n\n /** Amount of the required token in atomic format without factoring token decimals. */\n sourceTokenAmount: string;\n\n /** Minimum amount required of the target token in atomic format without factoring token decimals. */\n targetAmountMinimum: string;\n\n /** Chain ID of the target token. */\n targetChainId: Hex;\n\n /** Address of the target token. */\n targetTokenAddress: Hex;\n};\n\n/** Fees associated with a transaction pay quote. */\nexport type TransactionPayFees = {\n /** Whether a gas fee token is used to pay target network fees. */\n isTargetGasFeeToken?: boolean;\n\n /** Fee charged by the quote provider. */\n provider: FiatValue;\n\n /** Network fee for transactions on the source network. */\n sourceNetwork: {\n estimate: Amount;\n max: Amount;\n };\n\n /** Network fee for transactions on the target network. */\n targetNetwork: FiatValue;\n};\n\n/** Quote returned to retrieve a required token using the payment token. */\nexport type TransactionPayQuote<OriginalQuote> = {\n /** Additional amount provided by the quote beyond the minimum requested. */\n dust: FiatValue;\n\n /** Duration estimated for the transaction to complete in seconds. */\n estimatedDuration: number;\n\n /** Fees associated with the transaction pay quote. */\n fees: TransactionPayFees;\n\n /** Raw quote data returned by the provider. */\n original: OriginalQuote;\n\n /** Associated quote request. */\n request: QuoteRequest;\n\n /** Amount of source token required. */\n sourceAmount: Amount;\n\n /** Name of the strategy used to retrieve the quote. */\n strategy: TransactionPayStrategy;\n};\n\n/** Request to get quotes for a transaction. */\nexport type PayStrategyGetQuotesRequest = {\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quote requests for required tokens. */\n requests: QuoteRequest[];\n\n /** Metadata of the original target transaction. */\n transaction: TransactionMeta;\n};\n\n/** Request to submit quotes for a transaction. */\nexport type PayStrategyExecuteRequest<OriginalRequest> = {\n /** Callback to determine if the transaction is a smart transaction. */\n isSmartTransaction: (chainId: Hex) => boolean;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quotes to be submitted. */\n quotes: TransactionPayQuote<OriginalRequest>[];\n\n /** Metadata of the original target transaction. */\n transaction: TransactionMeta;\n};\n\n/** Request to get batch transactions for quotes. */\nexport type PayStrategyGetBatchRequest<OriginalQuote> = {\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n\n /** Quotes for required tokens. */\n quotes: TransactionPayQuote<OriginalQuote>[];\n};\n\n/** Request to get refresh interval for a specific strategy. */\nexport type PayStrategyGetRefreshIntervalRequest = {\n /** Chain ID of the source or payment token. */\n chainId: Hex;\n\n /** Controller messenger. */\n messenger: TransactionPayControllerMessenger;\n};\n\n/** Strategy used to obtain required tokens for a transaction. */\nexport type PayStrategy<OriginalQuote> = {\n /** Retrieve quotes for required tokens. */\n getQuotes: (\n request: PayStrategyGetQuotesRequest,\n ) => Promise<TransactionPayQuote<OriginalQuote>[]>;\n\n /** Retrieve batch transactions for quotes, if supported by the strategy. */\n getBatchTransactions?: (\n request: PayStrategyGetBatchRequest<OriginalQuote>,\n ) => Promise<BatchTransaction[]>;\n\n /**\n * Retrieve refresh interval for the strategy, if applicable.\n * Defaults to 30 seconds.\n */\n getRefreshInterval?: (\n request: PayStrategyGetRefreshIntervalRequest,\n ) => Promise<number | undefined>;\n\n /** Execute or submit the quotes to obtain required tokens. */\n execute: (request: PayStrategyExecuteRequest<OriginalQuote>) => Promise<{\n transactionHash?: Hex;\n }>;\n};\n\n/** Single fiat value in alternate currencies. */\nexport type FiatValue = {\n /** Value in the selected fiat currency. */\n fiat: string;\n\n /** Value in USD. */\n usd: string;\n};\n\n/** Calculated totals for a target transaction and all quotes. */\nexport type TransactionPayTotals = {\n /** Total estimated duration for the target transaction and all quotes. */\n estimatedDuration: number;\n\n /** Total fees for the target transaction and all quotes. */\n fees: TransactionPayFees;\n\n /** Total amount of source token required. */\n sourceAmount: Amount;\n\n /** Overall total cost for the target transaction and all quotes. */\n total: FiatValue;\n};\n\n/** Request to update the payment token for a transaction. */\nexport type UpdatePaymentTokenRequest = {\n /** ID of the transaction to update. */\n transactionId: string;\n\n /** Address of the new payment token. */\n tokenAddress: Hex;\n\n /** Chain ID of the new payment token. */\n chainId: Hex;\n};\n\n/** Callback to convert a transaction to a redeem delegation. */\nexport type GetDelegationTransactionCallback = ({\n transaction,\n}: {\n transaction: TransactionMeta;\n}) => Promise<{\n authorizationList?: AuthorizationList;\n data: Hex;\n to: Hex;\n value: Hex;\n}>;\n\n/** Single amount in alternate formats. */\nexport type Amount = FiatValue & {\n /** Amount in human-readable format factoring token decimals. */\n human: string;\n\n /** Amount in atomic format without factoring token decimals. */\n raw: string;\n};\n"]}
@@ -4,25 +4,48 @@ exports.calculateGasCost = exports.calculateTransactionGasCost = void 0;
4
4
  const controller_utils_1 = require("@metamask/controller-utils");
5
5
  const bignumber_js_1 = require("bignumber.js");
6
6
  const token_1 = require("./token.cjs");
7
+ const logger_1 = require("../logger.cjs");
8
+ const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'gas');
7
9
  /**
8
10
  *
9
11
  * Calculate the estimated gas cost for a given transaction in fiat.
10
12
  *
11
13
  * @param transaction - Transaction to calculate gas cost for
12
14
  * @param messenger - Controller messenger.
15
+ * @param options - Calculation options.
16
+ * @param options.isMax - Whether to calculate the maximum fee.
13
17
  * @returns Estimated gas cost for the transaction.
14
18
  */
15
- function calculateTransactionGasCost(transaction, messenger) {
16
- const { chainId, gasUsed, gasLimitNoBuffer, txParams } = transaction;
17
- const { gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;
19
+ function calculateTransactionGasCost(transaction, messenger, { isMax } = {}) {
20
+ const { chainId, gasUsed: gasUsedOriginal, gasLimitNoBuffer, txParams, } = transaction;
21
+ const { from, gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;
22
+ const gasUsed = isMax ? undefined : gasUsedOriginal;
18
23
  const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';
19
- return calculateGasCost({
24
+ const result = calculateGasCost({
20
25
  chainId,
21
26
  gas: finalGas,
27
+ isMax,
22
28
  maxFeePerGas,
23
29
  maxPriorityFeePerGas,
24
30
  messenger,
25
31
  });
32
+ const max = calculateGasCost({
33
+ chainId,
34
+ gas: finalGas,
35
+ isMax: true,
36
+ messenger,
37
+ });
38
+ const nativeBalance = (0, token_1.getTokenBalance)(messenger, from, chainId, (0, token_1.getNativeToken)(chainId));
39
+ const hasBalance = new bignumber_js_1.BigNumber(nativeBalance).gte(max.raw);
40
+ const gasFeeTokenCost = calculateGasFeeTokenCost({
41
+ hasBalance,
42
+ messenger,
43
+ transaction,
44
+ });
45
+ if (gasFeeTokenCost) {
46
+ return gasFeeTokenCost;
47
+ }
48
+ return result;
26
49
  }
27
50
  exports.calculateTransactionGasCost = calculateTransactionGasCost;
28
51
  /**
@@ -31,32 +54,37 @@ exports.calculateTransactionGasCost = calculateTransactionGasCost;
31
54
  * @param request - Gas cost calculation parameters.
32
55
  * @param request.chainId - ID of the chain.
33
56
  * @param request.gas - Amount of gas the transaction will use.
57
+ * @param request.isMax - Whether to calculate the maximum fee.
34
58
  * @param request.maxFeePerGas - Max fee to pay per gas.
35
59
  * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.
36
60
  * @param request.messenger - Controller messenger.
61
+
37
62
  * @returns Estimated gas cost for the transaction.
38
63
  */
39
64
  function calculateGasCost(request) {
40
- const { chainId: chainIdInput, gas, maxFeePerGas: maxFeePerGasInput, maxPriorityFeePerGas: maxPriorityFeePerGasInput, messenger, } = request;
65
+ const { chainId: chainIdInput, gas, isMax, maxFeePerGas: maxFeePerGasInput, maxPriorityFeePerGas: maxPriorityFeePerGasInput, messenger, } = request;
41
66
  const chainId = (0, controller_utils_1.toHex)(chainIdInput);
42
67
  const { estimatedBaseFee, maxFeePerGas: maxFeePerGasEstimate, maxPriorityFeePerGas: maxPriorityFeePerGasEstimate, } = getGasFee(chainId, messenger);
43
68
  const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;
44
69
  const maxPriorityFeePerGas = maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;
45
- const feePerGas = estimatedBaseFee && maxPriorityFeePerGas
70
+ const feePerGas = estimatedBaseFee && maxPriorityFeePerGas && !isMax
46
71
  ? new bignumber_js_1.BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)
47
72
  : new bignumber_js_1.BigNumber(maxFeePerGas || '0x0');
48
- const gasCostNative = new bignumber_js_1.BigNumber(gas)
49
- .multipliedBy(feePerGas)
50
- .shiftedBy(-18);
73
+ const rawValue = new bignumber_js_1.BigNumber(gas).multipliedBy(feePerGas);
74
+ const raw = rawValue.toString(10);
75
+ const humanValue = rawValue.shiftedBy(-18);
76
+ const human = humanValue.toString(10);
51
77
  const fiatRate = (0, token_1.getTokenFiatRate)(messenger, (0, token_1.getNativeToken)(chainId), chainId);
52
78
  if (!fiatRate) {
53
79
  throw new Error('Could not fetch fiat rate for native token');
54
80
  }
55
- const usd = gasCostNative.multipliedBy(fiatRate.usdRate).toString(10);
56
- const fiat = gasCostNative.multipliedBy(fiatRate.fiatRate).toString(10);
81
+ const usd = humanValue.multipliedBy(fiatRate.usdRate).toString(10);
82
+ const fiat = humanValue.multipliedBy(fiatRate.fiatRate).toString(10);
57
83
  return {
58
- usd,
59
84
  fiat,
85
+ human,
86
+ raw,
87
+ usd,
60
88
  };
61
89
  }
62
90
  exports.calculateGasCost = calculateGasCost;
@@ -84,4 +112,49 @@ function getGasFee(chainId, messenger) {
84
112
  : undefined;
85
113
  return { estimatedBaseFee, maxFeePerGas, maxPriorityFeePerGas };
86
114
  }
115
+ /**
116
+ * Calculate the cost of a gas fee token on a transaction.
117
+ *
118
+ * @param request - Request parameters.
119
+ * @param request.hasBalance - Whether the user has enough balance to cover the gas fee.
120
+ * @param request.messenger - Controller messenger.
121
+ * @param request.transaction - Transaction to calculate gas fee token cost for.
122
+ * @returns Cost of the gas fee token.
123
+ */
124
+ function calculateGasFeeTokenCost({ hasBalance, messenger, transaction, }) {
125
+ const { chainId, gasFeeTokens, isGasFeeTokenIgnoredIfBalance, selectedGasFeeToken, } = transaction;
126
+ if (!gasFeeTokens ||
127
+ !selectedGasFeeToken ||
128
+ (isGasFeeTokenIgnoredIfBalance && hasBalance)) {
129
+ return undefined;
130
+ }
131
+ log('Calculating gas fee token cost', { selectedGasFeeToken, chainId });
132
+ const gasFeeToken = gasFeeTokens?.find((t) => t.tokenAddress.toLowerCase() === selectedGasFeeToken.toLowerCase());
133
+ if (!gasFeeToken) {
134
+ log('Gas fee token not found', {
135
+ gasFeeTokens,
136
+ selectedGasFeeToken,
137
+ });
138
+ return undefined;
139
+ }
140
+ const tokenInfo = (0, token_1.getTokenInfo)(messenger, selectedGasFeeToken, chainId);
141
+ const tokenFiatRate = (0, token_1.getTokenFiatRate)(messenger, selectedGasFeeToken, chainId);
142
+ if (!tokenFiatRate || !tokenInfo) {
143
+ log('Cannot get gas fee token info');
144
+ return undefined;
145
+ }
146
+ const rawValue = new bignumber_js_1.BigNumber(gasFeeToken.amount);
147
+ const raw = rawValue.toString(10);
148
+ const humanValue = rawValue.shiftedBy(-tokenInfo.decimals);
149
+ const human = humanValue.toString(10);
150
+ const fiat = humanValue.multipliedBy(tokenFiatRate.fiatRate).toString(10);
151
+ const usd = humanValue.multipliedBy(tokenFiatRate.usdRate).toString(10);
152
+ return {
153
+ isGasFeeToken: true,
154
+ fiat,
155
+ human,
156
+ raw,
157
+ usd,
158
+ };
159
+ }
87
160
  //# sourceMappingURL=gas.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.cjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAInD,+CAAyC;AAEzC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AC,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC3D,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,oBAAoB,GAAG,wBAAwB;QACnD,CAAC,CAAC,IAAI,wBAAS,CAAC,wBAAwB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnE,CAAC,CAAC,SAAS,CAAC;IAEd,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,oBAAoB,EAAE,CAAC;AAClE,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { GasFeeEstimates } from '@metamask/gas-fee-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getNativeToken, getTokenFiatRate } from './token';\nimport type { TransactionPayControllerMessenger } from '..';\nimport type { FiatValue } from '../types';\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 calculateTransactionGasCost(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): FiatValue {\n const { chainId, gasUsed, gasLimitNoBuffer, txParams } = transaction;\n const { gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;\n const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';\n\n return calculateGasCost({\n chainId,\n gas: finalGas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n messenger,\n });\n}\n\n/**\n * Calculate the gas cost for the specified parameters.\n *\n * @param request - Gas cost calculation parameters.\n * @param request.chainId - ID of the chain.\n * @param request.gas - Amount of gas the transaction will use.\n * @param request.maxFeePerGas - Max fee to pay per gas.\n * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.\n * @param request.messenger - Controller messenger.\n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateGasCost(request: {\n chainId: number | Hex;\n gas: BigNumber.Value;\n maxFeePerGas?: BigNumber.Value;\n maxPriorityFeePerGas?: BigNumber.Value;\n messenger: TransactionPayControllerMessenger;\n}): FiatValue {\n const {\n chainId: chainIdInput,\n gas,\n maxFeePerGas: maxFeePerGasInput,\n maxPriorityFeePerGas: maxPriorityFeePerGasInput,\n messenger,\n } = request;\n\n const chainId = toHex(chainIdInput);\n\n const {\n estimatedBaseFee,\n maxFeePerGas: maxFeePerGasEstimate,\n maxPriorityFeePerGas: maxPriorityFeePerGasEstimate,\n } = getGasFee(chainId, messenger);\n\n const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;\n\n const maxPriorityFeePerGas =\n maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;\n\n const feePerGas =\n estimatedBaseFee && maxPriorityFeePerGas\n ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)\n : new BigNumber(maxFeePerGas || '0x0');\n\n const gasCostNative = new BigNumber(gas)\n .multipliedBy(feePerGas)\n .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 = gasCostNative.multipliedBy(fiatRate.usdRate).toString(10);\n const fiat = gasCostNative.multipliedBy(fiatRate.fiatRate).toString(10);\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 */\nfunction getGasFee(chainId: Hex, messenger: TransactionPayControllerMessenger) {\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"]}
1
+ {"version":3,"file":"gas.cjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAInD,+CAAyC;AAEzC,uCAKiB;AAEjB,0CAA8D;AAG9D,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,KAAK,CAAC,CAAC;AAErD;;;;;;;;;GASG;AACH,SAAgB,2BAA2B,CACzC,WAA4B,EAC5B,SAA4C,EAC5C,EAAE,KAAK,KAA0B,EAAE;IAEnC,MAAM,EACJ,OAAO,EACP,OAAO,EAAE,eAAe,EACxB,gBAAgB,EAChB,QAAQ,GACT,GAAG,WAAW,CAAC;IAEhB,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,oBAAoB,EAAE,GAAG,QAAQ,CAAC;IACnE,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC;IACpD,MAAM,QAAQ,GAAG,OAAO,IAAI,gBAAgB,IAAI,GAAG,IAAI,KAAK,CAAC;IAE7D,MAAM,MAAM,GAAG,gBAAgB,CAAC;QAC9B,OAAO;QACP,GAAG,EAAE,QAAQ;QACb,KAAK;QACL,YAAY;QACZ,oBAAoB;QACpB,SAAS;KACV,CAAC,CAAC;IAEH,MAAM,GAAG,GAAG,gBAAgB,CAAC;QAC3B,O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G,CAAC,yBAAyB,EAAE;YAC7B,YAAY;YACZ,mBAAmB;SACpB,CAAC,CAAC;QAEH,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,SAAS,GAAG,IAAA,oBAAY,EAAC,SAAS,EAAE,mBAAmB,EAAE,OAAO,CAAC,CAAC;IAExE,MAAM,aAAa,GAAG,IAAA,wBAAgB,EACpC,SAAS,EACT,mBAAmB,EACnB,OAAO,CACR,CAAC;IAEF,IAAI,CAAC,aAAa,IAAI,CAAC,SAAS,EAAE,CAAC;QACjC,GAAG,CAAC,+BAA+B,CAAC,CAAC;QACrC,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,wBAAS,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACnD,MAAM,GAAG,GAAG,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAElC,MAAM,UAAU,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IAC3D,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEtC,MAAM,IAAI,GAAG,UAAU,CAAC,YAAY,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC1E,MAAM,GAAG,GAAG,UAAU,CAAC,YAAY,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAExE,OAAO;QACL,aAAa,EAAE,IAAI;QACnB,IAAI;QACJ,KAAK;QACL,GAAG;QACH,GAAG;KACJ,CAAC;AACJ,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { GasFeeEstimates } from '@metamask/gas-fee-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport {\n getNativeToken,\n getTokenBalance,\n getTokenFiatRate,\n getTokenInfo,\n} from './token';\nimport type { TransactionPayControllerMessenger } from '..';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { Amount } 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 * @param options - Calculation options.\n * @param options.isMax - Whether to calculate the maximum fee.\n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateTransactionGasCost(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n { isMax }: { isMax?: boolean } = {},\n): Amount & { isGasFeeToken?: boolean } {\n const {\n chainId,\n gasUsed: gasUsedOriginal,\n gasLimitNoBuffer,\n txParams,\n } = transaction;\n\n const { from, gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;\n const gasUsed = isMax ? undefined : gasUsedOriginal;\n const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';\n\n const result = calculateGasCost({\n chainId,\n gas: finalGas,\n isMax,\n maxFeePerGas,\n maxPriorityFeePerGas,\n messenger,\n });\n\n const max = calculateGasCost({\n chainId,\n gas: finalGas,\n isMax: true,\n messenger,\n });\n\n const nativeBalance = getTokenBalance(\n messenger,\n from as Hex,\n chainId,\n getNativeToken(chainId),\n );\n\n const hasBalance = new BigNumber(nativeBalance).gte(max.raw);\n\n const gasFeeTokenCost = calculateGasFeeTokenCost({\n hasBalance,\n messenger,\n transaction,\n });\n\n if (gasFeeTokenCost) {\n return gasFeeTokenCost;\n }\n\n return result;\n}\n\n/**\n * Calculate the gas cost for the specified parameters.\n *\n * @param request - Gas cost calculation parameters.\n * @param request.chainId - ID of the chain.\n * @param request.gas - Amount of gas the transaction will use.\n * @param request.isMax - Whether to calculate the maximum fee.\n * @param request.maxFeePerGas - Max fee to pay per gas.\n * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.\n * @param request.messenger - Controller messenger.\n \n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateGasCost(request: {\n chainId: number | Hex;\n gas: BigNumber.Value;\n isMax?: boolean;\n maxFeePerGas?: BigNumber.Value;\n maxPriorityFeePerGas?: BigNumber.Value;\n messenger: TransactionPayControllerMessenger;\n}): Amount {\n const {\n chainId: chainIdInput,\n gas,\n isMax,\n maxFeePerGas: maxFeePerGasInput,\n maxPriorityFeePerGas: maxPriorityFeePerGasInput,\n messenger,\n } = request;\n\n const chainId = toHex(chainIdInput);\n\n const {\n estimatedBaseFee,\n maxFeePerGas: maxFeePerGasEstimate,\n maxPriorityFeePerGas: maxPriorityFeePerGasEstimate,\n } = getGasFee(chainId, messenger);\n\n const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;\n\n const maxPriorityFeePerGas =\n maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;\n\n const feePerGas =\n estimatedBaseFee && maxPriorityFeePerGas && !isMax\n ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)\n : new BigNumber(maxFeePerGas || '0x0');\n\n const rawValue = new BigNumber(gas).multipliedBy(feePerGas);\n const raw = rawValue.toString(10);\n\n const humanValue = rawValue.shiftedBy(-18);\n const human = humanValue.toString(10);\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 = humanValue.multipliedBy(fiatRate.usdRate).toString(10);\n const fiat = humanValue.multipliedBy(fiatRate.fiatRate).toString(10);\n\n return {\n fiat,\n human,\n raw,\n usd,\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 */\nfunction getGasFee(chainId: Hex, messenger: TransactionPayControllerMessenger) {\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\n/**\n * Calculate the cost of a gas fee token on a transaction.\n *\n * @param request - Request parameters.\n * @param request.hasBalance - Whether the user has enough balance to cover the gas fee.\n * @param request.messenger - Controller messenger.\n * @param request.transaction - Transaction to calculate gas fee token cost for.\n * @returns Cost of the gas fee token.\n */\nfunction calculateGasFeeTokenCost({\n hasBalance,\n messenger,\n transaction,\n}: {\n hasBalance: boolean;\n messenger: TransactionPayControllerMessenger;\n transaction: TransactionMeta;\n}): (Amount & { isGasFeeToken?: boolean }) | undefined {\n const {\n chainId,\n gasFeeTokens,\n isGasFeeTokenIgnoredIfBalance,\n selectedGasFeeToken,\n } = transaction;\n\n if (\n !gasFeeTokens ||\n !selectedGasFeeToken ||\n (isGasFeeTokenIgnoredIfBalance && hasBalance)\n ) {\n return undefined;\n }\n\n log('Calculating gas fee token cost', { selectedGasFeeToken, chainId });\n\n const gasFeeToken = gasFeeTokens?.find(\n (t) => t.tokenAddress.toLowerCase() === selectedGasFeeToken.toLowerCase(),\n );\n\n if (!gasFeeToken) {\n log('Gas fee token not found', {\n gasFeeTokens,\n selectedGasFeeToken,\n });\n\n return undefined;\n }\n\n const tokenInfo = getTokenInfo(messenger, selectedGasFeeToken, chainId);\n\n const tokenFiatRate = getTokenFiatRate(\n messenger,\n selectedGasFeeToken,\n chainId,\n );\n\n if (!tokenFiatRate || !tokenInfo) {\n log('Cannot get gas fee token info');\n return undefined;\n }\n\n const rawValue = new BigNumber(gasFeeToken.amount);\n const raw = rawValue.toString(10);\n\n const humanValue = rawValue.shiftedBy(-tokenInfo.decimals);\n const human = humanValue.toString(10);\n\n const fiat = humanValue.multipliedBy(tokenFiatRate.fiatRate).toString(10);\n const usd = humanValue.multipliedBy(tokenFiatRate.usdRate).toString(10);\n\n return {\n isGasFeeToken: true,\n fiat,\n human,\n raw,\n usd,\n };\n}\n"]}
@@ -2,32 +2,41 @@ import type { TransactionMeta } from "@metamask/transaction-controller";
2
2
  import type { Hex } from "@metamask/utils";
3
3
  import { BigNumber } from "bignumber.js";
4
4
  import type { TransactionPayControllerMessenger } from "../index.cjs";
5
- import type { FiatValue } from "../types.cjs";
5
+ import type { Amount } from "../types.cjs";
6
6
  /**
7
7
  *
8
8
  * Calculate the estimated gas cost for a given transaction in fiat.
9
9
  *
10
10
  * @param transaction - Transaction to calculate gas cost for
11
11
  * @param messenger - Controller messenger.
12
+ * @param options - Calculation options.
13
+ * @param options.isMax - Whether to calculate the maximum fee.
12
14
  * @returns Estimated gas cost for the transaction.
13
15
  */
14
- export declare function calculateTransactionGasCost(transaction: TransactionMeta, messenger: TransactionPayControllerMessenger): FiatValue;
16
+ export declare function calculateTransactionGasCost(transaction: TransactionMeta, messenger: TransactionPayControllerMessenger, { isMax }?: {
17
+ isMax?: boolean;
18
+ }): Amount & {
19
+ isGasFeeToken?: boolean;
20
+ };
15
21
  /**
16
22
  * Calculate the gas cost for the specified parameters.
17
23
  *
18
24
  * @param request - Gas cost calculation parameters.
19
25
  * @param request.chainId - ID of the chain.
20
26
  * @param request.gas - Amount of gas the transaction will use.
27
+ * @param request.isMax - Whether to calculate the maximum fee.
21
28
  * @param request.maxFeePerGas - Max fee to pay per gas.
22
29
  * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.
23
30
  * @param request.messenger - Controller messenger.
31
+
24
32
  * @returns Estimated gas cost for the transaction.
25
33
  */
26
34
  export declare function calculateGasCost(request: {
27
35
  chainId: number | Hex;
28
36
  gas: BigNumber.Value;
37
+ isMax?: boolean;
29
38
  maxFeePerGas?: BigNumber.Value;
30
39
  maxPriorityFeePerGas?: BigNumber.Value;
31
40
  messenger: TransactionPayControllerMessenger;
32
- }): FiatValue;
41
+ }): Amount;
33
42
  //# sourceMappingURL=gas.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.d.cts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAC3C,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAGzC,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAW;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,qBAAiB;AAE1C;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,eAAe,EAC5B,SAAS,EAAE,iCAAiC,GAC3C,SAAS,CAYX;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IACtB,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IAC/B,oBAAoB,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,GAAG,SAAS,CAgDZ"}
1
+ {"version":3,"file":"gas.d.cts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAC3C,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAQzC,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAW;AAE5D,OAAO,KAAK,EAAE,MAAM,EAAE,qBAAiB;AAIvC;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,eAAe,EAC5B,SAAS,EAAE,iCAAiC,EAC5C,EAAE,KAAK,EAAE,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAO,GAClC,MAAM,GAAG;IAAE,aAAa,CAAC,EAAE,OAAO,CAAA;CAAE,CAgDtC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IACtB,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IAC/B,oBAAoB,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,GAAG,MAAM,CAqDT"}
@@ -2,32 +2,41 @@ import type { TransactionMeta } from "@metamask/transaction-controller";
2
2
  import type { Hex } from "@metamask/utils";
3
3
  import { BigNumber } from "bignumber.js";
4
4
  import type { TransactionPayControllerMessenger } from "../index.mjs";
5
- import type { FiatValue } from "../types.mjs";
5
+ import type { Amount } from "../types.mjs";
6
6
  /**
7
7
  *
8
8
  * Calculate the estimated gas cost for a given transaction in fiat.
9
9
  *
10
10
  * @param transaction - Transaction to calculate gas cost for
11
11
  * @param messenger - Controller messenger.
12
+ * @param options - Calculation options.
13
+ * @param options.isMax - Whether to calculate the maximum fee.
12
14
  * @returns Estimated gas cost for the transaction.
13
15
  */
14
- export declare function calculateTransactionGasCost(transaction: TransactionMeta, messenger: TransactionPayControllerMessenger): FiatValue;
16
+ export declare function calculateTransactionGasCost(transaction: TransactionMeta, messenger: TransactionPayControllerMessenger, { isMax }?: {
17
+ isMax?: boolean;
18
+ }): Amount & {
19
+ isGasFeeToken?: boolean;
20
+ };
15
21
  /**
16
22
  * Calculate the gas cost for the specified parameters.
17
23
  *
18
24
  * @param request - Gas cost calculation parameters.
19
25
  * @param request.chainId - ID of the chain.
20
26
  * @param request.gas - Amount of gas the transaction will use.
27
+ * @param request.isMax - Whether to calculate the maximum fee.
21
28
  * @param request.maxFeePerGas - Max fee to pay per gas.
22
29
  * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.
23
30
  * @param request.messenger - Controller messenger.
31
+
24
32
  * @returns Estimated gas cost for the transaction.
25
33
  */
26
34
  export declare function calculateGasCost(request: {
27
35
  chainId: number | Hex;
28
36
  gas: BigNumber.Value;
37
+ isMax?: boolean;
29
38
  maxFeePerGas?: BigNumber.Value;
30
39
  maxPriorityFeePerGas?: BigNumber.Value;
31
40
  messenger: TransactionPayControllerMessenger;
32
- }): FiatValue;
41
+ }): Amount;
33
42
  //# sourceMappingURL=gas.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.d.mts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAC3C,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAGzC,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAW;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,qBAAiB;AAE1C;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,eAAe,EAC5B,SAAS,EAAE,iCAAiC,GAC3C,SAAS,CAYX;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IACtB,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IAC/B,oBAAoB,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,GAAG,SAAS,CAgDZ"}
1
+ {"version":3,"file":"gas.d.mts","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AACxE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAC3C,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAQzC,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAW;AAE5D,OAAO,KAAK,EAAE,MAAM,EAAE,qBAAiB;AAIvC;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,eAAe,EAC5B,SAAS,EAAE,iCAAiC,EAC5C,EAAE,KAAK,EAAE,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAO,GAClC,MAAM,GAAG;IAAE,aAAa,CAAC,EAAE,OAAO,CAAA;CAAE,CAgDtC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IACxC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;IACtB,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IAC/B,oBAAoB,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,GAAG,MAAM,CAqDT"}
@@ -1,25 +1,48 @@
1
1
  import { toHex } from "@metamask/controller-utils";
2
2
  import { BigNumber } from "bignumber.js";
3
- import { getNativeToken, getTokenFiatRate } from "./token.mjs";
3
+ import { getNativeToken, getTokenBalance, getTokenFiatRate, getTokenInfo } from "./token.mjs";
4
+ import { createModuleLogger, projectLogger } from "../logger.mjs";
5
+ const log = createModuleLogger(projectLogger, 'gas');
4
6
  /**
5
7
  *
6
8
  * Calculate the estimated gas cost for a given transaction in fiat.
7
9
  *
8
10
  * @param transaction - Transaction to calculate gas cost for
9
11
  * @param messenger - Controller messenger.
12
+ * @param options - Calculation options.
13
+ * @param options.isMax - Whether to calculate the maximum fee.
10
14
  * @returns Estimated gas cost for the transaction.
11
15
  */
12
- export function calculateTransactionGasCost(transaction, messenger) {
13
- const { chainId, gasUsed, gasLimitNoBuffer, txParams } = transaction;
14
- const { gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;
16
+ export function calculateTransactionGasCost(transaction, messenger, { isMax } = {}) {
17
+ const { chainId, gasUsed: gasUsedOriginal, gasLimitNoBuffer, txParams, } = transaction;
18
+ const { from, gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;
19
+ const gasUsed = isMax ? undefined : gasUsedOriginal;
15
20
  const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';
16
- return calculateGasCost({
21
+ const result = calculateGasCost({
17
22
  chainId,
18
23
  gas: finalGas,
24
+ isMax,
19
25
  maxFeePerGas,
20
26
  maxPriorityFeePerGas,
21
27
  messenger,
22
28
  });
29
+ const max = calculateGasCost({
30
+ chainId,
31
+ gas: finalGas,
32
+ isMax: true,
33
+ messenger,
34
+ });
35
+ const nativeBalance = getTokenBalance(messenger, from, chainId, getNativeToken(chainId));
36
+ const hasBalance = new BigNumber(nativeBalance).gte(max.raw);
37
+ const gasFeeTokenCost = calculateGasFeeTokenCost({
38
+ hasBalance,
39
+ messenger,
40
+ transaction,
41
+ });
42
+ if (gasFeeTokenCost) {
43
+ return gasFeeTokenCost;
44
+ }
45
+ return result;
23
46
  }
24
47
  /**
25
48
  * Calculate the gas cost for the specified parameters.
@@ -27,32 +50,37 @@ export function calculateTransactionGasCost(transaction, messenger) {
27
50
  * @param request - Gas cost calculation parameters.
28
51
  * @param request.chainId - ID of the chain.
29
52
  * @param request.gas - Amount of gas the transaction will use.
53
+ * @param request.isMax - Whether to calculate the maximum fee.
30
54
  * @param request.maxFeePerGas - Max fee to pay per gas.
31
55
  * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.
32
56
  * @param request.messenger - Controller messenger.
57
+
33
58
  * @returns Estimated gas cost for the transaction.
34
59
  */
35
60
  export function calculateGasCost(request) {
36
- const { chainId: chainIdInput, gas, maxFeePerGas: maxFeePerGasInput, maxPriorityFeePerGas: maxPriorityFeePerGasInput, messenger, } = request;
61
+ const { chainId: chainIdInput, gas, isMax, maxFeePerGas: maxFeePerGasInput, maxPriorityFeePerGas: maxPriorityFeePerGasInput, messenger, } = request;
37
62
  const chainId = toHex(chainIdInput);
38
63
  const { estimatedBaseFee, maxFeePerGas: maxFeePerGasEstimate, maxPriorityFeePerGas: maxPriorityFeePerGasEstimate, } = getGasFee(chainId, messenger);
39
64
  const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;
40
65
  const maxPriorityFeePerGas = maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;
41
- const feePerGas = estimatedBaseFee && maxPriorityFeePerGas
66
+ const feePerGas = estimatedBaseFee && maxPriorityFeePerGas && !isMax
42
67
  ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)
43
68
  : new BigNumber(maxFeePerGas || '0x0');
44
- const gasCostNative = new BigNumber(gas)
45
- .multipliedBy(feePerGas)
46
- .shiftedBy(-18);
69
+ const rawValue = new BigNumber(gas).multipliedBy(feePerGas);
70
+ const raw = rawValue.toString(10);
71
+ const humanValue = rawValue.shiftedBy(-18);
72
+ const human = humanValue.toString(10);
47
73
  const fiatRate = getTokenFiatRate(messenger, getNativeToken(chainId), chainId);
48
74
  if (!fiatRate) {
49
75
  throw new Error('Could not fetch fiat rate for native token');
50
76
  }
51
- const usd = gasCostNative.multipliedBy(fiatRate.usdRate).toString(10);
52
- const fiat = gasCostNative.multipliedBy(fiatRate.fiatRate).toString(10);
77
+ const usd = humanValue.multipliedBy(fiatRate.usdRate).toString(10);
78
+ const fiat = humanValue.multipliedBy(fiatRate.fiatRate).toString(10);
53
79
  return {
54
- usd,
55
80
  fiat,
81
+ human,
82
+ raw,
83
+ usd,
56
84
  };
57
85
  }
58
86
  /**
@@ -79,4 +107,49 @@ function getGasFee(chainId, messenger) {
79
107
  : undefined;
80
108
  return { estimatedBaseFee, maxFeePerGas, maxPriorityFeePerGas };
81
109
  }
110
+ /**
111
+ * Calculate the cost of a gas fee token on a transaction.
112
+ *
113
+ * @param request - Request parameters.
114
+ * @param request.hasBalance - Whether the user has enough balance to cover the gas fee.
115
+ * @param request.messenger - Controller messenger.
116
+ * @param request.transaction - Transaction to calculate gas fee token cost for.
117
+ * @returns Cost of the gas fee token.
118
+ */
119
+ function calculateGasFeeTokenCost({ hasBalance, messenger, transaction, }) {
120
+ const { chainId, gasFeeTokens, isGasFeeTokenIgnoredIfBalance, selectedGasFeeToken, } = transaction;
121
+ if (!gasFeeTokens ||
122
+ !selectedGasFeeToken ||
123
+ (isGasFeeTokenIgnoredIfBalance && hasBalance)) {
124
+ return undefined;
125
+ }
126
+ log('Calculating gas fee token cost', { selectedGasFeeToken, chainId });
127
+ const gasFeeToken = gasFeeTokens?.find((t) => t.tokenAddress.toLowerCase() === selectedGasFeeToken.toLowerCase());
128
+ if (!gasFeeToken) {
129
+ log('Gas fee token not found', {
130
+ gasFeeTokens,
131
+ selectedGasFeeToken,
132
+ });
133
+ return undefined;
134
+ }
135
+ const tokenInfo = getTokenInfo(messenger, selectedGasFeeToken, chainId);
136
+ const tokenFiatRate = getTokenFiatRate(messenger, selectedGasFeeToken, chainId);
137
+ if (!tokenFiatRate || !tokenInfo) {
138
+ log('Cannot get gas fee token info');
139
+ return undefined;
140
+ }
141
+ const rawValue = new BigNumber(gasFeeToken.amount);
142
+ const raw = rawValue.toString(10);
143
+ const humanValue = rawValue.shiftedBy(-tokenInfo.decimals);
144
+ const human = humanValue.toString(10);
145
+ const fiat = humanValue.multipliedBy(tokenFiatRate.fiatRate).toString(10);
146
+ const usd = humanValue.multipliedBy(tokenFiatRate.usdRate).toString(10);
147
+ return {
148
+ isGasFeeToken: true,
149
+ fiat,
150
+ human,
151
+ raw,
152
+ usd,
153
+ };
154
+ }
82
155
  //# sourceMappingURL=gas.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"gas.mjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAInD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,oBAAgB;AAI3D;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CACzC,WAA4B,EAC5B,SAA4C;IAE5C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAC;IACrE,MAAM,EAAE,GAAG,EAAE,YAAY,EAAE,oBAAoB,EAAE,GAAG,QAAQ,CAAC;IAC7D,MAAM,QAAQ,GAAG,OAAO,IAAI,gBAAgB,IAAI,GAAG,IAAI,KAAK,CAAC;IAE7D,OAAO,gBAAgB,CAAC;QACtB,OAAO;QACP,GAAG,EAAE,QAAQ;QACb,YAAY;QACZ,oBAAoB;QACpB,SAAS;KACV,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAMhC;IACC,MAAM,EACJ,OAAO,EAAE,YAAY,EACrB,GAAG,EACH,YAAY,EAAE,iBAAiB,EAC/B,oBAAoB,EAAE,yBAAyB,EAC/C,SAAS,GACV,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;IAEpC,MAAM,EACJ,gBAAgB,EAChB,YAAY,EAAE,oBAAoB,EAClC,oBAAoB,EAAE,4BAA4B,GACnD,GAAG,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAElC,MAAM,YAAY,GAAG,iBAAiB,IAAI,oBAAoB,CAAC;IAE/D,MAAM,oBAAoB,GACxB,yBAAyB,IAAI,4BAA4B,C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AAM,gBAAgB,GAAG,oBAAoB;QAC3C,CAAC,CAAC,IAAI,SAAS,CAAC,oBAAoB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC/D,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,YAAY,GAAG,gBAAgB;QACnC,CAAC,CAAC,IAAI,SAAS,CAAC,gBAAgB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC3D,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,oBAAoB,GAAG,wBAAwB;QACnD,CAAC,CAAC,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnE,CAAC,CAAC,SAAS,CAAC;IAEd,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,oBAAoB,EAAE,CAAC;AAClE,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { GasFeeEstimates } from '@metamask/gas-fee-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getNativeToken, getTokenFiatRate } from './token';\nimport type { TransactionPayControllerMessenger } from '..';\nimport type { FiatValue } from '../types';\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 calculateTransactionGasCost(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): FiatValue {\n const { chainId, gasUsed, gasLimitNoBuffer, txParams } = transaction;\n const { gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;\n const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';\n\n return calculateGasCost({\n chainId,\n gas: finalGas,\n maxFeePerGas,\n maxPriorityFeePerGas,\n messenger,\n });\n}\n\n/**\n * Calculate the gas cost for the specified parameters.\n *\n * @param request - Gas cost calculation parameters.\n * @param request.chainId - ID of the chain.\n * @param request.gas - Amount of gas the transaction will use.\n * @param request.maxFeePerGas - Max fee to pay per gas.\n * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.\n * @param request.messenger - Controller messenger.\n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateGasCost(request: {\n chainId: number | Hex;\n gas: BigNumber.Value;\n maxFeePerGas?: BigNumber.Value;\n maxPriorityFeePerGas?: BigNumber.Value;\n messenger: TransactionPayControllerMessenger;\n}): FiatValue {\n const {\n chainId: chainIdInput,\n gas,\n maxFeePerGas: maxFeePerGasInput,\n maxPriorityFeePerGas: maxPriorityFeePerGasInput,\n messenger,\n } = request;\n\n const chainId = toHex(chainIdInput);\n\n const {\n estimatedBaseFee,\n maxFeePerGas: maxFeePerGasEstimate,\n maxPriorityFeePerGas: maxPriorityFeePerGasEstimate,\n } = getGasFee(chainId, messenger);\n\n const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;\n\n const maxPriorityFeePerGas =\n maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;\n\n const feePerGas =\n estimatedBaseFee && maxPriorityFeePerGas\n ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)\n : new BigNumber(maxFeePerGas || '0x0');\n\n const gasCostNative = new BigNumber(gas)\n .multipliedBy(feePerGas)\n .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 = gasCostNative.multipliedBy(fiatRate.usdRate).toString(10);\n const fiat = gasCostNative.multipliedBy(fiatRate.fiatRate).toString(10);\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 */\nfunction getGasFee(chainId: Hex, messenger: TransactionPayControllerMessenger) {\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"]}
1
+ {"version":3,"file":"gas.mjs","sourceRoot":"","sources":["../../src/utils/gas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAInD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EACL,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACb,oBAAgB;AAEjB,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,sBAAkB;AAG9D,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;AAErD;;;;;;;;;GASG;AACH,MAAM,UAAU,2BAA2B,CACzC,WAA4B,EAC5B,SAA4C,EAC5C,EAAE,KAAK,KAA0B,EAAE;IAEnC,MAAM,EACJ,OAAO,EACP,OAAO,EAAE,eAAe,EACxB,gBAAgB,EAChB,QAAQ,GACT,GAAG,WAAW,CAAC;IAEhB,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,oBAAoB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ACL,aAAa,EAAE,IAAI;QACnB,IAAI;QACJ,KAAK;QACL,GAAG;QACH,GAAG;KACJ,CAAC;AACJ,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { GasFeeEstimates } from '@metamask/gas-fee-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport {\n getNativeToken,\n getTokenBalance,\n getTokenFiatRate,\n getTokenInfo,\n} from './token';\nimport type { TransactionPayControllerMessenger } from '..';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { Amount } 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 * @param options - Calculation options.\n * @param options.isMax - Whether to calculate the maximum fee.\n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateTransactionGasCost(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n { isMax }: { isMax?: boolean } = {},\n): Amount & { isGasFeeToken?: boolean } {\n const {\n chainId,\n gasUsed: gasUsedOriginal,\n gasLimitNoBuffer,\n txParams,\n } = transaction;\n\n const { from, gas, maxFeePerGas, maxPriorityFeePerGas } = txParams;\n const gasUsed = isMax ? undefined : gasUsedOriginal;\n const finalGas = gasUsed || gasLimitNoBuffer || gas || '0x0';\n\n const result = calculateGasCost({\n chainId,\n gas: finalGas,\n isMax,\n maxFeePerGas,\n maxPriorityFeePerGas,\n messenger,\n });\n\n const max = calculateGasCost({\n chainId,\n gas: finalGas,\n isMax: true,\n messenger,\n });\n\n const nativeBalance = getTokenBalance(\n messenger,\n from as Hex,\n chainId,\n getNativeToken(chainId),\n );\n\n const hasBalance = new BigNumber(nativeBalance).gte(max.raw);\n\n const gasFeeTokenCost = calculateGasFeeTokenCost({\n hasBalance,\n messenger,\n transaction,\n });\n\n if (gasFeeTokenCost) {\n return gasFeeTokenCost;\n }\n\n return result;\n}\n\n/**\n * Calculate the gas cost for the specified parameters.\n *\n * @param request - Gas cost calculation parameters.\n * @param request.chainId - ID of the chain.\n * @param request.gas - Amount of gas the transaction will use.\n * @param request.isMax - Whether to calculate the maximum fee.\n * @param request.maxFeePerGas - Max fee to pay per gas.\n * @param request.maxPriorityFeePerGas - Max priority fee to pay per gas.\n * @param request.messenger - Controller messenger.\n \n * @returns Estimated gas cost for the transaction.\n */\nexport function calculateGasCost(request: {\n chainId: number | Hex;\n gas: BigNumber.Value;\n isMax?: boolean;\n maxFeePerGas?: BigNumber.Value;\n maxPriorityFeePerGas?: BigNumber.Value;\n messenger: TransactionPayControllerMessenger;\n}): Amount {\n const {\n chainId: chainIdInput,\n gas,\n isMax,\n maxFeePerGas: maxFeePerGasInput,\n maxPriorityFeePerGas: maxPriorityFeePerGasInput,\n messenger,\n } = request;\n\n const chainId = toHex(chainIdInput);\n\n const {\n estimatedBaseFee,\n maxFeePerGas: maxFeePerGasEstimate,\n maxPriorityFeePerGas: maxPriorityFeePerGasEstimate,\n } = getGasFee(chainId, messenger);\n\n const maxFeePerGas = maxFeePerGasInput || maxFeePerGasEstimate;\n\n const maxPriorityFeePerGas =\n maxPriorityFeePerGasInput || maxPriorityFeePerGasEstimate;\n\n const feePerGas =\n estimatedBaseFee && maxPriorityFeePerGas && !isMax\n ? new BigNumber(estimatedBaseFee).plus(maxPriorityFeePerGas)\n : new BigNumber(maxFeePerGas || '0x0');\n\n const rawValue = new BigNumber(gas).multipliedBy(feePerGas);\n const raw = rawValue.toString(10);\n\n const humanValue = rawValue.shiftedBy(-18);\n const human = humanValue.toString(10);\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 = humanValue.multipliedBy(fiatRate.usdRate).toString(10);\n const fiat = humanValue.multipliedBy(fiatRate.fiatRate).toString(10);\n\n return {\n fiat,\n human,\n raw,\n usd,\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 */\nfunction getGasFee(chainId: Hex, messenger: TransactionPayControllerMessenger) {\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\n/**\n * Calculate the cost of a gas fee token on a transaction.\n *\n * @param request - Request parameters.\n * @param request.hasBalance - Whether the user has enough balance to cover the gas fee.\n * @param request.messenger - Controller messenger.\n * @param request.transaction - Transaction to calculate gas fee token cost for.\n * @returns Cost of the gas fee token.\n */\nfunction calculateGasFeeTokenCost({\n hasBalance,\n messenger,\n transaction,\n}: {\n hasBalance: boolean;\n messenger: TransactionPayControllerMessenger;\n transaction: TransactionMeta;\n}): (Amount & { isGasFeeToken?: boolean }) | undefined {\n const {\n chainId,\n gasFeeTokens,\n isGasFeeTokenIgnoredIfBalance,\n selectedGasFeeToken,\n } = transaction;\n\n if (\n !gasFeeTokens ||\n !selectedGasFeeToken ||\n (isGasFeeTokenIgnoredIfBalance && hasBalance)\n ) {\n return undefined;\n }\n\n log('Calculating gas fee token cost', { selectedGasFeeToken, chainId });\n\n const gasFeeToken = gasFeeTokens?.find(\n (t) => t.tokenAddress.toLowerCase() === selectedGasFeeToken.toLowerCase(),\n );\n\n if (!gasFeeToken) {\n log('Gas fee token not found', {\n gasFeeTokens,\n selectedGasFeeToken,\n });\n\n return undefined;\n }\n\n const tokenInfo = getTokenInfo(messenger, selectedGasFeeToken, chainId);\n\n const tokenFiatRate = getTokenFiatRate(\n messenger,\n selectedGasFeeToken,\n chainId,\n );\n\n if (!tokenFiatRate || !tokenInfo) {\n log('Cannot get gas fee token info');\n return undefined;\n }\n\n const rawValue = new BigNumber(gasFeeToken.amount);\n const raw = rawValue.toString(10);\n\n const humanValue = rawValue.shiftedBy(-tokenInfo.decimals);\n const human = humanValue.toString(10);\n\n const fiat = humanValue.multipliedBy(tokenFiatRate.fiatRate).toString(10);\n const usd = humanValue.multipliedBy(tokenFiatRate.usdRate).toString(10);\n\n return {\n isGasFeeToken: true,\n fiat,\n human,\n raw,\n usd,\n };\n}\n"]}
@@ -90,7 +90,7 @@ function syncTransaction({ batchTransactions, messenger, paymentToken, totals, t
90
90
  tx.metamaskPay = {
91
91
  bridgeFeeFiat: totals.fees.provider.usd,
92
92
  chainId: paymentToken.chainId,
93
- networkFeeFiat: totals.fees.sourceNetwork.usd,
93
+ networkFeeFiat: totals.fees.sourceNetwork.estimate.usd,
94
94
  tokenAddress: paymentToken.address,
95
95
  totalFiat: totals.total.usd,
96
96
  };
@@ -176,7 +176,7 @@ function buildQuoteRequests({ from, paymentToken, sourceAmounts, tokens, transac
176
176
  */
177
177
  async function getQuotes(transaction, requests, messenger) {
178
178
  const { id: transactionId } = transaction;
179
- const strategy = await (0, strategy_1.getStrategy)(messenger, transaction);
179
+ const strategy = (0, strategy_1.getStrategy)(messenger, transaction);
180
180
  let quotes = [];
181
181
  try {
182
182
  quotes = requests?.length