Skip to content

[Ethereum][Base] Bridge Helper Module with CCIP support #2550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import { AbstractSafeModule } from "./AbstractSafeModule.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { IRouterClient } from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import { Client } from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how these are included as a package


abstract contract AbstractCCIPBridgeHelperModule is AbstractSafeModule {
/**
* @notice Bridges a token from the source chain to the destination chain using CCIP
* @param ccipRouter The CCIP router contract
* @param destinationChainSelector The selector for the destination chain
* @param token The token to bridge
* @param amount The amount of token to bridge
*/
function _bridgeTokenWithCCIP(

Check warning on line 18 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L18

Added line #L18 was not covered by tests
IRouterClient ccipRouter,
uint64 destinationChainSelector,
IERC20 token,
uint256 amount
) internal {
bool success;

Check warning on line 24 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L24

Added line #L24 was not covered by tests

// Approve CCIP Router to move the token
success = safeContract.execTransactionFromModule(

Check warning on line 27 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L27

Added line #L27 was not covered by tests
address(token),
0, // Value
abi.encodeWithSelector(token.approve.selector, ccipRouter, amount),
0 // Call
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this the operation. The previous param is the call

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, yes, the previous param is the calldata. The fourth parameter takes 0 for normals calls and 1 for delegate calls. That's the Call I meant, should've been clear

);
require(success, "Failed to approve token");

Client.EVMTokenAmount[]

Check warning on line 35 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L35

Added line #L35 was not covered by tests
memory tokenAmounts = new Client.EVMTokenAmount[](1);
Client.EVMTokenAmount memory tokenAmount = Client.EVMTokenAmount({

Check warning on line 37 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L37

Added line #L37 was not covered by tests
token: address(token),
amount: amount
});
tokenAmounts[0] = tokenAmount;

Check warning on line 41 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L41

Added line #L41 was not covered by tests

Client.EVM2AnyMessage memory ccipMessage = Client.EVM2AnyMessage({

Check warning on line 43 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L43

Added line #L43 was not covered by tests
receiver: abi.encode(address(safeContract)), // ABI-encoded receiver address
data: abi.encode(""),
tokenAmounts: tokenAmounts,
extraArgs: Client._argsToBytes(
Client.EVMExtraArgsV1({ gasLimit: 0 })
),
feeToken: address(0)
});

// Get CCIP fee
uint256 ccipFee = ccipRouter.getFee(

Check warning on line 54 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L54

Added line #L54 was not covered by tests
destinationChainSelector,
ccipMessage
);

// Send CCIP message
success = safeContract.execTransactionFromModule(

Check warning on line 60 in contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractCCIPBridgeHelperModule.sol#L60

Added line #L60 was not covered by tests
address(ccipRouter),
ccipFee, // Value
abi.encodeWithSelector(
ccipRouter.ccipSend.selector,
destinationChainSelector,
ccipMessage
),
0 // Call
);
require(success, "Failed to send CCIP message");
}
}
51 changes: 2 additions & 49 deletions contracts/contracts/automation/AbstractLZBridgeHelperModule.sol
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import { AccessControlEnumerable } from "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import { ISafe } from "../interfaces/ISafe.sol";
import { AbstractSafeModule } from "./AbstractSafeModule.sol";

import { IOFT, SendParam } from "@layerzerolabs/oft-evm/contracts/interfaces/IOFT.sol";
import { MessagingFee } from "@layerzerolabs/oapp-evm/contracts/oapp/OAppSender.sol";
import { OptionsBuilder } from "@layerzerolabs/oapp-evm/contracts/oapp/libs/OptionsBuilder.sol";

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

