Skip to content

Commit

Permalink
chore(contracts): refactor and dynamic values
Browse files Browse the repository at this point in the history
  • Loading branch information
idea404 committed Jun 11, 2024
1 parent 9230d6b commit 89224fc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
4 changes: 1 addition & 3 deletions script/RollupGreeter.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/GlobalGreeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 4 additions & 12 deletions src/RollupGreeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/GlobalGreeter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
(
Expand Down
14 changes: 7 additions & 7 deletions test/RollupGreeter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -30,28 +30,28 @@ 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
vm.expectCall(
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()
)
);

Expand All @@ -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
Expand Down

0 comments on commit 89224fc

Please sign in to comment.