github.com/klaytn/klaytn@v1.12.1/contracts/bridge/BridgeTokens.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/ownership/Ownable.sol";
    20  
    21  contract BridgeTokens is Ownable {
    22      mapping(address => address) public registeredTokens; // <token, counterpart token>
    23      mapping(address => uint) public indexOfTokens; // <token, index>
    24      address[] public registeredTokenList;
    25      mapping(address => bool) public lockedTokens;
    26  
    27      event TokenRegistered(address indexed token);
    28      event TokenDeregistered(address indexed token);
    29      event TokenLocked(address indexed token);
    30      event TokenUnlocked(address indexed token);
    31  
    32      modifier onlyRegisteredToken(address _token) {
    33          require(registeredTokens[_token] != address(0), "not allowed token");
    34          _;
    35      }
    36  
    37      modifier onlyNotRegisteredToken(address _token) {
    38          require(registeredTokens[_token] == address(0), "allowed token");
    39          _;
    40      }
    41  
    42      modifier onlyLockedToken(address _token) {
    43          require(lockedTokens[_token] == true, "unlocked token");
    44          _;
    45      }
    46  
    47      modifier onlyUnlockedToken(address _token) {
    48          require(lockedTokens[_token] == false, "locked token");
    49          _;
    50      }
    51  
    52      function getRegisteredTokenList() external view returns(address[] memory) {
    53          return registeredTokenList;
    54      }
    55  
    56      // registerToken can update the allowed token with the counterpart token.
    57      function registerToken(address _token, address _cToken)
    58          external
    59          onlyOwner
    60          onlyNotRegisteredToken(_token)
    61      {
    62          registeredTokens[_token] = _cToken;
    63          indexOfTokens[_token] = registeredTokenList.length;
    64          registeredTokenList.push(_token);
    65  
    66          emit TokenRegistered(_token);
    67      }
    68  
    69      // deregisterToken can remove the token in registeredToken list.
    70      function deregisterToken(address _token)
    71          external
    72          onlyOwner
    73          onlyRegisteredToken(_token)
    74      {
    75          delete registeredTokens[_token];
    76          delete lockedTokens[_token];
    77  
    78          uint idx = indexOfTokens[_token];
    79          delete indexOfTokens[_token];
    80  
    81          if (idx < registeredTokenList.length-1) {
    82              registeredTokenList[idx] = registeredTokenList[registeredTokenList.length-1];
    83              indexOfTokens[registeredTokenList[idx]] = idx;
    84          }
    85          registeredTokenList.length--;
    86  
    87          emit TokenDeregistered(_token);
    88      }
    89  
    90      // lockToken can lock the token to prevent request token transferring.
    91      function lockToken(address _token)
    92          external
    93          onlyOwner
    94          onlyRegisteredToken(_token)
    95          onlyUnlockedToken(_token)
    96      {
    97          lockedTokens[_token] = true;
    98  
    99          emit TokenLocked(_token);
   100      }
   101  
   102      // unlockToken can unlock the token to  request token transferring.
   103      function unlockToken(address _token)
   104          external
   105          onlyOwner
   106          onlyRegisteredToken(_token)
   107          onlyLockedToken(_token)
   108      {
   109          delete lockedTokens[_token];
   110  
   111          emit TokenUnlocked(_token);
   112      }
   113  }