// File: contracts/SwapCore/PYESwapFactory.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { IPYESwapFactory } from "./interfaces/IPYESwapFactory.sol";
import { IPYESwapPair } from "./interfaces/IPYESwapPair.sol";
import { PYESwapPair, IToken } from "./PYESwapPair.sol";

contract PYESwapFactory is IPYESwapFactory, ReentrancyGuard {
    bytes32 public constant INIT_CODE_PAIR_HASH = keccak256(abi.encodePacked(type(PYESwapPair).creationCode));

    address public override feeTo;
    address public override feeToSetter;
    address public override routerAddress;
    bool public routerInit;

    mapping(address => mapping(address => address)) public override getPair;
    address[] public override allPairs;
    mapping (address => bool) public override pairExist;

    event PairCreated(address indexed token0, address indexed token1, address pair, uint);
    event FeeToUpdate(address feeTo);
    event FeeToSetterUpdate(address feeToSetter);

    constructor(address _fee) {
        feeToSetter = _fee;
    }

    function routerInitialize(address _router) external override {
        require(msg.sender == feeToSetter, "PYESwap: FORBIDDEN");
        require(!routerInit, "PYESwap: INITIALIZED_ALREADY");
        require(_router != address(0), "PYESwap: INVALID_ADDRESS");
        routerAddress = _router;
        routerInit = true;
    }

    function createPair(
        address tokenA, 
        address tokenB, 
        bool supportsTokenFee, 
        address feeTaker
    ) external override nonReentrant returns (address pair) {
        require(tokenA != tokenB, "PYESwap: IDENTICAL_ADDRESSES");
        (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
        require(token0 != address(0), "PYESwap: ZERO_ADDRESS");
        require(getPair[token0][token1] == address(0), "PYESwap: PAIR_EXISTS"); // single check is sufficient
        bytes memory bytecode = type(PYESwapPair).creationCode;
        bytes32 salt = keccak256(abi.encodePacked(token0, token1));
        assembly {
            pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        IPYESwapPair(pair).initialize(token0, token1, supportsTokenFee, tokenB == token0);
        getPair[token0][token1] = pair;
        getPair[token1][token0] = pair; // populate mapping in the reverse direction
        allPairs.push(pair);
        if(supportsTokenFee) {
            IPYESwapPair(pair).setBaseToken(tokenB);
            IPYESwapPair(pair).setFeeTaker(feeTaker);

            uint32 size;
            assembly {
                size := extcodesize(tokenA)
            }
            if(size > 0) {
                IToken(tokenA).addPair(pair, tokenB);
            }
        }
        pairExist[pair] = true;
        emit PairCreated(token0, token1, pair, allPairs.length);
    }

    function setFeeTo(address _feeTo) external {
        require(msg.sender == feeToSetter, "PYESwap: FORBIDDEN");
        require(_feeTo != address(0), "PYESwap: INVALID_ADDRESS");
        feeTo = _feeTo;
        emit FeeToUpdate(feeTo);
    }

    function setFeeToSetter(address _feeToSetter) external {
        require(msg.sender == feeToSetter, "PYESwap: FORBIDDEN");
        require(_feeToSetter != address(0), "PYESwap: INVALID_ADDRESS");
        feeToSetter = _feeToSetter;
        emit FeeToSetterUpdate(feeToSetter);
    }

    function allPairsLength() external override view returns (uint) {
        return allPairs.length;
    }
}

// File: contracts/SwapCore/PYESwapPair.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import { ReentrancyGuard } from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import { IPYESwapPair } from "./interfaces/IPYESwapPair.sol";
import { IPYESwapRouter } from "./interfaces/IPYESwapRouter.sol";
import { PYESwapERC20 } from "./PYESwapERC20.sol";
import { Math } from "./libraries/Math.sol";
import { UQ112x112 } from "./libraries/UQ112x112.sol";
import { IERC20 } from "./interfaces/IERC20.sol";
import { IPYESwapFactory } from "./interfaces/IPYESwapFactory.sol";
import { IPYESwapCallee } from "./interfaces/IPYESwapCallee.sol";
import { IToken } from "./interfaces/IToken.sol";

contract PYESwapPair is IPYESwapPair, PYESwapERC20, ReentrancyGuard {
    using UQ112x112 for uint224;

    uint public constant override MINIMUM_LIQUIDITY = 10**3;
    bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)")));

    IPYESwapRouter public routerAddress;

    address public immutable override factory;
    address public override token0;
    address public override token1;
    address public override baseToken;
    address public override feeTaker;
    uint8 private token0Fee;
    uint8 private token1Fee;
    bool public supportsTokenFee;
    bool public pairInit;

    uint112 private reserve0;           // uses single storage slot, accessible via getReserves
    uint112 private reserve1;           // uses single storage slot, accessible via getReserves
    uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves

    uint public override price0CumulativeLast;
    uint public override price1CumulativeLast;
    uint public override kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event

    uint private unlocked = 1;

    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);
    event Initialize(address token0, address token1, IPYESwapRouter router, address caller);
    event BaseTokenSet(address baseToken, address caller);
    event FeeTakerSet(address feeTaker, address caller);

    modifier lock() {
        require(msg.sender == address(routerAddress), "only router accessible");
        require(unlocked == 1, "PYESwap: LOCKED");
        unlocked = 0;
        _;
        unlocked = 1;
    }

    constructor() {
        factory = msg.sender;
    }

    // called once by the factory at time of deployment
    function initialize(address _token0, address _token1, bool _supportsTokenFee, bool _token0IsFee) external override {
        require(msg.sender == factory, "PYESwap: FORBIDDEN"); // sufficient check
        require(!pairInit, "PYESwap: INITIALIZED_ALREADY");
        require(_token0 != address(0) && _token1 != address(0), "PYESwap: INVALID_ADDRESS");
        token0 = _token0;
        token1 = _token1;
        supportsTokenFee = _supportsTokenFee;
        if (_token0IsFee) {
            token0Fee = 17;
        } else {
            token1Fee = 17;
        }
        routerAddress = IPYESwapRouter(IPYESwapFactory(factory).routerAddress());
        pairInit = true;
        emit Initialize(token0, token1, routerAddress, msg.sender);
    }

    // this low-level function should be called from a contract which performs important safety checks
    function mint(address to) external override lock returns (uint liquidity) {
        (uint112 _reserve0, uint112 _reserve1,,) = getReserves(); // gas savings
        uint balance0 = IERC20(token0).balanceOf(address(this));
        uint balance1 = IERC20(token1).balanceOf(address(this));
        uint amount0 = balance0 - _reserve0;
        uint amount1 = balance1 - _reserve1;

        bool feeOn = _mintFee(_reserve0, _reserve1);
        uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
        if (_totalSupply == 0) {
            liquidity = Math.sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY;
            _mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
        } else {
            liquidity = Math.min(
                (amount0 * _totalSupply) / _reserve0, 
                (amount1 * _totalSupply) / _reserve1
            );
        }
        require(liquidity > 0, "PYESwap: INSUFFICIENT_LIQUIDITY_MINTED");
        _mint(to, liquidity);

        _update(balance0, balance1, _reserve0, _reserve1);
        if (feeOn) kLast = uint(reserve0) * reserve1; // reserve0 and reserve1 are up-to-date
        emit Mint(msg.sender, amount0, amount1);
    }

    // this low-level function should be called from a contract which performs important safety checks
    function burn(address to) external override lock returns (uint amount0, uint amount1) {
        require(totalSupply != 0, "PYESwap: totalSupply must not be 0");
        (uint112 _reserve0, uint112 _reserve1,,) = getReserves(); // gas savings
        address _token0 = token0;                                // gas savings
        address _token1 = token1;                                // gas savings
        uint balance0 = IERC20(_token0).balanceOf(address(this));
        uint balance1 = IERC20(_token1).balanceOf(address(this));
        uint liquidity = balanceOf[address(this)];

        bool feeOn = _mintFee(_reserve0, _reserve1);
        uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
        amount0 = (liquidity * balance0) / _totalSupply; // using balances ensures pro-rata distribution
        amount1 = (liquidity * balance1) / _totalSupply; // using balances ensures pro-rata distribution
        require(amount0 > 0 && amount1 > 0, "PYESwap: INSUFFICIENT_LIQUIDITY_BURNED");
        _burn(address(this), liquidity);
        _safeTransfer(_token0, to, amount0, false);
        _safeTransfer(_token1, to, amount1, false);
        balance0 = IERC20(_token0).balanceOf(address(this));
        balance1 = IERC20(_token1).balanceOf(address(this));

        _update(balance0, balance1, _reserve0, _reserve1);
        if (feeOn) kLast = uint(reserve0) * reserve1; // reserve0 and reserve1 are up-to-date
        emit Burn(msg.sender, amount0, amount1, to);
    }

    // this low-level function should be called from a contract which performs important safety checks
    function swap(
        uint amount0Out, 
        uint amount1Out, 
        uint amount0Fee, 
        uint amount1Fee, 
        address to, 
        bytes calldata data
    ) external override lock {
        require(amount0Out > 0 || amount1Out > 0, "PYESwap: INSUFFICIENT_OUTPUT_AMOUNT");
        (uint112 _reserve0, uint112 _reserve1,,) = getReserves(); // gas savings
        require(amount0Out < _reserve0 && amount1Out < _reserve1, "PYESwap: INSUFFICIENT_LIQUIDITY");

        uint balance0;
        uint balance1;
        { // scope for _token{0,1}, avoids stack too deep errors
            address _token0 = token0;
            address _token1 = token1;
            require(to != _token0 && to != _token1, "PYESwap: INVALID_TO");
            if (amount0Out > 0) {
                _safeTransfer(_token0, to, amount0Out, true);
            }
            if (amount1Out > 0) {
                _safeTransfer(_token1, to, amount1Out, true);
            }

            if(amount0Fee > 0 && baseToken == token0) {
                bool success0 = IERC20(_token0).approve(_token1, amount0Fee);
                require(success0, "Swap2.0: Approval Failed");
                IToken(_token1).handleFee(amount0Fee, _token0);
            }
            if(amount1Fee > 0 && baseToken == token1) {
                bool success1 = IERC20(_token1).approve(_token0, amount1Fee);
                require(success1, "Swap2.0: Approval Failed");
                IToken(_token0).handleFee(amount1Fee, _token1);
            }

            if (data.length > 0) IPYESwapCallee(to).pyeSwapCall(msg.sender, amount0Out, amount1Out, data);
            balance0 = IERC20(_token0).balanceOf(address(this));
            balance1 = IERC20(_token1).balanceOf(address(this));
        }
        
        uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
        uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
        require(amount0In > 0 || amount1In > 0, "PYESwap: INSUFFICIENT_INPUT_AMOUNT");

        { // scope for reserve{0,1}Adjusted, avoids stack too deep errors
            uint balance0Adjusted = (balance0 * 10000) - (amount0In * token0Fee);
            uint balance1Adjusted = (balance1 * 10000) - (amount1In * token1Fee);
            require(
                (balance0Adjusted * balance1Adjusted) >= uint(_reserve0) * _reserve1 * 10000**2, 
                "PYESwap: K"
            );
        }

        _update(balance0, balance1, _reserve0, _reserve1);

        {
            uint _amount0Out = amount0Out > 0 ? amount0Out + amount0Fee : 0;
            uint _amount1Out = amount1Out > 0 ? amount1Out + amount1Fee : 0;
            address _to = to;
            emit Swap(msg.sender, amount0In, amount1In, _amount0Out, _amount1Out, _to);
        }
    }

    // force balances to match reserves
    function skim(address to) external override lock {
        address _token0 = token0; // gas savings
        address _token1 = token1; // gas savings
        _safeTransfer(_token0, to, IERC20(_token0).balanceOf(address(this)) - reserve0, false);
        _safeTransfer(_token1, to, IERC20(_token1).balanceOf(address(this)) - reserve1, false);
    }

    // force reserves to match balances
    function sync() external override lock {
        _update(
            IERC20(token0).balanceOf(address(this)), 
            IERC20(token1).balanceOf(address(this)), 
            reserve0, 
            reserve1
        );
    }

    function setBaseToken(address _baseToken) external override {
        require(msg.sender == factory, "PYESwap: NOT_FACTORY");
        require(_baseToken == token0 || _baseToken == token1, "PYESwap: WRONG_ADDRESS");

        baseToken = _baseToken;
        emit BaseTokenSet(baseToken, msg.sender);
    }

    function setFeeTaker(address _feeTaker) external override {
        require(msg.sender == factory, "PYESwap: NOT_FACTORY");
        require(_feeTaker == token0 || _feeTaker == token1, "PYESwap: WRONG_ADDRESS");

        feeTaker = _feeTaker;
        emit FeeTakerSet(feeTaker, msg.sender);
    }

    function getReserves() public view override returns (
        uint112 _reserve0, 
        uint112 _reserve1, 
        uint32 _blockTimestampLast, 
        address _baseToken
    ) {
        _reserve0 = reserve0;
        _reserve1 = reserve1;
        _blockTimestampLast = blockTimestampLast;
        _baseToken = baseToken;
    }

    function _safeTransfer(address token, address to, uint value, bool isSwapping) private nonReentrant {
        if(value == 0){
            return;
        }
        if (routerAddress.pairFeeAddress(address(this)) == token && isSwapping){
            uint256 adminFee = routerAddress.adminFee();
            if(adminFee != 0){
                uint256 getOutFee = (value * adminFee) / 10000;
                (bool success, bytes memory data) = token.call(
                    abi.encodeWithSelector(SELECTOR, routerAddress.adminFeeAddress(), getOutFee)
                );
                require(success && (data.length == 0 || abi.decode(data, (bool))), "PYESwap: TRANSFER_FAILED");
                value -= getOutFee;
            }
            (bool success1, bytes memory data1) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
            require(success1 && (data1.length == 0 || abi.decode(data1, (bool))), "PYESwap: TRANSFER_FAILED");
        }else {
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), "PYESwap: TRANSFER_FAILED");
        }
    }

    // update reserves and, on the first call per block, price accumulators
    function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
        require(balance0 <= type(uint112).max && balance1 <= type(uint112).max, "PYESwap: OVERFLOW");
        uint32 blockTimestamp = uint32(block.timestamp % 2**32);
        uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
        if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
            // * never overflows, and + overflow is desired
            price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
            price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
        }
        reserve0 = uint112(balance0);
        reserve1 = uint112(balance1);
        blockTimestampLast = blockTimestamp;
        emit Sync(reserve0, reserve1);
    }

    // if fee is on, mint liquidity equivalent to 1/6th of the growth in sqrt(k)
    function _mintFee(uint112 _reserve0, uint112 _reserve1) private returns (bool feeOn) {
        address feeTo = IPYESwapFactory(factory).feeTo();
        feeOn = feeTo != address(0);
        uint _kLast = kLast; // gas savings
        if (feeOn) {
            if (_kLast != 0) {
                uint rootK = Math.sqrt(uint(_reserve0) * _reserve1);
                uint rootKLast = Math.sqrt(_kLast);
                if (rootK > rootKLast) {
                    uint numerator = totalSupply * (rootK - rootKLast);
                    uint denominator = (rootK * 5) + rootKLast;
                    uint liquidity = numerator / denominator;
                    if (liquidity > 0) _mint(feeTo, liquidity);
                }
            }
        } else if (_kLast != 0) {
            kLast = 0;
        }
    }
}

