Skip to content

Commit

Permalink
fix: arbitrum chain id check in all ChainSpecificUtil methods (#10756)
Browse files Browse the repository at this point in the history
* fix: arbitrum chain id check in all CSU methods

The arbitrum chain id check needs to be the same in all of the methods
exported in the ChainSpecificUtil contract. Prior to this change
getBlockNumber, getCurrentTxL1GasFees and getL1CalldataGasCost did not
correctly check for all possible arbitrum chain ids.

To prevent this kind of error in the future, a new private function
isArbitrumChainId was created and used in all methods that need to check
the chain id.

* chore: prettier
  • Loading branch information
makramkd authored Sep 27, 2023
1 parent 6b1d465 commit 8ed253b
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 30 deletions.
22 changes: 14 additions & 8 deletions contracts/src/v0.8/ChainSpecificUtil.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ library ChainSpecificUtil {

function getBlockhash(uint64 blockNumber) internal view returns (bytes32) {
uint256 chainid = block.chainid;
if (
chainid == ARB_MAINNET_CHAIN_ID ||
chainid == ARB_GOERLI_TESTNET_CHAIN_ID ||
chainid == ARB_SEPOLIA_TESTNET_CHAIN_ID
) {
if (isArbitrumChainId(chainid)) {
if ((getBlockNumber() - blockNumber) > 256 || blockNumber >= getBlockNumber()) {
return "";
}
Expand All @@ -33,15 +29,15 @@ library ChainSpecificUtil {

function getBlockNumber() internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
if (isArbitrumChainId(chainid)) {
return ARBSYS.arbBlockNumber();
}
return block.number;
}

function getCurrentTxL1GasFees() internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
if (isArbitrumChainId(chainid)) {
return ARBGAS.getCurrentTxL1GasFees();
}
return 0;
Expand All @@ -53,12 +49,22 @@ library ChainSpecificUtil {
*/
function getL1CalldataGasCost(uint256 calldataSizeBytes) internal view returns (uint256) {
uint256 chainid = block.chainid;
if (chainid == ARB_MAINNET_CHAIN_ID || chainid == ARB_GOERLI_TESTNET_CHAIN_ID) {
if (isArbitrumChainId(chainid)) {
(, uint256 l1PricePerByte, , , , ) = ARBGAS.getPricesInWei();
// see https://developer.arbitrum.io/devs-how-tos/how-to-estimate-gas#where-do-we-get-all-this-information-from
// for the justification behind the 140 number.
return l1PricePerByte * (calldataSizeBytes + 140);
}
return 0;
}

/**
* @notice Return true if and only if the provided chain ID is an Arbitrum chain ID.
*/
function isArbitrumChainId(uint256 chainId) internal pure returns (bool) {
return
chainId == ARB_MAINNET_CHAIN_ID ||
chainId == ARB_GOERLI_TESTNET_CHAIN_ID ||
chainId == ARB_SEPOLIA_TESTNET_CHAIN_ID;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ed253b

Please sign in to comment.