RouterETH.sol 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: BUSL-1.1
  2. pragma solidity 0.7.6;
  3. pragma abicoder v2;
  4. import "./interfaces/IMirrorgateRouter.sol";
  5. import "./interfaces/IMirrorgateEthVault.sol";
  6. contract RouterETH {
  7. address public immutable mirrorgateEthVault;
  8. IMirrorgateRouter public immutable mirrorgateRouter;
  9. uint16 public immutable poolId;
  10. constructor(address _mirrorgateEthVault, address _mirrorgateRouter, uint16 _poolId){
  11. require(_mirrorgateEthVault != address(0x0), "RouterETH: _mirrorgateEthVault cant be 0x0");
  12. require(_mirrorgateRouter != address(0x0), "RouterETH: _mirrorgateRouter cant be 0x0");
  13. mirrorgateEthVault = _mirrorgateEthVault;
  14. mirrorgateRouter = IMirrorgateRouter(_mirrorgateRouter);
  15. poolId = _poolId;
  16. }
  17. function addLiquidityETH() external payable {
  18. require(msg.value > 0, "Mirrorgate: msg.value is 0");
  19. uint256 amountLD = msg.value;
  20. // wrap the ETH into WETH
  21. IMirrorgateEthVault(mirrorgateEthVault).deposit{value: amountLD}();
  22. IMirrorgateEthVault(mirrorgateEthVault).approve(address(mirrorgateRouter), amountLD);
  23. // addLiquidity using the WETH that was just wrapped,
  24. // and mint the LP token to the msg.sender
  25. mirrorgateRouter.addLiquidity(
  26. poolId,
  27. amountLD,
  28. msg.sender
  29. );
  30. }
  31. // compose mirrorgate to swap ETH on the source to ETH on the destination
  32. function swapETH(
  33. uint16 _dstChainId, // destination mirrorgate chainId
  34. address payable _refundAddress, // refund additional messageFee to this address
  35. bytes calldata _toAddress, // the receiver of the destination ETH
  36. uint256 _amountLD, // the amount, in Local Decimals, to be swapped
  37. uint256 _minAmountLD // the minimum amount accepted out on destination
  38. ) external payable {
  39. require(msg.value > _amountLD, "Mirrorgate: msg.value must be > _amountLD");
  40. // wrap the ETH into WETH
  41. IMirrorgateEthVault(mirrorgateEthVault).deposit{value: _amountLD}();
  42. IMirrorgateEthVault(mirrorgateEthVault).approve(address(mirrorgateRouter), _amountLD);
  43. // messageFee is the remainder of the msg.value after wrap
  44. uint256 messageFee = msg.value - _amountLD;
  45. // compose a mirrorgate swap() using the WETH that was just wrapped
  46. mirrorgateRouter.swap{value: messageFee}(
  47. _dstChainId, // destination mirrorgate chainId
  48. poolId, // WETH mirrorgate poolId on source
  49. poolId, // WETH mirrorgate poolId on destination
  50. _refundAddress, // message refund address if overpaid
  51. _amountLD, // the amount in Local Decimals to swap()
  52. _minAmountLD, // the minimum amount swap()er would allow to get out (ie: slippage)
  53. IMirrorgateRouter.lzTxObj(0, 0, "0x"),
  54. _toAddress, // address on destination to send to
  55. bytes("") // empty payload, since sending to EOA
  56. );
  57. }
  58. // this contract needs to accept ETH
  59. receive() external payable {}
  60. }