// File: contracts/SwapCore/interfaces/IPYESwapPair.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IPYESwapPair {

    function mint(address to) external returns (uint liquidity);

    function burn(address to) external returns (uint amount0, uint amount1);

    function swap(
        uint amount0Out, 
        uint amount1Out, 
        uint amount0Fee, 
        uint amount1Fee, 
        address to, 
        bytes calldata data
    ) external;

    function skim(address to) external;

    function sync() external;

    function initialize(address, address, bool, bool) external;

    function setBaseToken(address _baseToken) external;

    function setFeeTaker(address _feeTaker) external;

    function baseToken() external view returns (address);

    function feeTaker() external view returns (address);

    function factory() external view returns (address);

    function token0() external view returns (address);

    function token1() external view returns (address);

    function getReserves() external view returns (
        uint112 reserve0, 
        uint112 reserve1, 
        uint32 blockTimestampLast, 
        address _baseToken
    );

    function price0CumulativeLast() external view returns (uint);

    function price1CumulativeLast() external view returns (uint);

    function kLast() external view returns (uint);

    function MINIMUM_LIQUIDITY() external pure returns (uint);

}

// File: contracts/SwapCore/interfaces/IPYESwapFactory.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IPYESwapFactory {

    function createPair(
        address tokenA, 
        address tokenB, 
        bool supportsTokenFee, 
        address feeTaker
    ) external returns (
        address pair
    );

    function routerInitialize(address) external;

    function feeTo() external view returns (address);

    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);

    function allPairs(uint) external view returns (address pair);

    function allPairsLength() external view returns (uint);

    function pairExist(address pair) external view returns (bool);

    function routerAddress() external view returns (address);
    
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be _NOT_ENTERED
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

