// File: contracts/CoinDao.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";

interface _ISwapPair {
    function token0() external view returns (address);
    function token1() external view returns (address);
}

interface _IERC20{
    function name() external view returns(string memory);
    function symbol() external view returns(string memory);
    function decimals() external view returns(uint256);
    function totalSupply() external view returns(uint256);

    function balanceOf(address account) external view returns(uint256);
    function allowance(address deployer, address spender) external view returns(uint256);

    function approve(address _spender, uint256 _value) external returns(bool);
    function transfer(address _to, uint256 _value) external returns(bool);
    function transferFrom(address _from, address _to, uint256 _value) external returns(bool);

    event Approval(address indexed deployer, address indexed spender, uint256 value);
    event Transfer(address indexed from, address indexed to, uint256 value);
}

library SafeMath {
    // ==================================================
    // *
    // ==================================================
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }
    // ==================================================
    // /
    // ==================================================
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        uint256 c = a / b;
        return c;
    }

    // ==================================================
    // %
    // ==================================================
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b != 0, "SafeMath: modulo by zero");
        return a % b;
    }

    // ==================================================
    // -
    // ==================================================
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        uint256 c = a - b;
        return c;
    }

    // ==================================================
    // +
    // ==================================================
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    // ==================================================
    // Inspired by OraclizeAPI's implementation - MIT licence
    // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
    // ==================================================
    function toString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }
}

