Skip to content

Commit

Permalink
refactor: use messaging templating command to generate the introduc…
Browse files Browse the repository at this point in the history
…tory CCM tutorial (#55)
  • Loading branch information
fadeev authored Aug 25, 2023
1 parent 8cfecb8 commit 02ff50b
Show file tree
Hide file tree
Showing 10 changed files with 1,296 additions and 291 deletions.
6 changes: 6 additions & 0 deletions messaging/message/.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 messaging/message/.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"),
},
},
},
};
1 change: 1 addition & 0 deletions messaging/message/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ typechain-types
cache
artifacts

access_token
158 changes: 150 additions & 8 deletions messaging/message/README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,183 @@
# Template for a ZetaChain Hardhat Project

This is a simple Hardhat template that provides a starting point for developing smart contract applications on the ZetaChain blockchain.
This is a simple Hardhat template that provides a starting point for developing
smart contract applications on the ZetaChain blockchain.

## Prerequisites

Before getting started, ensure that you have [Node.js](https://nodejs.org/en/download) and [Yarn](https://yarnpkg.com/) installed on your system.
Before getting started, ensure that you have
[Node.js](https://nodejs.org/en/download) and [Yarn](https://yarnpkg.com/)
installed on your system.

## Getting Started

To get started, install the necessary dependencies by running the following command in your terminal:
To get started, install the necessary dependencies:

```
yarn
```

## Hardhat Tasks

This template includes two Hardhat tasks that can be used to generate a random wallet and request tokens from ZetaChain's faucet.
This template includes Hardhat tasks that can be used make it easier to build
with ZetaChain.

### Generating a Random Wallet

To generate a random wallet, run the following command in your terminal:
To generate a random wallet:

```
npx hardhat account --save
```

This will generate a random wallet, print information about the wallet to the terminal, and save the private key to a `.env` file to make it accessible to Hardhat. If you don't want to save the wallet (for example, if you just need an address to send tokens to), you can run the command without the `--save` flag.
This command generates a random wallet, prints information about the wallet to
the terminal, and saves the private key to a `.env` file to make it accessible
to Hardhat. If you don't want to save the wallet (for example, if you just need
an address to send tokens to for testing purposes), you can run the command
without the `--save` flag.

### Querying for Token Balances

To query for token balances:

```
npx hardhat balances
```

This command queries token balances for the account address derived from the
private key specified in the `.env`.

If you want to query for token balances for a different account, you can use the
`--address` flag:

```
npx hardhat balances --address ADDRESS
```

### Requesting Tokens from the Faucet

To request tokens from ZetaChain's faucet using the account from the `.env` file, run the following command in your terminal:
To request ZETA tokens from the faucet:

```
npx hardhat faucet
```

This command requests tokens from the faucet for the account address derived
from the private key specified in the `.env`. Tokens sent to the address on
ZetaChain. To specify a different chain use a flag:

```
npx hardhat faucet --chain goerli_testnet
```

You can also specify a different address to send the tokens to:

```
npx hardhat faucet --address ADDRESS
```

Alternatively, you can install a standalone faucet CLI:

```
yarn global add @zetachain/faucet-cli@athens3
```

You can then use it with the following command:

```
zetafaucet -h
```

### Creating an Omnichain Contract

To create a new omnichain contract:

```
npx hardhat omnichain MyContract
```

This command creates a new omnichain contract in `contracts/MyContract.sol`, a
task to deploy the contract in `tasks/deploy.ts`, and a task to interact with
the contract in `tasks/interact.ts`.

When an omnichain contract is called, it can receive data in the `data` field of
a transaction. This data is passed to the `message` parameter of the contract's
`onCrossChainCall` function. To specify the fields of the `message` parameter,
use positional arguments:

```
npx hardhat omnichain MyContract recepient:address description quantity:uint256
```

A field may have a type specified after the field name, separated by a colon. If
no type is specified, the type defaults to `bytes32`.

Learn more about omnichain contracts by following the
[tutorials](https://www.zetachain.com/docs/developers/omnichain/tutorials/hello/).

### Tracking a Cross-Chain Transaction

After broadcasting a cross-chain transaction on a connected chain either to a
cross-chain messaging contract or to trigger an omnichain contract, you can
track its status:

```
npx hardhat cctx --tx TX_HASH
```

### Verifying a Contract

To verify a contract deployed on ZetaChain:

```
npx hardhat verify:zeta --contract ADDRESS
```

Select the contract to verify:

```
? Select a contract to verify: (Use arrow keys)
@zetachain/zevm-protocol-contracts/contracts/interfaces/IZRC20.sol:IZRC20
@zetachain/zevm-protocol-contracts/contracts/interfaces/zContract.sol:zContract
❯ contracts/Withdraw.sol:Withdraw
```

After the confirmation the contract will be verified.

### Sending Tokens

Sending ZETA from ZetaChain to Goerli:

```
npx hardhat send-zeta --amount 1 --network zeta_testnet --destination goerli_testnet
```

Sending ZETA from Goerli to ZetaChain:

```
npx hardhat send-zeta --amount 1 --network goerli_testnet --destination zeta_testnet
```

Depositing gETH to ZetaChain as ZRC-20:

```
npx hardhat send-zrc20 --amount 1 --network goerli_testnet --destination zeta_testnet
```

Withdrawing ZRC-20 from ZetaChain go Goerli as gETH:

```
npx hardhat send-zrc20 --amount 1 --network zeta_testnet --destination goerli_testnet
```

Depositing tBTC from the Bitcoin testnet to ZetaChain:

```
npx hardhat send-btc --amount 1 --recipient TSS_ADDRESS --memo RECIPIENT_ADDRESS_WITHOUT_0x
```

## Next Steps

To learn more about building decentralized apps on ZetaChain, follow the tutorials available in on [the documentation](https://www.zetachain.com/docs/developers/overview/).
To learn more about building decentralized apps on ZetaChain, follow the
tutorials available in
[the documentation](https://www.zetachain.com/docs/developers/overview/).
46 changes: 15 additions & 31 deletions messaging/message/contracts/CrossChainMessage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@ interface CrossChainMessageErrors {
error InvalidMessageType();
}

/**
* @dev A simple contract able to send and receive Hello World messages from other chains.
* Emits a HelloWorldEvent on successful messages
* Emits a RevertedHelloWorldEvent on failed messages
*/
contract CrossChainMessage is
ZetaInteractor,
ZetaReceiver,
CrossChainMessageErrors
{
bytes32 public constant HELLO_WORLD_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_HELLO_WORLD");
bytes32 public constant CROSS_CHAIN_MESSAGE_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_CROSS_CHAIN_MESSAGE");

event HelloWorldEvent(string messageData);
event RevertedHelloWorldEvent(string messageData);
event CrossChainMessageEvent(string);
event CrossChainMessageRevertedEvent(string);

ZetaTokenConsumer private immutable _zetaConsumer;
IERC20 internal immutable _zetaToken;
Expand All @@ -38,7 +33,10 @@ contract CrossChainMessage is
_zetaConsumer = ZetaTokenConsumer(zetaConsumerAddress);
}

function sendHelloWorld(uint256 destinationChainId) external payable {
function sendMessage(
uint256 destinationChainId,
string memory message
) external payable {
if (!_isValidChainId(destinationChainId))
revert InvalidDestinationChainId();

Expand All @@ -53,10 +51,7 @@ contract CrossChainMessage is
destinationChainId: destinationChainId,
destinationAddress: interactorsByChainId[destinationChainId],
destinationGasLimit: 300000,
message: abi.encode(
HELLO_WORLD_MESSAGE_TYPE,
"Hello, Cross-Chain World!"
),
message: abi.encode(CROSS_CHAIN_MESSAGE_MESSAGE_TYPE, message),
zetaValueAndGas: zetaValueAndGas,
zetaParams: abi.encode("")
})
Expand All @@ -66,39 +61,28 @@ contract CrossChainMessage is
function onZetaMessage(
ZetaInterfaces.ZetaMessage calldata zetaMessage
) external override isValidMessageCall(zetaMessage) {
/**
* @dev Decode should follow the signature of the message provided to zeta.send.
*/
(bytes32 messageType, string memory helloWorldMessage) = abi.decode(
(bytes32 messageType, string memory message) = abi.decode(
zetaMessage.message,
(bytes32, string)
);

/**
* @dev Setting a message type is a useful pattern to distinguish between different messages.
*/
if (messageType != HELLO_WORLD_MESSAGE_TYPE)
if (messageType != CROSS_CHAIN_MESSAGE_MESSAGE_TYPE)
revert InvalidMessageType();

emit HelloWorldEvent(helloWorldMessage);
emit CrossChainMessageEvent(message);
}

/**
* @dev Called by the Zeta Connector contract when the message fails to be sent.
* Useful to cleanup and leave the application on its initial state.
* Note that the require statements and the functionality are similar to onZetaMessage.
*/
function onZetaRevert(
ZetaInterfaces.ZetaRevert calldata zetaRevert
) external override isValidRevertCall(zetaRevert) {
(bytes32 messageType, string memory helloWorldMessage) = abi.decode(
(bytes32 messageType, string memory message) = abi.decode(
zetaRevert.message,
(bytes32, string)
);

if (messageType != HELLO_WORLD_MESSAGE_TYPE)
if (messageType != CROSS_CHAIN_MESSAGE_MESSAGE_TYPE)
revert InvalidMessageType();

emit RevertedHelloWorldEvent(helloWorldMessage);
emit CrossChainMessageRevertedEvent(message);
}
}
9 changes: 5 additions & 4 deletions messaging/message/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/message";
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import { getHardhatConfigNetworks } from "@zetachain/networks";
import "@zetachain/toolkit/tasks";

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

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

export default config;
Loading

0 comments on commit 02ff50b

Please sign in to comment.