Skip to content

Commit

Permalink
Update pricing function
Browse files Browse the repository at this point in the history
  • Loading branch information
anikaraghu committed Dec 15, 2023
1 parent 6506438 commit 6e4e12c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
39 changes: 29 additions & 10 deletions packages/contracts-bedrock/src/L2/GasPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ contract GasPriceOracle is ISemver {
uint256 public constant DECIMALS = 6;

/// @notice Semantic version.
/// @custom:semver 1.1.0
string public constant version = "1.1.0";
/// @custom:semver 1.2.0
string public constant version = "1.2.0";

/// @notice Computes the L1 portion of the fee based on the size of the rlp encoded input
/// transaction, the current L1 base fee, and the various dynamic parameters.
/// @param _data Unsigned fully RLP-encoded transaction to get the L1 fee for.
/// @return L1 fee that should be paid for the tx
function getL1Fee(bytes memory _data) external view returns (uint256) {
uint256 l1GasUsed = getL1GasUsed(_data);
uint256 l1Fee = l1GasUsed * l1BaseFee();
uint256 scaledBaseFee = baseFeeScalar() * l1BaseFee();
uint256 scaledBlobBaseFee = blobBaseFeeScalar() * blobBaseFee();
uint256 divisor = 10 ** DECIMALS;
uint256 unscaled = l1Fee * scalar();
uint256 unscaled = l1GasUsed * (scaledBaseFee + scaledBlobBaseFee);
uint256 scaled = unscaled / divisor;
return scaled;
}
Expand All @@ -52,12 +53,14 @@ contract GasPriceOracle is ISemver {
return block.basefee;
}

/// @custom:legacy
/// @notice Retrieves the current fee overhead.
/// @return Current fee overhead.
function overhead() public view returns (uint256) {
return L1Block(Predeploys.L1_BLOCK_ATTRIBUTES).l1FeeOverhead();
}

/// @custom:legacy
/// @notice Retrieves the current fee scalar.
/// @return Current fee scalar.
function scalar() public view returns (uint256) {
Expand All @@ -70,17 +73,33 @@ contract GasPriceOracle is ISemver {
return L1Block(Predeploys.L1_BLOCK_ATTRIBUTES).basefee();
}

/// @notice Retrieves the current blob base fee.
/// @return Current blob base fee.
function blobBaseFee() public view returns (uint256) {
return L1Block(Predeploys.L1_BLOCK_ATTRIBUTES).blobBaseFee();
}

/// @notice Retrieves the current base fee scalar.
/// @return Current base fee scalar.
function baseFeeScalar() public view returns (uint256) {
return L1Block(Predeploys.L1_BLOCK_ATTRIBUTES).baseFeeScalar();
}

/// @notice Retrieves the current blob base fee scalar.
/// @return Current blob base fee scalar.
function blobBaseFeeScalar() public view returns (uint256) {
return L1Block(Predeploys.L1_BLOCK_ATTRIBUTES).blobBaseFeeScalar();
}

/// @custom:legacy
/// @notice Retrieves the number of decimals used in the scalar.
/// @return Number of decimals used in the scalar.
function decimals() public pure returns (uint256) {
return DECIMALS;
}

/// @notice Computes the amount of L1 gas used for a transaction. Adds the overhead which
/// represents the per-transaction gas overhead of posting the transaction and state
/// roots to L1. Adds 68 bytes of padding to account for the fact that the input does
/// not have a signature.
/// @notice Computes the amount of L1 gas used for a transaction. Adds 68 bytes
/// of padding to account for the fact that the input does not have a signature.
/// @param _data Unsigned fully RLP-encoded transaction to get the L1 gas for.
/// @return Amount of L1 gas used to publish the transaction.
function getL1GasUsed(bytes memory _data) public view returns (uint256) {
Expand All @@ -93,7 +112,7 @@ contract GasPriceOracle is ISemver {
total += 16;
}
}
uint256 unsigned = total + overhead();
return unsigned + (68 * 16);
uint256 compressedTxSize = total / 16;
return compressedTxSize + (68 * 16);
}
}
27 changes: 15 additions & 12 deletions packages/contracts-bedrock/src/L2/L1Block.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ contract L1Block is ISemver {
bytes32 public batcherHash;

/// @notice The overhead value applied to the L1 portion of the transaction fee.
/// @custom:legacy
uint256 public l1FeeOverhead;

/// @notice The scalar value applied to the L1 portion of the transaction fee.
/// @custom:legacy
uint256 public l1FeeScalar;

/// @notice The scalar value applied to the L1 base fee portion of the blob-capable L1 cost func
uint256 public basefeeScalar;
uint256 public baseFeeScalar;

/// @notice The scalar value applied to the L1 blob base fee portion of the blob-capable L1 cost func
uint256 public blobBasefeeScalar;
uint256 public blobBaseFeeScalar;

/// @notice The latest L1 blob basefee.
uint256 public blobBasefee;
uint256 public blobBaseFee;

/// @custom:semver 1.2.0
string public constant version = "1.2.0";

/// @custom:legacy
/// @notice Updates the L1 block values.
/// @param _number L1 blocknumber.
/// @param _timestamp L1 timestamp.
Expand Down Expand Up @@ -87,22 +90,22 @@ contract L1Block is ISemver {
/// @param _number L1 blocknumber.
/// @param _timestamp L1 timestamp.
/// @param _basefee L1 basefee.
/// @param _blobBasefee L1 blobBasefee.
/// @param _blobBaseFee L1 blobBaseFee.
/// @param _hash L1 blockhash.
/// @param _sequenceNumber Number of L2 blocks since epoch start.
/// @param _batcherHash Versioned hash to authenticate batcher by.
/// @param _basefeeScalar L1 base fee scalar
/// @param _blobBasefeeScalar L1 blob base fee scalar
/// @param _baseFeeScalar L1 base fee scalar
/// @param _blobBaseFeeScalar L1 blob base fee scalar
function setL1BlockValuesV2(
uint64 _number,
uint64 _timestamp,
uint256 _basefee,
uint256 _blobBasefee,
uint256 _blobBaseFee,
bytes32 _hash,
uint64 _sequenceNumber,
bytes32 _batcherHash,
uint256 _basefeeScalar,
uint256 _blobBasefeeScalar
uint256 _baseFeeScalar,
uint256 _blobBaseFeeScalar
)
external
{
Expand All @@ -111,11 +114,11 @@ contract L1Block is ISemver {
number = _number;
timestamp = _timestamp;
basefee = _basefee;
blobBasefee = _blobBasefee;
blobBasefee = _blobBaseFee;
hash = _hash;
sequenceNumber = _sequenceNumber;
batcherHash = _batcherHash;
basefeeScalar = _basefeeScalar;
blobBasefeeScalar = _blobBasefeeScalar;
basefeeScalar = _baseFeeScalar;
blobBasefeeScalar = _blobBaseFeeScalar;
}
}

0 comments on commit 6e4e12c

Please sign in to comment.