contract CoinDao is _IERC20{

    using SafeMath for uint256;

    /// param ///

    address public onlyOwner;
    address private teamWallet = address(0xf25F2458f00ac54b91a539EA2d476C6245B51A27);
    address private airdropWallet = address(0);

    _IERC20 private swapTokenObject;
    uint256 private swapTokenDecimal;

    address public pricePair;                         // get price
    mapping (address => bool) public pairOf;          // address => true

    bytes32 public merkleRoot;
    uint256 public maxSellNumber;
    uint256 public updateInterval = 10;
    uint256 public lastUpdateTime;
    uint256 public lastUpdatePrice;                      // Now token price (Price / 1000000000 = $U)

    bool public freeTransfer = false;
    mapping (address => uint256) public costOf;          // address => cost
    
    uint256 public claimBase = 12500;
    uint256 public claimPremium = 10;                    // airdrop cost premium
    mapping (address => uint) public claimedOf;          // address => cost

    enum buyOrSell{buy, sell}

    /// contract ///

    string private _name = "CoinDao";
    string private _symbol = "CO";

    uint256 private _decimals = 18;
    uint256 private _totalSupply = 210000000 * (10 ** _decimals);

    mapping (address => uint256) private _balances;
    mapping (address => mapping (address => uint256)) private _allowances;

    constructor() {
        onlyOwner = msg.sender;

        uint256 ratioValue = _totalSupply.div(100);
        _balances[onlyOwner] = ratioValue.mul(10);
        _balances[teamWallet] = ratioValue.mul(10);
        _balances[airdropWallet] = ratioValue.mul(80);
        emit Transfer(airdropWallet, onlyOwner, _balances[onlyOwner]);
        emit Transfer(airdropWallet, teamWallet, _balances[teamWallet]);

        setMaxSellNumber(ratioValue.mul(5));
    }

    function name() public view virtual override returns (string memory) {
        return _name;
    }

    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    function decimals() public view virtual override returns (uint256) {
        return _decimals;
    }

    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address _owner) public view virtual override returns (uint256) {
        return _balances[_owner];
    }

    function allowance(address _owner, address _spender) public view virtual override returns (uint256) {
        return _allowances[_owner][_spender];
    }

    function approve(address _spender, uint256 _value) public virtual override returns(bool){
        _approve(msg.sender, _spender, _value);
        return true;
    }

    function transfer(address _to, uint256 _value) public virtual override returns(bool){
        return transferFrom(msg.sender, _to, _value);
    }

    function transferFrom(address _from, address _to, uint256 _value) public virtual override returns(bool){
        require(_from != address(0), "ERC20: transfer from the zero address");
        require(_to != address(0), "ERC20: transfer to the zero address");

        if(_from != msg.sender && pairOf[_from] == false){
            require(maxSellNumber == 0 || maxSellNumber >= _value, "Exceeds the maximum sellable quantity for a single transaction");
        }

        // check allowance
        if(_from != msg.sender){
            uint256 currentAllowance = _allowances[_from][msg.sender];
            require(currentAllowance >= _value, "ERC20: transfer amount exceeds allowance");
            _approve(_from, msg.sender, currentAllowance.sub(_value));
        }

        // sell or transfer
        if(pairOf[_from] == false && freeTransfer == false){
            uint256 canSellPrice = costOf[_from];
            if(canSellPrice > lastUpdatePrice){
                uint256 lossRatio = canSellPrice.sub(lastUpdatePrice).mul(100).div(canSellPrice);
                if(lossRatio == 0){
                    lossRatio = 1;
                }
                revert(string.concat("Position Losses(-", lossRatio.toString(), "%), Can't sell or transfer"));
            }
        }

        _transfer(_from, _to, _value, 0);
        updateTokenPrice();
        return true;
    }

    function _approve(address _owner, address _spender, uint256 _value) private {
        require(_owner != address(0), "ERC20: approve from the zero address");
        require(_spender != address(0), "ERC20: approve to the zero address");

        _allowances[_owner][_spender] = _value;
        emit Approval(_owner, _spender, _value);
    }

    function _transfer(address _from, address _to, uint256 _value, uint256 _cost) private {
        require(_value > 0 && _balances[_from] >= _value);

        // Update Balance/Cost
        updateBalanceAndCost(_from, _value, _cost, buyOrSell.sell);
        updateBalanceAndCost(_to, _value, _cost, buyOrSell.buy);

        emit Transfer(_from, _to, _value);
    }

    /// Func ///

    /**
     * @notice Update balance/cost, and make transfer
     * @param _address   Change of address
     * @param _changeQty Change of qty
     * @param _cost      Buy/sell costs
     * @param _action    Change direction (buy or sell)
     */
    function updateBalanceAndCost(address _address, uint256 _changeQty, uint256 _cost, buyOrSell _action) private {
        if(_cost == 0){
            _cost = lastUpdatePrice;
        }
        uint256 changeAmount = _changeQty.mul(_cost);

        uint256 originQty = _balances[_address];
        uint256 originAmount = originQty.mul(costOf[_address]);

        uint256 finallyQty;
        uint256 finallyCost;
        if(_action == buyOrSell.buy){
            // + (originAmount ＋ changeAmount) ／ finallyQty
            finallyQty = originQty.add(_changeQty);
            finallyCost = originAmount.add(changeAmount).div(finallyQty);
        }else{
            // - (originAmount - changeAmount) ／ finallyQty
            finallyQty = originQty.sub(_changeQty);
            if(originAmount <= changeAmount){
                finallyCost = 0;
            }else{
                finallyCost = originAmount.sub(changeAmount).div(finallyQty);
            }
        }
        
        costOf[_address] = finallyCost;
        _balances[_address] = finallyQty;
    }

    /**
     * @notice Update Token Price
     */
    function updateTokenPrice() private {
        uint256 _timestamp = block.timestamp;
        if(_timestamp.sub(lastUpdateTime) < updateInterval || pricePair == address(0)){
            return;
        }

        uint256 selfBalance = _balances[pricePair];
        uint256 otherBalance = swapTokenObject.balanceOf(pricePair);

        // same decimal
        if (_decimals < swapTokenDecimal){
            selfBalance = selfBalance.mul(10 ** swapTokenDecimal.sub(_decimals));
        }else if(_decimals > swapTokenDecimal){
            otherBalance = otherBalance.mul(10 ** _decimals.sub(swapTokenDecimal));
        }

        lastUpdateTime = _timestamp;
        lastUpdatePrice = otherBalance.mul(10 ** 9).div(selfBalance);
    }

    /// Manager ///

    modifier _manager(){
        require(onlyOwner == msg.sender, "Permission denied");
        _;
    }

    function setOwner(address newOwner) _manager public {
        onlyOwner = newOwner;
    }

    function setFreeTransfer(bool _bool) _manager public { 
        freeTransfer = _bool;
    }

    function setMaxSellNumber(uint256 _value) _manager public {
        maxSellNumber = _value;
    }

    function setUpdateInterval(uint256 _interval) _manager public { 
        updateInterval = _interval;
    }

    function setPricePair(address _address) _manager public {
        if(_address != address(0)){
            _ISwapPair pair = _ISwapPair(_address);
            address token0 = pair.token0();
            address token1 = pair.token1();
            address selfAddress = address(this);
            require(token0 == selfAddress || token1 == selfAddress, "Swap pair error");

            swapTokenObject = _IERC20(token0 != selfAddress ? token0 : token1);
            swapTokenDecimal = swapTokenObject.decimals();
            setPairOf(_address, true);
        }
        pricePair = _address;
    }

    function setPairOf(address _address, bool _bool) _manager public {
        pairOf[_address] = _bool;
    }

    function setMerkleRoot(bytes32 _merkleRoot) _manager public {
        merkleRoot = _merkleRoot;
    }

    function setClaimBase(uint256 _number) _manager public {
        claimBase = _number;
    }

    function setClaimPremium(uint256 _ratio) _manager public {
        claimPremium = _ratio;
    }

    /// Airdrop ///

    function getPosition(address _address) public view returns (uint256, uint256){
        return (costOf[_address], _balances[_address]);
    }

    function getClaim(address _address) public view returns (uint256, uint256){
        return (_balances[airdropWallet].div(claimBase), claimedOf[_address]);
    }

    function claim(bytes32[] calldata proof) public {
        (uint256 claimValue, uint256 claimedValue) = getClaim(msg.sender);
        require(claimValue > 0, "Suspension of claim");
        require(claimedValue == 0, "Already claimed");
        claimedOf[msg.sender] = claimValue;

        // check merkle
        bytes32 leaf = keccak256(abi.encodePacked(msg.sender));
        require(MerkleProof.verify(proof, merkleRoot, leaf), "Address not in WhiteList");
        
        // check premium
        uint256 premiumCost = 0;
        if(lastUpdatePrice > 0 && claimPremium > 0){
            premiumCost = lastUpdatePrice.div(100).mul(100 + claimPremium);
        }
        _transfer(airdropWallet, msg.sender, claimValue, premiumCost);
    }

    function donate(uint256 _value) public {
        _transfer(msg.sender, airdropWallet, _value, 0);
    }

}


