@@ -930,7 +930,7 @@ export abstract class BaseProvider extends AbstractProvider {
930
930
? await this . _getBlockHash ( blockTag )
931
931
: undefined ; // if blockTag is latest, avoid explicit blockhash for better performance
932
932
933
- const { usedGas, gasLimit, usedStorage } = await this . estimateResources ( transaction , blockHash ) ;
933
+ const { usedGas, gasLimit, safeStorage } = await this . estimateResources ( transaction , blockHash ) ;
934
934
935
935
const tx = await resolveProperties ( transaction ) ;
936
936
const data = tx . data ?. toString ( ) ?? '0x' ;
@@ -939,7 +939,7 @@ export abstract class BaseProvider extends AbstractProvider {
939
939
data ,
940
940
toBN ( BigNumber . from ( tx . value ?? 0 ) ) ,
941
941
toBN ( gasLimit ) ,
942
- toBN ( usedStorage . isNegative ( ) ? 0 : usedStorage ) ,
942
+ toBN ( safeStorage ) ,
943
943
accessListify ( tx . accessList ?? [ ] ) ,
944
944
] as const ;
945
945
@@ -952,8 +952,8 @@ export abstract class BaseProvider extends AbstractProvider {
952
952
let txFee = await this . _estimateGasCost ( extrinsic , blockHash ) ;
953
953
txFee = txFee . mul ( gasLimit ) . div ( usedGas ) ; // scale it to the same ratio when estimate passing gasLimit
954
954
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 ) ;
957
957
txFee = txFee . add ( storageFee ) ;
958
958
}
959
959
@@ -963,7 +963,7 @@ export abstract class BaseProvider extends AbstractProvider {
963
963
964
964
const tokenTransferSelector = '0xa9059cbb' ; // transfer(address,uint256)
965
965
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 ) ;
967
967
} ;
968
968
969
969
_estimateGasCost = async (
@@ -1009,7 +1009,7 @@ export abstract class BaseProvider extends AbstractProvider {
1009
1009
gasLimit : BigNumber ;
1010
1010
} > => {
1011
1011
if ( ! gasLimit || ! storageLimit ) {
1012
- const { gasLimit : gas , usedStorage : storage } = await this . estimateResources ( transaction ) ;
1012
+ const { gasLimit : gas , safeStorage : storage } = await this . estimateResources ( transaction ) ;
1013
1013
gasLimit = gasLimit ?? gas ;
1014
1014
storageLimit = storageLimit ?? storage ;
1015
1015
}
@@ -1095,7 +1095,7 @@ export abstract class BaseProvider extends AbstractProvider {
1095
1095
) : Promise < {
1096
1096
usedGas : BigNumber ;
1097
1097
gasLimit : BigNumber ;
1098
- usedStorage : BigNumber ;
1098
+ safeStorage : BigNumber ;
1099
1099
} > => {
1100
1100
const ethTx = await getTransactionRequest ( transaction ) ;
1101
1101
@@ -1144,7 +1144,7 @@ export abstract class BaseProvider extends AbstractProvider {
1144
1144
await this . _ethCall ( {
1145
1145
...txRequest ,
1146
1146
gasLimit,
1147
- } ) ;
1147
+ } , blockHash ) ;
1148
1148
} catch {
1149
1149
gasAlreadyWorks = false ;
1150
1150
}
@@ -1160,7 +1160,7 @@ export abstract class BaseProvider extends AbstractProvider {
1160
1160
await this . _ethCall ( {
1161
1161
...txRequest ,
1162
1162
gasLimit : mid ,
1163
- } ) ;
1163
+ } , blockHash ) ;
1164
1164
highest = mid ;
1165
1165
1166
1166
if ( ( prevHighest - highest ) / prevHighest < 0.1 ) break ;
@@ -1179,10 +1179,11 @@ export abstract class BaseProvider extends AbstractProvider {
1179
1179
gasLimit = highest ;
1180
1180
}
1181
1181
1182
+ const safeStorage = Math . floor ( ( Math . max ( usedStorage , 0 ) + 64 ) * 1.1 ) ;
1182
1183
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
1186
1187
} ;
1187
1188
} ;
1188
1189
0 commit comments