Skip to content
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

PoC implementation of LayerZero cross chain module #178

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@
[submodule "lib/PermitC"]
path = lib/PermitC
url = https://github.com/limitbreakinc/PermitC
[submodule "lib/devtools"]
path = lib/devtools
url = https://github.com/LayerZero-Labs/devtools
[submodule "lib/layerzero-v2"]
path = lib/layerzero-v2
url = https://github.com/LayerZero-Labs/layerzero-v2
1 change: 1 addition & 0 deletions lib/devtools
Submodule devtools added at 811a69
1 change: 1 addition & 0 deletions lib/layerzero-v2
Submodule layerzero-v2 added at 7aebbd
2 changes: 2 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ forge-std/=lib/forge-std/src/
@erc721a-upgradeable/=lib/ERC721A-Upgradeable/contracts/
@limitbreak/creator-token-standards/=lib/creator-token-standards/src/
@limitbreak/permit-c/=lib/PermitC/src/
@layerzerolabs/oapp-evm/=lib/devtools/packages/oapp-evm/
@layerzerolabs/lz-evm-protocol-v2/=lib/layerzero-v2/packages/layerzero-v2/evm/protocol
94 changes: 94 additions & 0 deletions src/module/token/crosschain/LayerZero.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {Module} from "../../../Module.sol";

import {Role} from "../../../Role.sol";
import {IInstallationCallback} from "../../../interface/IInstallationCallback.sol";

import {CrossChain} from "./CrossChain.sol";

import {MessagingFee, OApp, Origin} from "@layerzerolabs/oapp-evm/contracts/oapp/OApp.sol";
import {OwnableRoles} from "@solady/auth/OwnableRoles.sol";

contract LayerZeroCrossChain is Module, OApp, CrossChain {

constructor(address _endpoint, address _owner) OApp(_endpoint, _owner) {}

/// @notice Returns all implemented callback and fallback functions.
function getModuleConfig() external pure override returns (ModuleConfig memory config) {
config.fallbackFunctions = new FallbackFunction[](3);

config.fallbackFunctions[0] = FallbackFunction({selector: this.getRouter.selector, permissionBits: 0});
config.fallbackFunctions[1] =
FallbackFunction({selector: this.setRouter.selector, permissionBits: Role._MANAGER_ROLE});
config.fallbackFunctions[2] =
FallbackFunction({selector: this.sendCrossChainTransaction.selector, permissionBits: 0});
}

/*//////////////////////////////////////////////////////////////
FALLBACK FUNCTIONS
//////////////////////////////////////////////////////////////*/

function getRouter() external view override returns (address) {
return address(endpoint);
}

function setRouter(address _router) external override {}

function sendCrossChainTransaction(
uint64 _destinationChain,
address _callAddress,
bytes calldata _payload,
bytes calldata _extraArgs
) external payable override {
(bytes memory options, uint256 zroTokenAmount) = abi.decode(_extraArgs, (bytes, uint256));

_lzSend(
uint32(_destinationChain),
_payload,
options,
// Fee in native gas and ZRO token.
MessagingFee(msg.value, zroTokenAmount),
// Refund address in case of failed source message.
payable(msg.sender)
);

onCrossChainTransactionSent(_destinationChain, _callAddress, _payload, _extraArgs);
}

function _lzReceive(
Origin calldata _origin,
bytes32 _guid,
bytes calldata payload,
address _sourceAddress, // Executor address as specified by the OApp.
bytes calldata _extraArgs // Any extra data or options to trigger on receipt.
) internal override {
bytes memory extraArgs = abi.encode(_origin, _guid, _extraArgs);

onCrossChainTransactionReceived(_origin.srcEid, _sourceAddress, payload, extraArgs);
}

/*//////////////////////////////////////////////////////////////
INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/

function onCrossChainTransactionSent(
uint64 _destinationChain,
address _callAddress,
bytes calldata _payload,
bytes calldata _extraArgs
) internal override {
/// post cross chain transaction sent logic goes here
}

function onCrossChainTransactionReceived(
uint64 _sourceChain,
address _sourceAddress,
bytes memory _payload,
bytes memory _extraArgs
) internal override {
/// post cross chain transaction received logic goes here
}

}