-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: lumtis <lucas.bertrand.22@gmail.com>
- Loading branch information
Showing
34 changed files
with
4,413 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.7; | ||
|
||
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; | ||
import "../../zevm/interfaces/IZRC20.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 Initializable, OwnableUpgradeable, UUPSUpgradeable { | ||
address public constant FUNGIBLE_MODULE_ADDRESS = 0x735b14BB79463307AAcBED86DAf3322B1e6226aB; | ||
|
||
error WithdrawalFailed(); | ||
error InsufficientZRC20Amount(); | ||
error GasFeeTransferFailed(); | ||
|
||
event Call(address indexed sender, bytes indexed receiver, bytes message); | ||
event Withdrawal(address indexed from, bytes to, uint256 value, uint256 gasfee, uint256 protocolFlatFee, bytes message); | ||
|
||
/// @custom:oz-upgrades-unsafe-allow constructor | ||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize() public initializer { | ||
__Ownable_init(); | ||
__UUPSUpgradeable_init(); | ||
} | ||
|
||
function _authorizeUpgrade(address newImplementation) internal override onlyOwner() {} | ||
|
||
function _withdraw(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(); | ||
} | ||
|
||
IZRC20(zrc20).transferFrom(msg.sender, address(this), amount); | ||
IZRC20(zrc20).burn(amount); | ||
|
||
return gasFee; | ||
} | ||
|
||
// 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(), ""); | ||
} | ||
|
||
// 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); | ||
} | ||
|
||
// Call smart contract on external chain without asset transfer | ||
function call(bytes memory receiver, bytes calldata message) external { | ||
emit Call(msg.sender, receiver, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.7; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "./interfaces.sol"; | ||
import "../../zevm/interfaces/IZRC20.sol"; | ||
|
||
contract Sender { | ||
address public gateway; | ||
|
||
constructor(address _gateway) { | ||
gateway = _gateway; | ||
} | ||
|
||
// Call receiver on EVM | ||
function callReceiver(bytes memory receiver, string memory str, uint256 num, bool flag) external { | ||
// Encode the function call to the receiver's receiveA method | ||
bytes memory message = abi.encodeWithSignature("receiveA(string,uint256,bool)", str, num, flag); | ||
|
||
// Pass encoded call to gateway | ||
IGatewayZEVM(gateway).call(receiver, message); | ||
} | ||
|
||
// Withdraw and call receiver on EVM | ||
function withdrawAndCallReceiver(bytes memory receiver, uint256 amount, address zrc20, string memory str, uint256 num, bool flag) external { | ||
// Encode the function call to the receiver's receiveA method | ||
bytes memory message = abi.encodeWithSignature("receiveA(string,uint256,bool)", str, num, flag); | ||
|
||
// Approve gateway to withdraw | ||
IZRC20(zrc20).approve(gateway, amount); | ||
|
||
// Pass encoded call to gateway | ||
IGatewayZEVM(gateway).withdrawAndCall(receiver, amount, zrc20, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.7; | ||
|
||
interface IGatewayZEVM { | ||
function withdraw(bytes memory receiver, uint256 amount, address zrc20) external; | ||
|
||
function withdrawAndCall(bytes memory receiver, uint256 amount, address zrc20, bytes calldata message) external; | ||
|
||
function call(bytes memory receiver, bytes calldata message) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,477 changes: 1,477 additions & 0 deletions
1,477
pkg/contracts/prototypes/zevm/gatewayzevm.sol/gatewayzevm.go
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.