github.com/klaytn/klaytn@v1.12.1/contracts/bridge/BridgeTransfer.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/math/SafeMath.sol";
    20  
    21  import "./BridgeHandledRequests.sol";
    22  import "./BridgeFee.sol";
    23  import "./BridgeOperator.sol";
    24  import "./BridgeTokens.sol";
    25  
    26  contract BridgeTransfer is BridgeHandledRequests, BridgeFee, BridgeOperator {
    27      bool public modeMintBurn = false;
    28      bool public isRunning = true;
    29  
    30      uint64 public requestNonce; // the number of value transfer request that this contract received.
    31      uint64 public lowerHandleNonce; // a minimum nonce of a value transfer request that will be handled.
    32      uint64 public upperHandleNonce; // a maximum nonce of the counterpart bridge's value transfer request that is handled.
    33      uint64 public recoveryBlockNumber = 1; // the block number that recovery start to filter log from.
    34      mapping(uint64 => uint64) public handleNoncesToBlockNums;  // <request nonce> => <request blockNum>
    35  
    36      using SafeMath for uint256;
    37  
    38      enum TokenType {
    39          KLAY,
    40          ERC20,
    41          ERC721
    42      }
    43  
    44      constructor(bool _modeMintBurn) BridgeFee(address(0)) internal {
    45          modeMintBurn = _modeMintBurn;
    46      }
    47  
    48      // start can allow or disallow the value transfer request.
    49      function start(bool _status)
    50          external
    51          onlyOwner
    52      {
    53          isRunning = _status;
    54      }
    55  
    56      /**
    57       * Event to log the request value transfer from the Bridge.
    58       * @param tokenType is the type of tokens (KLAY/ERC20/ERC721).
    59       * @param from is the requester of the request value transfer event.
    60       * @param to is the receiver of the value.
    61       * @param tokenAddress Address of token contract the token belong to.
    62       * @param valueOrTokenId is the value of KLAY/ERC20 or token ID of ERC721.
    63       * @param requestNonce is the order number of the request value transfer.
    64       * @param fee is fee of value transfer.
    65       * @param extraData is additional data for specific purpose of a service provider.
    66       */
    67      event RequestValueTransfer(
    68          TokenType tokenType,
    69          address indexed from,
    70          address indexed to,
    71          address indexed tokenAddress,
    72          uint256 valueOrTokenId,
    73          uint64 requestNonce,
    74          uint256 fee,
    75          bytes extraData
    76      );
    77  
    78      /**
    79       * Event to log the request value transfer from the Bridge.
    80       * @param tokenType is the type of tokens (KLAY/ERC20/ERC721).
    81       * @param from is the requester of the request value transfer event.
    82       * @param to is the receiver of the value.
    83       * @param tokenAddress Address of token contract the token belong to.
    84       * @param valueOrTokenId is the value of KLAY/ERC20 or token ID of ERC721.
    85       * @param requestNonce is the order number of the request value transfer.
    86       * @param fee is fee of value transfer.
    87       * @param extraData is additional data for specific purpose of a service provider.
    88       * @param encodingVer indicates encodedData version.
    89       * @param encodedData is a packed set of values.
    90       */
    91      event RequestValueTransferEncoded(
    92          TokenType tokenType,
    93          address indexed from,
    94          address indexed to,
    95          address indexed tokenAddress,
    96          uint256 valueOrTokenId,
    97          uint64 requestNonce,
    98          uint256 fee,
    99          bytes extraData,
   100          uint8 encodingVer,
   101          bytes encodedData
   102      );
   103  
   104      /**
   105       * Event to log the handle value transfer from the Bridge.
   106       * @param requestTxHash is a transaction hash of request value transfer.
   107       * @param tokenType is the type of tokens (KLAY/ERC20/ERC721).
   108       * @param from is an address of the account who requested the value transfer.
   109       * @param to is an address of the account who will received the value.
   110       * @param tokenAddress Address of token contract the token belong to.
   111       * @param valueOrTokenId is the value of KLAY/ERC20 or token ID of ERC721.
   112       * @param handleNonce is the order number of the handle value transfer.
   113       * @param extraData is additional data for specific purpose of a service provider.
   114       */
   115      event HandleValueTransfer(
   116          bytes32 requestTxHash,
   117          TokenType tokenType,
   118          address indexed from,
   119          address indexed to,
   120          address indexed tokenAddress,
   121          uint256 valueOrTokenId,
   122          uint64 handleNonce,
   123          uint64 lowerHandleNonce,
   124          bytes extraData
   125      );
   126  
   127      // _updateHandleNonce increases lower and upper handle nonce after the _requestedNonce is handled.
   128      function _updateHandleNonce(uint64 _requestedNonce) internal {
   129          if (_requestedNonce > upperHandleNonce) {
   130              upperHandleNonce = _requestedNonce;
   131          }
   132  
   133          uint64 limit = lowerHandleNonce + 200;
   134          if (limit > upperHandleNonce) {
   135              limit = upperHandleNonce;
   136          }
   137  
   138          uint64 i;
   139          for (i = lowerHandleNonce; i <= limit && handleNoncesToBlockNums[i] > 0; i++) {
   140              recoveryBlockNumber = handleNoncesToBlockNums[i];
   141              delete handleNoncesToBlockNums[i];
   142              delete closedValueTransferVotes[i];
   143          }
   144          lowerHandleNonce = i;
   145      }
   146  
   147      function _lowerHandleNonceCheck(uint64 _requestedNonce) internal {
   148          require(lowerHandleNonce <= _requestedNonce, "removed vote");
   149      }
   150  
   151      // setFeeReceivers sets fee receiver.
   152      function setFeeReceiver(address payable _feeReceiver)
   153          external
   154          onlyOwner
   155      {
   156          _setFeeReceiver(_feeReceiver);
   157      }
   158  }