github.com/klaytn/klaytn@v1.12.1/contracts/bridge/BridgeFee.sol (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  pragma solidity 0.5.6;
    18  
    19  import "../externals/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
    20  import "../externals/openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol";
    21  import "../externals/openzeppelin-solidity/contracts/math/SafeMath.sol";
    22  
    23  
    24  contract BridgeFee {
    25      using SafeMath for uint256;
    26  
    27      address payable public feeReceiver = address(0);
    28      uint256 public feeOfKLAY = 0;
    29      mapping (address => uint256) public feeOfERC20;
    30  
    31      event KLAYFeeChanged(uint256 indexed fee);
    32      event ERC20FeeChanged(address indexed token, uint256 indexed fee);
    33      event FeeReceiverChanged(address indexed feeReceiver);
    34  
    35      constructor(address payable _feeReceiver) internal {
    36          feeReceiver = _feeReceiver;
    37      }
    38  
    39      function _payKLAYFeeAndRefundChange(uint256 _feeLimit) internal returns(uint256) {
    40          uint256 fee = feeOfKLAY;
    41  
    42          if (feeReceiver != address(0) && fee > 0) {
    43              require(_feeLimit >= fee, "insufficient feeLimit");
    44  
    45              feeReceiver.transfer(fee);
    46              if (_feeLimit.sub(fee) > 0) {
    47                  msg.sender.transfer(_feeLimit.sub(fee));
    48              }
    49  
    50              return fee;
    51          }
    52  
    53          msg.sender.transfer(_feeLimit);
    54          return 0;
    55      }
    56  
    57      function _payERC20FeeAndRefundChange(address from, address _token, uint256 _feeLimit) internal returns(uint256) {
    58          uint256 fee = feeOfERC20[_token];
    59  
    60          if (feeReceiver != address(0) && fee > 0) {
    61              require(_feeLimit >= fee, "insufficient feeLimit");
    62  
    63              require(IERC20(_token).transfer(feeReceiver, fee), "_payERC20FeeAndRefundChange: transfer failed");
    64              if (_feeLimit.sub(fee) > 0) {
    65                  require(IERC20(_token).transfer(from, _feeLimit.sub(fee)), "_payERC20FeeAndRefundChange: transfer failed");
    66              }
    67  
    68              return fee;
    69          }
    70  
    71          require(IERC20(_token).transfer(from, _feeLimit), "_payERC20FeeAndRefundChange: transfer failed");
    72          return 0;
    73      }
    74  
    75      function _setKLAYFee(uint256 _fee) internal {
    76          feeOfKLAY = _fee;
    77          emit KLAYFeeChanged(_fee);
    78      }
    79  
    80      function _setERC20Fee(address _token, uint256 _fee) internal {
    81          feeOfERC20[_token] = _fee;
    82          emit ERC20FeeChanged(_token, _fee);
    83      }
    84  
    85      function _setFeeReceiver(address payable _to) internal {
    86          feeReceiver = _to;
    87          emit FeeReceiverChanged(_to);
    88      }
    89  }