diff --git a/nucypher/blockchain/eth/interfaces.py b/nucypher/blockchain/eth/interfaces.py index 9c4540e74..04491e78f 100644 --- a/nucypher/blockchain/eth/interfaces.py +++ b/nucypher/blockchain/eth/interfaces.py @@ -62,7 +62,7 @@ from nucypher.blockchain.eth.sol.compile.types import SourceBundle from nucypher.blockchain.eth.utils import get_transaction_name, prettify_eth_amount from nucypher.characters.control.emitters import JSONRPCStdoutEmitter, StdoutEmitter from nucypher.utilities.ethereum import encode_constructor_arguments -from nucypher.utilities.gas_strategies import datafeed_fallback_gas_price_strategy, WEB3_GAS_STRATEGIES +from nucypher.utilities.gas_strategies import construct_datafeed_fallback_strategy, WEB3_GAS_STRATEGIES from nucypher.utilities.logging import GlobalLoggerSettings, Logger Web3Providers = Union[IPCProvider, WebsocketProvider, HTTPProvider, EthereumTester] # TODO: Move to types.py @@ -284,9 +284,8 @@ class BlockchainInterface: if gas_strategy: reported_gas_strategy = f"fixed/{gas_strategy.name}" elif isinstance(self.client, InfuraClient): - gas_strategy = datafeed_fallback_gas_price_strategy - self.gas_strategy = 'fast' - reported_gas_strategy = "datafeed/fast" + gas_strategy = construct_datafeed_fallback_strategy(speed=self.gas_strategy) + reported_gas_strategy = f"datafeed/{self.gas_strategy}" else: reported_gas_strategy = f"web3/{self.gas_strategy}" gas_strategy = self.get_gas_strategy(self.gas_strategy) diff --git a/nucypher/utilities/datafeeds.py b/nucypher/utilities/datafeeds.py index ba243bb9a..bb3e7069d 100644 --- a/nucypher/utilities/datafeeds.py +++ b/nucypher/utilities/datafeeds.py @@ -74,10 +74,10 @@ class EthereumGasPriceDatafeed(Datafeed): return gas_price_wei @classmethod - def construct_gas_strategy(cls): + def construct_gas_strategy(cls, speed: Optional[str] = None): def gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei: feed = cls() - gas_price = feed.get_gas_price() + gas_price = feed.get_gas_price(speed=speed) return gas_price return gas_price_strategy diff --git a/nucypher/utilities/gas_strategies.py b/nucypher/utilities/gas_strategies.py index d60f96dbd..7cf11292f 100644 --- a/nucypher/utilities/gas_strategies.py +++ b/nucypher/utilities/gas_strategies.py @@ -36,20 +36,22 @@ class GasStrategyError(RuntimeError): # -def datafeed_fallback_gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei: - feeds = (EtherchainGasPriceDatafeed, UpvestGasPriceDatafeed) +def construct_datafeed_fallback_strategy(speed: Optional[str] = None) -> Callable: + def datafeed_fallback_gas_price_strategy(web3: Web3, transaction_params: TxParams = None) -> Wei: + feeds = (EtherchainGasPriceDatafeed, UpvestGasPriceDatafeed) - for gas_price_feed_class in feeds: - try: - gas_strategy = gas_price_feed_class.construct_gas_strategy() - gas_price = gas_strategy(web3, transaction_params) - except Datafeed.DatafeedError: - continue + for gas_price_feed_class in feeds: + try: + gas_strategy = gas_price_feed_class.construct_gas_strategy(speed=speed) + gas_price = gas_strategy(web3, transaction_params) + except Datafeed.DatafeedError: + continue + else: + return gas_price else: - return gas_price - else: - # Worst-case scenario, we get the price from the ETH node itself - return rpc_gas_price_strategy(web3, transaction_params) + # Worst-case scenario, we get the price from the ETH node itself + return rpc_gas_price_strategy(web3, transaction_params) + return datafeed_fallback_gas_price_strategy # diff --git a/tests/unit/test_datafeeds.py b/tests/unit/test_datafeeds.py index 31fdc189f..dfc16ee50 100644 --- a/tests/unit/test_datafeeds.py +++ b/tests/unit/test_datafeeds.py @@ -27,7 +27,7 @@ from nucypher.utilities.datafeeds import ( Datafeed, UpvestGasPriceDatafeed, EthereumGasPriceDatafeed) -from nucypher.utilities.gas_strategies import datafeed_fallback_gas_price_strategy +from nucypher.utilities.gas_strategies import construct_datafeed_fallback_strategy etherchain_json = { "safeLow": "99.0", @@ -166,6 +166,7 @@ def test_datafeed_fallback_gas_price_strategy(): # In normal circumstances, the first datafeed (Etherchain) will return the gas price with patch('nucypher.utilities.datafeeds.EtherchainGasPriceDatafeed.construct_gas_strategy', return_value=mock_gas_strategy): + datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy() assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price # If the first datafeed in the chain fails, we resort to the second one @@ -173,6 +174,7 @@ def test_datafeed_fallback_gas_price_strategy(): side_effect=Datafeed.DatafeedError): with patch('nucypher.utilities.datafeeds.UpvestGasPriceDatafeed.construct_gas_strategy', return_value=mock_gas_strategy): + datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy() assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price # If both datafeeds fail, we fallback to the rpc_gas_price_strategy @@ -181,4 +183,5 @@ def test_datafeed_fallback_gas_price_strategy(): with patch('nucypher.utilities.datafeeds.UpvestGasPriceDatafeed._probe_feed', side_effect=Datafeed.DatafeedError): with patch('nucypher.utilities.gas_strategies.rpc_gas_price_strategy', side_effect=mock_gas_strategy): + datafeed_fallback_gas_price_strategy = construct_datafeed_fallback_strategy() assert datafeed_fallback_gas_price_strategy("web3", "tx") == mocked_gas_price