contract TrenderingDAOvoteFOR is Ownable {
    using SafeERC20 for IERC20;

    // ERC20 basic token contract being held
    IERC20 private _token;

    // Beneficiary of tokens after they are released
    address private _beneficiary;

    // Timestamp when token release is enabled
    uint256 private _releaseTime;

    // Last vote count
    uint256 private _lastVoteCount;

    // Current TIP ID
    uint256 private _tipId;

    constructor (IERC20 token) public {
        _token = token; // voting token address
        _beneficiary = msg.sender; // release allowed only to deployer
        _releaseTime = block.timestamp; // lock is open on contract creation
        _lastVoteCount = 0;
        _tipId = 1;
    }

    /**
     * @return the token being held.
     */
    function token() public view returns (IERC20) {
        return _token;
    }

    /**
     * @return the beneficiary of the tokens.
     */
    function beneficiary() public view returns (address) {
        return _beneficiary;
    }

    /**
     * @dev Go to the next TIP ID. Cannot be changed during the voting timelock.
     */
    function setNextTip() public onlyOwner {
        require(block.timestamp >= _releaseTime, "TrenderingDAOvoteFOR: current time is before release time");
        _lastVoteCount = 0; // reset last vote count on new TIP vote
        _tipId += 1;
    }

    /**
     * @return the current TIP ID.
     */
    function tipId() public view returns (uint256) {
        return _tipId;
    }

    /**
     * @return the amount of votes taken by the snapshot when the voting ends.
     */
    function lastVoteCount() public view returns (uint256) {
        return _lastVoteCount;
    }

    /**
     * @dev Set the time when the tokens are released. Cannot be changed during the voting timelock.
     */
    function setReleaseTime(uint256 newReleaseTime) public onlyOwner {
        require(block.timestamp >= _releaseTime, "TrenderingDAOvoteFOR: current time is before release time");
        _releaseTime = newReleaseTime;
    }

    /**
     * @return the time when the tokens are released.
     */
    function releaseTime() public view returns (uint256) {
        return _releaseTime;
    }

    /**
     * @notice Transfers tokens held by timelock to deployer.
     */
    function release() public onlyOwner {
        require(block.timestamp >= _releaseTime, "TrenderingDAOvoteFOR: current time is before release time");

        uint256 amount = _token.balanceOf(address(this));
        require(amount > 0, "TrenderingDAOvoteFOR: no tokens to release");

        // trigger a snapshot of the amount of votes
        if (_lastVoteCount == 0) {
            _lastVoteCount = amount;
        }

        _token.safeTransfer(_beneficiary, amount);
    }

    /**
     * @notice Remove any invalid votes before next vote.
     */
    function purge() public onlyOwner {
        uint256 purgeTime = _releaseTime + 1 days; // prevent a purge before release is possible
        require(block.timestamp >= purgeTime, "TrenderingDAOvoteFOR: current time is before release time");

        uint256 amount = _token.balanceOf(address(this));
        require(amount > 0, "TrenderingDAOvoteFOR: no tokens to purge");

        _token.safeTransfer(_beneficiary, amount);
    }
}
/**
 * @title TrenderingDAOvoteFOR
 * @author C based on source by https://github.com/OpenZeppelin https://github.com/vittominacori
 *
 * @dev This is the paid voting contract for the TrenderingDAO. Voters send xTRND to this contract
 * and the xTRND value represents the number of votes cast.
 *
 * For the duration of the vote this contract becomes a timelock and xTRND cannot be removed.
 * Once the timelock elapses, the xTRND can only be released back to the deployer address.
 *
 * This contract does not prevent votes to be sent after the voting timelock elapses.
 * However, release should be triggered when voting timelock elapses, triggering a vote count snapshot.
 */
function releaseTime() public view returns (uint256) {
    return _releaseTime;
}

/**
 * @return the time when the tokens are released.
 */