@metamask-previews/user-operation-controller 41.0.0-preview-159e76e4 → 41.0.0-preview-7cfbb337
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.
package/CHANGELOG.md
CHANGED
|
@@ -9,13 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
- Move peer dependencies for controller and service packages to direct dependencies ([#7209](https://github.com/MetaMask/core/pull/7209), [#7220](https://github.com/MetaMask/core/pull/7220), [#7236](https://github.com/MetaMask/core/pull/7236), [#7257](https://github.com/MetaMask/core/pull/7257), [#7258](https://github.com/MetaMask/core/pull/7258))
|
|
12
|
+
- Move peer dependencies for controller and service packages to direct dependencies ([#7209](https://github.com/MetaMask/core/pull/7209), [#7220](https://github.com/MetaMask/core/pull/7220), [#7236](https://github.com/MetaMask/core/pull/7236), [#7257](https://github.com/MetaMask/core/pull/7257), [#7258](https://github.com/MetaMask/core/pull/7258), [#7289](https://github.com/MetaMask/core/pull/7289))
|
|
13
13
|
- The dependencies moved are:
|
|
14
14
|
- `@metamask/approval-controller` (^8.0.0)
|
|
15
15
|
- `@metamask/gas-fee-controller` (^26.0.0)
|
|
16
16
|
- `@metamask/keyring-controller` (^25.0.0)
|
|
17
17
|
- `@metamask/network-controller` (^27.0.0)
|
|
18
|
-
- `@metamask/transaction-controller` (^62.
|
|
18
|
+
- `@metamask/transaction-controller` (^62.4.0)
|
|
19
19
|
- In clients, it is now possible for multiple versions of these packages to exist in the dependency tree.
|
|
20
20
|
- For example, this scenario would be valid: a client relies on `@metamask/controller-a` 1.0.0 and `@metamask/controller-b` 1.0.0, and `@metamask/controller-b` depends on `@metamask/controller-a` 1.1.0.
|
|
21
21
|
- Note, however, that the versions specified in the client's `package.json` always "win", and you are expected to keep them up to date so as not to break controller and service intercommunication.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file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AG,IAAA,aAAK,EAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAQ,CAAC;IAEhE,GAAG,CAAC,yCAAyC,EAAE,YAAY,CAAC,CAAC;IAE7D,OAAO,EAAE,YAAY,EAAE,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,IAAA,wBAAK,EAAC,IAAA,iCAAc,EAAC,KAAK,CAAC,CAAC,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CAAC,KAAK,IAAI,KAAK,KAAK,uBAAW,CAAC;AACzC,CAAC","sourcesContent":["import {\n gweiDecToWEIBN,\n ORIGIN_METAMASK,\n query,\n toHex,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport {\n GAS_ESTIMATE_TYPES,\n type GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { Provider } from '@metamask/network-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport { UserFeeLevel } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { add0x } from '@metamask/utils';\n\nimport { EMPTY_BYTES } from '../constants';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { UserOperation, UserOperationMetadata } from '../types';\nimport type { AddUserOperationRequest } from '../UserOperationController';\n\nconst log = createModuleLogger(projectLogger, 'gas-fees');\n\nexport type UpdateGasFeesRequest = {\n getGasFeeEstimates: () => Promise<GasFeeState>;\n metadata: UserOperationMetadata;\n originalRequest: AddUserOperationRequest;\n provider: Provider;\n transaction?: TransactionParams;\n};\n\ntype SuggestedGasFees = {\n maxFeePerGas?: string;\n maxPriorityFeePerGas?: string;\n};\n\n/**\n * Populates the gas fee properties for a user operation.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.metadata - The metadata for the user operation.\n * @param request.originalRequest - The original request to add the user operation.\n * @param request.provider - A provider to query the network.\n * @param request.transaction - The transaction that created the user operation.\n */\nexport async function updateGasFees(request: UpdateGasFeesRequest) {\n const { metadata, originalRequest, transaction } = request;\n const { userOperation } = metadata;\n let suggestedGasFees: SuggestedGasFees | undefined;\n\n const getGasFeeEstimates = async () => {\n if (!suggestedGasFees) {\n suggestedGasFees = await getSuggestedGasFees(request);\n }\n\n return suggestedGasFees;\n };\n\n userOperation.maxFeePerGas = await getMaxFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n transaction,\n );\n\n userOperation.maxPriorityFeePerGas = await getMaxPriorityFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n userOperation,\n transaction,\n );\n\n metadata.userFeeLevel = getUserFeeLevel(\n metadata,\n originalRequest,\n suggestedGasFees,\n transaction,\n );\n}\n\n/**\n * Gets the maxFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxFeePerGas for the user operation.\n */\nasync function getMaxFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n\n if (!isGasFeeEmpty(maxFeePerGas)) {\n log('Using maxFeePerGas from request', maxFeePerGas);\n return maxFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxPriorityFeePerGas) && gasPrice) {\n log('Setting maxFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxFeePerGas: suggestedMaxFeePerGas } = await getGetFasEstimates();\n\n if (!suggestedMaxFeePerGas) {\n throw new Error('Failed to get gas fee estimate for maxFeePerGas');\n }\n\n log('Using maxFeePerGas from estimate', suggestedMaxFeePerGas);\n\n return suggestedMaxFeePerGas;\n}\n\n/**\n * Gets the maxPriorityFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param userOperation - The user operation being updated.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxPriorityFeePerGas for the user operation.\n */\nasync function getMaxPriorityFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n userOperation: UserOperation,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n const { maxFeePerGas: updatedMaxFeePerGas } = userOperation;\n\n if (!isGasFeeEmpty(maxPriorityFeePerGas)) {\n log('Using maxPriorityFeePerGas from request', maxPriorityFeePerGas);\n return maxPriorityFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxFeePerGas) && gasPrice) {\n log('Setting maxPriorityFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas } =\n await getGetFasEstimates();\n\n if (suggestedMaxPriorityFeePerGas) {\n log(\n 'Using maxPriorityFeePerGas from estimate',\n suggestedMaxPriorityFeePerGas,\n );\n return suggestedMaxPriorityFeePerGas;\n }\n\n log('Setting maxPriorityFeePerGas to maxFeePerGas', updatedMaxFeePerGas);\n\n return updatedMaxFeePerGas;\n}\n\n/**\n * Gets the userFeeLevel for a user operation.\n *\n * @param metadata - The metadata for the user operation.\n * @param originalRequest - The original request to add the user operation.\n * @param suggestedGasFees - The suggested gas fees, if any.\n * @param transaction - The transaction that created the user operation.\n * @returns The userFeeLevel for the user operation.\n */\nfunction getUserFeeLevel(\n metadata: UserOperationMetadata,\n originalRequest: AddUserOperationRequest,\n suggestedGasFees?: SuggestedGasFees,\n transaction?: TransactionParams,\n): UserFeeLevel {\n const { origin } = metadata;\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n\n const {\n maxFeePerGas: suggestedMaxFeePerGas,\n maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas,\n } = suggestedGasFees || {};\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n transaction?.gasPrice\n ) {\n return origin === ORIGIN_METAMASK\n ? UserFeeLevel.CUSTOM\n : UserFeeLevel.DAPP_SUGGESTED;\n }\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n suggestedMaxFeePerGas &&\n suggestedMaxPriorityFeePerGas\n ) {\n return UserFeeLevel.MEDIUM;\n }\n\n if (origin === ORIGIN_METAMASK) {\n return UserFeeLevel.CUSTOM;\n }\n\n return UserFeeLevel.DAPP_SUGGESTED;\n}\n\n/**\n * Gets suggested gas fees.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.provider - A provider to query the network.\n * @returns The suggested gas fees.\n */\nasync function getSuggestedGasFees(\n request: UpdateGasFeesRequest,\n): Promise<{ maxFeePerGas?: Hex; maxPriorityFeePerGas?: Hex }> {\n const { getGasFeeEstimates, provider } = request;\n\n try {\n const { gasFeeEstimates, gasEstimateType } = await getGasFeeEstimates();\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n /* istanbul ignore next */\n const {\n medium: { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = {},\n } = gasFeeEstimates;\n\n if (suggestedMaxPriorityFeePerGas && suggestedMaxFeePerGas) {\n const values = {\n maxFeePerGas: gweiDecimalToWeiHex(suggestedMaxFeePerGas),\n maxPriorityFeePerGas: gweiDecimalToWeiHex(\n suggestedMaxPriorityFeePerGas,\n ),\n };\n\n log('Using medium values from fee market estimate', values);\n\n return values;\n }\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.medium);\n\n log('Using medium value from legacy estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.gasPrice);\n\n log('Using gasPrice from estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n } catch (error) {\n log('Failed to get estimate', error);\n }\n\n const gasPriceDecimal = (await query(new EthQuery(provider), 'gasPrice')) as\n | number\n | undefined;\n\n if (!gasPriceDecimal) {\n return {};\n }\n\n const maxFeePerGas = add0x(gasPriceDecimal.toString(16)) as Hex;\n\n log('Using gasPrice from network as fallback', maxFeePerGas);\n\n return { maxFeePerGas };\n}\n\n/**\n * Converts a GWEI decimal string to a WEI hexadecimal string.\n *\n * @param value - The GWEI decimal string to convert.\n * @returns The WEI hexadecimal string.\n */\nfunction gweiDecimalToWeiHex(value: string) {\n return toHex(gweiDecToWEIBN(value));\n}\n\n/**\n * Checks if a gas fee property is empty.\n *\n * @param value - The gas fee value to check.\n * @returns Whether the gas fee property is empty.\n */\nfunction isGasFeeEmpty(value?: string): boolean {\n return !value || value === EMPTY_BYTES;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"gas-fees.cjs","sourceRoot":"","sources":["../../src/utils/gas-fees.ts"],"names":[],"mappings":";;;;;;AAAA,iEAKoC;AACpC,oEAA2C;AAC3C,qEAGsC;AAGtC,6EAAgE;AAEhE,2CAAwC;AAExC,gDAA2C;AAC3C,0CAA8D;AAI9D,MAAM,GAAG,GAAG,IAAA,2BAAkB,EAAC,sBAAa,EAAE,UAAU,CAAC,CAAC;AAe1D;;;;;;;;;GASG;AACI,KAAK,UAAU,aAAa,CAAC,OAA6B;IAC/D,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAC3D,MAAM,EAAE,aAAa,EAAE,GAAG,QAAQ,CAAC;IACnC,IAAI,gBAA8C,CAAC;IAEnD,MAAM,kBAAkB,GAAG,KAAK,IAAI,EAAE;QACpC,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACtB,gBAAgB,GAAG,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AAe,CAAC,MAAM,CAAC,CAAC;YAEjE,GAAG,CAAC,yCAAyC,EAAE,YAAY,CAAC,CAAC;YAE7D,OAAO;gBACL,YAAY;aACb,CAAC;QACJ,CAAC;QAED,IAAI,eAAe,KAAK,uCAAkB,CAAC,YAAY,EAAE,CAAC;YACxD,MAAM,YAAY,GAAG,mBAAmB,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;YAEnE,GAAG,CAAC,8BAA8B,EAAE,YAAY,CAAC,CAAC;YAElD,OAAO;gBACL,YAAY;aACb,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IACvC,CAAC;IAED,MAAM,eAAe,GAAG,CAAC,MAAM,IAAA,wBAAK,EAAC,IAAI,mBAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAE3D,CAAC;IAEd,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,YAAY,GAAG,IAAA,aAAK,EAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;IAEzD,GAAG,CAAC,yCAAyC,EAAE,YAAY,CAAC,CAAC;IAE7D,OAAO,EAAE,YAAY,EAAE,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,IAAA,wBAAK,EAAC,IAAA,iCAAc,EAAC,KAAK,CAAC,CAAC,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CAAC,KAAK,IAAI,KAAK,KAAK,uBAAW,CAAC;AACzC,CAAC","sourcesContent":["import {\n gweiDecToWEIBN,\n ORIGIN_METAMASK,\n query,\n toHex,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport {\n GAS_ESTIMATE_TYPES,\n type GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { Provider } from '@metamask/network-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport { UserFeeLevel } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { add0x } from '@metamask/utils';\n\nimport { EMPTY_BYTES } from '../constants';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { UserOperation, UserOperationMetadata } from '../types';\nimport type { AddUserOperationRequest } from '../UserOperationController';\n\nconst log = createModuleLogger(projectLogger, 'gas-fees');\n\nexport type UpdateGasFeesRequest = {\n getGasFeeEstimates: () => Promise<GasFeeState>;\n metadata: UserOperationMetadata;\n originalRequest: AddUserOperationRequest;\n provider: Provider;\n transaction?: TransactionParams;\n};\n\ntype SuggestedGasFees = {\n maxFeePerGas?: string;\n maxPriorityFeePerGas?: string;\n};\n\n/**\n * Populates the gas fee properties for a user operation.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.metadata - The metadata for the user operation.\n * @param request.originalRequest - The original request to add the user operation.\n * @param request.provider - A provider to query the network.\n * @param request.transaction - The transaction that created the user operation.\n */\nexport async function updateGasFees(request: UpdateGasFeesRequest) {\n const { metadata, originalRequest, transaction } = request;\n const { userOperation } = metadata;\n let suggestedGasFees: SuggestedGasFees | undefined;\n\n const getGasFeeEstimates = async () => {\n if (!suggestedGasFees) {\n suggestedGasFees = await getSuggestedGasFees(request);\n }\n\n return suggestedGasFees;\n };\n\n userOperation.maxFeePerGas = await getMaxFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n transaction,\n );\n\n userOperation.maxPriorityFeePerGas = await getMaxPriorityFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n userOperation,\n transaction,\n );\n\n metadata.userFeeLevel = getUserFeeLevel(\n metadata,\n originalRequest,\n suggestedGasFees,\n transaction,\n );\n}\n\n/**\n * Gets the maxFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxFeePerGas for the user operation.\n */\nasync function getMaxFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n\n if (!isGasFeeEmpty(maxFeePerGas)) {\n log('Using maxFeePerGas from request', maxFeePerGas);\n return maxFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxPriorityFeePerGas) && gasPrice) {\n log('Setting maxFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxFeePerGas: suggestedMaxFeePerGas } = await getGetFasEstimates();\n\n if (!suggestedMaxFeePerGas) {\n throw new Error('Failed to get gas fee estimate for maxFeePerGas');\n }\n\n log('Using maxFeePerGas from estimate', suggestedMaxFeePerGas);\n\n return suggestedMaxFeePerGas;\n}\n\n/**\n * Gets the maxPriorityFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param userOperation - The user operation being updated.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxPriorityFeePerGas for the user operation.\n */\nasync function getMaxPriorityFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n userOperation: UserOperation,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n const { maxFeePerGas: updatedMaxFeePerGas } = userOperation;\n\n if (!isGasFeeEmpty(maxPriorityFeePerGas)) {\n log('Using maxPriorityFeePerGas from request', maxPriorityFeePerGas);\n return maxPriorityFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxFeePerGas) && gasPrice) {\n log('Setting maxPriorityFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas } =\n await getGetFasEstimates();\n\n if (suggestedMaxPriorityFeePerGas) {\n log(\n 'Using maxPriorityFeePerGas from estimate',\n suggestedMaxPriorityFeePerGas,\n );\n return suggestedMaxPriorityFeePerGas;\n }\n\n log('Setting maxPriorityFeePerGas to maxFeePerGas', updatedMaxFeePerGas);\n\n return updatedMaxFeePerGas;\n}\n\n/**\n * Gets the userFeeLevel for a user operation.\n *\n * @param metadata - The metadata for the user operation.\n * @param originalRequest - The original request to add the user operation.\n * @param suggestedGasFees - The suggested gas fees, if any.\n * @param transaction - The transaction that created the user operation.\n * @returns The userFeeLevel for the user operation.\n */\nfunction getUserFeeLevel(\n metadata: UserOperationMetadata,\n originalRequest: AddUserOperationRequest,\n suggestedGasFees?: SuggestedGasFees,\n transaction?: TransactionParams,\n): UserFeeLevel {\n const { origin } = metadata;\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n\n const {\n maxFeePerGas: suggestedMaxFeePerGas,\n maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas,\n } = suggestedGasFees || {};\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n transaction?.gasPrice\n ) {\n return origin === ORIGIN_METAMASK\n ? UserFeeLevel.CUSTOM\n : UserFeeLevel.DAPP_SUGGESTED;\n }\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n suggestedMaxFeePerGas &&\n suggestedMaxPriorityFeePerGas\n ) {\n return UserFeeLevel.MEDIUM;\n }\n\n if (origin === ORIGIN_METAMASK) {\n return UserFeeLevel.CUSTOM;\n }\n\n return UserFeeLevel.DAPP_SUGGESTED;\n}\n\n/**\n * Gets suggested gas fees.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.provider - A provider to query the network.\n * @returns The suggested gas fees.\n */\nasync function getSuggestedGasFees(\n request: UpdateGasFeesRequest,\n): Promise<{ maxFeePerGas?: Hex; maxPriorityFeePerGas?: Hex }> {\n const { getGasFeeEstimates, provider } = request;\n\n try {\n const { gasFeeEstimates, gasEstimateType } = await getGasFeeEstimates();\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n /* istanbul ignore next */\n const {\n medium: { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = {},\n } = gasFeeEstimates;\n\n if (suggestedMaxPriorityFeePerGas && suggestedMaxFeePerGas) {\n const values = {\n maxFeePerGas: gweiDecimalToWeiHex(suggestedMaxFeePerGas),\n maxPriorityFeePerGas: gweiDecimalToWeiHex(\n suggestedMaxPriorityFeePerGas,\n ),\n };\n\n log('Using medium values from fee market estimate', values);\n\n return values;\n }\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.medium);\n\n log('Using medium value from legacy estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.gasPrice);\n\n log('Using gasPrice from estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n } catch (error) {\n log('Failed to get estimate', error);\n }\n\n const gasPriceDecimal = (await query(new EthQuery(provider), 'gasPrice')) as\n | number\n | undefined;\n\n if (!gasPriceDecimal) {\n return {};\n }\n\n const maxFeePerGas = add0x(gasPriceDecimal.toString(16));\n\n log('Using gasPrice from network as fallback', maxFeePerGas);\n\n return { maxFeePerGas };\n}\n\n/**\n * Converts a GWEI decimal string to a WEI hexadecimal string.\n *\n * @param value - The GWEI decimal string to convert.\n * @returns The WEI hexadecimal string.\n */\nfunction gweiDecimalToWeiHex(value: string) {\n return toHex(gweiDecToWEIBN(value));\n}\n\n/**\n * Checks if a gas fee property is empty.\n *\n * @param value - The gas fee value to check.\n * @returns Whether the gas fee property is empty.\n */\nfunction isGasFeeEmpty(value?: string): boolean {\n return !value || value === EMPTY_BYTES;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gas-fees.mjs","sourceRoot":"","sources":["../../src/utils/gas-fees.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,cAAc,EACd,eAAe,EACf,KAAK,EACL,KAAK,EACN,mCAAmC;AACpC,OAAO,SAAQ,4BAA4B;;AAC3C,OAAO,EACL,kBAAkB,EAEnB,qCAAqC;AAGtC,OAAO,EAAE,YAAY,EAAE,yCAAyC;AAEhE,OAAO,EAAE,KAAK,EAAE,wBAAwB;AAExC,OAAO,EAAE,WAAW,EAAE,yBAAqB;AAC3C,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,sBAAkB;AAI9D,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC;AAe1D;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,OAA6B;IAC/D,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IAC3D,MAAM,EAAE,aAAa,EAAE,GAAG,QAAQ,CAAC;IACnC,IAAI,gBAA8C,CAAC;IAEnD,MAAM,kBAAkB,GAAG,KAAK,IAAI,EAAE;QACpC,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACtB,gBAAgB,GAAG,MAAM,mBAAmB,CAAC,OAAO,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,gBAAgB,CAAC;IAC1B,CAAC,CAAC;IAEF,aAAa,CAAC,YAAY,GAAG,MAAM,eAAe,CAChD,eAAe,EACf,kBAAkB,EAClB,WAAW,CACZ,CAAC;IAEF,aAAa,CAAC,oBAAoB,GAAG,MAAM,uBAAuB,CAChE,eAAe,EACf,kBAAkB,EAClB,aAAa,EACb,WAAW,CACZ,CAAC;IAEF,QAAQ,CAAC,YAAY,GAAG,eAAe,CACrC,QAAQ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C,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAAC;IACvC,CAAC;IAED,MAAM,eAAe,GAAG,CAAC,MAAM,KAAK,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,EAAE,UAAU,CAAC,CAE3D,CAAC;IAEd,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,YAAY,GAAG,KAAK,CAAC,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAQ,CAAC;IAEhE,GAAG,CAAC,yCAAyC,EAAE,YAAY,CAAC,CAAC;IAE7D,OAAO,EAAE,YAAY,EAAE,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CAAC,KAAK,IAAI,KAAK,KAAK,WAAW,CAAC;AACzC,CAAC","sourcesContent":["import {\n gweiDecToWEIBN,\n ORIGIN_METAMASK,\n query,\n toHex,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport {\n GAS_ESTIMATE_TYPES,\n type GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { Provider } from '@metamask/network-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport { UserFeeLevel } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { add0x } from '@metamask/utils';\n\nimport { EMPTY_BYTES } from '../constants';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { UserOperation, UserOperationMetadata } from '../types';\nimport type { AddUserOperationRequest } from '../UserOperationController';\n\nconst log = createModuleLogger(projectLogger, 'gas-fees');\n\nexport type UpdateGasFeesRequest = {\n getGasFeeEstimates: () => Promise<GasFeeState>;\n metadata: UserOperationMetadata;\n originalRequest: AddUserOperationRequest;\n provider: Provider;\n transaction?: TransactionParams;\n};\n\ntype SuggestedGasFees = {\n maxFeePerGas?: string;\n maxPriorityFeePerGas?: string;\n};\n\n/**\n * Populates the gas fee properties for a user operation.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.metadata - The metadata for the user operation.\n * @param request.originalRequest - The original request to add the user operation.\n * @param request.provider - A provider to query the network.\n * @param request.transaction - The transaction that created the user operation.\n */\nexport async function updateGasFees(request: UpdateGasFeesRequest) {\n const { metadata, originalRequest, transaction } = request;\n const { userOperation } = metadata;\n let suggestedGasFees: SuggestedGasFees | undefined;\n\n const getGasFeeEstimates = async () => {\n if (!suggestedGasFees) {\n suggestedGasFees = await getSuggestedGasFees(request);\n }\n\n return suggestedGasFees;\n };\n\n userOperation.maxFeePerGas = await getMaxFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n transaction,\n );\n\n userOperation.maxPriorityFeePerGas = await getMaxPriorityFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n userOperation,\n transaction,\n );\n\n metadata.userFeeLevel = getUserFeeLevel(\n metadata,\n originalRequest,\n suggestedGasFees,\n transaction,\n );\n}\n\n/**\n * Gets the maxFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxFeePerGas for the user operation.\n */\nasync function getMaxFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n\n if (!isGasFeeEmpty(maxFeePerGas)) {\n log('Using maxFeePerGas from request', maxFeePerGas);\n return maxFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxPriorityFeePerGas) && gasPrice) {\n log('Setting maxFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxFeePerGas: suggestedMaxFeePerGas } = await getGetFasEstimates();\n\n if (!suggestedMaxFeePerGas) {\n throw new Error('Failed to get gas fee estimate for maxFeePerGas');\n }\n\n log('Using maxFeePerGas from estimate', suggestedMaxFeePerGas);\n\n return suggestedMaxFeePerGas;\n}\n\n/**\n * Gets the maxPriorityFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param userOperation - The user operation being updated.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxPriorityFeePerGas for the user operation.\n */\nasync function getMaxPriorityFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n userOperation: UserOperation,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n const { maxFeePerGas: updatedMaxFeePerGas } = userOperation;\n\n if (!isGasFeeEmpty(maxPriorityFeePerGas)) {\n log('Using maxPriorityFeePerGas from request', maxPriorityFeePerGas);\n return maxPriorityFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxFeePerGas) && gasPrice) {\n log('Setting maxPriorityFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas } =\n await getGetFasEstimates();\n\n if (suggestedMaxPriorityFeePerGas) {\n log(\n 'Using maxPriorityFeePerGas from estimate',\n suggestedMaxPriorityFeePerGas,\n );\n return suggestedMaxPriorityFeePerGas;\n }\n\n log('Setting maxPriorityFeePerGas to maxFeePerGas', updatedMaxFeePerGas);\n\n return updatedMaxFeePerGas;\n}\n\n/**\n * Gets the userFeeLevel for a user operation.\n *\n * @param metadata - The metadata for the user operation.\n * @param originalRequest - The original request to add the user operation.\n * @param suggestedGasFees - The suggested gas fees, if any.\n * @param transaction - The transaction that created the user operation.\n * @returns The userFeeLevel for the user operation.\n */\nfunction getUserFeeLevel(\n metadata: UserOperationMetadata,\n originalRequest: AddUserOperationRequest,\n suggestedGasFees?: SuggestedGasFees,\n transaction?: TransactionParams,\n): UserFeeLevel {\n const { origin } = metadata;\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n\n const {\n maxFeePerGas: suggestedMaxFeePerGas,\n maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas,\n } = suggestedGasFees || {};\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n transaction?.gasPrice\n ) {\n return origin === ORIGIN_METAMASK\n ? UserFeeLevel.CUSTOM\n : UserFeeLevel.DAPP_SUGGESTED;\n }\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n suggestedMaxFeePerGas &&\n suggestedMaxPriorityFeePerGas\n ) {\n return UserFeeLevel.MEDIUM;\n }\n\n if (origin === ORIGIN_METAMASK) {\n return UserFeeLevel.CUSTOM;\n }\n\n return UserFeeLevel.DAPP_SUGGESTED;\n}\n\n/**\n * Gets suggested gas fees.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.provider - A provider to query the network.\n * @returns The suggested gas fees.\n */\nasync function getSuggestedGasFees(\n request: UpdateGasFeesRequest,\n): Promise<{ maxFeePerGas?: Hex; maxPriorityFeePerGas?: Hex }> {\n const { getGasFeeEstimates, provider } = request;\n\n try {\n const { gasFeeEstimates, gasEstimateType } = await getGasFeeEstimates();\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n /* istanbul ignore next */\n const {\n medium: { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = {},\n } = gasFeeEstimates;\n\n if (suggestedMaxPriorityFeePerGas && suggestedMaxFeePerGas) {\n const values = {\n maxFeePerGas: gweiDecimalToWeiHex(suggestedMaxFeePerGas),\n maxPriorityFeePerGas: gweiDecimalToWeiHex(\n suggestedMaxPriorityFeePerGas,\n ),\n };\n\n log('Using medium values from fee market estimate', values);\n\n return values;\n }\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.medium);\n\n log('Using medium value from legacy estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.gasPrice);\n\n log('Using gasPrice from estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n } catch (error) {\n log('Failed to get estimate', error);\n }\n\n const gasPriceDecimal = (await query(new EthQuery(provider), 'gasPrice')) as\n | number\n | undefined;\n\n if (!gasPriceDecimal) {\n return {};\n }\n\n const maxFeePerGas = add0x(gasPriceDecimal.toString(16)) as Hex;\n\n log('Using gasPrice from network as fallback', maxFeePerGas);\n\n return { maxFeePerGas };\n}\n\n/**\n * Converts a GWEI decimal string to a WEI hexadecimal string.\n *\n * @param value - The GWEI decimal string to convert.\n * @returns The WEI hexadecimal string.\n */\nfunction gweiDecimalToWeiHex(value: string) {\n return toHex(gweiDecToWEIBN(value));\n}\n\n/**\n * Checks if a gas fee property is empty.\n *\n * @param value - The gas fee value to check.\n * @returns Whether the gas fee property is empty.\n */\nfunction isGasFeeEmpty(value?: string): boolean {\n return !value || value === EMPTY_BYTES;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"gas-fees.mjs","sourceRoot":"","sources":["../../src/utils/gas-fees.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,cAAc,EACd,eAAe,EACf,KAAK,EACL,KAAK,EACN,mCAAmC;AACpC,OAAO,SAAQ,4BAA4B;;AAC3C,OAAO,EACL,kBAAkB,EAEnB,qCAAqC;AAGtC,OAAO,EAAE,YAAY,EAAE,yCAAyC;AAEhE,OAAO,EAAE,KAAK,EAAE,wBAAwB;AAExC,OAA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7D,OAAO,EAAE,YAAY,EAAE,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,SAAS,mBAAmB,CAAC,KAAa;IACxC,OAAO,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CAAC,KAAK,IAAI,KAAK,KAAK,WAAW,CAAC;AACzC,CAAC","sourcesContent":["import {\n gweiDecToWEIBN,\n ORIGIN_METAMASK,\n query,\n toHex,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport {\n GAS_ESTIMATE_TYPES,\n type GasFeeState,\n} from '@metamask/gas-fee-controller';\nimport type { Provider } from '@metamask/network-controller';\nimport type { TransactionParams } from '@metamask/transaction-controller';\nimport { UserFeeLevel } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { add0x } from '@metamask/utils';\n\nimport { EMPTY_BYTES } from '../constants';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { UserOperation, UserOperationMetadata } from '../types';\nimport type { AddUserOperationRequest } from '../UserOperationController';\n\nconst log = createModuleLogger(projectLogger, 'gas-fees');\n\nexport type UpdateGasFeesRequest = {\n getGasFeeEstimates: () => Promise<GasFeeState>;\n metadata: UserOperationMetadata;\n originalRequest: AddUserOperationRequest;\n provider: Provider;\n transaction?: TransactionParams;\n};\n\ntype SuggestedGasFees = {\n maxFeePerGas?: string;\n maxPriorityFeePerGas?: string;\n};\n\n/**\n * Populates the gas fee properties for a user operation.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.metadata - The metadata for the user operation.\n * @param request.originalRequest - The original request to add the user operation.\n * @param request.provider - A provider to query the network.\n * @param request.transaction - The transaction that created the user operation.\n */\nexport async function updateGasFees(request: UpdateGasFeesRequest) {\n const { metadata, originalRequest, transaction } = request;\n const { userOperation } = metadata;\n let suggestedGasFees: SuggestedGasFees | undefined;\n\n const getGasFeeEstimates = async () => {\n if (!suggestedGasFees) {\n suggestedGasFees = await getSuggestedGasFees(request);\n }\n\n return suggestedGasFees;\n };\n\n userOperation.maxFeePerGas = await getMaxFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n transaction,\n );\n\n userOperation.maxPriorityFeePerGas = await getMaxPriorityFeePerGas(\n originalRequest,\n getGasFeeEstimates,\n userOperation,\n transaction,\n );\n\n metadata.userFeeLevel = getUserFeeLevel(\n metadata,\n originalRequest,\n suggestedGasFees,\n transaction,\n );\n}\n\n/**\n * Gets the maxFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxFeePerGas for the user operation.\n */\nasync function getMaxFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n\n if (!isGasFeeEmpty(maxFeePerGas)) {\n log('Using maxFeePerGas from request', maxFeePerGas);\n return maxFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxPriorityFeePerGas) && gasPrice) {\n log('Setting maxFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxFeePerGas: suggestedMaxFeePerGas } = await getGetFasEstimates();\n\n if (!suggestedMaxFeePerGas) {\n throw new Error('Failed to get gas fee estimate for maxFeePerGas');\n }\n\n log('Using maxFeePerGas from estimate', suggestedMaxFeePerGas);\n\n return suggestedMaxFeePerGas;\n}\n\n/**\n * Gets the maxPriorityFeePerGas for a user operation.\n *\n * @param originalRequest - The original request to add the user operation.\n * @param getGetFasEstimates - A callback to get gas fee estimates.\n * @param userOperation - The user operation being updated.\n * @param transaction - The transaction that created the user operation.\n * @returns The maxPriorityFeePerGas for the user operation.\n */\nasync function getMaxPriorityFeePerGas(\n originalRequest: AddUserOperationRequest,\n getGetFasEstimates: () => Promise<SuggestedGasFees>,\n userOperation: UserOperation,\n transaction?: TransactionParams,\n) {\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n const { gasPrice } = transaction ?? {};\n const { maxFeePerGas: updatedMaxFeePerGas } = userOperation;\n\n if (!isGasFeeEmpty(maxPriorityFeePerGas)) {\n log('Using maxPriorityFeePerGas from request', maxPriorityFeePerGas);\n return maxPriorityFeePerGas as string;\n }\n\n if (isGasFeeEmpty(maxFeePerGas) && gasPrice) {\n log('Setting maxPriorityFeePerGas to transaction gasPrice', gasPrice);\n return gasPrice;\n }\n\n const { maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas } =\n await getGetFasEstimates();\n\n if (suggestedMaxPriorityFeePerGas) {\n log(\n 'Using maxPriorityFeePerGas from estimate',\n suggestedMaxPriorityFeePerGas,\n );\n return suggestedMaxPriorityFeePerGas;\n }\n\n log('Setting maxPriorityFeePerGas to maxFeePerGas', updatedMaxFeePerGas);\n\n return updatedMaxFeePerGas;\n}\n\n/**\n * Gets the userFeeLevel for a user operation.\n *\n * @param metadata - The metadata for the user operation.\n * @param originalRequest - The original request to add the user operation.\n * @param suggestedGasFees - The suggested gas fees, if any.\n * @param transaction - The transaction that created the user operation.\n * @returns The userFeeLevel for the user operation.\n */\nfunction getUserFeeLevel(\n metadata: UserOperationMetadata,\n originalRequest: AddUserOperationRequest,\n suggestedGasFees?: SuggestedGasFees,\n transaction?: TransactionParams,\n): UserFeeLevel {\n const { origin } = metadata;\n const { maxFeePerGas, maxPriorityFeePerGas } = originalRequest;\n\n const {\n maxFeePerGas: suggestedMaxFeePerGas,\n maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas,\n } = suggestedGasFees || {};\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n transaction?.gasPrice\n ) {\n return origin === ORIGIN_METAMASK\n ? UserFeeLevel.CUSTOM\n : UserFeeLevel.DAPP_SUGGESTED;\n }\n\n if (\n isGasFeeEmpty(maxFeePerGas) &&\n isGasFeeEmpty(maxPriorityFeePerGas) &&\n suggestedMaxFeePerGas &&\n suggestedMaxPriorityFeePerGas\n ) {\n return UserFeeLevel.MEDIUM;\n }\n\n if (origin === ORIGIN_METAMASK) {\n return UserFeeLevel.CUSTOM;\n }\n\n return UserFeeLevel.DAPP_SUGGESTED;\n}\n\n/**\n * Gets suggested gas fees.\n *\n * @param request - The request to update the gas fees.\n * @param request.getGasFeeEstimates - A callback to get gas fee estimates.\n * @param request.provider - A provider to query the network.\n * @returns The suggested gas fees.\n */\nasync function getSuggestedGasFees(\n request: UpdateGasFeesRequest,\n): Promise<{ maxFeePerGas?: Hex; maxPriorityFeePerGas?: Hex }> {\n const { getGasFeeEstimates, provider } = request;\n\n try {\n const { gasFeeEstimates, gasEstimateType } = await getGasFeeEstimates();\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {\n /* istanbul ignore next */\n const {\n medium: { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = {},\n } = gasFeeEstimates;\n\n if (suggestedMaxPriorityFeePerGas && suggestedMaxFeePerGas) {\n const values = {\n maxFeePerGas: gweiDecimalToWeiHex(suggestedMaxFeePerGas),\n maxPriorityFeePerGas: gweiDecimalToWeiHex(\n suggestedMaxPriorityFeePerGas,\n ),\n };\n\n log('Using medium values from fee market estimate', values);\n\n return values;\n }\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.medium);\n\n log('Using medium value from legacy estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n\n if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {\n const maxFeePerGas = gweiDecimalToWeiHex(gasFeeEstimates.gasPrice);\n\n log('Using gasPrice from estimate', maxFeePerGas);\n\n return {\n maxFeePerGas,\n };\n }\n } catch (error) {\n log('Failed to get estimate', error);\n }\n\n const gasPriceDecimal = (await query(new EthQuery(provider), 'gasPrice')) as\n | number\n | undefined;\n\n if (!gasPriceDecimal) {\n return {};\n }\n\n const maxFeePerGas = add0x(gasPriceDecimal.toString(16));\n\n log('Using gasPrice from network as fallback', maxFeePerGas);\n\n return { maxFeePerGas };\n}\n\n/**\n * Converts a GWEI decimal string to a WEI hexadecimal string.\n *\n * @param value - The GWEI decimal string to convert.\n * @returns The WEI hexadecimal string.\n */\nfunction gweiDecimalToWeiHex(value: string) {\n return toHex(gweiDecToWEIBN(value));\n}\n\n/**\n * Checks if a gas fee property is empty.\n *\n * @param value - The gas fee value to check.\n * @returns Whether the gas fee property is empty.\n */\nfunction isGasFeeEmpty(value?: string): boolean {\n return !value || value === EMPTY_BYTES;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/user-operation-controller",
|
|
3
|
-
"version": "41.0.0-preview-
|
|
3
|
+
"version": "41.0.0-preview-7cfbb337",
|
|
4
4
|
"description": "Creates user operations and manages their life cycle",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@metamask/polling-controller": "^16.0.0",
|
|
61
61
|
"@metamask/rpc-errors": "^7.0.2",
|
|
62
62
|
"@metamask/superstruct": "^3.1.0",
|
|
63
|
-
"@metamask/transaction-controller": "^62.
|
|
63
|
+
"@metamask/transaction-controller": "^62.4.0",
|
|
64
64
|
"@metamask/utils": "^11.8.1",
|
|
65
65
|
"bn.js": "^5.2.1",
|
|
66
66
|
"immer": "^9.0.6",
|