-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AUTO-8804: create chain specific modules for l1 gas calculations (#11896
) * AUTO-8804: added a chain specific module for automation * add modules * create specific modules for different chains * implement modules * addressed some feedbacks * update tests * generate wrappers * fix foundry * run yarn prettier:write * remove unnecessary import * remove unnecessary checks * update gas overheads to pass tests * regen wrappers * fix sonarcube issues * address some comments * adjust gas overheads * prettier * remove only * adjust gas overhead again * dont use const * rebase to latest and add chainmodule getter --------- Co-authored-by: lei shi <lei.shi@smartcontract.com> Co-authored-by: Akshay Aggarwal <akshay.aggarwal@smartcontract.com>
- Loading branch information
1 parent
efdb8af
commit 9a687b0
Showing
21 changed files
with
1,002 additions
and
244 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
contracts/src/v0.8/automation/dev/chains/ArbitrumModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.19; | ||
|
||
import {ArbSys} from "../../../vendor/@arbitrum/nitro-contracts/src/precompiles/ArbSys.sol"; | ||
import {ArbGasInfo} from "../../../vendor/@arbitrum/nitro-contracts/src/precompiles/ArbGasInfo.sol"; | ||
import {ChainModuleBase} from "./ChainModuleBase.sol"; | ||
|
||
contract ArbitrumModule is ChainModuleBase { | ||
/// @dev ARB_SYS_ADDR is the address of the ArbSys precompile on Arbitrum. | ||
/// @dev reference: https://github.com/OffchainLabs/nitro/blob/v2.0.14/contracts/src/precompiles/ArbSys.sol#L10 | ||
address private constant ARB_SYS_ADDR = 0x0000000000000000000000000000000000000064; | ||
ArbSys private constant ARB_SYS = ArbSys(ARB_SYS_ADDR); | ||
|
||
/// @dev ARB_GAS_ADDR is the address of the ArbGasInfo precompile on Arbitrum. | ||
/// @dev reference: https://github.com/OffchainLabs/nitro/blob/v2.0.14/contracts/src/precompiles/ArbGasInfo.sol#L10 | ||
address private constant ARB_GAS_ADDR = 0x000000000000000000000000000000000000006C; | ||
ArbGasInfo private constant ARB_GAS = ArbGasInfo(ARB_GAS_ADDR); | ||
|
||
function blockHash(uint256 n) external view override returns (bytes32) { | ||
uint256 blockNum = ARB_SYS.arbBlockNumber(); | ||
if (n >= blockNum || blockNum - n > 256) { | ||
return ""; | ||
} | ||
return ARB_SYS.arbBlockHash(n); | ||
} | ||
|
||
function blockNumber() external view override returns (uint256) { | ||
return ARB_SYS.arbBlockNumber(); | ||
} | ||
|
||
function getCurrentL1Fee() external view override returns (uint256) { | ||
return ARB_GAS.getCurrentTxL1GasFees(); | ||
} | ||
|
||
function getMaxL1Fee(uint256 dataSize) external view override returns (uint256) { | ||
(, uint256 perL1CalldataUnit, , , , ) = ARB_GAS.getPricesInWei(); | ||
return perL1CalldataUnit * dataSize * 16; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
contracts/src/v0.8/automation/dev/chains/ChainModuleBase.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.19; | ||
|
||
import {IChainModule} from "../interfaces/v2_2/IChainModule.sol"; | ||
|
||
contract ChainModuleBase is IChainModule { | ||
function blockNumber() external view virtual returns (uint256) { | ||
return block.number; | ||
} | ||
|
||
function blockHash(uint256 n) external view virtual returns (bytes32) { | ||
if (n >= block.number || block.number - n > 256) { | ||
return ""; | ||
} | ||
return blockhash(n); | ||
} | ||
|
||
function getCurrentL1Fee() external view virtual returns (uint256) { | ||
return 0; | ||
} | ||
|
||
function getMaxL1Fee(uint256) external view virtual returns (uint256) { | ||
return 0; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
contracts/src/v0.8/automation/dev/chains/OptimismModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.19; | ||
|
||
import {OVM_GasPriceOracle} from "../../../vendor/@eth-optimism/contracts/v0.8.9/contracts/L2/predeploys/OVM_GasPriceOracle.sol"; | ||
import {ChainModuleBase} from "./ChainModuleBase.sol"; | ||
|
||
contract OptimismModule is ChainModuleBase { | ||
/// @dev OP_L1_DATA_FEE_PADDING includes 35 bytes for L1 data padding for Optimism and BASE | ||
bytes private constant OP_L1_DATA_FEE_PADDING = | ||
hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; | ||
/// @dev OVM_GASPRICEORACLE_ADDR is the address of the OVM_GasPriceOracle precompile on Optimism. | ||
/// @dev reference: https://community.optimism.io/docs/developers/build/transaction-fees/#estimating-the-l1-data-fee | ||
address private constant OVM_GASPRICEORACLE_ADDR = 0x420000000000000000000000000000000000000F; | ||
OVM_GasPriceOracle private constant OVM_GASPRICEORACLE = OVM_GasPriceOracle(OVM_GASPRICEORACLE_ADDR); | ||
|
||
function getCurrentL1Fee() external view override returns (uint256) { | ||
return OVM_GASPRICEORACLE.getL1Fee(bytes.concat(msg.data, OP_L1_DATA_FEE_PADDING)); | ||
} | ||
|
||
function getMaxL1Fee(uint256 dataSize) external view override returns (uint256) { | ||
// fee is 4 per 0 byte, 16 per non-zero byte. Worst case we can have all non zero-bytes. | ||
// Instead of setting bytes to non-zero, we initialize 'new bytes' of length 4*dataSize to cover for zero bytes. | ||
bytes memory txCallData = new bytes(4 * dataSize); | ||
return OVM_GASPRICEORACLE.getL1Fee(bytes.concat(txCallData, OP_L1_DATA_FEE_PADDING)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.19; | ||
|
||
import {IScrollL1GasPriceOracle} from "../../../vendor/@scroll-tech/contracts/src/L2/predeploys/IScrollL1GasPriceOracle.sol"; | ||
import {ChainModuleBase} from "./ChainModuleBase.sol"; | ||
|
||
contract ScrollModule is ChainModuleBase { | ||
/// @dev SCROLL_L1_FEE_DATA_PADDING includes 120 bytes for L1 data padding for Optimism | ||
/// @dev according to testing, this padding allows automation registry to properly estimates L1 data fee with 3-5% buffer | ||
/// @dev this MAY NOT work for a different product and this may get out of date if transmit function is changed | ||
bytes private constant SCROLL_L1_FEE_DATA_PADDING = | ||
hex"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; | ||
/// @dev SCROLL_ORACLE_ADDR is the address of the L1GasPriceOracle precompile on Optimism. | ||
/// @dev reference: https://docs.scroll.io/en/developers/transaction-fees-on-scroll/#estimating-the-l1-data-fee | ||
address private constant SCROLL_ORACLE_ADDR = 0x5300000000000000000000000000000000000002; | ||
IScrollL1GasPriceOracle private constant SCROLL_ORACLE = IScrollL1GasPriceOracle(SCROLL_ORACLE_ADDR); | ||
|
||
function getCurrentL1Fee() external view override returns (uint256) { | ||
return SCROLL_ORACLE.getL1Fee(bytes.concat(msg.data, SCROLL_L1_FEE_DATA_PADDING)); | ||
} | ||
|
||
function getMaxL1Fee(uint256 dataSize) external view override returns (uint256) { | ||
// fee is 4 per 0 byte, 16 per non-zero byte. Worst case we can have all non zero-bytes. | ||
// Instead of setting bytes to non-zero, we initialize 'new bytes' of length 4*dataSize to cover for zero bytes. | ||
// this is the same as OP. | ||
bytes memory txCallData = new bytes(4 * dataSize); | ||
return SCROLL_ORACLE.getL1Fee(bytes.concat(txCallData, SCROLL_L1_FEE_DATA_PADDING)); | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
contracts/src/v0.8/automation/dev/interfaces/v2_2/IAutomationRegistryMaster.sol
Large diffs are not rendered by default.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
contracts/src/v0.8/automation/dev/interfaces/v2_2/IChainModule.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.0; | ||
|
||
interface IChainModule { | ||
// retrieve the native block number of a chain. e.g. L2 block number on Arbitrum | ||
function blockNumber() external view returns (uint256); | ||
|
||
// retrieve the native block hash of a chain. | ||
function blockHash(uint256) external view returns (bytes32); | ||
|
||
// retrieve the L1 data fee for a L2 transaction. it should return 0 for L1 chains and | ||
// L2 chains which don't have L1 fee component. it uses msg.data to estimate L1 data so | ||
// it must be used with a transaction. | ||
function getCurrentL1Fee() external view returns (uint256); | ||
|
||
// retrieve the L1 data fee for a L2 simulation. it should return 0 for L1 chains and | ||
// L2 chains which don't have L1 fee component. | ||
function getMaxL1Fee(uint256 dataSize) external view returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.