Skip to content

Commit

Permalink
add 'from' field when est gas cost
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenghaven committed Jul 11, 2024
1 parent 48d3be9 commit 8793829
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions PyEthHelper/EthContractHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,17 @@ def IsAccountType(obj: Any) -> bool:
def _EstimateGas(
executable: Union[ ContractConstructor, ContractFunction ],
value: int,
fromAcc: Union[ None, Account ],
) -> int:
logger = logging.getLogger(__name__ + '.' + _EstimateGas.__name__)

gas = executable.estimate_gas({
txDict = {
'value': value,
})
}
if fromAcc is not None:
txDict['from'] = fromAcc.address

gas = executable.estimate_gas(txDict)
logger.info('Estimated gas: {}'.format(gas))
# add a little bit flexibility
gas = int(gas * 1.1)
Expand All @@ -136,11 +141,12 @@ def _DetermineGas(
executable: Union[ ContractConstructor, ContractFunction ],
gas: Union[ None, int ],
value: int,
fromAcc: Union[ None, Account ],
) -> int:
logger = logging.getLogger(__name__ + '.' + _DetermineGas.__name__)

if gas is None:
gas = _EstimateGas(executable, value)
gas = _EstimateGas(executable, value, fromAcc=fromAcc)

logger.debug('Gas: {}; Value: {}'.format(gas, value))

Expand Down Expand Up @@ -280,7 +286,7 @@ def _DoTransaction(
if (privKey is not None) and (not IsAccountType(privKey)):
privKey = Account.from_key(privKey)

gas = _DetermineGas(executable, gas, value)
gas = _DetermineGas(executable, gas, value, fromAcc=privKey)
msg = _FillMessage(w3, gas, value, privKey, feeCalculator)

if privKey is None:
Expand Down

0 comments on commit 8793829

Please sign in to comment.