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

feat: cross-chain messaging #38

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 contracts/messaging/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.yarn
artifacts
cache
coverage
node_modules
typechain-types
47 changes: 47 additions & 0 deletions contracts/messaging/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require("path");

/**
* @type {import("eslint").Linter.Config}
*/
module.exports = {
env: {
browser: false,
es2021: true,
mocha: true,
node: true,
},
extends: ["plugin:prettier/recommended"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
},
plugins: [
"@typescript-eslint",
"prettier",
"simple-import-sort",
"sort-keys-fix",
"typescript-sort-keys",
],
rules: {
"@typescript-eslint/sort-type-union-intersection-members": "error",
camelcase: "off",
"simple-import-sort/exports": "error",
"simple-import-sort/imports": "error",
"sort-keys-fix/sort-keys-fix": "error",
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error",
},
settings: {
"import/parsers": {
"@typescript-eslint/parser": [".js", ".jsx", ".ts", ".tsx", ".d.ts"],
},
"import/resolver": {
node: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".d.ts"],
},
typescript: {
project: path.join(__dirname, "tsconfig.json"),
},
},
},
};
19 changes: 19 additions & 0 deletions contracts/messaging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types
dependencies

# Hardhat files
cache
artifacts

# Foundry files
out
cache_forge

access_token

localnet.json
21 changes: 21 additions & 0 deletions contracts/messaging/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 ZetaChain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions contracts/messaging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Cross-Chain Messaging
114 changes: 114 additions & 0 deletions contracts/messaging/contracts/Example.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "./Messaging.sol";

contract Example is Messaging {
event OnMessageReceiveEvent(bytes);
event OnMessageRevertEvent();

constructor(
address payable gateway,
address owner,
address router
) Messaging(gateway, owner, router) {}

function onMessageReceive(
bytes memory data,
address sender,
uint256 amount
) internal override {
emit OnMessageReceiveEvent(data);
}

function onMessageRevert(
bytes memory data,
address sender,
uint256 amount
) internal override {
emit OnMessageRevertEvent();
// Revert from destination chain
}

function onRevert(
RevertContext calldata context
) external payable override onlyGateway {
if (context.sender != router) revert("Unauthorized");
emit OnRevertEvent("Event from onRevert()", context);
// Revert from ZetaChain
}

function sendMessage(
address destination,
bytes memory data,
CallOptions memory callOptions,
RevertOptions memory revertOptions
) external payable {
bytes memory message = abi.encode(
connected[destination],
destination,
data,
callOptions,
revertOptions
);
gateway.depositAndCall{value: msg.value}(
router,
message,
revertOptions
);
}

// Simplified function for demo purposes

function sendSimpleMessage(
address destination,
string memory data
) external payable {
RevertOptions memory revertOptions = RevertOptions(
address(0),
false,
address(0),
"",
0
);
bytes memory message = abi.encode(
connected[destination],
destination,
data,
CallOptions(200000, false),
revertOptions
);
gateway.depositAndCall{value: msg.value}(
router,
message,
revertOptions
);
}

// Send a message with an ERC-20 token. Currently, not supported.
//
// function sendMessage(
// address targetToken,
// uint256 amount,
// address asset,
// bytes memory data,
// CallOptions memory callOptions,
// RevertOptions memory revertOptions
// ) external {
// bytes memory message = abi.encode(
// abi.encodePacked(counterparty),
// targetToken,
// data,
// callOptions,
// revertOptions
// );
// if (!IERC20(asset).transferFrom(msg.sender, address(this), amount)) {
// revert TransferFailed();
// }
// if (!IERC20(asset).approve(address(gateway), amount)) {
// revert ApprovalFailed();
// }

// gateway.depositAndCall(router, amount, asset, message, revertOptions);
// }
}
91 changes: 91 additions & 0 deletions contracts/messaging/contracts/Messaging.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/GatewayEVM.sol";
import {RevertContext} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import {CallOptions} from "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";

contract Messaging is Ownable {
GatewayEVM public immutable gateway;
uint256 private _nextTokenId;
address public router;

event HelloEvent(string, string);
event OnCallEvent(string);
event OnRevertEvent(string, RevertContext);

error Unauthorized();
error TransferFailed();
error ApprovalFailed();

modifier onlyGateway() {
if (msg.sender != address(gateway)) revert Unauthorized();
_;
}

mapping(address => address) public connected;

function setConnected(
address zrc20,
address contractAddress
) external onlyOwner {
connected[zrc20] = contractAddress;
}

constructor(
address payable gatewayAddress,
address ownerAddress,
address routerAddress
) Ownable(ownerAddress) {
gateway = GatewayEVM(gatewayAddress);
router = routerAddress;
}

function onCall(
MessageContext calldata context,
bytes calldata message
) external payable onlyGateway returns (bytes4) {
if (context.sender != router) revert Unauthorized();
(
bytes memory data,
address sender,
uint256 amount,
bool isCall,
address zrc20
) = abi.decode(message, (bytes, address, uint256, bool, address));
if (sender != connected[zrc20]) revert Unauthorized();
if (isCall) {
onMessageReceive(data, sender, amount);
} else {
onMessageRevert(data, sender, amount);
}
return "";
}

// onRevert is executed when router's onCall reverts
function onRevert(
RevertContext calldata context
) external payable virtual onlyGateway {
if (context.sender != router) revert("Unauthorized");
emit OnRevertEvent("Event from onRevert()", context);
}

function onMessageReceive(
bytes memory data,
address sender,
uint256 amount
) internal virtual {
// To be overridden in the child contract
}

function onMessageRevert(
bytes memory data,
address sender,
uint256 amount
) internal virtual {
// To be overridden in the child contract
}

receive() external payable {}
}
Loading
Loading