// File: contracts/SwapCore/interfaces/IToken.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IToken {
    function addPair(address pair, address token) external;
    function handleFee(uint amount, address token) external;
}

// File: contracts/SwapCore/interfaces/IPYESwapCallee.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IPYESwapCallee {
    function pyeSwapCall(address sender, uint amount0, uint amount1, bytes calldata data) external;
}

// File: contracts/SwapCore/interfaces/IERC20.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IERC20 {

    event Approval(address indexed owner, address indexed spender, uint value);

    event Transfer(address indexed from, address indexed to, uint value);

    function approve(address spender, uint value) external returns (bool);

    function transfer(address to, uint value) external returns (bool);

    function transferFrom(address from, address to, uint value) external returns (bool);

    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);

    function totalSupply() external view returns (uint);

    function balanceOf(address owner) external view returns (uint);

    function allowance(address owner, address spender) external view returns (uint);
    
}
// File: contracts/SwapCore/libraries/UQ112x112.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))

// range: [0, 2**112 - 1]
// resolution: 1 / 2**112

library UQ112x112 {
    uint224 constant private Q112 = 2**112;

    // encode a uint112 as a UQ112x112
    function encode(uint112 y) internal pure returns (uint224 z) {
        z = uint224(y) * Q112; // never overflows
    }

    // divide a UQ112x112 by a uint112, returning a UQ112x112
    function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
        z = x / uint224(y);
    }
}

