Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 20, 2024
1 parent 7bfeadb commit f7d796b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
34 changes: 26 additions & 8 deletions universal/hello/contracts/Hello.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@ pragma solidity 0.8.26;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {RevertContext} from "@zetachain/protocol-contracts/contracts/Revert.sol";
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";

contract Hello is UniversalContract {
event HelloEvent(string message);
event HelloEvent(string, string);

event ContextDataRevert(RevertContext revertContext);

address constant gateway = 0x610178dA211FEF7D417bC0e6FeD39F05609AD788;

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override {
emit HelloEvent("Hello from a universal app");
revert("REVERTING!!!");
// string memory decodedMessage;
// if (message.length > 0) {
// decodedMessage = abi.decode(message, (string));
// }
string memory decodedMessage;
if (message.length > 0) {
decodedMessage = abi.decode(message, (string));
}
emit HelloEvent("Hello from a universal app", decodedMessage);
}

function callFromZetaChain(
bytes memory receiver,
address zrc20,
bytes calldata message,
uint256 gasLimit,
RevertOptions memory revertOptions
) external {
IGatewayZEVM(gateway).call(
receiver,
zrc20,
message,
gasLimit,
revertOptions
);
}

function onRevert(RevertContext calldata revertContext) external override {
Expand Down
4 changes: 2 additions & 2 deletions universal/hello/contracts/RevertContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ contract RevertContract {
event ContextDataRevert(RevertContext revertContext);

function onRevert(RevertContext calldata revertContext) external {
emit ContextDataRevert(revertContext);
// emit RevertEvent("Event from RevertContract!!!");
// emit ContextDataRevert(revertContext);
emit RevertEvent("Event from RevertContract!!!");
}

receive() external payable {}
Expand Down
1 change: 1 addition & 0 deletions universal/hello/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/callFromZetaChain";
import "./tasks/solana/interact";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
Expand Down
5 changes: 3 additions & 2 deletions universal/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint:fix": "npx eslint . --ext .js,.ts --fix",
"lint": "npx eslint . --ext .js,.ts"
"lint": "npx eslint . --ext .js,.ts",
"deploy": "npx hardhat compile --force && npx hardhat deploy --network localhost --name Hello && npx hardhat deploy --network localhost --name RevertContract"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -58,4 +59,4 @@
"@solana/spl-memo": "^0.2.5",
"@solana/web3.js": "^1.95.2"
}
}
}
63 changes: 63 additions & 0 deletions universal/hello/tasks/callFromZetaChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import { utils } from "ethers";

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

const contractArtifact = await hre.artifacts.readArtifact("Hello");
const contract = new hre.ethers.Contract(
args.contract,
contractArtifact.abi,
signer
);

const message = hre.ethers.utils.defaultAbiCoder.encode(
["string"],
[args.message]
);

const revertMessageBytes = hre.ethers.utils.toUtf8Bytes(args.revertMessage);

try {
const tx = await contract.callFromZetaChain(
hre.ethers.utils.hexlify(args.receiver),
args.zrc20,
message,
args.gasLimit,
{
revertAddress: args.revertAddress,
callOnRevert: args.callOnRevert,
abortAddress: "0x0000000000000000000000000000000000000000", // not used
revertMessage: hre.ethers.utils.hexlify(revertMessageBytes),
},
{
gasPrice: 10000000000,
gasLimit: 7000000,
}
);

await tx.wait();
console.log("Successfully called the contract on ZetaChain!");
} catch (e) {
console.error("Error calling contract:", e);
}
};

task(
"call-from-zetachain",
"Calls the callFromZetaChain function on a universal app",
main
)
.addParam("message", "A message")
.addParam("contract", "The address of the universal app on ZetaChain")
.addOptionalParam(
"zrc20",
"The address of the ZRC20 token",
"0x9fd96203f7b22bCF72d9DCb40ff98302376cE09c"
)
.addParam("gasLimit", "The gas limit for the transaction", 7000000, types.int)
.addFlag("callOnRevert", "Whether to call on revert")
.addParam("revertAddress")
.addParam("revertMessage")
.addParam("receiver", "The address of the receiver contract on EVM");
14 changes: 8 additions & 6 deletions universal/hello/tasks/interact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import type { HardhatRuntimeEnvironment } from "hardhat/types";
import GatewayABI from "@zetachain/protocol-contracts/abi/GatewayEVM.sol/GatewayEVM.json";
import { utils } from "ethers";
Expand All @@ -12,27 +12,29 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
signer
);

const revertMessageBytes = hre.ethers.utils.toUtf8Bytes(args.revertMessage);

const message = hre.ethers.utils.defaultAbiCoder.encode(
["string"],
[args.name]
);
try {
const callTx = await gateway[
"depositAndCall(address,bytes,(address,bool,address,bytes))"
// "call(address,bytes,(address,bool,address,bytes))"
// "depositAndCall(address,bytes,(address,bool,address,bytes))"
"call(address,bytes,(address,bool,address,bytes))"
](
args.contract,
message,
{
revertAddress: args.revertAddress,
callOnRevert: args.callOnRevert,
abortAddress: "0x0000000000000000000000000000000000000000", // not used
revertMessage: args.revertMessage,
revertMessage: hre.ethers.utils.hexlify(revertMessageBytes),
},
{
gasPrice: 10000000000,
gasLimit: 7000000,
value: hre.ethers.utils.parseEther(args.amount),
// value: hre.ethers.utils.parseEther(args.amount),
}
);
await callTx.wait();
Expand All @@ -52,7 +54,7 @@ task("interact", "calls zevm zcontract from evm account", main)
"contract address of gateway on EVM",
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
)
.addFlag("callOnRevert", "Whether to call on revert")
.addParam("revertAddress")
.addFlag("callOnRevert")
.addParam("revertMessage")
.addParam("amount", "amount of ETH to send with the transaction");

0 comments on commit f7d796b

Please sign in to comment.