@@ -930,7 +930,7 @@ export abstract class BaseProvider extends AbstractProvider {
930930 ? await this . _getBlockHash ( blockTag )
931931 : undefined ; // if blockTag is latest, avoid explicit blockhash for better performance
932932
933- const { usedGas, gasLimit, usedStorage } = await this . estimateResources ( transaction , blockHash ) ;
933+ const { usedGas, gasLimit, safeStorage } = await this . estimateResources ( transaction , blockHash ) ;
934934
935935 const tx = await resolveProperties ( transaction ) ;
936936 const data = tx . data ?. toString ( ) ?? '0x' ;
@@ -939,7 +939,7 @@ export abstract class BaseProvider extends AbstractProvider {
939939 data ,
940940 toBN ( BigNumber . from ( tx . value ?? 0 ) ) ,
941941 toBN ( gasLimit ) ,
942- toBN ( usedStorage . isNegative ( ) ? 0 : usedStorage ) ,
942+ toBN ( safeStorage ) ,
943943 accessListify ( tx . accessList ?? [ ] ) ,
944944 ] as const ;
945945
@@ -952,8 +952,8 @@ export abstract class BaseProvider extends AbstractProvider {
952952 let txFee = await this . _estimateGasCost ( extrinsic , blockHash ) ;
953953 txFee = txFee . mul ( gasLimit ) . div ( usedGas ) ; // scale it to the same ratio when estimate passing gasLimit
954954
955- if ( usedStorage . gt ( 0 ) ) {
956- const storageFee = usedStorage . mul ( this . _getGasConsts ( ) . storageDepositPerByte ) ;
955+ if ( safeStorage . gt ( 0 ) ) {
956+ const storageFee = safeStorage . mul ( this . _getGasConsts ( ) . storageDepositPerByte ) ;
957957 txFee = txFee . add ( storageFee ) ;
958958 }
959959
@@ -963,7 +963,7 @@ export abstract class BaseProvider extends AbstractProvider {
963963
964964 const tokenTransferSelector = '0xa9059cbb' ; // transfer(address,uint256)
965965 const isTokenTransfer = hexlify ( await transaction . data ?? '0x' ) . startsWith ( tokenTransferSelector ) ;
966- return encodeGasLimit ( txFee , gasPrice , gasLimit , usedStorage , isTokenTransfer ) ;
966+ return encodeGasLimit ( txFee , gasPrice , gasLimit , safeStorage , isTokenTransfer ) ;
967967 } ;
968968
969969 _estimateGasCost = async (
@@ -1009,7 +1009,7 @@ export abstract class BaseProvider extends AbstractProvider {
10091009 gasLimit : BigNumber ;
10101010 } > => {
10111011 if ( ! gasLimit || ! storageLimit ) {
1012- const { gasLimit : gas , usedStorage : storage } = await this . estimateResources ( transaction ) ;
1012+ const { gasLimit : gas , safeStorage : storage } = await this . estimateResources ( transaction ) ;
10131013 gasLimit = gasLimit ?? gas ;
10141014 storageLimit = storageLimit ?? storage ;
10151015 }
@@ -1095,7 +1095,7 @@ export abstract class BaseProvider extends AbstractProvider {
10951095 ) : Promise < {
10961096 usedGas : BigNumber ;
10971097 gasLimit : BigNumber ;
1098- usedStorage : BigNumber ;
1098+ safeStorage : BigNumber ;
10991099 } > => {
11001100 const ethTx = await getTransactionRequest ( transaction ) ;
11011101
@@ -1144,7 +1144,7 @@ export abstract class BaseProvider extends AbstractProvider {
11441144 await this . _ethCall ( {
11451145 ...txRequest ,
11461146 gasLimit,
1147- } ) ;
1147+ } , blockHash ) ;
11481148 } catch {
11491149 gasAlreadyWorks = false ;
11501150 }
@@ -1160,7 +1160,7 @@ export abstract class BaseProvider extends AbstractProvider {
11601160 await this . _ethCall ( {
11611161 ...txRequest ,
11621162 gasLimit : mid ,
1163- } ) ;
1163+ } , blockHash ) ;
11641164 highest = mid ;
11651165
11661166 if ( ( prevHighest - highest ) / prevHighest < 0.1 ) break ;
@@ -1179,10 +1179,11 @@ export abstract class BaseProvider extends AbstractProvider {
11791179 gasLimit = highest ;
11801180 }
11811181
1182+ const safeStorage = Math . floor ( ( Math . max ( usedStorage , 0 ) + 64 ) * 1.1 ) ;
11821183 return {
1183- usedGas : BigNumber . from ( usedGas ) , // actual used gas
1184- gasLimit : BigNumber . from ( gasLimit ) , // gasLimit to pass execution
1185- usedStorage : BigNumber . from ( usedStorage ) ,
1184+ usedGas : BigNumber . from ( usedGas ) , // actual used gas
1185+ gasLimit : BigNumber . from ( gasLimit ) , // gasLimit to pass execution
1186+ safeStorage : BigNumber . from ( safeStorage ) , // slightly over estimated storage to be safer
11861187 } ;
11871188 } ;
11881189
0 commit comments