Skip to content

Commit

Permalink
Emit events on revert
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 4, 2024
1 parent cfafa0d commit 047c664
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
24 changes: 19 additions & 5 deletions packages/localnet/src/handleOnRevertZEVM.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { ethers, NonceManager } from "ethers";
import { logErr } from "./log";

export const handleOnRevertZEVM = async ({
revertOptions,
err,
provider,
tss,
log,
fungibleModuleSigner,
protocolContracts,
deployOpts,
exitOnError = false,
}: {
revertOptions: any;
err: any;
provider: any;
fungibleModuleSigner: any;
tss: NonceManager;
log: (chain: "EVM" | "ZetaChain", ...messages: string[]) => void;
protocolContracts: any;
Expand All @@ -30,18 +35,27 @@ export const handleOnRevertZEVM = async ({
log("ZetaChain", "Gateway: calling executeRevert");
try {
tss.reset();
await protocolContracts.gatewayZEVM
.connect(tss)
const tx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.executeRevert(revertAddress, revertContext, deployOpts);
log("ZetaChain", "Gateway: Call onRevert success");
await tx.wait();
log("ZetaChain", "Gateway: successfully called onRevert");
const logs = await provider.getLogs({
address: revertAddress,
fromBlock: "latest",
});

logs.forEach((data: any) => {
log("ZetaChain", `Event from onRevert: ${JSON.stringify(data)}`);
});
} catch (err) {
const error = `Gateway: Call onRevert failed: ${err}`;
log("ZetaChain", error);
logErr("ZetaChain", error);
if (exitOnError) throw new Error(error);
}
} else {
const error = `Tx reverted without callOnRevert: ${err}`;
log("ZetaChain", error);
logErr("ZetaChain", error);
if (exitOnError) throw new Error(error);
}
};
5 changes: 5 additions & 0 deletions packages/localnet/src/handleOnZEVMCalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export const handleOnZEVMCalled = async ({
provider,
protocolContracts,
args,
fungibleModuleSigner,
exitOnError = false,
}: {
tss: any;
provider: ethers.JsonRpcProvider;
protocolContracts: any;
args: any;
fungibleModuleSigner: any;
exitOnError: boolean;
}) => {
log("ZetaChain", "Gateway: 'Called' event emitted");
Expand All @@ -38,10 +40,13 @@ export const handleOnZEVMCalled = async ({
});
await executeTx.wait();
} catch (err) {
logErr("EVM", `Error executing a contract: ${err}`);
const revertOptions = args[5];
return await handleOnRevertZEVM({
revertOptions,
err,
provider,
fungibleModuleSigner,
tss,
log,
protocolContracts,
Expand Down
4 changes: 4 additions & 0 deletions packages/localnet/src/handleOnZEVMWithdrawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const handleOnZEVMWithdrawn = async ({
provider,
protocolContracts,
args,
fungibleModuleSigner,
deployer,
foreignCoins,
exitOnError = false,
Expand All @@ -18,6 +19,7 @@ export const handleOnZEVMWithdrawn = async ({
provider: ethers.JsonRpcProvider;
protocolContracts: any;
args: any;
fungibleModuleSigner: any;
deployer: any;
foreignCoins: any[];
exitOnError: boolean;
Expand Down Expand Up @@ -98,8 +100,10 @@ export const handleOnZEVMWithdrawn = async ({
return await handleOnRevertZEVM({
revertOptions,
err,
provider,
tss,
log,
fungibleModuleSigner,
protocolContracts,
deployOpts,
exitOnError,
Expand Down
11 changes: 9 additions & 2 deletions packages/localnet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as Custody from "@zetachain/protocol-contracts/abi/ERC20Custody.sol/ERC
import * as ERC1967Proxy from "@zetachain/protocol-contracts/abi/ERC1967Proxy.sol/ERC1967Proxy.json";
import * as TestERC20 from "@zetachain/protocol-contracts/abi/TestERC20.sol/TestERC20.json";
import * as SystemContract from "@zetachain/protocol-contracts/abi/SystemContractMock.sol/SystemContractMock.json";
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";
Expand Down Expand Up @@ -305,7 +304,14 @@ export const initLocalnet = async ({

// Listen to contracts events
protocolContracts.gatewayZEVM.on("Called", async (...args: Array<any>) => {
handleOnZEVMCalled({ tss, provider, protocolContracts, args, exitOnError });
handleOnZEVMCalled({
tss,
provider,
fungibleModuleSigner,
protocolContracts,
args,
exitOnError,
});
});

protocolContracts.gatewayZEVM.on("Withdrawn", async (...args: Array<any>) => {
Expand All @@ -315,6 +321,7 @@ export const initLocalnet = async ({
protocolContracts,
args,
deployer,
fungibleModuleSigner,
foreignCoins,
exitOnError,
});
Expand Down

0 comments on commit 047c664

Please sign in to comment.