// File: erc20.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import "./IERC20.sol";
import "./Ownable.sol";
import "./Whitelist.sol";



library SafeMath {

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        assert(c / a == b);
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // assert(b > 0); // Solidity automatically throws when dividing by 0
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold
        return c;       
    }       

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
    }
}

/**
 * @title ERC20 Basic.
 *
 * @dev ERC20 ODYA token.
 */
contract ODYA is IERC20, Ownable, Whitelist {
    
    using SafeMath for uint256;

    string public name = "ODYA Coin";
    string public symbol = "ODYA";
    uint8 public constant decimals = 18;

    uint256 _initialSupply = 1000000000;
    uint256 private _totalSupply = _initialSupply * (10 ** uint256(decimals));
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(address => uint256) private _lockedAmount;

    event Lock(address indexed owner, address account, uint256 amount);
    event Unlock(address indexed owner, address account, uint256 amount);

    constructor() {
        _balances[msg.sender] = _totalSupply;
    }

    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    function allowance(address owner, address spender) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @return The amount of tokens locked to `account`.
     */
    function lockedAmountOf(address account) public view returns (uint256) {
        return _lockedAmount[account];
    }

    function transfer(address to, uint256 amount) public override returns (bool) {
        _transfer(msg.sender, to, amount);
        return true;
    }

    /**
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     */
    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    // OPTIONAL
    function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
        _spendAllowance(from, msg.sender, amount);
        _transfer(from, to, amount);
        return true;
    }

    function _transfer(address from, address to, uint256 amount) internal {
        require(from != address(0), "ODYA: transfer from the zero address");
        require(to != address(0), "ODYA: transfer to the zero address");

        uint256 currentBalance = balanceOf(from);
        uint256 lockedAmount = lockedAmountOf(from);
        uint256 availableAmount;

        require(currentBalance >= lockedAmount);
        unchecked { availableAmount = currentBalance.sub(lockedAmount); }
        require(availableAmount >= amount, "ODYA: transfer amount exceeds balance");

        unchecked {
            _balances[from] = _balances[from].sub(amount);
            _balances[to] = _balances[to].add(amount);
            require(_balances[to] >= amount, "ODYA: overflow of the to's balance");
        }

        emit Transfer(from, to, amount);
    }

    function _approve(address owner, address spender, uint256 amount) internal {
        require(owner != address(0), "ODYA: approve owner the zero address");
        require(spender != address(0), "ODYA: approve spender the zero address");

        _allowances[owner][spender] = amount;

        emit Approval(owner, spender, amount);
    }

    /**
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     */
    function _spendAllowance(address owner, address spender, uint256 amount) internal {
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= amount, "ODYA: insufficient allowance");

        if (currentAllowance != type(uint256).max) {
            unchecked {
                _approve(owner, spender, currentAllowance.sub(amount));
            }
        }
    }

    function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
        uint256 currentAllowance = allowance(msg.sender, spender);
        unchecked {
            uint256 newAllowance = currentAllowance.add(addedValue);
            require(newAllowance >= currentAllowance, "ODYA: overflow of the allowance");

            _approve(msg.sender, spender, newAllowance);
        }

        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
        uint256 currentAllowance = allowance(msg.sender, spender);
        require(currentAllowance >= subtractedValue, "ODYA: decreased allowance below zero");

        unchecked {
            _approve(msg.sender, spender, currentAllowance.sub(subtractedValue));
        }

        return true;
    }

    function lock(address account, uint256 amount) public onlyWhitelisted returns (bool) {
        require(balanceOf(account) >= amount, "ODYA: Insufficient balance to lock");

        unchecked {
            _lockedAmount[account] = _lockedAmount[account].add(amount);
            require(_lockedAmount[account] >= amount, "ODYA: overflow of locked amount");

            emit Lock(msg.sender, account, amount);
        }

        return true;
    }

    function unlock(address account, uint256 amount) public onlyWhitelisted returns (bool) {
        require(_lockedAmount[account] >= amount, "ODYA: underflow of locked amount");

        unchecked {
            _lockedAmount[account] = _lockedAmount[account].sub(amount);

            emit Unlock(msg.sender, account, amount);
        }

        return true;
    }

    function transferWithLock(address to, uint256 amount) public onlyWhitelisted returns (bool) {
        _transfer(msg.sender, to, amount);
        lock(to, amount);

        return true;
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ODYA: burn from the zero address");

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ODYA: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance.sub(amount);
            // Overflow not possible: amount <= accountBalance <= totalSupply.
            _totalSupply = _totalSupply.sub(amount);
        }

        emit Transfer(account, address(0), amount);
    }

    function burn(uint256 amount) public returns (bool) {
        _burn(address(msg.sender), amount);
        
        return true;
    }
}
// File: IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.9;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    // OPTIONAL public state variable getter is automatically generated.
    // function name() public view returns (string);
    // function symbol() public view returns (uint8);
    // function decimals() public view returns (uint256);

    /**
     * @return The amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @param account ? caller msg.sender ?
     * @return The amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev This is zero by default.
     * This value changes when {approve} or {transferFrom} are called.
     *
     * @return The remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Emits a {Transfer} event.
     *
     * @return A boolean value indicating whether the operation succeeded.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Emits an {Approval} event.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * @return A boolean value indicating whether the operation succeeded.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Emits a {Transfer} event.
     *
     * @return A boolean value indicating whether the operation succeeded.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}.
     * `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File: Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)

pragma solidity ^0.8.9;

/**
 * @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 {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(msg.sender);
    }

    /**
     * @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() == msg.sender, "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: Whitelist.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

import "./Ownable.sol";

contract Whitelist is Ownable {
    mapping(address => bool) private whitelist;
    mapping(address => uint) private addressIdx;
    address[] private whiteArr;

    event WhitelistedAddressAdded(address addr);
    event WhitelistedAddressRemoved(address addr);

    constructor() {
        addAddressToWhitelist(msg.sender);
    }

    modifier onlyWhitelisted() {
        require(whitelist[msg.sender]);
        _;
    }

    function isWhitelist(address addr) public view returns (bool success) {
        return whitelist[addr];
    }

    function addAddressToWhitelist(address addr) onlyOwner public returns (bool success) {
        if (!whitelist[addr]) {
            whitelist[addr] = true;
            addressIdx[addr] = whiteArr.length;
            whiteArr.push(addr);

            emit WhitelistedAddressAdded(addr);
            success = true;
        }
    }

    function removeAddressFromWhitelist(address addr) onlyOwner public returns (bool success) {
        if (whitelist[addr]) {
            whitelist[addr] = false;
            uint deleteIdx = addressIdx[addr];
            address lastAddr = whiteArr[whiteArr.length - 1];
            addressIdx[lastAddr] = deleteIdx;
            delete addressIdx[addr];
            whiteArr[deleteIdx] = lastAddr;
            whiteArr.pop();

            emit WhitelistedAddressRemoved(addr);
            success = true;
        }
    }

    function getCountWhiteArr() public onlyOwner view returns (uint count) {
        count = whiteArr.length;
    }

    function getWhiteArrByIdx(uint idx) public onlyOwner view returns (address whiteAddress) {
        whiteAddress = whiteArr[idx];
    }

    function getWhiteArr() public onlyOwner view returns (address[] memory whiteArray) {
        whiteArray = whiteArr;
    }
}