From 89224fc05f107cca63f5d4bbfdb742adda60975f Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 11 Jun 2024 19:49:14 +0200 Subject: [PATCH] chore(contracts): refactor and dynamic values --- script/RollupGreeter.s.sol | 4 +--- src/GlobalGreeter.sol | 8 ++++---- src/RollupGreeter.sol | 16 ++++------------ test/GlobalGreeter.t.sol | 2 +- test/RollupGreeter.t.sol | 14 +++++++------- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/script/RollupGreeter.s.sol b/script/RollupGreeter.s.sol index 1c1cde5..41ee6e9 100644 --- a/script/RollupGreeter.s.sol +++ b/script/RollupGreeter.s.sol @@ -17,14 +17,12 @@ contract DeployRollupGreeter is Script { address portalAddress = vm.envAddress("PORTAL_ADDRESS"); // Get the address of the global greeter from the environment address globalGreeterAddress = vm.envAddress("GLOBAL_GREETER_ADDRESS"); - // Get the Omni chain ID from the environment - uint64 omniChainId = uint64(vm.envUint("GLOBAL_CHAIN_ID")); // Start broadcasting the transaction vm.startBroadcast(); // Deploy the RollupGreeter contract - RollupGreeter rollupGreeter = new RollupGreeter(portalAddress, omniChainId, globalGreeterAddress); + RollupGreeter rollupGreeter = new RollupGreeter(portalAddress, globalGreeterAddress); console.log("Deployed RollupGreeter at:", address(rollupGreeter)); // Stop broadcasting the transaction diff --git a/src/GlobalGreeter.sol b/src/GlobalGreeter.sol index d3b8dea..d998e9f 100644 --- a/src/GlobalGreeter.sol +++ b/src/GlobalGreeter.sol @@ -13,9 +13,9 @@ import {Greeting} from "src/Greeting.sol"; */ contract GlobalGreeter is XApp { /** - * @notice Gas limit used for a cross-chain greet call + * @notice Gas limit used for a cross-chain greet call at destination */ - uint64 public constant GAS_LIMIT = 200_000; + uint64 public constant DESTINATION_TX_GAS_LIMIT = 120_000; /** * @notice The latest greeting recorded by the contract @@ -27,7 +27,7 @@ contract GlobalGreeter is XApp { * @dev Initializes a new GlobalGreeter contract with the specified portal address * @param portal Address of the portal or relay used for cross-chain communication */ - constructor(address portal) XApp(portal, ConfLevel.Finalized) {} + constructor(address portal) XApp(portal, ConfLevel.Latest) {} /** * @notice Records a greeting from any chain @@ -40,7 +40,7 @@ contract GlobalGreeter is XApp { uint256 fee = 0; if (isXCall() && xmsg.sourceChainId != omni.chainId()) { // Calculate the fee for the cross-chain call - fee = feeFor(xmsg.sourceChainId, abi.encodeWithSelector(this.greet.selector, _greeting), GAS_LIMIT); + fee = feeFor(xmsg.sourceChainId, abi.encodeWithSelector(this.greet.selector, _greeting), DESTINATION_TX_GAS_LIMIT); } // Create a Greeting struct to store information about the received greeting diff --git a/src/RollupGreeter.sol b/src/RollupGreeter.sol index eba7613..a2cf728 100644 --- a/src/RollupGreeter.sol +++ b/src/RollupGreeter.sol @@ -13,15 +13,9 @@ import {GlobalGreeter} from "./GlobalGreeter.sol"; */ contract RollupGreeter is XApp { /** - * @notice Gas limit used for a cross-chain greet call + * @notice Gas limit used for a cross-chain greet call at destination */ - uint64 public constant GAS_LIMIT = 200_000; - - /** - * @notice Chain ID of the Omni network - * @dev State variable to store the Omni Network's specific chain ID - */ - uint64 public omniChainId; + uint64 public constant DESTINATION_TX_GAS_LIMIT = 120_000; /** * @notice Address of the greeter contract deployed on the global chain @@ -32,11 +26,9 @@ contract RollupGreeter is XApp { /** * @dev Initializes a new RollupGreeter contract with necessary addresses and identifiers * @param portal Address of the portal or relay used for cross-chain communication - * @param _omniChainId Chain ID for the Omni Network, specific chain for state coordination * @param _omniChainGreeter Address of the greeter contract deployed on the global chain */ - constructor(address portal, uint64 _omniChainId, address _omniChainGreeter) XApp(portal, ConfLevel.Finalized) { - omniChainId = _omniChainId; + constructor(address portal, address _omniChainGreeter) XApp(portal, ConfLevel.Finalized) { omniChainGreeter = _omniChainGreeter; } @@ -50,7 +42,7 @@ contract RollupGreeter is XApp { bytes memory data = abi.encodeWithSelector(GlobalGreeter.greet.selector, greeting); // Calculate the cross-chain call fee - uint256 fee = xcall(omniChainId, omniChainGreeter, data, GAS_LIMIT); + uint256 fee = xcall(omni.omniChainId(), omniChainGreeter, data, DESTINATION_TX_GAS_LIMIT); // Ensure that the caller provides sufficient value to cover the fee require(msg.value >= fee, "RollupGreeter: little fee"); diff --git a/test/GlobalGreeter.t.sol b/test/GlobalGreeter.t.sol index f266946..6f913a0 100644 --- a/test/GlobalGreeter.t.sol +++ b/test/GlobalGreeter.t.sol @@ -53,7 +53,7 @@ contract GlobalGreeterTest is Test { address(portal), address(greeter), abi.encodeWithSelector(GlobalGreeter.greet.selector, greeting), - greeter.GAS_LIMIT() + greeter.DESTINATION_TX_GAS_LIMIT() ); Greeting memory lastGreet; ( diff --git a/test/RollupGreeter.t.sol b/test/RollupGreeter.t.sol index d770a1b..5361eab 100644 --- a/test/RollupGreeter.t.sol +++ b/test/RollupGreeter.t.sol @@ -20,7 +20,7 @@ contract RollupGreeterTest is Test { function setUp() public { portal = new MockPortal(); // Deploy RollupGreeter contract with MockPortal address, Omni chain ID, and greeter contract address - rollupGreeter = new RollupGreeter(address(portal), 1, address(0xdeadbeef)); + rollupGreeter = new RollupGreeter(address(portal), address(0xdeadbeef)); } /** @@ -30,7 +30,7 @@ contract RollupGreeterTest is Test { string memory greeting = "Hello, world!"; // Get the fee required for the greet function uint256 fee = portal.feeFor( - rollupGreeter.omniChainId(), abi.encodeWithSignature("greet(string)", greeting), rollupGreeter.GAS_LIMIT() + portal.omniChainId(), abi.encodeWithSignature("greet(string)", greeting), rollupGreeter.DESTINATION_TX_GAS_LIMIT() ); // Mock expected calls to the portal contract @@ -38,20 +38,20 @@ contract RollupGreeterTest is Test { address(portal), abi.encodeWithSignature( "feeFor(uint64,bytes,uint64)", - rollupGreeter.omniChainId(), + portal.omniChainId(), abi.encodeWithSignature("greet(string)", greeting), - rollupGreeter.GAS_LIMIT() + rollupGreeter.DESTINATION_TX_GAS_LIMIT() ) ); vm.expectCall( address(portal), abi.encodeWithSignature( "xcall(uint64,uint8,address,bytes,uint64)", - rollupGreeter.omniChainId(), + portal.omniChainId(), ConfLevel.Finalized, rollupGreeter.omniChainGreeter(), abi.encodeWithSignature("greet(string)", greeting), - rollupGreeter.GAS_LIMIT() + rollupGreeter.DESTINATION_TX_GAS_LIMIT() ) ); @@ -66,7 +66,7 @@ contract RollupGreeterTest is Test { string memory greeting = "Hello, world!"; // Get the fee required for the greet function uint256 fee = portal.feeFor( - rollupGreeter.omniChainId(), abi.encodeWithSignature("greet(string)", greeting), rollupGreeter.GAS_LIMIT() + portal.omniChainId(), abi.encodeWithSignature("greet(string)", greeting), rollupGreeter.DESTINATION_TX_GAS_LIMIT() ); // Deal ether to the contract address to simulate insufficient funds