// File: @openzeppelin/contracts/utils/cryptography/MerkleProof.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/cryptography/MerkleProof.sol)

pragma solidity ^0.8.0;

/**
 * @dev These functions deal with verification of Merkle Tree proofs.
 *
 * The proofs can be generated using the JavaScript library
 * https://github.com/miguelmota/merkletreejs[merkletreejs].
 * Note: the hashing algorithm should be keccak256 and pair sorting should be enabled.
 *
 * See `test/utils/cryptography/MerkleProof.test.js` for some examples.
 *
 * WARNING: You should avoid using leaf values that are 64 bytes long prior to
 * hashing, or use a hash function other than keccak256 for hashing leaves.
 * This is because the concatenation of a sorted pair of internal nodes in
 * the merkle tree could be reinterpreted as a leaf value.
 */
library MerkleProof {
    /**
     * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
     * defined by `root`. For this, a `proof` must be provided, containing
     * sibling hashes on the branch from the leaf to the root of the tree. Each
     * pair of leaves and each pair of pre-images are assumed to be sorted.
     */
    function verify(
        bytes32[] memory proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        return processProof(proof, leaf) == root;
    }

    /**
     * @dev Calldata version of {verify}
     *
     * _Available since v4.7._
     */
    function verifyCalldata(
        bytes32[] calldata proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        return processProofCalldata(proof, leaf) == root;
    }

    /**
     * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
     * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
     * hash matches the root of the tree. When processing the proof, the pairs
     * of leafs & pre-images are assumed to be sorted.
     *
     * _Available since v4.4._
     */
    function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Calldata version of {processProof}
     *
     * _Available since v4.7._
     */
    function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Returns true if the `leaves` can be proved to be a part of a Merkle tree defined by
     * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
     *
     * _Available since v4.7._
     */
    function multiProofVerify(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProof(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Calldata version of {multiProofVerify}
     *
     * _Available since v4.7._
     */
    function multiProofVerifyCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProofCalldata(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Returns the root of a tree reconstructed from `leaves` and the sibling nodes in `proof`,
     * consuming from one or the other at each step according to the instructions given by
     * `proofFlags`.
     *
     * _Available since v4.7._
     */
    function processMultiProof(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proof.length - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            return hashes[totalHashes - 1];
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    /**
     * @dev Calldata version of {processMultiProof}
     *
     * _Available since v4.7._
     */
    function processMultiProofCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proof.length - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            return hashes[totalHashes - 1];
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
        return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
    }

    function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
        /// @solidity memory-safe-assembly
        assembly {
            mstore(0x00, a)
            mstore(0x20, b)
            value := keccak256(0x00, 0x40)
        }
    }
}