Skip to content

Commit

Permalink
Add onZetaRevert to send contract (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello authored Sep 4, 2023
1 parent f11e620 commit 90685e8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ contract MultiChainValue is ZetaInteractor, MultiChainValueErrors {
destinationChainId: destinationChainId,
destinationAddress: destinationAddress,
destinationGasLimit: 300000,
message: abi.encode(),
message: abi.encode(msg.sender),
zetaValueAndGas: zetaValueAndGas,
zetaParams: abi.encode("")
})
Expand All @@ -94,10 +94,18 @@ contract MultiChainValue is ZetaInteractor, MultiChainValueErrors {
destinationChainId: destinationChainId,
destinationAddress: destinationAddress,
destinationGasLimit: 300000,
message: abi.encode(),
message: abi.encode(msg.sender),
zetaValueAndGas: zetaValueAndGas,
zetaParams: abi.encode("")
})
);
}

function onZetaRevert(ZetaInterfaces.ZetaRevert calldata zetaRevert) external isValidRevertCall(zetaRevert) {
address messageFrom = abi.decode(zetaRevert.message, (address));

bool success1 = ZetaEth(zetaToken).approve(address(this), zetaRevert.remainingZetaValue);
bool success2 = ZetaEth(zetaToken).transferFrom(address(this), messageFrom, zetaRevert.remainingZetaValue);
if (!(success1 && success2)) revert ErrorTransferringZeta();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@ import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.so

contract ZetaConnectorMockValue is ZetaConnector {
function send(ZetaInterfaces.SendInput calldata input) external override {}

function onRevert(
address zetaTxSenderAddress,
uint256 sourceChainId,
bytes calldata destinationAddress,
uint256 destinationChainId,
uint256 remainingZetaValue,
bytes calldata message,
bytes32 internalSendHash
) external {
ZetaReceiver(zetaTxSenderAddress).onZetaRevert(
ZetaInterfaces.ZetaRevert(
zetaTxSenderAddress,
sourceChainId,
destinationAddress,
destinationChainId,
remainingZetaValue,
message
)
);
}
}
24 changes: 24 additions & 0 deletions packages/zeta-app-contracts/test/MultiChainValue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ZetaEth } from "@zetachain/interfaces/typechain-types";
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";

import {
Expand All @@ -24,6 +25,8 @@ describe("MultiChainValue tests", () => {
let deployerAddress: string;
let account1Address: string;

const encoder = new ethers.utils.AbiCoder();

beforeEach(async () => {
zetaConnectorMockContract = await deployZetaConnectorMock();
zetaEthMockContract = await deployZetaEthMock();
Expand Down Expand Up @@ -89,6 +92,27 @@ describe("MultiChainValue tests", () => {
await (await multiChainValueContractA.addAvailableChainId(1)).wait();
});

it("Should send the tokens back to the sender", async () => {
await (await multiChainValueContractA.addAvailableChainId(chainAId)).wait();
const chainId = await deployer.getChainId();

const remainingZetaValue = parseEther("15");
await zetaEthMockContract.transfer(multiChainValueContractA.address, remainingZetaValue);

await zetaConnectorMockContract.onRevert(
multiChainValueContractA.address,
chainId,
ethers.utils.solidityPack(["address"], [multiChainValueContractA.address]),
chainBId,
remainingZetaValue,
encoder.encode(["address"], [account1.address]),
ethers.utils.hexZeroPad("0x0", 32)
);

const balance = await zetaEthMockContract.balanceOf(account1.address);
await expect(balance).to.be.eq(remainingZetaValue);
});

describe("Given a valid input", () => {
it("Should send value", async () => {
await (await multiChainValueContractA.addAvailableChainId(1)).wait();
Expand Down

0 comments on commit 90685e8

Please sign in to comment.