Skip to content

Commit

Permalink
withdraw and call erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 22, 2024
1 parent 33c46c1 commit ff9d75f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 48 deletions.
1 change: 0 additions & 1 deletion packages/localnet/src/handleOnZEVMCalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const handleOnZEVMCalled = async ({
const messageContext = {
sender: isArbitraryCall ? ethers.ZeroAddress : sender,
};
console.log(messageContext);
log("EVM", `Calling ${receiver} with message ${message}`);

const executeTx = await protocolContracts.gatewayEVM
Expand Down
69 changes: 22 additions & 47 deletions packages/localnet/src/handleOnZEVMWithdrawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,29 @@ export const handleOnZEVMWithdrawn = async ({
const coinType = await zrc20Contract.COIN_TYPE();
const isGasToken = coinType === 1n;
const isERC20orZETA = coinType === 2n;
if (message !== "0x") {
// 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, { value: amount, ...deployOpts });
await executeTx.wait();
} else {
const erc20 = getERC20ByZRC20(zrc20);
const executeTx = await protocolContracts.custody
.connect(tss)
.withdrawAndCall(receiver, erc20, amount, message, deployOpts);
await executeTx.wait();
}
const logs = await provider.getLogs({
address: receiver,
fromBlock: "latest",
if (isGasToken) {
const tx = await tss.sendTransaction({
to: receiver,
value: amount,
...deployOpts,
});
logs.forEach((data) => {
log("EVM", `Event from contract: ${JSON.stringify(data)}`);
});
} else {
// The message is empty, so this is a withdraw operation
if (isGasToken) {
const tx = await tss.sendTransaction({
to: receiver,
value: amount,
...deployOpts,
});
await tx.wait();
log(
"EVM",
`Transferred ${ethers.formatEther(
amount
)} native gas tokens from TSS to ${receiver}`
);
} else if (isERC20orZETA) {
const erc20 = getERC20ByZRC20(zrc20);
const tx = await protocolContracts.custody
.connect(tss)
.withdraw(receiver, erc20, amount, deployOpts);
await tx.wait();
log(
"EVM",
`Transferred ${amount} ERC-20 tokens from Custody to ${receiver}`
);
}
await tx.wait();
log(
"EVM",
`Transferred ${ethers.formatEther(
amount
)} native gas tokens from TSS to ${receiver}`
);
} else if (isERC20orZETA) {
const erc20 = getERC20ByZRC20(zrc20);
const tx = await protocolContracts.custody
.connect(tss)
.withdraw(receiver, erc20, amount, deployOpts);
await tx.wait();
log(
"EVM",
`Transferred ${amount} ERC-20 tokens from Custody to ${receiver}`
);
}
} catch (err) {
const revertOptions = args[9];
Expand Down
101 changes: 101 additions & 0 deletions packages/localnet/src/handleOnZEVMWithdrawnAndCalled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ethers, NonceManager } from "ethers";
import { handleOnRevertZEVM } from "./handleOnRevertZEVM";
import { log, logErr } from "./log";
import { deployOpts } from "./deployOpts";
import * as ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";

export const handleOnZEVMWithdrawnAndCalled = async ({
tss,
provider,
protocolContracts,
args,
fungibleModuleSigner,
deployer,
foreignCoins,
exitOnError = false,
}: {
tss: any;
provider: ethers.JsonRpcProvider;
protocolContracts: any;
args: any;
fungibleModuleSigner: any;
deployer: any;
foreignCoins: any[];
exitOnError: boolean;
}) => {
log("ZetaChain", "Gateway: 'WithdrawnAndCalled' event emitted");
console.log(args);
const getERC20ByZRC20 = (zrc20: string) => {
const foreignCoin = foreignCoins.find(
(coin: any) => coin.zrc20_contract_address === zrc20
);
if (!foreignCoin) {
logErr("EVM", `Foreign coin not found for ZRC20 address: ${zrc20}`);
return;
}
return foreignCoin.asset;
};
const sender = args[0];
const zrc20 = args[3];
const amount = args[4];
const callOptions = args[8];
const isArbitraryCall = callOptions[1];
const messageContext = {
sender: isArbitraryCall ? ethers.ZeroAddress : sender,
};
try {
const receiver = args[2];
const message = args[7];
(tss as NonceManager).reset();
const zrc20Contract = new ethers.Contract(zrc20, ZRC20.abi, deployer);
const coinType = await zrc20Contract.COIN_TYPE();
const isGasToken = coinType === 1n;
const isERC20orZETA = coinType === 2n;
// 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(messageContext, receiver, message, {
value: amount,
...deployOpts,
});
await executeTx.wait();
} else {
const erc20 = getERC20ByZRC20(zrc20);
const executeTx = await protocolContracts.custody
.connect(tss)
.withdrawAndCall(
messageContext,
receiver,
erc20,
amount,
message,
deployOpts
);
await executeTx.wait();
}
const logs = await provider.getLogs({
address: receiver,
fromBlock: "latest",
});
logs.forEach((data) => {
log("EVM", `Event from contract: ${JSON.stringify(data)}`);
});
} catch (err) {
const revertOptions = args[9];
return await handleOnRevertZEVM({
revertOptions,
err,
provider,
tss,
asset: getERC20ByZRC20(zrc20),
amount,
log,
fungibleModuleSigner,
protocolContracts,
deployOpts,
exitOnError,
});
}
};
17 changes: 17 additions & 0 deletions packages/localnet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { deployOpts } from "./deployOpts";
import { handleOnEVMDeposited } from "./handleOnEVMDeposited";
import { handleOnZEVMWithdrawn } from "./handleOnZEVMWithdrawn";
import { createToken } from "./createToken";
import { handleOnZEVMWithdrawnAndCalled } from "./handleOnZEVMWithdrawnAndCalled";

const FUNGIBLE_MODULE_ADDRESS = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB";

Expand Down Expand Up @@ -353,6 +354,22 @@ export const initLocalnet = async ({
});
});

protocolContracts.gatewayZEVM.on(
"WithdrawnAndCalled",
async (...args: Array<any>) => {
handleOnZEVMWithdrawnAndCalled({
tss,
provider,
protocolContracts,
args,
deployer,
fungibleModuleSigner,
foreignCoins,
exitOnError,
});
}
);

protocolContracts.gatewayEVM.on("Called", async (...args: Array<any>) => {
return await handleOnEVMCalled({
tss,
Expand Down

0 comments on commit ff9d75f

Please sign in to comment.