// File: contracts/SwapCore/libraries/Math.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

// a library for performing various math operations

library Math {
    function min(uint x, uint y) internal pure returns (uint z) {
        z = x < y ? x : y;
    }

    // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
    function sqrt(uint y) internal pure returns (uint z) {
        if (y > 3) {
            z = y;
            uint x = y / 2 + 1;
            while (x < z) {
                z = x;
                x = (y / x + x) / 2;
            }
        } else if (y != 0) {
            z = 1;
        }
    }
}

// File: contracts/SwapCore/PYESwapERC20.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import { IPYESwapERC20 } from "./interfaces/IPYESwapERC20.sol";
import { ECDSA } from "./libraries/ECDSA.sol";

contract PYESwapERC20 is IPYESwapERC20 {

    string public constant override name = "PYESwap-LP";
    string public constant override symbol = "PYE-LP";
    uint8 public constant override decimals = 18;
    uint public override totalSupply;
    mapping(address => uint) public override balanceOf;
    mapping(address => mapping(address => uint)) public override allowance;

    bytes32 public immutable override DOMAIN_SEPARATOR;
    bytes32 public constant override PERMIT_TYPEHASH = 
        0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
    mapping(address => uint) public override nonces;

    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    constructor() {
        uint chainId = block.chainid;
        DOMAIN_SEPARATOR = keccak256(
            abi.encode(
                keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                keccak256(bytes(name)),
                keccak256(bytes("1")),
                chainId,
                address(this)
            )
        );
    }

    function approve(address spender, uint value) external virtual override returns (bool) {
        _approve(msg.sender, spender, value);
        return true;
    }

    function transfer(address to, uint value) external override returns (bool) {
        _transfer(msg.sender, to, value);
        return true;
    }

    function transferFrom(address from, address to, uint value) external override returns (bool) {
        if (allowance[from][msg.sender] != type(uint).max - 1) {
            allowance[from][msg.sender] = allowance[from][msg.sender] - value;
        }
        _transfer(from, to, value);
        return true;
    }

    function permit(
        address owner, 
        address spender, 
        uint value, 
        uint deadline, 
        uint8 v, 
        bytes32 r, 
        bytes32 s
    ) external override {
        require(deadline >= block.timestamp, "PYESwap: EXPIRED");
        bytes32 digest = keccak256(
            abi.encodePacked(
                "\x19\x01",
                DOMAIN_SEPARATOR,
                keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
            )
        );
        address recoveredAddress = ECDSA.recover(digest, v, r, s);
        require(recoveredAddress != address(0) && recoveredAddress == owner, "PYESwap: INVALID_SIGNATURE");
        _approve(owner, spender, value);
    }

    function _mint(address to, uint value) internal {
        totalSupply += value;
        balanceOf[to] += value;
        emit Transfer(address(0), to, value);
    }

    function _burn(address from, uint value) internal {
        balanceOf[from] -= value;
        totalSupply -= value;
        emit Transfer(from, address(0), value);
    }

    function _approve(address owner, address spender, uint value) private {
        allowance[owner][spender] = value;
        emit Approval(owner, spender, value);
    }

    function _transfer(address from, address to, uint value) private {
        balanceOf[from] -= value;
        balanceOf[to] += value;
        emit Transfer(from, to, value);
    }
}

