Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add onrevert to zevm connector #141

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 115 additions & 15 deletions contracts/zevm/ZetaConnectorZEVM.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import "./interfaces/IWZETA.sol";

interface ZetaInterfaces {
/**
Expand Down Expand Up @@ -46,13 +47,23 @@ interface ZetaInterfaces {
}
}

interface WZETA {
function transferFrom(address src, address dst, uint wad) external returns (bool);
interface ZetaReceiver {
/**
* @dev onZetaMessage is called when a cross-chain message reaches a contract
*/
function onZetaMessage(ZetaInterfaces.ZetaMessage calldata zetaMessage) external;

function withdraw(uint wad) external;
/**
* @dev onZetaRevert is called when a cross-chain message reverts.
* It's useful to rollback to the original state
*/
function onZetaRevert(ZetaInterfaces.ZetaRevert calldata zetaRevert) external;
}

contract ZetaConnectorZEVM is ZetaInterfaces {
contract ZetaConnectorZEVM {
/// @notice WZETA token address.
address public wzeta;
andresaiello marked this conversation as resolved.
Show resolved Hide resolved

/// @notice Contract custom errors.
error OnlyWZETA();
error WZETATransferFailed();
Expand All @@ -61,8 +72,8 @@ contract ZetaConnectorZEVM is ZetaInterfaces {

/// @notice Fungible module address.
address public constant FUNGIBLE_MODULE_ADDRESS = payable(0x735b14BB79463307AAcBED86DAf3322B1e6226aB);
/// @notice WZETA token address.
address public wzeta;

event SetWZETA(address wzeta_);

event ZetaSent(
address sourceTxOriginAddress,
Expand All @@ -74,7 +85,33 @@ contract ZetaConnectorZEVM is ZetaInterfaces {
bytes message,
bytes zetaParams
);
event SetWZETA(address wzeta_);

event ZetaReceived(
bytes zetaTxSenderAddress,
uint256 indexed sourceChainId,
address indexed destinationAddress,
uint256 zetaValue,
bytes message,
bytes32 indexed internalSendHash
);

event ZetaReverted(
address zetaTxSenderAddress,
uint256 sourceChainId,
uint256 indexed destinationChainId,
bytes destinationAddress,
uint256 remainingZetaValue,
bytes message,
bytes32 indexed internalSendHash
);

/**
* @dev Modifier to restrict actions to fungible module.
*/
modifier onlyFungibleModule() {
if (msg.sender != FUNGIBLE_MODULE_ADDRESS) revert OnlyFungibleModule();
_;
}

constructor(address wzeta_) {
wzeta = wzeta_;
Expand All @@ -85,14 +122,19 @@ contract ZetaConnectorZEVM is ZetaInterfaces {
if (msg.sender != wzeta) revert OnlyWZETA();
}

function setWzetaAddress(address wzeta_) external onlyFungibleModule {
wzeta = wzeta_;
emit SetWZETA(wzeta_);
}

/**
* @dev Sends ZETA and bytes messages (to execute it) crosschain.
* @param input, SendInput struct, checkout above.
*/
function send(ZetaInterfaces.SendInput calldata input) external {
// Transfer wzeta to "fungible" module, which will be burnt by the protocol post processing via hooks.
if (!WZETA(wzeta).transferFrom(msg.sender, address(this), input.zetaValueAndGas)) revert WZETATransferFailed();
WZETA(wzeta).withdraw(input.zetaValueAndGas);
if (!IWETH9(wzeta).transferFrom(msg.sender, address(this), input.zetaValueAndGas)) revert WZETATransferFailed();
IWETH9(wzeta).withdraw(input.zetaValueAndGas);
(bool sent, ) = FUNGIBLE_MODULE_ADDRESS.call{value: input.zetaValueAndGas}("");
if (!sent) revert FailedZetaSent();
emit ZetaSent(
Expand All @@ -108,12 +150,70 @@ contract ZetaConnectorZEVM is ZetaInterfaces {
}

/**
* @dev Sends ZETA and bytes messages (to execute it) crosschain.
* @param wzeta_, new WZETA address.
* @dev Handler to receive data from other chain.
* This method can be called only by Fungible Module.
* Transfer the Zeta tokens to destination and calls onZetaMessage if it's needed.
* To perform the transfer wrap the new tokens
*/
function setWzetaAddress(address wzeta_) external {
if (msg.sender != FUNGIBLE_MODULE_ADDRESS) revert OnlyFungibleModule();
wzeta = wzeta_;
emit SetWZETA(wzeta_);
function onReceive(
bytes calldata zetaTxSenderAddress,
uint256 sourceChainId,
address destinationAddress,
uint256 zetaValue,
bytes calldata message,
bytes32 internalSendHash
) external onlyFungibleModule {
IWETH9(wzeta).deposit{value: zetaValue}();
if (!IWETH9(wzeta).transferFrom(address(this), destinationAddress, zetaValue)) revert WZETATransferFailed();

if (message.length > 0) {
ZetaReceiver(destinationAddress).onZetaMessage(
ZetaInterfaces.ZetaMessage(zetaTxSenderAddress, sourceChainId, destinationAddress, zetaValue, message)
);
}

emit ZetaReceived(zetaTxSenderAddress, sourceChainId, destinationAddress, zetaValue, message, internalSendHash);
}

/**
* @dev Handler to receive errors from other chain.
* This method can be called only by Fungible Module.
* Transfer the Zeta tokens to destination and calls onZetaRevert if it's needed.
*/
function onRevert(
address zetaTxSenderAddress,
uint256 sourceChainId,
bytes calldata destinationAddress,
uint256 destinationChainId,
uint256 remainingZetaValue,
bytes calldata message,
bytes32 internalSendHash
) external onlyFungibleModule {
IWETH9(wzeta).deposit{value: remainingZetaValue}();
if (!IWETH9(wzeta).transferFrom(address(this), zetaTxSenderAddress, remainingZetaValue))
revert WZETATransferFailed();

if (message.length > 0) {
ZetaReceiver(zetaTxSenderAddress).onZetaRevert(
ZetaInterfaces.ZetaRevert(
zetaTxSenderAddress,
sourceChainId,
destinationAddress,
destinationChainId,
remainingZetaValue,
message
)
);
}

emit ZetaReverted(
zetaTxSenderAddress,
sourceChainId,
destinationChainId,
destinationAddress,
remainingZetaValue,
message,
internalSendHash
);
}
}
Loading
Loading