@@ -16,6 +16,7 @@ import type {
1616} from '@metamask/bridge-controller' ;
1717import { toHex } from '@metamask/controller-utils' ;
1818import {
19+ GasFeeEstimateType ,
1920 TransactionStatus ,
2021 TransactionType ,
2122} from '@metamask/transaction-controller' ;
@@ -58,6 +59,22 @@ const is7702Tx = (tx: TransactionMeta) => {
5859 ) ;
5960} ;
6061
62+ const getGasFeeEstimates = async (
63+ messenger : BridgeStatusControllerMessenger ,
64+ args : Parameters < TransactionController [ 'estimateGasFee' ] > [ 0 ] ,
65+ ) => {
66+ const { estimates } = await messenger . call (
67+ 'TransactionController:estimateGasFee' ,
68+ args ,
69+ ) ;
70+
71+ if ( estimates ?. type === GasFeeEstimateType . FeeMarket ) {
72+ return estimates [ BRIDGE_PREFERRED_GAS_ESTIMATE ] ;
73+ }
74+
75+ return undefined ;
76+ } ;
77+
6178export const getTransactions = ( messenger : BridgeStatusControllerMessenger ) => {
6279 return messenger . call ( 'TransactionController:getState' ) . transactions ?? [ ] ;
6380} ;
@@ -298,7 +315,6 @@ export const toQuoteAndTxMetadata = ({
298315 * @param trade.gasLimit - the gas limit to use for the gas fee estimates
299316 * @param networkClientId - the network client ID to use for the gas fee estimates
300317 * @param chainId - the chain ID to use for the gas fee estimates
301- * @param isGasIncluded7702 - whether the gas is included via 7702
302318 * @param simulatedGasFeeLimits - either the txFee from the quote or the simulated gas fee limits for the batch sell
303319 * @returns The gas fee estimates for the transaction
304320 */
@@ -307,7 +323,6 @@ export const toTransactionParams = async (
307323 { chainId : tradeChainId , gasLimit, ...trade } : TxData ,
308324 networkClientId : string ,
309325 chainId : Hex ,
310- isGasIncluded7702 : boolean ,
311326 simulatedGasFeeLimits ?: SimulatedGasFeeLimits | TxFeeGasLimits ,
312327) : Promise < BatchTransactionParams > => {
313328 const normalizedTrade = {
@@ -317,16 +332,14 @@ export const toTransactionParams = async (
317332 from : trade . from ,
318333 value : trade . value ,
319334 } ;
320- if ( isGasIncluded7702 && ! simulatedGasFeeLimits ) {
321- return normalizedTrade ;
322- }
323335 const transactionParams = {
324336 ...trade ,
325337 // Only add gasLimit and gas if they're truthy
326338 gas : gasLimit ? toHex ( gasLimit ) : undefined ,
327339 ...normalizedTrade ,
328340 } ;
329341
342+ // Use bridge-api's provided gas fee estimates
330343 if ( simulatedGasFeeLimits ) {
331344 return {
332345 ...transactionParams ,
@@ -336,32 +349,16 @@ export const toTransactionParams = async (
336349 }
337350
338351 // Get transaction's 1559 gas fee estimates
339- const { estimates } = await messenger . call (
340- 'TransactionController:estimateGasFee' ,
341- {
342- transactionParams,
343- networkClientId,
344- chainId,
345- } ,
346- ) ;
347-
348- let gasFeeEstimates : Partial < Record < keyof SimulatedGasFeeLimits , Hex > > = {
349- maxFeePerGas : undefined ,
350- maxPriorityFeePerGas : undefined ,
351- } ;
352- if (
353- estimates &&
354- BRIDGE_PREFERRED_GAS_ESTIMATE in estimates &&
355- typeof estimates [ BRIDGE_PREFERRED_GAS_ESTIMATE ] === 'object' &&
356- 'maxFeePerGas' in estimates [ BRIDGE_PREFERRED_GAS_ESTIMATE ]
357- ) {
358- gasFeeEstimates = estimates [ BRIDGE_PREFERRED_GAS_ESTIMATE ] ;
359- }
352+ const gasFeeEstimates = await getGasFeeEstimates ( messenger , {
353+ transactionParams,
354+ networkClientId,
355+ chainId,
356+ } ) ;
360357
361358 return {
362359 ...transactionParams ,
363- maxFeePerGas : gasFeeEstimates . maxFeePerGas ,
364- maxPriorityFeePerGas : gasFeeEstimates . maxPriorityFeePerGas ,
360+ maxFeePerGas : gasFeeEstimates ? .maxFeePerGas ,
361+ maxPriorityFeePerGas : gasFeeEstimates ? .maxPriorityFeePerGas ,
365362 } ;
366363} ;
367364
@@ -390,14 +387,13 @@ export const getAddTransactionBatchParams = async ({
390387 const networkClientId = getNetworkClientIdByChainId ( messenger , hexChainId ) ;
391388
392389 const transactions : TransactionBatchSingleRequest [ ] = await Promise . all (
393- tradeData . map ( async ( { tx, txFee, quoteResponse : { quote } , ...rest } ) => ( {
390+ tradeData . map ( async ( { tx, txFee, quoteResponse : _ , ...rest } ) => ( {
394391 params : await toTransactionParams (
395392 messenger ,
396393 tx ,
397394 networkClientId ,
398395 hexChainId ,
399- Boolean ( quote . gasIncluded7702 ) ,
400- isDelegatedAccount ? undefined : txFee ,
396+ txFee ,
401397 ) ,
402398 ...rest ,
403399 } ) ) ,
@@ -635,7 +631,6 @@ export const submitEvmTransaction = async ({
635631 trade ,
636632 networkClientId ,
637633 hexChainId ,
638- false ,
639634 txFee ,
640635 ) ;
641636
0 commit comments