Skip to content

Commit

Permalink
deduct gas costs on revert for gas assets
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 17, 2024
1 parent 54caf43 commit 3fbd3c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions packages/localnet/src/handleOnEVMDeposited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ethers, NonceManager } from "ethers";
import { handleOnRevertEVM } from "./handleOnRevertEVM";
import { log, logErr } from "./log";
import { deployOpts } from "./deployOpts";
import * as ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";

// event Deposited(address indexed sender, address indexed receiver, uint256 amount, address asset, bytes payload, RevertOptions revertOptions);
export const handleOnEVMDeposited = async ({
Expand All @@ -28,21 +29,20 @@ export const handleOnEVMDeposited = async ({
const amount = args[2];
const asset = args[3];
const message = args[4];
try {
let foreignCoin;
if (asset === ethers.ZeroAddress) {
foreignCoin = foreignCoins.find((coin) => coin.coin_type === "Gas");
} else {
foreignCoin = foreignCoins.find((coin) => coin.asset === asset);
}

if (!foreignCoin) {
logErr("ZetaChain", `Foreign coin not found for asset: ${asset}`);
return;
}
let foreignCoin;
if (asset === ethers.ZeroAddress) {
foreignCoin = foreignCoins.find((coin) => coin.coin_type === "Gas");
} else {
foreignCoin = foreignCoins.find((coin) => coin.asset === asset);
}

const zrc20 = foreignCoin.zrc20_contract_address;
if (!foreignCoin) {
logErr("ZetaChain", `Foreign coin not found for asset: ${asset}`);
return;
}

const zrc20 = foreignCoin.zrc20_contract_address;
try {
const context = {
origin: protocolContracts.gatewayZEVM.target,
sender: await fungibleModuleSigner.getAddress(),
Expand Down Expand Up @@ -84,10 +84,15 @@ export const handleOnEVMDeposited = async ({
} catch (err) {
logErr("ZetaChain", `Error depositing: ${err}`);
const revertOptions = args[5];
const zrc20Contract = new ethers.Contract(zrc20, ZRC20.abi, deployer);
const [gasZRC20, gasFee] = await zrc20Contract.withdrawGasFeeWithGasLimit(
revertOptions[4]
);
const amountReverted = amount - gasFee;
return await handleOnRevertEVM({
revertOptions,
asset,
amount,
amount: amountReverted,
err,
tss,
provider,
Expand Down
2 changes: 1 addition & 1 deletion packages/localnet/src/handleOnRevertEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const handleOnRevertEVM = async ({
const revertMessage = revertOptions[3];
const revertContext = {
asset,
amount, // this should deduct the gas costs
amount,
revertMessage,
};
if (callOnRevert) {
Expand Down

0 comments on commit 3fbd3c0

Please sign in to comment.