Skip to content

Conversation

@0xMuang
Copy link
Collaborator

@0xMuang 0xMuang commented Jan 27, 2026

Summary

Replace floating point arithmetic with EIP-1559 integer math in base fee estimation to prevent precision loss with large values.

Changes

eth/api.rs (Issue #133)

Before (floating point - precision loss possible):

let increase = (last_base_fee as f64 * 0.125 * (last_ratio - 0.5) * 2.0) as u128;

After (integer - EIP-1559 standard):

let gas_target = last_gas_limit / 2;
let delta = (last_gas_used - gas_target) as u128;
let change = last_base_fee * delta / gas_target as u128 / 8;

This follows the EIP-1559 specification:

  • gas_target = gas_limit / 2
  • base_fee_change = parent_base_fee × |gas_used - gas_target| / gas_target / BASE_FEE_MAX_CHANGE_DENOMINATOR
  • BASE_FEE_MAX_CHANGE_DENOMINATOR = 8

Test Plan

  • cargo check passes
  • cargo fmt --check passes

Closes #133

Replace floating point arithmetic with EIP-1559 integer math
to prevent precision loss with large base fee values.

Before (floating point):
  increase = (base_fee as f64 * 0.125 * (ratio - 0.5) * 2.0) as u128

After (integer):
  change = base_fee * (gas_used - gas_target) / gas_target / 8
@0xMuang 0xMuang self-assigned this Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Floating point arithmetic in base fee calculation

2 participants