Skip to content

Commit

Permalink
Router contract example
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 4, 2024
1 parent 4b324d3 commit 936b7ab
Show file tree
Hide file tree
Showing 19 changed files with 9,309 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/router/.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 examples/router/.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"),
},
},
},
};
17 changes: 17 additions & 0 deletions examples/router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types
dependencies

# Hardhat files
cache
artifacts

# Foundry files
out
cache_forge

access_token
21 changes: 21 additions & 0 deletions examples/router/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.
4 changes: 4 additions & 0 deletions examples/router/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# NFT Example

This example currently only works with localnet `v4.0.0-rc*`, which supports
authenticated calls and multiple EVM chains.
76 changes: 76 additions & 0 deletions examples/router/contracts/Connected.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 Connected is Ownable {
GatewayEVM public immutable gateway;
uint256 private _nextTokenId;
address public counterparty;
event HelloEvent(string, string);
event OnCallEvent(string, bytes);

function setCounterparty(address contractAddress) external onlyOwner {
counterparty = contractAddress;
}

modifier onlyGateway() {
require(msg.sender == address(gateway), "Caller is not the gateway");
_;
}

constructor(
address payable gatewayAddress,
address initialOwner
) Ownable(initialOwner) {
gateway = GatewayEVM(gatewayAddress);
}

function transferCrossChain(
bytes memory receiver,
address destination,
bytes memory encodedData,
CallOptions memory callOptions,
RevertOptions memory revertOptions
) external payable {
bytes memory message = abi.encode(
receiver,
destination,
encodedData,
callOptions,
revertOptions
);
gateway.depositAndCall{value: msg.value}(
counterparty,
message,
revertOptions
);
}

function hello(string memory message) external payable {
emit HelloEvent("Event from hello()", message);
}

function onCall(
MessageContext calldata messageContext,
bytes calldata message
) external payable onlyGateway returns (bytes4) {
emit OnCallEvent("Event from onCall()", message);
}

function onRevert(RevertContext calldata context) external onlyGateway {
// (uint256 tokenId, address sender, string memory uri) = abi.decode(
// context.revertMessage,
// (uint256, address, string)
// );
// _safeMint(sender, tokenId);
// _setTokenURI(tokenId, uri);
}

receive() external payable {}

fallback() external payable {}
}
91 changes: 91 additions & 0 deletions examples/router/contracts/Universal.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 {RevertContext, RevertOptions} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";
import "@zetachain/protocol-contracts/contracts/zevm/GatewayZEVM.sol";
import {SwapHelperLib} from "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import {SystemContract} from "@zetachain/toolkit/contracts/SystemContract.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Universal is UniversalContract, Ownable {
GatewayZEVM public immutable gateway;
SystemContract public immutable systemContract =
SystemContract(0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9);
bool public isUniversal = true;
uint256 public gasLimit = 700000;

error TransferFailed();

mapping(address => bytes) public counterparty;

event CounterpartySet(address indexed zrc20, bytes indexed contractAddress);

modifier onlyGateway() {
require(msg.sender == address(gateway), "Caller is not the gateway");
_;
}

constructor(
address payable gatewayAddress,
address initialOwner
) Ownable(initialOwner) {
gateway = GatewayZEVM(gatewayAddress);
}

function setCounterparty(
address zrc20,
bytes memory contractAddress
) external onlyOwner {
counterparty[zrc20] = contractAddress;
emit CounterpartySet(zrc20, contractAddress);
}

function onCall(
MessageContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override onlyGateway {
(
bytes memory receiver,
address destination,
bytes memory encodedData,
CallOptions memory callOptions,
RevertOptions memory revertOptions
) = abi.decode(
message,
(bytes, address, bytes, CallOptions, RevertOptions)
);

(, uint256 gasFee) = IZRC20(destination).withdrawGasFeeWithGasLimit(
callOptions.gasLimit
);
SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
amount,
destination,
0
);
IZRC20(destination).approve(address(gateway), gasFee);

gateway.call(
receiver,
destination,
encodedData,
callOptions,
revertOptions
);
}

function onRevert(RevertContext calldata context) external onlyGateway {
// (uint256 tokenId, address sender, string memory uri) = abi.decode(
// context.revertMessage,
// (uint256, address, string)
// );
// _safeMint(sender, tokenId);
// _setTokenURI(tokenId, uri);
}
}
11 changes: 11 additions & 0 deletions examples/router/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[profile.default]
src = 'contracts'
out = 'out'
viaIR = true
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
verbosity = 3

[dependencies]
forge-std = { version = "1.9.2" }
21 changes: 21 additions & 0 deletions examples/router/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "./tasks/deploy";
import "./tasks/deploy";
import "./tasks/mint";
import "./tasks/transfer";
import "./tasks/universalSetCounterparty";
import "./tasks/connectedSetCounterparty";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";

import { getHardhatConfigNetworks } from "@zetachain/networks";
import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
networks: {
...getHardhatConfigNetworks(),
},
solidity: "0.8.26",
};

export default config;
Loading

0 comments on commit 936b7ab

Please sign in to comment.