github.com/klaytn/klaytn@v1.12.1/contracts/bridge/BridgeTransferKLAY.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 "./BridgeTransfer.sol";
    20  
    21  
    22  contract BridgeTransferKLAY is BridgeTransfer {
    23      bool public isLockedKLAY;
    24  
    25      event KLAYLocked();
    26      event KLAYUnlocked();
    27  
    28      modifier lockedKLAY {
    29          require(isLockedKLAY == true, "unlocked");
    30          _;
    31      }
    32  
    33      modifier unlockedKLAY {
    34          require(isLockedKLAY == false, "locked");
    35          _;
    36      }
    37  
    38      // lockKLAY can to prevent request KLAY transferring.
    39      function lockKLAY()
    40          external
    41          onlyOwner
    42          unlockedKLAY
    43      {
    44          isLockedKLAY = true;
    45  
    46          emit KLAYLocked();
    47      }
    48  
    49      // unlockToken can allow request KLAY transferring.
    50      function unlockKLAY()
    51          external
    52          onlyOwner
    53          lockedKLAY
    54      {
    55          isLockedKLAY = false;
    56  
    57          emit KLAYUnlocked();
    58      }
    59  
    60      // handleKLAYTransfer sends the KLAY by the request.
    61      function handleKLAYTransfer(
    62          bytes32 _requestTxHash,
    63          address _from,
    64          address payable _to,
    65          uint256 _value,
    66          uint64 _requestedNonce,
    67          uint64 _requestedBlockNumber,
    68          bytes memory _extraData
    69      )
    70          public
    71          onlyOperators
    72      {
    73          _lowerHandleNonceCheck(_requestedNonce);
    74  
    75          if (!_voteValueTransfer(_requestedNonce)) {
    76              return;
    77          }
    78  
    79          _setHandledRequestTxHash(_requestTxHash);
    80  
    81          handleNoncesToBlockNums[_requestedNonce] = _requestedBlockNumber;
    82          _updateHandleNonce(_requestedNonce);
    83  
    84          emit HandleValueTransfer(
    85              _requestTxHash,
    86              TokenType.KLAY,
    87              _from,
    88              _to,
    89              address(0),
    90              _value,
    91              _requestedNonce,
    92              lowerHandleNonce,
    93              _extraData
    94          );
    95  
    96          _to.transfer(_value);
    97      }
    98  
    99      // _requestKLAYTransfer requests transfer KLAY to _to on relative chain.
   100      function _requestKLAYTransfer(address _to, uint256 _feeLimit,  bytes memory _extraData)
   101          internal
   102          unlockedKLAY
   103      {
   104          require(isRunning, "stopped bridge");
   105          require(msg.value > _feeLimit, "insufficient amount");
   106  
   107          uint256 fee = _payKLAYFeeAndRefundChange(_feeLimit);
   108  
   109          emit RequestValueTransfer(
   110              TokenType.KLAY,
   111              msg.sender,
   112              _to,
   113              address(0),
   114              msg.value.sub(_feeLimit),
   115              requestNonce,
   116              fee,
   117              _extraData
   118          );
   119          requestNonce++;
   120      }
   121  
   122      // () requests transfer KLAY to msg.sender address on relative chain.
   123      function () external payable {
   124          _requestKLAYTransfer(msg.sender, feeOfKLAY, new bytes(0));
   125      }
   126  
   127      // requestKLAYTransfer requests transfer KLAY to _to on relative chain.
   128      function requestKLAYTransfer(address _to, uint256 _value, bytes calldata _extraData) external payable {
   129          uint256 feeLimit = msg.value.sub(_value);
   130          _requestKLAYTransfer(_to, feeLimit, _extraData);
   131      }
   132  
   133      // chargeWithoutEvent sends KLAY to this contract without event for increasing
   134      // the withdrawal limit.
   135      function chargeWithoutEvent() external payable {}
   136  
   137      // setKLAYFee set the fee of KLAY transfer.
   138      function setKLAYFee(uint256 _fee, uint64 _requestNonce)
   139          external
   140          onlyOperators
   141      {
   142          if (!_voteConfiguration(_requestNonce)) {
   143              return;
   144          }
   145          _setKLAYFee(_fee);
   146      }
   147  }