Skip to content

Commit

Permalink
Reorg of cross-chain messaging contracts (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored May 8, 2024
1 parent 8aee1b6 commit 0d8bacc
Show file tree
Hide file tree
Showing 49 changed files with 434 additions and 1,095 deletions.
183 changes: 0 additions & 183 deletions messaging/counter/README.md

This file was deleted.

23 changes: 0 additions & 23 deletions messaging/counter/tasks/counter_show.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions messaging/erc20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Hardhat Template for ZetaChain

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

## Prerequisites

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

## Getting Started

To get started, install the necessary dependencies:

```
yarn
```

## Hardhat Tasks

This template includes Hardhat tasks that streamline smart contract development.
Learn more about the template and the functionality it provides
[in the docs](https://www.zetachain.com/docs/developers/template/).

## Next Steps

To learn more about building decentralized apps on ZetaChain, follow the
tutorials available in
[the introduction to ZetaChain](https://www.zetachain.com/docs/developers/overview/).
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/tools/ZetaInteractor.sol";
import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract Counter is ZetaInteractor, ZetaReceiver {
error InvalidMessageType();
error DecrementOverflow();
contract CrossChainERC20 is ERC20, ERC20Burnable, ZetaInteractor, ZetaReceiver {
error InsufficientBalance();

event CounterEvent(address);
event CounterRevertedEvent(address);
mapping(address => uint256) public counter;
event CrossChainERC20Event(address, uint256);
event CrossChainERC20RevertedEvent(address, uint256);

bytes32 public constant COUNTER_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_COUNTER");
ZetaTokenConsumer private immutable _zetaConsumer;
IERC20 internal immutable _zetaToken;

constructor(
address connectorAddress,
address zetaTokenAddress,
address zetaConsumerAddress
) ZetaInteractor(connectorAddress) {
) ERC20("CrossChain Token", "CCT") ZetaInteractor(connectorAddress) {
_zetaToken = IERC20(zetaTokenAddress);
_zetaConsumer = ZetaTokenConsumer(zetaConsumerAddress);

_mint(msg.sender, 1000000 * 10 ** decimals());
}

function sendMessage(uint256 destinationChainId) external payable {
function sendMessage(
uint256 destinationChainId,
address to,
uint256 value
) external payable {
if (!_isValidChainId(destinationChainId))
revert InvalidDestinationChainId();

Expand All @@ -38,12 +42,15 @@ contract Counter is ZetaInteractor, ZetaReceiver {
}(address(this), crossChainGas);
_zetaToken.approve(address(connector), zetaValueAndGas);

if (balanceOf(msg.sender) < value) revert InsufficientBalance();
_burn(msg.sender, value);

connector.send(
ZetaInterfaces.SendInput({
destinationChainId: destinationChainId,
destinationAddress: interactorsByChainId[destinationChainId],
destinationGasLimit: 300000,
message: abi.encode(COUNTER_MESSAGE_TYPE, msg.sender),
message: abi.encode(to, value, msg.sender),
zetaValueAndGas: zetaValueAndGas,
zetaParams: abi.encode("")
})
Expand All @@ -53,29 +60,26 @@ contract Counter is ZetaInteractor, ZetaReceiver {
function onZetaMessage(
ZetaInterfaces.ZetaMessage calldata zetaMessage
) external override isValidMessageCall(zetaMessage) {
(bytes32 messageType, address from) = abi.decode(
(address to, uint256 value) = abi.decode(
zetaMessage.message,
(bytes32, address)
(address, uint256)
);

if (messageType != COUNTER_MESSAGE_TYPE) revert InvalidMessageType();
_mint(to, value);

counter[from]++;
emit CounterEvent(from);
emit CrossChainERC20Event(to, value);
}

function onZetaRevert(
ZetaInterfaces.ZetaRevert calldata zetaRevert
) external override isValidRevertCall(zetaRevert) {
(bytes32 messageType, address from) = abi.decode(
(address to, uint256 value, address from) = abi.decode(
zetaRevert.message,
(bytes32, address)
(address, uint256, address)
);

if (messageType != COUNTER_MESSAGE_TYPE) revert InvalidMessageType();
_mint(from, value);

if (counter[from] <= 0) revert DecrementOverflow();
counter[from]--;
emit CounterRevertedEvent(from);
emit CrossChainERC20RevertedEvent(to, value);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "6.0.0",
"@zetachain/toolkit": "7.0.0",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down
Loading

0 comments on commit 0d8bacc

Please sign in to comment.