diff --git a/thor_requests/connect.py b/thor_requests/connect.py index dd27a75..45d471f 100644 --- a/thor_requests/connect.py +++ b/thor_requests/connect.py @@ -1,4 +1,3 @@ -from thor_requests.const import VTHO_ABI, VTHO_ADDRESS import time import json from typing import Union, List @@ -12,16 +11,16 @@ calc_nonce, any_emulate_failed, calc_tx_signed, - calc_tx_unsigned, inject_decoded_event, inject_decoded_return, inject_revert_reason, read_vm_gases, - calc_gas, build_params, + suggest_gas_for_tx, ) from .wallet import Wallet from .contract import Contract +from .const import VTHO_ABI, VTHO_ADDRESS class Connect: @@ -455,17 +454,15 @@ def transact( # Get gas estimation from remote node # Calculate a safe gas for user - _vm_gases = read_vm_gases(e_responses) - _supposed_vm_gas = _vm_gases[0] - _tx_obj = calc_tx_unsigned(tx_body) - _intrincis_gas = _tx_obj.get_intrinsic_gas() - _supposed_safe_gas = calc_gas(_supposed_vm_gas, _intrincis_gas) - if gas and gas < _supposed_safe_gas: - raise Exception(f"gas {gas} < emulated gas {_supposed_safe_gas}") + vm_gas = sum(read_vm_gases(e_responses)) + safe_gas = suggest_gas_for_tx(vm_gas, tx_body) + if gas and gas < safe_gas: + if force == False: + raise Exception(f"gas {gas} < emulated gas {safe_gas}") # Fill out the gas for user if not gas: - tx_body["gas"] = _supposed_safe_gas + tx_body["gas"] = safe_gas # Post it to the remote node encoded_raw = calc_tx_signed(wallet, tx_body, True) @@ -490,17 +487,15 @@ def transact_multi( # Get gas estimation from remote node # Calculate a safe gas for user - _vm_gases = read_vm_gases(e_responses) - _supposed_vm_gas = sum(_vm_gases) - _tx_obj = calc_tx_unsigned(tx_body) - _intrincis_gas = _tx_obj.get_intrinsic_gas() - _supposed_safe_gas = calc_gas(_supposed_vm_gas, _intrincis_gas) - if gas and gas < _supposed_safe_gas: - raise Exception(f"gas {gas} < emulated gas {_supposed_safe_gas}") + vm_gas = sum(read_vm_gases(e_responses)) + safe_gas = suggest_gas_for_tx(vm_gas, tx_body) + if gas and gas < safe_gas: + if force == False: + raise Exception(f"gas {gas} < emulated gas {safe_gas}") # Fill out the gas for user if not gas: - tx_body["gas"] = _supposed_safe_gas + tx_body["gas"] = safe_gas # Post it to the remote node encoded_raw = calc_tx_signed(wallet, tx_body, True) @@ -558,14 +553,11 @@ def deploy( raise Exception(f"Tx will revert: {e_responses}") # Get gas estimation from remote - _vm_gases = read_vm_gases(e_responses) - _supposed_vm_gas = _vm_gases[0] - _tx_obj = calc_tx_unsigned(tx_body) - _intrincis_gas = _tx_obj.get_intrinsic_gas() - _supposed_safe_gas = calc_gas(_supposed_vm_gas, _intrincis_gas) + vm_gas = sum(read_vm_gases(e_responses)) + safe_gas = suggest_gas_for_tx(vm_gas, tx_body) # Fill out the gas for user. - tx_body["gas"] = _supposed_safe_gas + tx_body["gas"] = safe_gas encoded_raw = calc_tx_signed(wallet, tx_body, True) return self.post_tx(encoded_raw) diff --git a/thor_requests/utils.py b/thor_requests/utils.py index 4df7c4e..7ef55b9 100644 --- a/thor_requests/utils.py +++ b/thor_requests/utils.py @@ -271,3 +271,11 @@ def read_created_contracts(receipt: dict) -> list: def is_contract(account: dict) -> bool: """Check if the address online is a contract""" return account["hasCode"] + + +def suggest_gas_for_tx(vm_gas: int, tx_body: dict) -> int: + '''Calculate the suggested gas for a transaction''' + tx_obj = calc_tx_unsigned(tx_body) + intrincis_gas = tx_obj.get_intrinsic_gas() + supposed_safe_gas = calc_gas(vm_gas, intrincis_gas) + return supposed_safe_gas \ No newline at end of file