@ -154,10 +154,7 @@ export default class SwapsController {
approvalNeeded : null ,
approvalNeeded : null ,
} ) )
} ) )
} else if ( ! isPolledRequest ) {
} else if ( ! isPolledRequest ) {
const { gasLimit : approvalGas } = await this . timedoutGasReturn ( {
const { gasLimit : approvalGas } = await this . timedoutGasReturn ( Object . values ( newQuotes ) [ 0 ] . approvalNeeded )
... Object . values ( newQuotes ) [ 0 ] . approvalNeeded ,
gas : DEFAULT _ERC20 _APPROVE _GAS ,
} )
newQuotes = mapValues ( newQuotes , ( quote ) => ( {
newQuotes = mapValues ( newQuotes , ( quote ) => ( {
... quote ,
... quote ,
@ -255,10 +252,7 @@ export default class SwapsController {
async getAllQuotesWithGasEstimates ( quotes ) {
async getAllQuotesWithGasEstimates ( quotes ) {
const quoteGasData = await Promise . all (
const quoteGasData = await Promise . all (
Object . values ( quotes ) . map ( async ( quote ) => {
Object . values ( quotes ) . map ( async ( quote ) => {
const { gasLimit , simulationFails } = await this . timedoutGasReturn ( {
const { gasLimit , simulationFails } = await this . timedoutGasReturn ( quote . trade )
... quote . trade ,
gas : ` 0x ${ ( quote . maxGas || MAX _GAS _LIMIT ) . toString ( 16 ) } ` ,
} )
return [ gasLimit , simulationFails , quote . aggregator ]
return [ gasLimit , simulationFails , quote . aggregator ]
} ) ,
} ) ,
)
)
@ -292,7 +286,17 @@ export default class SwapsController {
resolve ( { gasLimit : null , simulationFails : true } )
resolve ( { gasLimit : null , simulationFails : true } )
} , 5000 )
} , 5000 )
this . getBufferedGasLimit ( { txParams : tradeTxParams } , 1 )
// Remove gas from params that will be passed to the `estimateGas` call
// Including it can cause the estimate to fail if the actual gas needed
// exceeds the passed gas
const tradeTxParamsForGasEstimate = {
data : tradeTxParams . data ,
from : tradeTxParams . from ,
to : tradeTxParams . to ,
value : tradeTxParams . value ,
}
this . getBufferedGasLimit ( { txParams : tradeTxParamsForGasEstimate } , 1 )
. then ( ( { gasLimit , simulationFails } ) => {
. then ( ( { gasLimit , simulationFails } ) => {
if ( ! gasTimedOut ) {
if ( ! gasTimedOut ) {
clearTimeout ( gasTimeout )
clearTimeout ( gasTimeout )
@ -309,7 +313,7 @@ export default class SwapsController {
} )
} )
}
}
async setInitialGasEstimate ( initialAggId , baseGasEstimate ) {
async setInitialGasEstimate ( initialAggId ) {
const { swapsState } = this . store . getState ( )
const { swapsState } = this . store . getState ( )
const quoteToUpdate = { ... swapsState . quotes [ initialAggId ] }
const quoteToUpdate = { ... swapsState . quotes [ initialAggId ] }
@ -317,10 +321,7 @@ export default class SwapsController {
const {
const {
gasLimit : newGasEstimate ,
gasLimit : newGasEstimate ,
simulationFails ,
simulationFails ,
} = await this . timedoutGasReturn ( {
} = await this . timedoutGasReturn ( quoteToUpdate . trade )
... quoteToUpdate . trade ,
gas : baseGasEstimate ,
} )
if ( newGasEstimate && ! simulationFails ) {
if ( newGasEstimate && ! simulationFails ) {
const gasEstimateWithRefund = calculateGasEstimateWithRefund ( quoteToUpdate . maxGas , quoteToUpdate . estimatedRefund , newGasEstimate )
const gasEstimateWithRefund = calculateGasEstimateWithRefund ( quoteToUpdate . maxGas , quoteToUpdate . estimatedRefund , newGasEstimate )