Skip to content

Commit

Permalink
feat: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 20, 2024
1 parent 09188af commit 3f149bf
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@zetachain/protocol-contracts": "10.0.0-rc8",
"ansis": "^3.3.2",
"concurrently": "^8.2.2",
"ethers": "^6.13.2",
"hardhat": "^2.22.8",
Expand Down
80 changes: 47 additions & 33 deletions packages/localnet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import * as ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";
import * as GatewayZEVM from "@zetachain/protocol-contracts/abi/GatewayZEVM.sol/GatewayZEVM.json";
import * as ZetaConnectorNonNative from "@zetachain/protocol-contracts/abi/ZetaConnectorNonNative.sol/ZetaConnectorNonNative.json";
import * as WETH9 from "@zetachain/protocol-contracts/abi/WZETA.sol/WETH9.json";
import ansis from "ansis";

const FUNGIBLE_MODULE_ADDRESS = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB";

const log = (chain: "EVM" | "ZetaChain", ...messages: any[]) => {
const color = chain === "ZetaChain" ? ansis.green : ansis.cyan;
const combinedMessage = messages.join(" ");
console.log(color(`[${ansis.bold(chain)}]: ${combinedMessage}`));
};

let protocolContracts: any;
let deployer: Signer;
const deployOpts = {
Expand Down Expand Up @@ -54,6 +61,7 @@ const deployProtocolContracts = async (
ERC1967Proxy.bytecode,
deployer
);

const proxyEVM = (await proxyEVMFactory.deploy(
gatewayEVMImpl.target,
gatewayEVMInitdata,
Expand Down Expand Up @@ -223,14 +231,14 @@ export const initLocalnet = async (port: number) => {
// Listen to contracts events
// event Called(address indexed sender, address indexed zrc20, bytes receiver, bytes message, uint256 gasLimit, RevertOptions revertOptions);
protocolContracts.gatewayZEVM.on("Called", async (...args: Array<any>) => {
console.log("Worker: Called event on GatewayZEVM.");
console.log("Worker: Calling ReceiverEVM through GatewayEVM...");
log("ZetaChain", "Gateway: 'Called' event emitted");
try {
(deployer as NonceManager).reset();

const receiver = args[2];
const message = args[3];

log("EVM", "Gateway: 'gatewayEVM.execute' method is called");
const executeTx = await protocolContracts.gatewayEVM
.connect(deployer)
.execute(receiver, message, deployOpts);
Expand Down Expand Up @@ -262,14 +270,9 @@ export const initLocalnet = async (port: number) => {
}
});

// testContracts.receiverEVM.on("ReceivedPayable", () => {
// console.log("ReceiverEVM: receivePayable called!");
// });

// event Called(address indexed sender, address indexed receiver, bytes payload, RevertOptions revertOptions);
protocolContracts.gatewayEVM.on("Called", async (...args: Array<any>) => {
console.log("Worker: Called event on GatewayEVM.");
console.log("Worker: Calling UniversalContract through GatewayZEVM...");
log("EVM", "Gateway: 'Called' event emitted");
try {
const universalContract = args[1];
const payload = args[2];
Expand All @@ -280,7 +283,6 @@ export const initLocalnet = async (port: number) => {
const sender = await fungibleModuleSigner.getAddress();
const chainID = 1;

// Call the execute function
const executeTx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.execute(
Expand All @@ -295,6 +297,7 @@ export const initLocalnet = async (port: number) => {
payload,
deployOpts
);
log("ZetaChain", "Gateway: calling execute");
await executeTx.wait();
} catch (e) {
const revertOptions = args[3];
Expand All @@ -304,27 +307,35 @@ export const initLocalnet = async (port: number) => {

// event Deposited(address indexed sender, address indexed receiver, uint256 amount, address asset, bytes payload, RevertOptions revertOptions);
protocolContracts.gatewayEVM.on("Deposited", async (...args: Array<any>) => {
console.log("Worker: Deposited event on GatewayEVM.");
console.log("Worker: Calling TestUniversalContract through GatewayZEVM...");
log("EVM", "Gateway: 'Deposited' event emitted");
try {
const receiver = args[1];
const amount = args[2];
const payload = args[4];
if (payload != "0x") {
const message = args[4];
if (message != "0x") {
const context = {
origin: protocolContracts.gatewayZEVM.target,
sender: await fungibleModuleSigner.getAddress(),
chainID: 1,
};
const zrc20 = protocolContracts.zrc20Eth.target;
const executeTx = await (protocolContracts.gatewayZEVM as any)
.connect(fungibleModuleSigner)
.execute(
[
protocolContracts.gatewayZEVM.target,
await fungibleModuleSigner.getAddress(),
1,
],
protocolContracts.zrc20Eth.target,
amount,
receiver,
payload,
deployOpts
);
.execute(context, zrc20, amount, receiver, message, deployOpts);
log(
"ZetaChain",
`Universal contract ${receiver} onCrossChainCall is executed (context: ${JSON.stringify(
context
)}), zrc20: ${zrc20}, amount: ${amount}, message: ${message})`
);
const logs = await provider.getLogs({
address: receiver,
fromBlock: "latest",
});

logs.forEach((data) => {
log("ZetaChain", `Universal contract event: ${JSON.stringify(data)}`);
});
await executeTx.wait();
}
} catch (e) {
Expand All @@ -343,18 +354,18 @@ export const initLocalnet = async (port: number) => {
revertMessage,
};
if (callOnRevert) {
console.log("Tx reverted, calling executeRevert on GatewayEVM...");
log("EVM", "Gateway: calling executeRevert");
try {
(deployer as NonceManager).reset();
await protocolContracts.gatewayEVM
.connect(deployer)
.executeRevert(revertAddress, "0x", revertContext, deployOpts);
console.log("Call onRevert success");
log("EVM", "Gateway: Call onRevert success");
} catch (e) {
console.log("Call onRevert failed:", e);
log("EVM", "Gateway: Call onRevert failed", e);
}
} else {
console.log("Tx reverted without callOnRevert: ", err);
log("EVM", "Tx reverted without callOnRevert: ", err);
}
};

Expand All @@ -368,18 +379,18 @@ export const initLocalnet = async (port: number) => {
revertMessage,
};
if (callOnRevert) {
console.log("Tx reverted, calling executeRevert on GatewayZEVM...");
log("ZetaChain", "Gateway: calling executeRevert");
try {
(deployer as NonceManager).reset();
await protocolContracts.gatewayZEVM
.connect(deployer)
.executeRevert(revertAddress, revertContext, deployOpts);
console.log("Call onRevert success");
log("ZetaChain", "Gateway: Call onRevert success");
} catch (e) {
console.log("Call onRevert failed:", e);
log("ZetaChain", "Gateway: Call onRevert failed", e);
}
} else {
console.log("Tx reverted without callOnRevert: ", err);
log("ZetaChain", "Tx reverted without callOnRevert: ", err);
}
};

Expand All @@ -388,5 +399,8 @@ export const initLocalnet = async (port: number) => {
return {
gatewayEVM: protocolContracts.gatewayEVM.target,
gatewayZetaChain: protocolContracts.gatewayZEVM.target,
zetaEVM: protocolContracts.testEVMZeta.target,
zetaZetaChain: protocolContracts.wzeta.target,
zrc20ETHZetaChain: protocolContracts.zrc20Eth.target,
};
};
27 changes: 24 additions & 3 deletions packages/tasks/src/localnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { task, types } from "hardhat/config";
import { initLocalnet } from "../../localnet/src";
import { exec } from "child_process";
import waitOn from "wait-on";
import ansis from "ansis";

const main = async (args: any) => {
const port = args.port || 8545;
Expand All @@ -20,10 +21,30 @@ const main = async (args: any) => {

await waitOn({ resources: [`tcp:127.0.0.1:${port}`] });

const { gatewayEVM, gatewayZetaChain } = await initLocalnet(port);
const {
gatewayEVM,
gatewayZetaChain,
zetaEVM,
zetaZetaChain,
zrc20ETHZetaChain,
} = await initLocalnet(port);

console.log("Gateway EVM:", gatewayEVM);
console.log("Gateway ZetaChain:", gatewayZetaChain);
console.log(ansis.cyan`
EVM Contract Addresses
======================
Gateway EVM: ${gatewayEVM}
ZETA: ${zetaEVM}
`);

console.log(ansis.green`
ZetaChain Contract Addresses
============================
Gateway ZetaChain: ${gatewayZetaChain}
ZETA: ${zetaZetaChain}
ZRC-20 ETH: ${zrc20ETHZetaChain}
`);

process.on("SIGINT", () => {
console.log("\nReceived Ctrl-C, shutting down anvil...");
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,11 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"

ansis@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.3.2.tgz#15adc36fea112da95c74d309706e593618accac3"
integrity sha512-cFthbBlt+Oi0i9Pv/j6YdVWJh54CtjGACaMPCIrEV4Ha7HWsIjXDwseYV79TIL0B4+KfSwD5S70PeQDkPUd1rA==

anymatch@~3.1.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e"
Expand Down

0 comments on commit 3f149bf

Please sign in to comment.