Skip to content

Commit

Permalink
Handle withdrawAndCall with ERC-20s and emit events on revert (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 8, 2024
1 parent 1ac7d74 commit d7f050f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/localnet/src/createToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export const createToken = async ({
ethers.parseUnits("1000000", erc20Decimals),
deployOpts
);
await (erc20 as any)
.connect(deployer)
.mint(
tss.getAddress(),
ethers.parseUnits("1000000", erc20Decimals),
deployOpts
);
await (erc20 as any)
.connect(deployer)
.mint(
Expand Down
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
14 changes: 9 additions & 5 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 All @@ -44,19 +46,19 @@ export const handleOnZEVMWithdrawn = async ({
return foreignCoin.asset;
};
if (message !== "0x") {
// The message is not empty, so this is a withhdrawAndCall operation
// The message is not empty, so this is a withdrawAndCall operation
log("EVM", `Calling ${receiver} with message ${message}`);
if (isGasToken) {
const executeTx = await protocolContracts.gatewayEVM
.connect(tss)
.execute(receiver, message, deployOpts);
.execute(receiver, message, { value: amount, ...deployOpts });
await executeTx.wait();
} else {
console.log("!!!");
const erc20 = getERC20ByZRC20(zrc20);

const executeTx = await protocolContracts.gatewayEVM
const executeTx = await protocolContracts.custody
.connect(tss)
.executeWithERC20(erc20, receiver, message, deployOpts);
.withdrawAndCall(receiver, erc20, amount, message, deployOpts);
await executeTx.wait();
}
const logs = await provider.getLogs({
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 d7f050f

Please sign in to comment.