From 04f5e2f633b1ec60811cc14d8f04c0b932563d42 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 16 Feb 2021 19:01:06 +0100 Subject: [PATCH] Configurable Gas Price details We let DEFAULT_GAS_PRICE used for transaction submission and MAX_GAS_PRICE used as a limit for gas price bumps to be configurable instead of hardcoding them. The default values will be set the same as previously hardcoded: DEFAULT_GAS_PRICE = 100 Gwei MAX_GAS_PRICE = 600 Gwei Having it configurable is better from multi-environment perspective, as it doesn't make sense to start with 100 Gwei on Ropsten. It also gives possibility to easily tweak settings for mainnet without the need to rebuild the code. Following properties can be used to configure it: DEFAULT_GAS_PRICE_GWEI and MAX_GAS_PRICE_GWEI. Note that configured values are expected to be provided in Gwei (not wei!). --- maintainer/maintainer/config/.sample.env | 6 ++++++ maintainer/maintainer/config/__init__.py | 2 ++ maintainer/maintainer/ethereum/shared.py | 12 +++++++----- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/maintainer/maintainer/config/.sample.env b/maintainer/maintainer/config/.sample.env index 815c7a25..6085a2c5 100644 --- a/maintainer/maintainer/config/.sample.env +++ b/maintainer/maintainer/config/.sample.env @@ -40,3 +40,9 @@ SUMMA_RELAY_INFURA_KEY="" # no default # target relay smart contract address SUMMA_RELAY_CONTRACT=0x... + +# default: 100 +DEFAULT_GAS_PRICE_GWEI=100 + +# default: 600 +MAX_GAS_PRICE_GWEI=600 diff --git a/maintainer/maintainer/config/__init__.py b/maintainer/maintainer/config/__init__.py index 96bd6344..58200c6e 100644 --- a/maintainer/maintainer/config/__init__.py +++ b/maintainer/maintainer/config/__init__.py @@ -86,6 +86,8 @@ def set() -> RelayConfig: BCOIN_WS_URL=f'ws://{BCOIN_HOST}:{BCOIN_PORT}', PROJECT_ID=os.environ.get('SUMMA_RELAY_INFURA_KEY', ''), CONTRACT=os.environ.get('SUMMA_RELAY_CONTRACT', ''), + DEFAULT_GAS_PRICE_GWEI=os.environ.get('DEFAULT_GAS_PRICE_GWEI', 100), + MAX_GAS_PRICE_GWEI=os.environ.get('MAX_GAS_PRICE_GWEI', 600), ) return CONFIG diff --git a/maintainer/maintainer/ethereum/shared.py b/maintainer/maintainer/ethereum/shared.py index ee1205fc..b2d4f4c9 100644 --- a/maintainer/maintainer/ethereum/shared.py +++ b/maintainer/maintainer/ethereum/shared.py @@ -11,11 +11,8 @@ logger = logging.getLogger('root.summa_relay.shared_eth') - GWEI = 1000000000 DEFAULT_GAS = 500_000 -DEFAULT_GAS_PRICE = 100 * GWEI -MAX_GAS_PRICE = 600 * GWEI CONNECTION: ethrpc.BaseRPC NONCE: Iterator[int] # yields ints, takes no sends @@ -32,6 +29,7 @@ def _nonce(i: int) -> Iterator[int]: async def init() -> None: '''Set up a connection to the interwebs''' global CONNECTION + global MAX_GAS_PRICE c = config.get() network = c['NETWORK'] @@ -39,6 +37,8 @@ async def init() -> None: uri = c['ETHER_URL'] force_https = project_id != '' + MAX_GAS_PRICE = int(c['MAX_GAS_PRICE_GWEI']) * GWEI + logger.info(f'contract is {c["CONTRACT"]}') CONNECTION = ethrpc.get_client( @@ -213,10 +213,12 @@ def _adjust_gas_price(gas_price: int) -> int: def _compute_tx_gas_price(tx_nonce, tx_ticks): '''Compute the proper gas price, adjusting for other pending txes and how long this tx has been pending, taking the max gas price into account.''' + defaultGasPrice = int(config.get()['DEFAULT_GAS_PRICE_GWEI']) * GWEI + gas_price_factor = max((LATEST_PENDING_NONCE - tx_nonce + 1) * tx_ticks, 0) - adjusted_gas_price = round((1 + gas_price_factor * 0.5) * DEFAULT_GAS_PRICE) + adjusted_gas_price = round((1 + gas_price_factor * 0.5) * defaultGasPrice) - return max(min(adjusted_gas_price, MAX_GAS_PRICE), DEFAULT_GAS_PRICE) + return max(min(adjusted_gas_price, MAX_GAS_PRICE), defaultGasPrice) async def _track_tx_result(tx: UnsignedEthTx, tx_id: str, ticks: int = 0) -> None: '''Keep track of the result of a transaction by polling every 25 seconds'''