Skip to content

Commit

Permalink
fix: gas price type errors [APE-1193] (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 17, 2023
1 parent adb1e04 commit 56a0495
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
raises_not_implemented,
run_until_complete,
spawn,
to_int,
)
from ape.utils.misc import DEFAULT_MAX_RETRIES_TX, _create_raises_not_implemented_error

Expand Down Expand Up @@ -855,7 +856,8 @@ def chain_id(self) -> int:

@property
def gas_price(self) -> int:
return self._web3.eth.generate_gas_price() # type: ignore
price = self.web3.eth.generate_gas_price() or 0
return to_int(price)

@property
def priority_fee(self) -> int:
Expand Down
6 changes: 2 additions & 4 deletions src/ape/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import requests
import yaml
from eth_utils import is_0x_prefixed
from ethpm_types import HexBytes
from importlib_metadata import PackageNotFoundError, distributions, packages_distributions
from importlib_metadata import version as version_metadata
Expand Down Expand Up @@ -271,10 +272,7 @@ def to_int(value) -> int:
if isinstance(value, int):
return value
elif isinstance(value, str):
if value.startswith("0x"):
return int(value, 16)
else:
return int(value)
return int(value, 16) if is_0x_prefixed(value) else int(value)
elif isinstance(value, bytes):
return int.from_bytes(value, "big")

Expand Down
17 changes: 14 additions & 3 deletions tests/functional/test_geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,15 @@ def test_custom_error_on_deploy(error_contract_container, owner, chain):
owner.deploy(error_contract_container, 0)

assert isinstance(err.value, ContractLogicError)
contract = chain.contracts.instance_at(err.value.address)
if err.value.address:
contract = chain.contracts.instance_at(err.value.address)

# Ensure it is the custom error.
assert isinstance(err.value, contract.OtherError)
# Ensure it is the custom error.
assert isinstance(err.value, contract.OtherError)

else:
# skip this test - still covered in reverts() tests anyway.
return


@geth_process_test
Expand Down Expand Up @@ -504,3 +509,9 @@ def test_out_of_gas_error(geth_contract, geth_account, geth_provider):
geth_account.call(txn)

assert err.value.txn is not None


@geth_process_test
def test_gas_price(geth_provider):
actual = geth_provider.gas_price
assert isinstance(actual, int)
5 changes: 5 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,8 @@ def test_get_virtual_machine_error_panic(eth_tester_provider, mocker):
assert enrich_spy.call_count == 1
enrich_spy.assert_called_once_with(actual)
assert isinstance(actual, ContractLogicError)


def test_gas_price(eth_tester_provider):
actual = eth_tester_provider.gas_price
assert isinstance(actual, int)
8 changes: 8 additions & 0 deletions tests/functional/utils/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from ethpm_types import HexBytes
from packaging.version import Version
from web3.types import Wei

from ape.exceptions import APINotImplementedError
from ape.utils.misc import (
Expand All @@ -11,6 +13,7 @@
is_zero_hex,
raises_not_implemented,
run_until_complete,
to_int,
)


Expand Down Expand Up @@ -99,3 +102,8 @@ def test_is_not_zero_address(owner):
assert not is_zero_hex(owner)
assert not is_zero_hex("MyContract")
assert not is_zero_hex("0x01")


@pytest.mark.parametrize("val", (5, "0x5", "0x05", "0x0005", HexBytes(5), Wei(5)))
def test_to_int(val):
assert to_int(val) == 5

0 comments on commit 56a0495

Please sign in to comment.