Skip to content

Commit

Permalink
Gateway localnet examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 12, 2024
1 parent b50991b commit 098f242
Show file tree
Hide file tree
Showing 14 changed files with 7,682 additions and 5 deletions.
10 changes: 5 additions & 5 deletions omnichain/swap/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name as ParamChainName;

if (!/zeta_(testnet|mainnet)/.test(network)) {
throw new Error(
'🚨 Please use either "zeta_testnet" or "zeta_mainnet" network to deploy to ZetaChain.'
);
}
// if (!/zeta_(testnet|mainnet)/.test(network)) {
// throw new Error(
// '🚨 Please use either "zeta_testnet" or "zeta_mainnet" network to deploy to ZetaChain.'
// );
// }

const [signer] = await hre.ethers.getSigners();
if (signer === undefined) {
Expand Down
6 changes: 6 additions & 0 deletions universal/hello/.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 universal/hello/.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"),
},
},
},
};
16 changes: 16 additions & 0 deletions universal/hello/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# Foundry files
out
cache_forge

access_token
21 changes: 21 additions & 0 deletions universal/hello/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.
15 changes: 15 additions & 0 deletions universal/hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ZetaChain Contracts Template

## Getting Started

Install dependencies:

```
yarn
```

## Next Steps

Ready to dive in? Follow our [**🚀 smart contract
tutorials**](https://www.zetachain.com/docs/developers/tutorials/intro/) to
start building universal app contracts.
94 changes: 94 additions & 0 deletions universal/hello/contracts/Hello.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";

/// @title Hello
/// @notice This contract is used just for testing.
/// @dev Implements the UniversalContract interface for handling cross-chain calls and reverts.
contract Hello is UniversalContract {
/// @notice Emitted when a cross-chain call is received.
/// @param origin The origin address on the external chain.
/// @param sender The sender address on the external chain.
/// @param chainID The chain ID of the external chain.
/// @param msgSender The sender address on the current chain.
/// @param message The decoded message from the cross-chain call.
event ContextData(
bytes origin,
address sender,
uint256 chainID,
address msgSender,
string message
);

/// @notice Emitted when a cross-chain call is reverted.
/// @param origin The origin address on the external chain.
/// @param sender The sender address on the external chain.
/// @param chainID The chain ID of the external chain.
/// @param msgSender The sender address on the current chain.
/// @param message The decoded message from the revert call.
event ContextDataRevert(
bytes origin,
address sender,
uint256 chainID,
address msgSender,
string message
);

/// @notice Handles a cross-chain call.
/// @param context The context of the cross-chain call.
/// @param zrc20 The address of the ZRC20 token.
/// @param amount The amount of tokens transferred.
/// @param message The calldata passed to the contract call.
/// @dev Decodes the message and emits a ContextData event.
function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override {
string memory decodedMessage;
if (message.length > 0) {
decodedMessage = abi.decode(message, (string));
}
emit ContextData(
context.origin,
context.sender,
context.chainID,
msg.sender,
decodedMessage
);
}

/// @notice Handles a cross-chain call revert.
/// @param context The context of the revert call.
/// @param zrc20 The address of the ZRC20 token.
/// @param amount The amount of tokens to revert.
/// @param message The calldata passed to the contract call.
/// @dev Decodes the message and emits a ContextDataRevert event.
function onRevert(
revertContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override {
string memory decodedMessage;
if (message.length > 0) {
decodedMessage = abi.decode(message, (string));
}
emit ContextDataRevert(
context.origin,
context.sender,
context.chainID,
msg.sender,
decodedMessage
);
}

/// @notice Allows the contract to receive ETH.
receive() external payable {}

/// @notice Fallback function to receive ETH.
fallback() external payable {}
}
7 changes: 7 additions & 0 deletions universal/hello/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[profile.default]
src = 'contracts'
out = 'out'
viaIR = true
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
17 changes: 17 additions & 0 deletions universal/hello/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "./tasks/interact";
import "./tasks/deploy";
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.7",
};

export default config;
57 changes: 57 additions & 0 deletions universal/hello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "example-template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint:fix": "npx eslint . --ext .js,.ts --fix",
"lint": "npx eslint . --ext .js,.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@ethersproject/abi": "^5.4.7",
"@ethersproject/providers": "^5.4.7",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
"@nomicfoundation/hardhat-foundry": "^1.1.2",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-etherscan": "^3.0.0",
"@typechain/ethers-v5": "^10.1.0",
"@typechain/hardhat": "^6.1.2",
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "^10.0.0",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
"envfile": "^6.18.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-typescript-sort-keys": "^2.3.0",
"ethers": "^5.4.7",
"hardhat": "^2.17.2",
"hardhat-gas-reporter": "^1.0.8",
"prettier": "^2.8.8",
"solidity-coverage": "^0.8.0",
"ts-node": ">=8.0.0",
"typechain": "^8.1.0",
"typescript": ">=4.5.0"
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72",
"dependencies": {
"@zetachain/localnet": "1.0.0-rc6",
"@zetachain/protocol-contracts": "^9.0.0"
}
}
41 changes: 41 additions & 0 deletions universal/hello/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getAddress, ParamChainName } from "@zetachain/protocol-contracts";
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name as ParamChainName;

// if (!/zeta_(testnet|mainnet)/.test(network)) {
// throw new Error(
// '🚨 Please use either "zeta_testnet" or "zeta_mainnet" network to deploy to ZetaChain.'
// );
// }

const [signer] = await hre.ethers.getSigners();
if (signer === undefined) {
throw new Error(
`Wallet not found. Please, run "npx hardhat account --save" or set PRIVATE_KEY env variable (for example, in a .env file)`
);
}

// const systemContract = getAddress("systemContract", network);

const factory = await hre.ethers.getContractFactory(args.name);
// const contract = await factory.deploy(systemContract);
const contract = await factory.deploy();
await contract.deployed();

if (args.json) {
console.log(JSON.stringify(contract));
} else {
console.log(`🔑 Using account: ${signer.address}
🚀 Successfully deployed contract on ${network}.
📜 Contract address: ${contract.address}
`);
}
};

task("deploy", "Deploy the contract", main)
.addFlag("json", "Output in JSON")
.addOptionalParam("name", "Contract to deploy", "Hello");
34 changes: 34 additions & 0 deletions universal/hello/tasks/interact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { task } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import GatewayABI from "@zetachain/protocol-contracts/abi/prototypes/evm/GatewayEVM.sol/GatewayEVM.json";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();

const gateway = new hre.ethers.Contract(
args.gatewayEVM,
GatewayABI.abi,
signer
);

const message = hre.ethers.utils.defaultAbiCoder.encode(
["string"],
[args.name]
);
try {
const callTx = await gateway.call(args.contract, message);
await callTx.wait();
console.log("Contract on ZetaChain called from EVM");
} catch (e) {
console.error("Error calling TestZContract:", e);
}
};

task("interact", "calls zevm zcontract from evm account", main)
.addParam("name")
.addParam("contract", "contract address of a universal app on ZetaChain")
.addOptionalParam(
"gatewayEVM",
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
);
11 changes: 11 additions & 0 deletions universal/hello/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
}
Loading

0 comments on commit 098f242

Please sign in to comment.