abstract contract AbstractLZBridgeHelperModule is AccessControlEnumerable {
abstract contract AbstractLZBridgeHelperModule is AbstractSafeModule {
using OptionsBuilder for bytes;

ISafe public immutable safeContract;

bytes32 public constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");

modifier onlyOperator() {
require(
hasRole(OPERATOR_ROLE, msg.sender),
"Caller is not an operator"
);
_;
}

constructor(address _safeContract) {
safeContract = ISafe(_safeContract);
_grantRole(DEFAULT_ADMIN_ROLE, address(safeContract));
_grantRole(OPERATOR_ROLE, address(safeContract));
}

/**
* @dev Bridges token using LayerZero.
* @param lzEndpointId LayerZero endpoint id.
Expand Down Expand Up @@ -105,32 +86,4 @@ abstract contract AbstractLZBridgeHelperModule is AccessControlEnumerable {
);
require(success, "Failed to bridge token");
}

/**
* @dev Helps recovering any tokens accidentally sent to this module.
* @param token Token to transfer. 0x0 to transfer Native token.
* @param amount Amount to transfer. 0 to transfer all balance.
*/
function transferTokens(address token, uint256 amount)
external
onlyOperator
{
if (address(token) == address(0)) {
// Move ETH
amount = amount > 0 ? amount : address(this).balance;
payable(address(safeContract)).transfer(amount);
return;
}

// Move all balance if amount set to 0
amount = amount > 0 ? amount : IERC20(token).balanceOf(address(this));

// Transfer to Safe contract
// slither-disable-next-line unchecked-transfer unused-return
IERC20(token).transfer(address(safeContract), amount);
}

receive() external payable {
// Accept ETH to pay for bridge fees
}
}
59 changes: 59 additions & 0 deletions contracts/contracts/automation/AbstractSafeModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import { AccessControlEnumerable } from "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { ISafe } from "../interfaces/ISafe.sol";

abstract contract AbstractSafeModule is AccessControlEnumerable {
ISafe public immutable safeContract;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the names of the ISafe.execTransactionFromModule parameters to the interface. Thanks


bytes32 public constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");

modifier onlySafe() {

Check warning on line 13 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L13

Added line #L13 was not covered by tests
require(
msg.sender == address(safeContract),
"Caller is not the safe contract"
);
_;

Check warning on line 18 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L18

Added line #L18 was not covered by tests
}

modifier onlyOperator() {

Check warning on line 21 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L21

Added line #L21 was not covered by tests
require(
hasRole(OPERATOR_ROLE, msg.sender),
"Caller is not an operator"
);
_;

Check warning on line 26 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L26

Added line #L26 was not covered by tests
}

constructor(address _safeContract) {
safeContract = ISafe(_safeContract);
_grantRole(DEFAULT_ADMIN_ROLE, address(safeContract));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could just use _safeContract instead of address(safeContract)

_grantRole(OPERATOR_ROLE, address(safeContract));

Check warning on line 32 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L29-L32

Added lines #L29 - L32 were not covered by tests
}

/**
* @dev Helps recovering any tokens accidentally sent to this module.
* @param token Token to transfer. 0x0 to transfer Native token.
* @param amount Amount to transfer. 0 to transfer all balance.
*/
function transferTokens(address token, uint256 amount) external onlySafe {
if (address(token) == address(0)) {
// Move ETH
amount = amount > 0 ? amount : address(this).balance;
payable(address(safeContract)).transfer(amount);
return;

Check warning on line 45 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L44-L45

Added lines #L44 - L45 were not covered by tests
}

// Move all balance if amount set to 0
amount = amount > 0 ? amount : IERC20(token).balanceOf(address(this));

// Transfer to Safe contract
// slither-disable-next-line unchecked-transfer unused-return
IERC20(token).transfer(address(safeContract), amount);

Check warning on line 53 in contracts/contracts/automation/AbstractSafeModule.sol

View check run for this annotation

Codecov / codecov/patch

contracts/contracts/automation/AbstractSafeModule.sol#L53

Added line #L53 was not covered by tests
}

receive() external payable {
// Accept ETH to pay for bridge fees
}
}
Loading
Loading