Skip to content

Commit

Permalink
fix: default gas strategy getter
Browse files Browse the repository at this point in the history
Signed-off-by: OjusWiZard <[email protected]>
  • Loading branch information
OjusWiZard committed Feb 3, 2025
1 parent 218c4aa commit 79d4b16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
14 changes: 12 additions & 2 deletions docs/api/plugins/aea_ledger_ethereum/ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ def get_base_fee_multiplier(

Returns multiplier value.

<a id="plugins.aea-ledger-ethereum.aea_ledger_ethereum.ethereum.get_default_gas_strategy"></a>

#### get`_`default`_`gas`_`strategy

```python
def get_default_gas_strategy(chain_id: int) -> Dict[str, Any]
```

Get default gas strategy for the given chain ID.

<a id="plugins.aea-ledger-ethereum.aea_ledger_ethereum.ethereum.estimate_priority_fee"></a>

#### estimate`_`priority`_`fee
Expand All @@ -53,7 +63,7 @@ Returns multiplier value.
def estimate_priority_fee(
web3_object: Web3, block_number: int,
default_priority_fee: Optional[int], fee_history_blocks: int,
fee_history_percentile: int, min_allowed_tip: Dict[int, int],
fee_history_percentile: int, min_allowed_tip: int,
priority_fee_increase_boundary: int) -> Optional[int]
```

Expand All @@ -67,7 +77,7 @@ Estimate priority fee from base fee.
def get_gas_price_strategy_eip1559(
max_gas_fast: int, fee_history_blocks: int, fee_history_percentile: int,
default_priority_fee: Optional[int], fallback_estimate: Dict[str, Wei],
min_allowed_tip: Dict[int, int], priority_fee_increase_boundary: int
min_allowed_tip: int, priority_fee_increase_boundary: int
) -> Callable[[Web3, TxParams], Dict[str, Wei]]
```

Expand Down
27 changes: 18 additions & 9 deletions plugins/aea-ledger-ethereum/aea_ledger_ethereum/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@

PRIORITY_FEE_INCREASE_BOUNDARY = 200 # percentage

DEFAULT_MIN_ALLOWED_TIP = {
# this is the minimum allowed max fee per gas on Gnosis
100: to_wei(1, "gwei"),
}
DEFAULT_MIN_ALLOWED_TIP = 1

DEFAULT_EIP1559_STRATEGY = {
"max_gas_fast": MAX_GAS_FAST,
Expand Down Expand Up @@ -183,13 +180,23 @@ def get_base_fee_multiplier(base_fee_gwei: Union[int, decimal.Decimal]) -> float
return BASE_FEE_MULTIPLIER[min(valid_fees)]


def get_default_gas_strategy(chain_id: int) -> Dict[str, Any]:
"""Get default gas strategy for the given chain ID."""
default_strategy = deepcopy(DEFAULT_GAS_PRICE_STRATEGIES)
if chain_id == 100:
# this is the minimum allowed max fee per gas on Gnosis
default_strategy[EIP1559]["min_allowed_tip"] = to_wei(1, "gwei")

return default_strategy


def estimate_priority_fee(
web3_object: Web3,
block_number: int,
default_priority_fee: Optional[int],
fee_history_blocks: int,
fee_history_percentile: int,
min_allowed_tip: Dict[int, int],
min_allowed_tip: int,
priority_fee_increase_boundary: int,
) -> Optional[int]:
"""Estimate priority fee from base fee."""
Expand All @@ -207,7 +214,7 @@ def estimate_priority_fee(
[
reward[0]
for reward in fee_history.get("reward", [])
if reward[0] >= min_allowed_tip.get(web3_object.eth.chain_id, 1)
if reward[0] >= min_allowed_tip
]
)
if len(rewards) == 0:
Expand Down Expand Up @@ -241,7 +248,7 @@ def get_gas_price_strategy_eip1559(
fee_history_percentile: int,
default_priority_fee: Optional[int],
fallback_estimate: Dict[str, Wei],
min_allowed_tip: Dict[int, int],
min_allowed_tip: int,
priority_fee_increase_boundary: int,
) -> Callable[[Web3, TxParams], Dict[str, Wei]]:
"""Get the gas price strategy."""
Expand Down Expand Up @@ -913,7 +920,7 @@ def __init__(self, **kwargs: Any):
) # pragma: nocover

self._gas_price_strategies: Dict[str, Dict] = kwargs.pop(
"gas_price_strategies", DEFAULT_GAS_PRICE_STRATEGIES
"gas_price_strategies", get_default_gas_strategy(self._chain_id)
)

self._poa_chain = kwargs.pop("poa_chain", False)
Expand Down Expand Up @@ -1093,7 +1100,9 @@ def _get_gas_price_strategy(
gas_price_strategy
]

parameters = cast(dict, DEFAULT_GAS_PRICE_STRATEGIES.get(gas_price_strategy))
parameters = cast(
dict, get_default_gas_strategy(self._chain_id).get(gas_price_strategy)
)
parameters.update(self._gas_price_strategies.get(gas_price_strategy, {}))
parameters.update(extra_config or {})
return gas_price_strategy, gas_price_strategy_getter(**parameters)
Expand Down
13 changes: 6 additions & 7 deletions plugins/aea-ledger-ethereum/tests/test_ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,23 +1080,23 @@ def test_estimate_priority_fee() -> None:
web3_mock = Mock()
web3_mock.eth.fee_history = Mock(return_value={"reward": []})
web3_mock.eth.chain_id = 100
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, {100: 1}, 1) is None
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, 1, 1) is None

# test a single reward
web3_mock.eth.fee_history = Mock(return_value={"reward": [[1]]})
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, {100: 1}, 1) == 1
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, 1, 1) == 1

# test 2 rewards
web3_mock.eth.fee_history = Mock(return_value={"reward": [[1], [2]]})
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, {100: 1}, 1) == 2
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, 1, 1) == 2

# If we have big increase in value, we could be considering "outliers" in our estimate
# Skip the low elements and take a new median
web3_mock.eth.fee_history = Mock(return_value={"reward": [[1], [10], [10000]]})
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, {100: 1}, 1) == 10000
assert estimate_priority_fee(web3_mock, 1, None, 11, 1, 1, 1) == 10000

# test the default priority fee
assert estimate_priority_fee(web3_mock, 1, 20, 11, 1, {100: 1}, 1) == 20
assert estimate_priority_fee(web3_mock, 1, 20, 11, 1, 1, 1) == 20

# set the fee history for block 38255060 on Gnosis as an example,
# which was causing issues with the gas estimation in `v1.61.0`:
Expand Down Expand Up @@ -1128,8 +1128,7 @@ def test_estimate_priority_fee() -> None:
}
)
assert (
estimate_priority_fee(web3_mock, 1, None, 20, 5, {100: 1000000000}, 200)
== 1000000000
estimate_priority_fee(web3_mock, 1, None, 20, 5, 1000000000, 200) == 1000000000
)


Expand Down

0 comments on commit 79d4b16

Please sign in to comment.