MirrorgateEthVault.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: BUSL-1.1
  2. // Copyright (C) 2015, 2016, 2017 Dapphub
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. pragma solidity ^0.7.6;
  14. import "@openzeppelin/contracts/access/Ownable.sol";
  15. import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
  16. import "./interfaces/IMirrorgateEthVault.sol";
  17. // This contract always UNWRAPS the erc20 for native gas token on transfer + transferFrom.
  18. // If you wish to disable the transfer auto-unwrap, you can specify _to addresses with `setNoUnwrapTo`
  19. contract MirrorgateEthVault is IMirrorgateEthVault, Ownable, ReentrancyGuard {
  20. string public constant name = "Mirrorgate Ether Vault";
  21. string public constant symbol = "MGETH";
  22. uint8 public constant decimals = 18;
  23. uint256 public totalSupply;
  24. event Approval(address indexed src, address indexed guy, uint wad);
  25. event Transfer(address indexed src, address indexed dst, uint wad);
  26. event Deposit(address indexed dst, uint wad);
  27. event Withdrawal(address indexed src, uint wad);
  28. event TransferNative(address indexed src, address indexed dst, uint wad);
  29. mapping(address => uint) public balanceOf;
  30. mapping(address => mapping(address => uint)) public allowance;
  31. mapping(address => bool) public noUnwrapTo;
  32. // if you do NOT wish to unwrap eth on transfers TO certain addresses
  33. function setNoUnwrapTo(address _addr) external onlyOwner {
  34. noUnwrapTo[_addr] = true;
  35. }
  36. function deposit() public payable override {
  37. balanceOf[msg.sender] += msg.value;
  38. totalSupply += msg.value;
  39. emit Deposit(msg.sender, msg.value);
  40. }
  41. function withdraw(uint wad) external override {
  42. require(balanceOf[msg.sender] >= wad);
  43. balanceOf[msg.sender] -= wad;
  44. msg.sender.transfer(wad);
  45. totalSupply -= wad;
  46. emit Withdrawal(msg.sender, wad);
  47. }
  48. function approve(address guy, uint wad) external override returns (bool) {
  49. allowance[msg.sender][guy] = wad;
  50. emit Approval(msg.sender, guy, wad);
  51. return true;
  52. }
  53. function transfer(address dst, uint wad) external override returns (bool) {
  54. return transferFrom(msg.sender, dst, wad);
  55. }
  56. function transferFrom(address src, address dst, uint wad) public override nonReentrant returns (bool) {
  57. require(balanceOf[src] >= wad);
  58. if (src != msg.sender && allowance[src][msg.sender] != uint(- 1)) {
  59. require(allowance[src][msg.sender] >= wad);
  60. allowance[src][msg.sender] -= wad;
  61. }
  62. // always decrement the src (payer) address
  63. balanceOf[src] -= wad;
  64. if (noUnwrapTo[dst]) {
  65. // we do *not* unwrap
  66. balanceOf[dst] += wad;
  67. emit Transfer(src, dst, wad);
  68. } else {
  69. // unwrap and send native gas token
  70. totalSupply -= wad;
  71. // if its getting unwrapped, decrement the totalSupply
  72. (bool success,) = dst.call{value : wad}("");
  73. require(success, "SGETH: failed to transfer");
  74. emit TransferNative(src, dst, wad);
  75. }
  76. return true;
  77. }
  78. function renounceOwnership() public override onlyOwner {}
  79. receive() external payable {
  80. deposit();
  81. }
  82. }