// File: contracts/SwapCore/interfaces/IPYESwapRouter.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IPYESwapRouter {
    function pairFeeAddress(address pair) external view returns (address);
    function adminFee() external view returns (uint256);
    function swapFee() external view returns (uint256);
    function feeAddressGet() external view returns (address);
    function adminFeeAddress() external view returns (address);
    function owner() external view returns (address);
}

// File: contracts/SwapCore/libraries/ECDSA.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;


/**
 * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
 *
 * These functions can be used to verify that a message was signed by the holder
 * of the private keys of a given address.
 */
library ECDSA {
    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature`. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {toEthSignedMessageHash} on it.
     */
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        // Check the signature length
        if (signature.length != 65) {
            revert("ECDSA: invalid signature length");
        }

        // Divide the signature in r, s and v variables
        bytes32 r;
        bytes32 s;
        uint8 v;

        // ecrecover takes the signature parameters, and the only way to get them
        // currently is to use assembly.
        // solhint-disable-next-line no-inline-assembly
        assembly {
            r := mload(add(signature, 0x20))
            s := mload(add(signature, 0x40))
            v := byte(0, mload(add(signature, 0x60)))
        }

        return recover(hash, v, r, s);
    }

    /**
     * @dev Overload of {ECDSA-recover-bytes32-bytes-} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
        // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
        // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
        // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
        // signatures from current libraries generate a unique signature with an s-value in the lower half order.
        //
        // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
        // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
        // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
        // these malleable signatures as well.
        require(uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, 
            "ECDSA: invalid signature 's' value");
        require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");

        // If the signature is valid (and not malleable), return the signer address
        address signer = ecrecover(hash, v, r, s);
        require(signer != address(0), "ECDSA: invalid signature");

        return signer;
    }

    /**
     * @dev Returns an Ethereum Signed Message, created from a `hash`. This
     * replicates the behavior of the
     * https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign[`eth_sign`]
     * JSON-RPC method.
     *
     * See {recover}.
     */
    function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
        // 32 is the length in bytes of hash,
        // enforced by the type signature above
        return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
    }
}
// File: contracts/SwapCore/interfaces/IPYESwapERC20.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

interface IPYESwapERC20 {

    function approve(address spender, uint value) external returns (bool);

    function transfer(address to, uint value) external returns (bool);

    function transferFrom(address from, address to, uint value) external returns (bool);
    
    function permit(
        address owner, 
        address spender, 
        uint value, 
        uint deadline, 
        uint8 v, 
        bytes32 r, 
        bytes32 s
    ) external;

    function totalSupply() external view returns (uint);

    function balanceOf(address owner) external view returns (uint);

    function allowance(address owner, address spender) external view returns (uint);

    function DOMAIN_SEPARATOR() external view returns (bytes32);

    function nonces(address owner) external view returns (uint);

    function name() external pure returns (string memory);

    function symbol() external pure returns (string memory);

    function decimals() external pure returns (uint8);

    function PERMIT_TYPEHASH() external pure returns (bytes32);

}