github.com/status-im/status-go@v1.1.0/contracts/community-tokens/contracts/CommunityOwnerTokenRegistry.sol (about)

     1  // SPDX-License-Identifier: UNLICENSED
     2  
     3  pragma solidity ^0.8.17;
     4  
     5  import { Ownable2Step } from "@openzeppelin/contracts/access/Ownable2Step.sol";
     6  import { IAddressRegistry } from "./interfaces/IAddressRegistry.sol";
     7  import { OwnerToken } from "./tokens/OwnerToken.sol";
     8  
     9  /**
    10   * @title CommunityOwnerTokenRegistry contract
    11   * @author 0x-r4bbit
    12   *
    13   * This contract serves as a simple registry to map Status community addresses
    14   * to Status community `OwnerToken` addresses.
    15   * The `CommunityTokenDeployer` contract uses this registry contract to maintain
    16   * a list of community address and their token addresses.
    17   * @notice This contract will be deployed by Status similar to the `CommunityTokenDeployer`
    18   * contract.
    19   * @notice This contract maps community addresses to `OwnerToken` addresses.
    20   * @notice Only one entry per community address can exist in the registry.
    21   * @dev This registry has been extracted into its own contract so that it's possible
    22   * to introduce different version of a `CommunityDeployerContract` without needing to
    23   * migrate existing registry data, as the deployer contract would simply point at this
    24   * registry contract.
    25   * @dev Only `tokenDeployer` can add entries to the registry.
    26   */
    27  contract CommunityOwnerTokenRegistry is IAddressRegistry, Ownable2Step {
    28      error CommunityOwnerTokenRegistry_NotAuthorized();
    29      error CommunityOwnerTokenRegistry_EntryAlreadyExists();
    30      error CommunityOwnerTokenRegistry_InvalidAddress();
    31  
    32      event TokenDeployerAddressChange(address indexed);
    33      event AddEntry(address indexed, address indexed);
    34  
    35      /// @dev The address of the token deployer contract.
    36      address public tokenDeployer;
    37  
    38      mapping(address => address) public communityAddressToTokenAddress;
    39  
    40      modifier onlyTokenDeployer() {
    41          if (msg.sender != tokenDeployer) {
    42              revert CommunityOwnerTokenRegistry_NotAuthorized();
    43          }
    44          _;
    45      }
    46  
    47      /**
    48       * @notice Sets the address of the community token deployer contract. This is needed to
    49       * ensure only the known token deployer contract can add new entries to the registry.
    50       * @dev Only the owner of this contract can call this function.
    51       * @dev Emits a {TokenDeployerAddressChange} event.
    52       *
    53       * @param _tokenDeployer The address of the community token deployer contract
    54       */
    55      function setCommunityTokenDeployerAddress(address _tokenDeployer) external onlyOwner {
    56          if (_tokenDeployer == address(0)) {
    57              revert CommunityOwnerTokenRegistry_InvalidAddress();
    58          }
    59          tokenDeployer = _tokenDeployer;
    60          emit TokenDeployerAddressChange(tokenDeployer);
    61      }
    62  
    63      /**
    64       * @notice Adds an entry to the registry. Only one entry per community address can exist.
    65       * @dev Only the token deployer contract can call this function.
    66       * @dev Reverts when the entry already exists.
    67       * @dev Reverts when either `_communityAddress` or `_tokenAddress` are zero addresses.
    68       * @dev Emits a {AddEntry} event.
    69       */
    70      function addEntry(address _communityAddress, address _tokenAddress) external onlyTokenDeployer {
    71          if (getEntry(_communityAddress) != address(0)) {
    72              revert CommunityOwnerTokenRegistry_EntryAlreadyExists();
    73          }
    74  
    75          if (_communityAddress == address(0) || _tokenAddress == address(0)) {
    76              revert CommunityOwnerTokenRegistry_InvalidAddress();
    77          }
    78  
    79          communityAddressToTokenAddress[_communityAddress] = _tokenAddress;
    80          emit AddEntry(_communityAddress, _tokenAddress);
    81      }
    82  
    83      /**
    84       * @notice Returns the owner token address for a given community address.
    85       * @param _communityAddress The community address to look up an owner token address.
    86       * @return address The owner token address for the community addres, or zero address .
    87       */
    88      function getEntry(address _communityAddress) public view returns (address) {
    89          return communityAddressToTokenAddress[_communityAddress];
    90      }
    91  }