Skip to content

Commit

Permalink
zeta withdraw zevm
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jul 9, 2024
1 parent b8cc11b commit 0e03d10
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 99 deletions.
35 changes: 29 additions & 6 deletions contracts/prototypes/zevm/GatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "../../zevm/interfaces/IZRC20.sol";
import "../../zevm/interfaces/zContract.sol";
import "./interfaces.sol";
import "../../zevm/interfaces/IWZETA.sol";


// The GatewayZEVM contract is the endpoint to call smart contracts on omnichain
// The contract doesn't hold any funds and should never have active allowances
contract GatewayZEVM is IGatewayZEVMEvents, IGatewayZEVMErrors, Initializable, OwnableUpgradeable, UUPSUpgradeable {
address public constant FUNGIBLE_MODULE_ADDRESS = 0x735b14BB79463307AAcBED86DAf3322B1e6226aB;
address public wzeta;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize() public initializer {
function initialize(address _wzeta) public initializer {
__Ownable_init();
__UUPSUpgradeable_init();
wzeta = wzeta;
}

function _authorizeUpgrade(address newImplementation) internal override onlyOwner() {}

function _withdraw(uint256 amount, address zrc20) internal returns (uint256) {
function _withdrawZRC20(uint256 amount, address zrc20) internal returns (uint256) {
(address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee();
if (!IZRC20(gasZRC20).transferFrom(msg.sender, FUNGIBLE_MODULE_ADDRESS, gasFee)) {
revert GasFeeTransferFailed();
Expand All @@ -40,16 +44,35 @@ contract GatewayZEVM is IGatewayZEVMEvents, IGatewayZEVMErrors, Initializable, O
return gasFee;
}

function _withdrawZETA(uint256 amount) internal {
if (!IWETH9(wzeta).transferFrom(msg.sender, address(this), amount)) revert WZETATransferFailed();
IWETH9(wzeta).withdraw(amount);
(bool sent, ) = FUNGIBLE_MODULE_ADDRESS.call{value: amount}("");
if (!sent) revert FailedZetaSent();
}

// Withdraw ZRC20 tokens to external chain
function withdraw(bytes memory receiver, uint256 amount, address zrc20) external {
uint256 gasFee = _withdraw(amount, zrc20);
emit Withdrawal(msg.sender, receiver, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), "");
uint256 gasFee = _withdrawZRC20(amount, zrc20);
emit Withdrawal(msg.sender, zrc20, receiver, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), "");
}

// Withdraw ZRC20 tokens and call smart contract on external chain
function withdrawAndCall(bytes memory receiver, uint256 amount, address zrc20, bytes calldata message) external {
uint256 gasFee = _withdraw(amount, zrc20);
emit Withdrawal(msg.sender, receiver, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), message);
uint256 gasFee = _withdrawZRC20(amount, zrc20);
emit Withdrawal(msg.sender, zrc20, receiver, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), message);
}

// Withdraw ZETA to external chain
function withdraw(uint256 amount) external {
_withdrawZETA(amount);
emit Withdrawal(msg.sender, address(0), abi.encodePacked(FUNGIBLE_MODULE_ADDRESS), amount, 0, 0, "");
}

// Withdraw ZETA and call smart contract on external chain
function withdrawAndCall(uint256 amount, bytes calldata message) external {
_withdrawZETA(amount);
emit Withdrawal(msg.sender, address(0), abi.encodePacked(FUNGIBLE_MODULE_ADDRESS), amount, 0, 0, message);
}

// Call smart contract on external chain without asset transfer
Expand Down
4 changes: 3 additions & 1 deletion contracts/prototypes/zevm/interfaces.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface IGatewayZEVM {

interface IGatewayZEVMEvents {
event Call(address indexed sender, bytes receiver, bytes message);
event Withdrawal(address indexed from, bytes to, uint256 value, uint256 gasfee, uint256 protocolFlatFee, bytes message);
event Withdrawal(address indexed from, address zrc20, bytes to, uint256 value, uint256 gasfee, uint256 protocolFlatFee, bytes message);
}

interface IGatewayZEVMErrors {
Expand All @@ -46,4 +46,6 @@ interface IGatewayZEVMErrors {
error GasFeeTransferFailed();
error CallerIsNotFungibleModule();
error InvalidTarget();
error WZETATransferFailed();
error FailedZetaSent();
}
3 changes: 2 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ out = 'out'
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
no-match-contract = '.*EchidnaTest$'
no-match-contract = '.*EchidnaTest$'
auto_detect_solc = true

Large diffs are not rendered by default.

132 changes: 103 additions & 29 deletions pkg/contracts/prototypes/zevm/gatewayzevm.sol/gatewayzevm.go

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0e03d10

Please sign in to comment.