alloy_eips/
eip7623.rs

1//! Constants and utils for calldata cost.
2//!
3//! See also [EIP-7623](https://eips.ethereum.org/EIPS/eip-7623): Increase calldata cost
4
5/// The standard cost of calldata token.
6pub const STANDARD_TOKEN_COST: u64 = 4;
7
8/// The cost of a non-zero byte in calldata.
9pub const NON_ZERO_BYTE_DATA_COST: u64 = 68;
10
11/// The multiplier for a non zero byte in calldata.
12pub const NON_ZERO_BYTE_MULTIPLIER: u64 = NON_ZERO_BYTE_DATA_COST / STANDARD_TOKEN_COST;
13
14/// The cost floor per token
15pub const TOTAL_COST_FLOOR_PER_TOKEN: u64 = 10;
16
17/// Retrieve the total number of tokens in calldata.
18#[inline]
19pub fn tokens_in_calldata(input: &[u8]) -> u64 {
20    let zero_data_len = input.iter().filter(|v| **v == 0).count() as u64;
21    let non_zero_data_len = input.len() as u64 - zero_data_len;
22    zero_data_len + non_zero_data_len * NON_ZERO_BYTE_MULTIPLIER
23}
24
25/// Calculate the transaction cost floor as specified in EIP-7623.
26///
27/// Any transaction with a gas limit below this value is considered invalid.
28#[inline]
29pub fn transaction_floor_cost(tokens_in_calldata: u64) -> u64 {
30    21_000 + TOTAL_COST_FLOOR_PER_TOKEN * tokens_in_calldata
31}