Skip to content

Commit

Permalink
Merge pull request #770 from Divya-Solulab/fix/estimate-priority-fee
Browse files Browse the repository at this point in the history
fix: rewards issue
  • Loading branch information
Adamantios authored Nov 14, 2024
2 parents 1a454d8 + d67b793 commit 8fa3599
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions plugins/aea-ledger-ethereum/aea_ledger_ethereum/ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
POLYGON_GAS_ENDPOINT = "https://gasstation-mainnet.matic.network/v2"
MAX_GAS_FAST = 1500
RPC_CALL_MAX_WORKERS = 1
N_RETRIES = 3
PERCENTILE_INCREASE = 5

# How many blocks to consider for priority fee estimation
FEE_HISTORY_BLOCKS = 10
Expand Down Expand Up @@ -174,23 +176,30 @@ def estimate_priority_fee(
if default_priority_fee is not None:
return default_priority_fee

fee_history = web3_object.eth.fee_history(
fee_history_blocks, block_number, [fee_history_percentile] # type: ignore
)

# This is going to break if more percentiles are introduced in the future,
# i.e., `fee_history_percentile` param becomes a `List[int]`.
rewards = sorted(
[reward[0] for reward in fee_history.get("reward", []) if reward[0] > 0]
)
if len(rewards) == 0:
for _ in range(N_RETRIES):
fee_history = web3_object.eth.fee_history(
fee_history_blocks, block_number, [fee_history_percentile] # type: ignore
)
# This is going to break if more percentiles are introduced in the future,
# i.e., `fee_history_percentile` param becomes a `List[int]`.
rewards = sorted(
[reward[0] for reward in fee_history.get("reward", []) if reward[0] > 0]
)
# we need atleast 2 rewards to proceed further
if len(rewards) >= 2:
break
# Increment percentile for next attempt
fee_history_percentile = min(100, fee_history_percentile + PERCENTILE_INCREASE)

# Return None if fewer than 2 rewards after retries
if len(rewards) < 2:
return None

# Calculate percentage increases from between ordered list of fees
percentage_increases = [
((j - i) / i) * 100 if i != 0 else 0 for i, j in zip(rewards[:-1], rewards[1:])
]
highest_increase = max(*percentage_increases)
highest_increase = max(percentage_increases)
highest_increase_index = percentage_increases.index(highest_increase)

values = rewards.copy()
Expand Down

0 comments on commit 8fa3599

Please sign in to comment.