Skip to content

Commit

Permalink
handle withdraw (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 3, 2024
1 parent 3480303 commit 1ac7d74
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions packages/localnet/src/handleOnZEVMWithdrawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,69 @@ 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",
});
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];
Expand Down

0 comments on commit 1ac7d74

Please sign in to comment.