-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/ccip-warp-route' into pb/default…
…hook-sdk
- Loading branch information
Showing
18 changed files
with
1,829 additions
and
33 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@hyperlane-xyz/core': minor | ||
--- | ||
|
||
Implement warp route amount routing ISM |
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,5 @@ | ||
--- | ||
'@hyperlane-xyz/core': minor | ||
--- | ||
|
||
Implement CCIP hook and ISM |
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,5 @@ | ||
--- | ||
'@hyperlane-xyz/core': minor | ||
--- | ||
|
||
Implement warp amount routing hook |
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
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,83 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
/*@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
@@@@@ HYPERLANE @@@@@@@ | ||
@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@*/ | ||
|
||
// ============ Internal Imports ============ | ||
import {AbstractMessageIdAuthHook} from "./libs/AbstractMessageIdAuthHook.sol"; | ||
import {Message} from "../libs/Message.sol"; | ||
import {TypeCasts} from "../libs/TypeCasts.sol"; | ||
|
||
// ============ External Imports ============ | ||
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"; | ||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
/** | ||
* @title CCIPHook | ||
* @notice Message hook to inform the CCIP of messages published through CCIP. | ||
*/ | ||
contract CCIPHook is AbstractMessageIdAuthHook { | ||
using Message for bytes; | ||
using TypeCasts for bytes32; | ||
|
||
IRouterClient internal immutable ccipRouter; | ||
uint64 public immutable ccipDestination; | ||
|
||
// ============ Constructor ============ | ||
|
||
constructor( | ||
address _ccipRouter, | ||
uint64 _ccipDestination, | ||
address _mailbox, | ||
uint32 _destination, | ||
bytes32 _ism | ||
) AbstractMessageIdAuthHook(_mailbox, _destination, _ism) { | ||
ccipDestination = _ccipDestination; | ||
ccipRouter = IRouterClient(_ccipRouter); | ||
} | ||
|
||
// ============ Internal functions ============ | ||
|
||
function _buildCCIPMessage( | ||
bytes calldata message | ||
) internal view returns (Client.EVM2AnyMessage memory) { | ||
// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message | ||
return | ||
Client.EVM2AnyMessage({ | ||
receiver: abi.encode(ism), | ||
data: abi.encode(message.id()), | ||
tokenAmounts: new Client.EVMTokenAmount[](0), | ||
extraArgs: "", | ||
feeToken: address(0) | ||
}); | ||
} | ||
|
||
function _quoteDispatch( | ||
bytes calldata /*metadata*/, | ||
bytes calldata message | ||
) internal view override returns (uint256) { | ||
Client.EVM2AnyMessage memory ccipMessage = _buildCCIPMessage(message); | ||
|
||
return ccipRouter.getFee(ccipDestination, ccipMessage); | ||
} | ||
|
||
function _sendMessageId( | ||
bytes calldata /*metadata*/, | ||
bytes calldata message | ||
) internal override { | ||
Client.EVM2AnyMessage memory ccipMessage = _buildCCIPMessage(message); | ||
|
||
ccipRouter.ccipSend{value: msg.value}(ccipDestination, ccipMessage); | ||
} | ||
} |
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,53 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
// ============ External Imports ============ | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
// ============ Internal Imports ============ | ||
import {AbstractPostDispatchHook} from "../libs/AbstractPostDispatchHook.sol"; | ||
import {IPostDispatchHook} from "../../interfaces/hooks/IPostDispatchHook.sol"; | ||
import {AmountPartition} from "../../token/libs/AmountPartition.sol"; | ||
import {IInterchainSecurityModule} from "../../interfaces/IInterchainSecurityModule.sol"; | ||
import {Message} from "../../libs/Message.sol"; | ||
import {PackageVersioned} from "../../PackageVersioned.sol"; | ||
import {TokenMessage} from "../../token/libs/TokenMessage.sol"; | ||
|
||
/** | ||
* @title AmountRoutingHook | ||
*/ | ||
contract AmountRoutingHook is AmountPartition, AbstractPostDispatchHook { | ||
constructor( | ||
address _lowerHook, | ||
address _upperHook, | ||
uint256 _threshold | ||
) AmountPartition(_lowerHook, _upperHook, _threshold) {} | ||
|
||
function hookType() external pure override returns (uint8) { | ||
return uint8(IPostDispatchHook.Types.AMOUNT_ROUTING); | ||
} | ||
|
||
function _postDispatch( | ||
bytes calldata _metadata, | ||
bytes calldata _message | ||
) internal override { | ||
uint256 quote = _quoteDispatch(_metadata, _message); | ||
IPostDispatchHook(_partition(_message)).postDispatch{value: quote}( | ||
_metadata, | ||
_message | ||
); | ||
return _refund(_metadata, _message, msg.value - quote); | ||
} | ||
|
||
function _quoteDispatch( | ||
bytes calldata _metadata, | ||
bytes calldata _message | ||
) internal view override returns (uint256) { | ||
return | ||
IPostDispatchHook(_partition(_message)).quoteDispatch( | ||
_metadata, | ||
_message | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
/*@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
@@@@@ HYPERLANE @@@@@@@ | ||
@@@@@@@@@@@@@@@@@@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@@ | ||
@@@@@@@@@ @@@@@@@@*/ | ||
|
||
// ============ Internal Imports ============ | ||
|
||
import {IInterchainSecurityModule} from "../../interfaces/IInterchainSecurityModule.sol"; | ||
import {Message} from "../../libs/Message.sol"; | ||
import {TypeCasts} from "../../libs/TypeCasts.sol"; | ||
import {AbstractMessageIdAuthorizedIsm} from "./AbstractMessageIdAuthorizedIsm.sol"; | ||
|
||
// ============ External Imports ============ | ||
import {Address} from "@openzeppelin/contracts/utils/Address.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"; | ||
import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol"; | ||
|
||
/** | ||
* @title CCIPIsm | ||
* @notice Uses CCIP hook to verify interchain messages. | ||
*/ | ||
contract CCIPIsm is AbstractMessageIdAuthorizedIsm, CCIPReceiver { | ||
using TypeCasts for bytes32; | ||
|
||
// ============ Constants ============ | ||
|
||
uint8 public constant moduleType = | ||
uint8(IInterchainSecurityModule.Types.NULL); | ||
|
||
uint64 public immutable ccipOrigin; | ||
|
||
// ============ Storage ============ | ||
constructor( | ||
address _ccipRouter, | ||
uint64 _ccipOrigin | ||
) CCIPReceiver(_ccipRouter) { | ||
ccipOrigin = _ccipOrigin; | ||
} | ||
|
||
// ============ Internal functions ============ | ||
function _ccipReceive( | ||
Client.Any2EVMMessage memory any2EvmMessage | ||
) internal override { | ||
require( | ||
ccipOrigin == any2EvmMessage.sourceChainSelector, | ||
"Unauthorized origin" | ||
); | ||
|
||
bytes32 sender = abi.decode(any2EvmMessage.sender, (bytes32)); | ||
require(sender == authorizedHook, "Unauthorized hook"); | ||
|
||
bytes32 messageId = abi.decode(any2EvmMessage.data, (bytes32)); | ||
preVerifyMessage(messageId, msg.value); | ||
} | ||
|
||
function _isAuthorized() internal view override returns (bool) { | ||
return msg.sender == getRouter(); | ||
} | ||
} |
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,38 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
// ============ External Imports ============ | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
// ============ Internal Imports ============ | ||
import {AbstractRoutingIsm} from "../routing/AbstractRoutingIsm.sol"; | ||
import {AmountPartition} from "../../token/libs/AmountPartition.sol"; | ||
import {IInterchainSecurityModule} from "../../interfaces/IInterchainSecurityModule.sol"; | ||
import {Message} from "../../libs/Message.sol"; | ||
import {PackageVersioned} from "../../PackageVersioned.sol"; | ||
import {TokenMessage} from "../../token/libs/TokenMessage.sol"; | ||
|
||
/** | ||
* @title AmountRoutingIsm | ||
*/ | ||
contract AmountRoutingIsm is AmountPartition, AbstractRoutingIsm { | ||
constructor( | ||
address _lowerIsm, | ||
address _upperIsm, | ||
uint256 _threshold | ||
) AmountPartition(_lowerIsm, _upperIsm, _threshold) {} | ||
|
||
// ============ Public Functions ============ | ||
/** | ||
* @notice Returns the ISM responsible for verifying _message | ||
* @dev Routes to upperISM ISM if amount > threshold, otherwise lowerISM ISM. | ||
* @param _message Formatted Hyperlane message (see Message.sol). | ||
* @return module The ISM to use to verify _message | ||
*/ | ||
function route( | ||
bytes calldata _message | ||
) public view override returns (IInterchainSecurityModule) { | ||
return IInterchainSecurityModule(_partition(_message)); | ||
} | ||
} |
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,44 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
// ============ External Imports ============ | ||
import {Address} from "@openzeppelin/contracts/utils/Address.sol"; | ||
|
||
// ============ Internal Imports ============ | ||
import {Message} from "../../libs/Message.sol"; | ||
import {PackageVersioned} from "../../PackageVersioned.sol"; | ||
import {TokenMessage} from "../../token/libs/TokenMessage.sol"; | ||
|
||
/** | ||
* @title AmountPartition | ||
*/ | ||
abstract contract AmountPartition is PackageVersioned { | ||
using Message for bytes; | ||
using TokenMessage for bytes; | ||
using Address for address; | ||
|
||
address public immutable lower; | ||
address public immutable upper; | ||
uint256 public immutable threshold; | ||
|
||
constructor(address _lower, address _upper, uint256 _threshold) { | ||
require( | ||
_lower.isContract() && _upper.isContract(), | ||
"AmountPartition: lower and upper must be contracts" | ||
); | ||
lower = _lower; | ||
upper = _upper; | ||
threshold = _threshold; | ||
} | ||
|
||
function _partition( | ||
bytes calldata _message | ||
) internal view returns (address) { | ||
uint256 amount = _message.body().amount(); | ||
if (amount >= threshold) { | ||
return upper; | ||
} else { | ||
return lower; | ||
} | ||
} | ||
} |
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
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.