// Contract: L2TT0
// Target function: function cl(uint256 shift, bytes20 base, string memory alpha) private pure returns (string memory)
21.onERC721Received.selector`.
     */
    function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}


/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
        (bool success, ) = recipient.call{ value: amount }("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain`call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
      return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.call{ value: value }(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                // solhint-disable-next-line no-inline-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant alphabet = "0123456789abcdef";

    /**
     * @dev Converts a `uint256` to its ASCII `string` decimal representation.
     */
    function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        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);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
     */
    function toHexString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

    /**
     * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
     */
    function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "";
        buffer[1] = "";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = alphabet[value & 0xf];
            value >>= 4; 
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }
    
    // adapted from tkeber solution: https://ethereum.stackexchange.com/a/8447
    function toAsciiString(address x) internal pure returns (string memory) {
        bytes memory s = new bytes(40);
        for (uint i = 0; i < 20; i++) {
            bytes1 b = bytes1(uint8(uint(uint160(x)) / (2**(8*(19 - i)))));
            bytes1 hi = bytes1(uint8(b) / 16);
            bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
            s[2*i] = char(hi);
            s[2*i+1] = char(lo);            
        }
        return string(s);
    }
    
    function char(bytes1 b) internal pure returns (bytes1 c) {
        if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
        else return bytes1(uint8(b) + 0x57);
    }    
    
    // adapted from t-nicci solution https://ethereum.stackexchange.com/a/31470
    function subString(string memory str, uint startIndex, uint endIndex) internal pure returns (string memory) {
        bytes memory strBytes = bytes(str);
        bytes memory result = new bytes(endIndex-startIndex);
        for(uint i = startIndex; i < endIndex; i++) {
            result[i-startIndex] = strBytes[i];
        }
        return string(result);
    }    
    
    function compareStrings(string memory a, string memory b) internal pure returns (bool) {
        return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
    }
    
    // nice solution: https://ethereum.stackexchange.com/questions/56319/how-to-convert-bytes32-to-string
    function toShortString(bytes32 _data) internal pure returns (string memory) {
      bytes memory _bytesContainer = new bytes(32);
      uint256 _charCount = 0;
      // loop through every element in bytes32
      for (uint256 _bytesCounter = 0; _bytesCounter < 32; _bytesCounter++) {
        bytes1 _char = bytes1(bytes32(uint256(_data) * 2 ** (8 * _bytesCounter)));
        if (_char != 0) {
          _bytesContainer[_charCount] = _char;
          _charCount++;
        }
      }
    
      bytes memory _bytesContainerTrimmed = new bytes(_charCount);
    
      for (uint256 _charCounter = 0; _charCounter < _charCount; _charCounter++) {
        _bytesContainerTrimmed[_charCounter] = _bytesContainer[_charCounter];
      }
    
      return string(_bytesContainerTrimmed);
    }    
    
}

// base layer
contract the_coin {
    function ownerOf(uint256 tokenId) external view returns (address) {}
    function reveal(uint256 tokenId) external view returns (string memory) {}
    function tokenURI(uint256 tokenId) external view returns (string memory) {}
    function isApprovedForAll(address owner, address operator) external view returns (bool) {}
}

/**
 * @title layer_two contract
 * @dev Extends ERC721 Non-Fungible Token Standard basic implementation
 */
contract L2TT0 is ERC721, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    mapping (uint256 => uint256) private last_minted;
    address private the_coin_address = 0xf76c5d925b27a63a3745A6b787664A7f38fA79bd; // mainnet
    uint256 private waitTimeInBlocks = 210000/5; // just under a week

    struct L2 {
        address builder;
        uint256 nonce;
        uint256 mint_block;
        uint256 base_layer;
    }
    mapping (uint256 => L2) private L2s;    

    // estimate next minting time for a base layer token
    function nextMinting(uint256 baseTokenId) public view returns (string memory) {
        require(baseTokenId < 85 && baseTokenId > 0, "ERROR: not valid base token ID");
        uint256 nextTime = waitTimeInBlocks+last_minted[baseTokenId];
        string memory userMsg;
        if (block.number >= nextTime) {
            userMsg = "READY,0,0,0,0,0";
        } else {
            uint256 b = nextTime - block.number;
            userMsg = string(abi.encodePacked("WAIT,",
                        s(b),",", // blocks
                        s(b*13),",", // ~seconds
                        s(b*13/60),",", // ~mins
                        s(b*13/60/60),",", // ~hrs
                        s(b*13/60/60/24) // ~days
                    ));
        }
        return userMsg;
    }

    // wee bit of tidying...
    function s(uint256 v) private pure returns (string memory) {
        return Strings.toString(v);
    }

    // single css rendering function for L2 token; restyles stochastically
    function makeStyle(uint256 nonce) private view returns (string memory) {
        
        // pseudorand using keccak... color bases and transform base
        bytes20 cb1 = bytes20(keccak256(abi.encodePacked(nonce)));
        bytes20 cb2 = bytes20(keccak256(abi.encodePacked(nonce*2+1)));
        bytes20 tfb;

        uint256 h = vl(0,cb1) % 4 + 1; // nth child count
        string memory out = '';

        for (uint256 i = 1; i <= h; i++) {
            tfb = bytes20(keccak256(abi.encodePacked(nonce+i))); // unique for each nth child
            uint256 j = (i-1) * 2 + (i%2); // shifting color bytes
            out = string(abi.encodePacked(out,"line, text, circle, path:nth-child(",s(h),"n+",s(i-1),"){fill:#",cl(j,cb1,"77"),"!important;"));
            out = string(abi.encodePacked(out,"stroke:#",cl(j,cb2,"77"),"!important;"));
            out = string(abi.encodePacked(out,"stroke-width:",s(vl(1,tfb)/2),"pt!important;"));
            out = string(abi.encodePacked(out,"transform:skew(",s(7*(vl(2,tfb)/5)),"deg,",s(7*(vl(3,tfb)/5)),"deg)"));
            out = string(abi.encodePacked(out," scale(",s(vl(4,tfb)/128+1),".",s(vl(4,tfb)%10),",",s(vl(5,tfb)/128+1),".",s(vl(5,tfb)%10),");}"));
        }
        out = string(abi.encodePacked(out,c[0],cl(10,tfb,"ff"),"!important;transform:scale(1,1)!important;}"));        
        return out;
    }

    // clip out element from bytes20
    function vl(uint256 i, bytes20 base) private pure returns (uint256) {
        return uint256(uint8(base[i]));
    }