abstract contract ERC20Permit is ERC20, IERC2612Permit {
    using Counters for Counters.Counter;

    mapping(address => Counters.Counter) private _nonces;

    // Mapping of ChainID to domain separators. This is a very gas efficient way
    // to not recalculate the domain separator on every call, while still
    // automatically detecting ChainID changes.
    mapping(uint256 => bytes32) public domainSeparators;

    constructor() {
        _updateDomainSeparator();
    }

    /**
     * @dev See {IERC2612Permit-permit}.
     *
     * If https://eips.ethereum.org/EIPS/eip-1344[ChainID] ever changes, the
     * EIP712 Domain Separator is automatically recalculated.
     */
    function permit(
        address _owner,
        address _spender,
        uint256 _amount,
        uint256 _deadline,
        uint8 _v,
        bytes32 _r,
        bytes32 _s
    ) public virtual override {
        require(block.timestamp <= _deadline, "ERC20Permit: expired _deadline");

        // Assembly for more efficiently computing:
        // bytes32 hashStruct = keccak256(
        //     abi.encode(
        //         _PERMIT_TYPEHASH,
        //         _owner,
        //         _spender,
        //         _amount,
        //         nonces[_owner].current(),
        //         _deadline
        //     )
        // );

        bytes32 hashStruct;
        Counters.Counter storage nonceCounter = _nonces[_owner];
        uint256 nonce = nonceCounter.current();

        assembly {
            // Load free memory pointer
            let memPtr := mload(64)

            // keccak256("Permit(address _owner,address _spender,uint256 value,uint256 nonce,uint256 _deadline)")
            mstore(memPtr, 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9)
            mstore(add(memPtr, 32), _owner)
            mstore(add(memPtr, 64), _spender)
            mstore(add(memPtr, 96), _amount)
            mstore(add(memPtr, 128), nonce)
            mstore(add(memPtr, 160), _deadline)

            hashStruct := keccak256(memPtr, 192)
        }

        bytes32 eip712DomainHash = _domainSeparator();

        // Assembly for more efficient computing:
        // bytes32 hash = keccak256(
        //     abi.encodePacked(uint16(0x1901), eip712DomainHash, hashStruct)
        // );

        bytes32 hash;

        assembly {
            // Load free memory pointer
            let memPtr := mload(64)

            mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000)  // EIP191 header
            mstore(add(memPtr, 2), eip712DomainHash)                                            // EIP712 domain hash
            mstore(add(memPtr, 34), hashStruct)                                                 // Hash of struct

            hash := keccak256(memPtr, 66)
        }

        address signer = _recover(hash, _v, _r, _s);

        require(signer == _owner, "ERC20Permit: invalid signature");

        nonceCounter.increment();
        _approve(_owner, _spender, _amount);
    }

    /**
     * @dev See {IERC2612Permit-nonces}.
     */
    function nonces(address _owner) public override view returns (uint256) {
        return _nonces[_owner].current();
    }

    function _updateDomainSeparator() private returns (bytes32) {
        uint256 chainID = _chainID();

        // no need for assembly, running very rarely
        bytes32 newDomainSeparator = keccak256(
            abi.encode(
                keccak256(
                    "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
                ),
                keccak256(bytes(name())), // ERC-20 Name
                keccak256(bytes("1")),    // Version
                chainID,
                address(this)
            )
        );

        domainSeparators[chainID] = newDomainSeparator;

        return newDomainSeparator;
    }

    // Returns the domain separator, updating it if chainID changes
    function _domainSeparator() private returns (bytes32) {
        bytes32 domainSeparator = domainSeparators[_chainID()];

        if (domainSeparator != 0x00) {
            return domainSeparator;
        }

        return _updateDomainSeparator();
    }

    function _chainID() private pure returns (uint256) {
        uint256 chainID;
        assembly {
            chainID := chainid()
        }

        return chainID;
    }

    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.
        if (
            uint256(_s) >
            0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0
        ) {
            revert("ECDSA: invalid signature '_s' value");
        }

        if (_v != 27 && _v != 28) {
            revert("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 Extension of {ERC20} that allows token holders to use their tokens
 * without sending any transactions by setting {IERC20-allowance} with a
 * signature using the {permit} method, and then spend them via
 * {IERC20-transferFrom}.
 *
 * The {permit} signature mechanism conforms to the {IERC2612Permit} interface.
 */
function permit(
    address _owner,
    address _spender,
    uint256 _amount,
    uint256 _deadline,
    uint8 _v,
    bytes32 _r,
    bytes32 _s
) public virtual override {
    require(block.timestamp <= _deadline, "ERC20Permit: expired _deadline");

    // Assembly for more efficiently computing:
    // bytes32 hashStruct = keccak256(
    //     abi.encode(
    //         _PERMIT_TYPEHASH,
    //         _owner,
    //         _spender,
    //         _amount,
    //         nonces[_owner].current(),
    //         _deadline
    //     )
    // );

    bytes32 hashStruct;
    Counters.Counter storage nonceCounter = _nonces[_owner];
    uint256 nonce = nonceCounter.current();

    assembly {
        // Load free memory pointer
        let memPtr := mload(64)

        // keccak256("Permit(address _owner,address _spender,uint256 value,uint256 nonce,uint256 _deadline)")
        mstore(memPtr, 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9)
        mstore(add(memPtr, 32), _owner)
        mstore(add(memPtr, 64), _spender)
        mstore(add(memPtr, 96), _amount)
        mstore(add(memPtr, 128), nonce)
        mstore(add(memPtr, 160), _deadline)

        hashStruct := keccak256(memPtr, 192)
    }

    bytes32 eip712DomainHash = _domainSeparator();

    // Assembly for more efficient computing:
    // bytes32 hash = keccak256(
    //     abi.encodePacked(uint16(0x1901), eip712DomainHash, hashStruct)
    // );

    bytes32 hash;

    assembly {
        // Load free memory pointer
        let memPtr := mload(64)

        mstore(memPtr, 0x1901000000000000000000000000000000000000000000000000000000000000)  // EIP191 header
        mstore(add(memPtr, 2), eip712DomainHash)                                            // EIP712 domain hash
        mstore(add(memPtr, 34), hashStruct)                                                 // Hash of struct

        hash := keccak256(memPtr, 66)
    }

    address signer = _recover(hash, _v, _r, _s);

    require(signer == _owner, "ERC20Permit: invalid signature");

    nonceCounter.increment();
    _approve(_owner, _spender, _amount);
}

/**
 * @dev See {IERC2612Permit-permit}.
 *
 * If https://eips.ethereum.org/EIPS/eip-1344[ChainID] ever changes, the
 * EIP712 Domain Separator is automatically recalculated.
 */