class DynamicGasPrice(GasPrice): GWEI = 1000000000 def __init__(self, arguments): self.gas_station = None if arguments.ethgasstation_api_key: self.gas_station = EthGasStation( refresh_interval=60, expiry=600, api_key=arguments.ethgasstation_api_key) elif arguments.etherchain_gas: self.gas_station = EtherchainOrg(refresh_interval=60, expiry=600) elif arguments.poanetwork_gas: self.gas_station = POANetwork(refresh_interval=60, expiry=600, alt_url=arguments.poanetwork_url) def get_gas_price(self, time_elapsed: int) -> Optional[int]: # start with standard price plus backup in case gas price API is down, then do fast if 0 <= time_elapsed <= 60: standard_price = self.gas_station.standard_price( ) if self.gas_station else None if standard_price is not None: return int(standard_price * 1.1) else: return self.default_gas_pricing(time_elapsed) # move to fast after a minute else: fast_price = self.gas_station.fast_price( ) if self.gas_station else None if fast_price is not None: return int(fast_price * 1.1) else: return self.default_gas_pricing(time_elapsed) # default gas pricing when EthGasStation feed is down def default_gas_pricing(self, time_elapsed: int): return IncreasingGasPrice(initial_price=5 * self.GWEI, increase_by=10 * self.GWEI, every_secs=60, max_price=100 * self.GWEI).get_gas_price(time_elapsed)
def test_poanetwork_integration(): logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.DEBUG) logging.getLogger('urllib3.connectionpool').setLevel(logging.INFO) logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.INFO) poa = POANetwork(10, 600) while True: safe_low_price = poa.safe_low_price() logging.info(safe_low_price) standard_price = poa.standard_price() logging.info(standard_price) fast_price = poa.fast_price() logging.info(fast_price) if safe_low_price is not None and standard_price is not None and fast_price is not None: break time.sleep(1)