// File: contracts/RACTokenDistributor.sol



//SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/access/Ownable.sol";

interface IERC20 {
  function transfer(address recipient, uint256 amount) external returns (bool);

  function balanceOf(address account) external view returns (uint256);
}

/// @title RAC Token Distributor Smart Contract
/// @author HIFI Labs Inc.
/// @notice This contract allows an address to claim tokens if they have any pending tokens to claim

contract RACTokenDistributor is Ownable {
  /// @notice instantiates RAC token
  IERC20 racInstance;

  /// @notice maps and address to an withdrawable rac token amount
  mapping(address => uint256) public claimableTokensPerAddress;

  /// @notice Emitted when user can claim tokens
  /// @param user The address of user
  /// @param amountClaimable amount of claimable tokens for the user
  event ClaimableRACTokens(address indexed user, uint256 amountClaimable);

  /// @notice Emitted when user claims
  /// @param user The address of user
  /// @param amountClaimed amount claimed
  event RACClaimed(address indexed user, uint256 amountClaimed);

  /// @notice create a constructor to initialize racInstance
  /// @param erc20Address The address of erc20 token
  constructor(address erc20Address) {
    racInstance = IERC20(erc20Address);
  }

  /// @notice withdraws contract rac token balance
  /// @param _address address to send rac tokens to
  /// @dev this function is callable by only admin
  function withdrawContractBalance(address _address) external onlyOwner {
    uint256 contractBalance = racInstance.balanceOf(address(this));

    require(

      contractBalance > 0,
      "there are no tokens to transfer"
    );

    racInstance.transfer(_address, contractBalance);
  }

  /// @notice batch set claimable tokens per address
  /// @param _users arrays of users
  /// @param _claimableTokens arrays of subscription types which users will be mapped to respectively
  /// @param _shouldAccumulate specifies if new value should be added with existing claimable balance
  /// @dev this function is callable by only admin
  function batchUpdateClaimableRACTokensPerAddress(
    address[] calldata _users,
    uint256[] calldata _claimableTokens,
    bool _shouldAccumulate
  ) external onlyOwner {
    require(
      _users.length == _claimableTokens.length,
      "users and claimableTokens length mismatch"
    );

    for (uint256 i = 0; i < _users.length; i++) {
      if (_shouldAccumulate) {
        claimableTokensPerAddress[_users[i]] += _claimableTokens[i];
      } else {
        claimableTokensPerAddress[_users[i]] = _claimableTokens[i];
      }
      emit ClaimableRACTokens(_users[i], _claimableTokens[i]);
    }
  }

  /// @notice function to claim available RAC Tokens
  function claimRACTokens() external {
    uint256 claimableTokens = claimableTokensPerAddress[msg.sender];

    require(
      claimableTokens > 0,
      "there are no tokens to claim for this address"
    );

    claimableTokensPerAddress[msg.sender] = 0;

    bool sent = racInstance.transfer(msg.sender, claimableTokens);

    require(sent, "Token transfer failed");

    emit RACClaimed(msg.sender, claimableTokens);
  }
}

// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}