diff --git a/packages/localnet/src/handleOnZEVMWithdrawn.ts b/packages/localnet/src/handleOnZEVMWithdrawn.ts index 2c61072..3dd8412 100644 --- a/packages/localnet/src/handleOnZEVMWithdrawn.ts +++ b/packages/localnet/src/handleOnZEVMWithdrawn.ts @@ -29,12 +29,36 @@ export const handleOnZEVMWithdrawn = async ({ const amount = args[4]; 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; + 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; + }; if (message !== "0x") { + // The message is not empty, so this is a withhdrawAndCall operation log("EVM", `Calling ${receiver} with message ${message}`); - const executeTx = await protocolContracts.gatewayEVM - .connect(tss) - .execute(receiver, message, deployOpts); - await executeTx.wait(); + if (isGasToken) { + const executeTx = await protocolContracts.gatewayEVM + .connect(tss) + .execute(receiver, message, deployOpts); + await executeTx.wait(); + } else { + const erc20 = getERC20ByZRC20(zrc20); + + const executeTx = await protocolContracts.gatewayEVM + .connect(tss) + .executeWithERC20(erc20, receiver, message, deployOpts); + await executeTx.wait(); + } const logs = await provider.getLogs({ address: receiver, fromBlock: "latest", @@ -42,39 +66,32 @@ export const handleOnZEVMWithdrawn = async ({ logs.forEach((data) => { log("EVM", `Event from contract: ${JSON.stringify(data)}`); }); - } - const zrc20Contract = new ethers.Contract(zrc20, ZRC20.abi, deployer); - const coinType = await zrc20Contract.COIN_TYPE(); - if (coinType === 1n) { - 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 (coinType === 2n) { - const foreignCoin = foreignCoins.find( - (coin: any) => coin.zrc20_contract_address === zrc20 - ); - if (!foreignCoin) { - logErr("EVM", `Foreign coin not found for ZRC20 address: ${zrc20}`); - return; + } 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}` + ); } - const erc20 = foreignCoin.asset; - 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];