// File: contracts/DigitalArtCollectibleERC1155Wrapper.sol



pragma solidity ^0.5.0;

import "./AbstractWrapper.sol";

contract DigitalArtCollectibleERC1155Wrapper is AbstractWrapper {

    function initialize() internal {
        create(1, 1, "ipfs://QmWAP5TQokmAXWw7mYFapCdDa9piYZE2kVJiqP53NqoQD2");
        create(4, 50, "ipfs://Qmf3UT9mmzPZwgf3cSQDEa7ZBji8ntZskrLDBEN1gVscpp");
        create(6, 10, "ipfs://QmToz4QuznqSFkb9h4K9soKJ7aMsr9nREnFmYqLCSe3RdH");
        create(7, 100, "ipfs://QmRFtHmyQnmXJ75Zxcq9Jb6XPgFyVK9iNdJWwepYx9Eire");
        create(8, 10, "ipfs://QmcbfX6GYWTXYw9y6GEWwiuco4cXDZJTnfK88SFuXzfurk");
        create(9, 100, "ipfs://QmRWB53RU2YkRDpytq4wrDf7AVvwD65vLKQxjUeAswHU66");
        create(10, 100, "ipfs://QmTKR3GjmRqxWaw6QJph7pRnrnfYf23WSMjfAGbeDvt9bA");
        create(11, 100, "ipfs://QmVddaQPEX7kREHNq7q7yEBxWcf5WoDHKqiCb7cLvtNZrg");
        create(12, 50, "ipfs://QmbaR52MFHjN2hQi9uJGoYX8GNhtLQPPuokBPwJ4ok32W5");
        create(13, 50, "ipfs://QmTsy7cSX7UijWndeEsqZb1VKzecyKbosqYrHuEu3Apevh");
        create(14, 100, "ipfs://QmZg5DNNBV1wZ8jX5ZMXPLguBxDMQt5DUpyvwVwNiEcR7G");
        create(15, 200, "ipfs://QmUMXZTDLQcsB222MYsSSt8TncANDkamnNwhUF7pD6QLCk");
        create(16, 50, "ipfs://QmehKVW9PueWFaaaxZ2rHmVJPEKfyjGVCZMgiFYxwVJZAv");
        create(17, 100, "ipfs://QmUH2Mf5UdRfL1UeRnJmBNUX1T5L3qGSP7vD5apsu4cp9i");
        create(18, 200, "ipfs://QmULb4tuKQHXTro5N9U3jVxxNiLdoGYyippzwacExbTDkW");
        create(19, 50, "ipfs://QmYU8WttFNtNmncbxwfcrgay6y25NPNU86PDTWHth4Y7Bs");
        create(20, 50, "ipfs://QmW6vr9cmAakVQWxaojo6inFub7QgSGBfBS18fZRNNFmpX");
        create(21, 100, "ipfs://QmQ537xkrPNr9wVVF1Lh9Wbf4rZmBQQzxTpCa3tjjBwj9N");
        create(22, 200, "ipfs://QmcXtodSRshYVih4VPiHRogg4ysGEwTQEuegx4S9eqStgM");
        create(23, 200, "ipfs://QmPLVtPKBC8wAFfe1uLu11ym6wFGKERQc4bb3odjj4YhAC");
        create(24, 200, "ipfs://QmPsZyWu1GsCem4ARUGa5QhQYfT3CXNdkHhmVZCET1kFDX");
        create(25, 200, "ipfs://QmfXJtR7bCcq28vcYCqFZX8PXtbhK6nxLRqgg21E9qaMSM");
        create(26, 100, "ipfs://QmQYH2yyqRGCCtk3oK3r5WVCdJF2Ff7D9dWE7ZFZyo2kXY");
        create(27, 200, "ipfs://QmVvaffp7Do9LhEvPGkmSM5B1tEnM34YrCiQTwfDtEABck");
        create(28, 100, "ipfs://QmPB8pfwgpnbHRiUz3JwFNpvmKsnBqKhtaEqWggN2FSFdm");
        create(29, 200, "ipfs://Qmex4cmDTQecLcXnkVAGc7RHDjgQbaDcydtwmwTyGYNt1e");
        create(30, 10, "ipfs://QmcCMakqG9kBAHvFkCUUNVriiXFnEh4ibbTjd3kdZ8hqZk");
        create(31, 200, "ipfs://QmfXe9nQurA2uCQPjmCmJk4dQjLK2ifCARVnskNejMd76t");
        create(32, 10, "ipfs://QmWdbif88zqWXLfMP9riz1cBBUYuUVHS1CrgdKZtnyRiTj");
        create(33, 200, "ipfs://QmdsCLuTN9ongEDmjCurs989PwYPPQofPHjUp5UaJMisvX");
        create(34, 50, "ipfs://QmdkHQ2arBVsarLxinm4JG4tk3B1zfFTnWPminTg42GZxm");
        create(35, 200, "ipfs://QmX2vGQQLUqRMuHJMHtfPdzSAmc2RXTWen5eLHUCQxdxA6");
        create(36, 50, "ipfs://Qmdvjn9WCUKMgYcy2NHi3LSQJofLvW3mZFjdvr1N5AYcYS");
        create(37, 200, "ipfs://QmSVKpf6fyVZZ2PVJkCRnLad7BffF1Xh6zEbP5kqkYA2SK");
        create(38, 200, "ipfs://QmdQNgpNfJX59wzCHS1UfxSVMJY4YEWLkfqxbnTgA8SCuG");
        create(39, 200, "ipfs://QmRsxTm2BCofrVQ5xnWvcmdoDFkvcHpbSJ93Xvr9aDAexq");
        create(40, 100, "ipfs://QmY39vaPzVUnd2UTJx89QPJXjBMWXBh75oTvnrdfYVWkHR");
        create(41, 100, "ipfs://QmYv4o4L4k1GTS2T2iqjF9JKRXf6oD8kxN4YDGiAZFgNPF");
        create(42, 200, "ipfs://QmPhb71kNgm8Rnr9cYESPa68mRn8dWPNRm3UZgjgg94YmS");
        create(43, 200, "ipfs://QmSoEVqdf8R4LLAaDGVmu79Y5N1WzauV8tiv4D8PQARz9a");
        create(44, 100, "ipfs://Qmbj7MKjZx9vfuLThsBWZsBJffkChcFKhGXhLp2iCtU2u8");
        create(45, 100, "ipfs://QmeNUDM6avcRMZYiFqEBTDU3Fz9biypqtTiDnZV6jT7CDi");
        create(46, 100, "ipfs://QmXmo2oaQKiVx3rSB5xpHjKq9XYBo6iY2SqUfXr7aERvRy");
        create(47, 200, "ipfs://QmSm2C2sZeoNdvQcPKaaVHgrF18sA9rLZJWdzEVw43pCSK");
        create(48, 200, "ipfs://QmXh2o3o1qeXYUzzYQXUKUjuvQJuUgGKw8wRM9TKCvCqiR");
        create(49, 200, "ipfs://QmPP2hKCVRoo5X9tjZnuAoyvPrKzkjhxBrr3T5awmbBMJT");
        create(50, 200, "ipfs://QmZJa3nosLH4csDHs577BR4fPNTQ8beubsksznsyrGeLvJ");
        create(51, 200, "ipfs://QmQBhWbbZjbZ7wawBnViZgK4j6xpa7irMXsY9mDnrDLwYp");
        create(52, 200, "ipfs://QmQdXA2Mwuho4mqjn7RfvTda7BKXo9A9UiZivrZmZNMtvg");
        create(53, 200, "ipfs://QmTCeqs7og4RfJefk7UMwmB1Wm9vWoqXVBJ1LwZy44ECVm");
        create(54, 200, "ipfs://QmQQarCamg6DTbegfuTi6GmcaJxj3otS1xp5kk7meymWwX");
        create(55, 200, "ipfs://QmeZ9ZjiLRA46ygNCyzMniM4LVF5KGNNMj1zL3E8Wit6j5");
        create(56, 100, "ipfs://QmYSfQJBJzAfEUCjfijus67RAdxazY5KiLtSXYVY7BqyXR");
        create(57, 200, "ipfs://QmPAzd37Agu4xspCXUKeuerTsTHVrNHK7QN2HxT1r5uPNG");
        create(58, 200, "ipfs://QmXkrMH9B5ehk9ftfKqSWZaQ13RzeVrQ2qk5VzeUyPsN5S");
        create(61, 100, "ipfs://QmSr3hcyg6DBcncy85pN6bZfTStEQCqrc4pswAtcMJUK7B");
        create(62, 200, "ipfs://QmcBPVKpESwSNLhbQnxQv1oEBqR46jZrUNnW5cbL4Dr7LC");
        create(63, 200, "ipfs://QmZyTS3NmG97L6gP2DorGTp67Bw3GXUGMDMrQfPYd6uD2B");
        create(64, 200, "ipfs://Qmc3sGP18Gy8J7oh7MjD4Ym8eJDapv4mk1nM1nv3z2yhuv");
        create(65, 200, "ipfs://QmedZmtCg5c5h4pDZzM26kE5vQmUp5xUKEBAbGhhUhvcxH");
        create(66, 50, "ipfs://QmVpqZC1aKYNhPeJap5Dz3UdJVqf8kRXk1FpGVTE3za1ec");
        create(67, 200, "ipfs://QmZ5xPstBG7PMLqFed1xLVJJWDuwAP3Y512mSjpM4vNW9j");
        create(68, 100, "ipfs://QmXsUV7QEYxXXbgYjXdqmM8duYg1aJsf2Nr7fbbjxpQAKP");
        create(69, 10, "ipfs://QmUbrnb3zXSdc55mBLRSYBMiBpvG1MNo9YbCno2RcB3oif");
        create(70, 50, "ipfs://QmcQuf4f7QyxdArtpBNenakyy1JQFGPfMGBwbk9FBtAAcw");
        create(71, 100, "ipfs://QmSCqCzwv36cpsnkTDYqL7zSxkDfBQLyvM1ttaFcCUWszR");
    }

    constructor(address _proxyRegistryAddress, address digitalArtCollectibleAddress) AbstractWrapper(_proxyRegistryAddress, digitalArtCollectibleAddress) public {

    }

}

// File: contracts/AbstractWrapper.sol



pragma solidity ^0.5.0;

import "./ERC1155.sol";
import "./IDigitalArtCollectible.sol";
import "./IProxyRegistry.sol";
import "./SafeMath.sol";
import "./Address.sol";

contract AbstractWrapper is ERC1155 {
    using SafeMath for uint256;
    using Address for address;

    IDigitalArtCollectible public _DigitalArtCollectibleContract;

    // OpenSea's Proxy Registry
    IProxyRegistry public proxyRegistry;

    // Standard attributes
    string public name;
    string public symbol;
    
    // starts as allowed
    bool isExecutionAllowed = true;

    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    // nft id => nft metadata IPFS URI
    mapping (uint256 => string) public metadatas;

    // nft id => nft total supply
    mapping(uint => uint) public tokenSupply;

    // print index => whether is wrapped
    mapping(uint => bool) public wrappedPrints;

    function initialize() internal;

    /**
        @notice Initialize an nft id's data.
    */
    function create(uint256 _id, uint256 _totalSupply, string memory _uri) internal {

        require(tokenSupply[_id] == 0, "id already exists");
        tokenSupply[_id] = _totalSupply;

        // mint 0 just to let explorers know it exists
        // emit TransferSingle(msg.sender, address(0), msg.sender, _id, 0);

        metadatas[_id] = _uri;
        emit URI(_uri, _id);
    }

    constructor(address _proxyRegistryAddress, address digitalArtCollectibleAddress) public {
        proxyRegistry = IProxyRegistry(_proxyRegistryAddress);
        _DigitalArtCollectibleContract = IDigitalArtCollectible(digitalArtCollectibleAddress);
        _owner = msg.sender;
        
        // Set the name for display purposes
        name = "Cryptocards";
        // Set the symbol for display purposes
        symbol = "WƉ";
        
        initialize();
    }

    /**
       @return the address of the owner.
     */
    function owner() public view returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(isOwner(), "Not owner");
        _;
    }

    /**
     * @return true if `msg.sender` is the owner of the contract.
     */
    function isOwner() public view returns (bool) {
        return msg.sender == _owner;
    }

    /**
     * @dev Allows the current owner to transfer control of the contract to a newOwner.
     * @param newOwner The address to transfer ownership to.
     */
    function transferOwnership(address newOwner) public onlyOwner {
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers control of the contract to a newOwner.
     * @param newOwner The address to transfer ownership to.
     */
    function _transferOwnership(address newOwner) internal {
        require(newOwner != address(0));
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }

    /**
        @dev override ERC1155 uri function to return IPFS ref.
        @param _id NFT ID
        @return IPFS URI pointing to NFT ID's metadata.
    */
    function uri(uint256 _id) public view returns (string memory) {
        return metadatas[_id];
    }

    /**
    * @dev Will update the metadata for a token
    * @param _tokenId The token to update. _msgSender() must be its creator.
    * @param _newURI New URI for the token.
    */
    function setCustomURI(uint256 _tokenId, string memory _newURI) public onlyOwner {
        metadatas[_tokenId] = _newURI;
        emit URI(_newURI, _tokenId);
    }

    /**
        @notice Queries the approval status of an operator for a given owner.
        @param _user     The owner of the Tokens
        @param _operator  Address of authorized operator
        @return           True if the operator is approved, false if not
    */
    function isApprovedForAll(address _user, address _operator) public view returns (bool) {
        // Whitelist OpenSea proxy contract for easy trading.
        if (proxyRegistry.proxies(_user) == _operator) {
            return true;
        }

        return super.isApprovedForAll(_user, _operator);
    }

     /**
    * @dev Returns the total quantity for a token ID
    * @param _id uint256 ID of the token to query
    * @return amount of token in existence
    */
    function totalSupply(
        uint256 _id
    ) public view returns (uint256) {
        return tokenSupply[_id];
    }

    /**
        @dev helper function to see if NFT ID exists, makes OpenSea happy.
        @param _id NFT ID
        @return if NFT ID exists.
    */
    function exists(uint256 _id) external view returns(bool) {
        // requires the drawing id to actually exist
        return tokenSupply[_id] != 0;
    }

    /**
        @dev for an NFT ID, queries and transfers tokens from the appropriate
        collectible contract to itself, and mints and transfers corresponding new
        ERC-1155 tokens to caller.
     */
    function wrap(uint256 _id, uint256[] calldata _printIndexes) external {
        require(isExecutionAllowed);

        address _to = msg.sender;

        uint256 _quantity = _printIndexes.length;

        for (uint256 i=0; i < _quantity; ++i) {
            uint256 _printIndex = _printIndexes[i];

            address owner_address = _DigitalArtCollectibleContract.DrawingPrintToAddress(_printIndex);
            // Check if caller owns the ERC20
            require(owner_address == msg.sender, "Does not own ERC20");

            _DigitalArtCollectibleContract.buyCollectible(_id, _printIndex);

            // Check if buy succeeded
            require(_DigitalArtCollectibleContract.DrawingPrintToAddress(_printIndex) == address(this), "An error occured");

            wrappedPrints[_printIndex] = true;

            balances[_id][msg.sender] = balances[_id][msg.sender].add(1);
        }

        // mint
        emit TransferSingle(msg.sender, address(0), msg.sender, _id, _quantity);

        if (_to.isContract()) {
           _doSafeTransferAcceptanceCheck(msg.sender, msg.sender, msg.sender, _id, _quantity, '');
        }
    }


    /**
        @dev for an NFT ID, burns ERC-1155 quantity and transfers cryptoart ERC-20
        tokens to caller.
     */
    function unwrap(uint256 _id, uint256[] calldata _printIndexes) external {
        require(isExecutionAllowed);

        uint256 _quantity = _printIndexes.length;

        require(balances[_id][msg.sender] >= _quantity, "insufficient balance");

        for (uint256 i=0; i < _quantity; ++i) {
            uint256 _printIndex = _printIndexes[i];
            bool success = _DigitalArtCollectibleContract.transfer(msg.sender, _id, _printIndex);

            // Check if transfer succeeded
            require(success, "An error occured while transferring");

            wrappedPrints[_printIndex] = false;

            balances[_id][msg.sender] = balances[_id][msg.sender].sub(1);
        }

        // burn
        emit TransferSingle(msg.sender, address(this), address(0), _id, _quantity);
    }

    /**
        @dev bulk buy presale items
     */
    function bulkPreBuyCollectible(uint256 _id, uint256[] calldata _printIndexes, uint256 initialPrintPrice) external payable  {
        require(isExecutionAllowed);

        address _to = msg.sender;
        uint256 _quantity = _printIndexes.length;
        

        for (uint256 i=0; i < _quantity; ++i) {
            uint256 _printIndex = _printIndexes[i];
             
            _DigitalArtCollectibleContract.alt_buyCollectible.value(initialPrintPrice)(_id, _printIndex);

            require(_DigitalArtCollectibleContract.DrawingPrintToAddress(_printIndex) == address(this), "An error occured");

            wrappedPrints[_printIndex] = true;

            balances[_id][msg.sender] = balances[_id][msg.sender].add(1);
        }

          // mint
        emit TransferSingle(msg.sender, address(0), msg.sender, _id, _quantity);

        if (_to.isContract()) {
           _doSafeTransferAcceptanceCheck(msg.sender, msg.sender, msg.sender, _id, _quantity, '');
        }
    }

    function flipSwitchTo(bool state) public onlyOwner {
        isExecutionAllowed = state;
    }
}

// File: contracts/ERC1155.sol



pragma solidity ^0.5.0;

import "./SafeMath.sol";
import "./Address.sol";
import "./Common.sol";
import "./IERC1155TokenReceiver.sol";
import "./IERC1155.sol";

// A sample implementation of core ERC1155 function.
contract ERC1155 is IERC1155, ERC165, CommonConstants
{
    using SafeMath for uint256;
    using Address for address;

    // id => (owner => balance)
    mapping (uint256 => mapping(address => uint256)) internal balances;

    // owner => (operator => approved)
    mapping (address => mapping(address => bool)) internal operatorApproval;

/////////////////////////////////////////// ERC165 //////////////////////////////////////////////

    /*
        bytes4(keccak256('supportsInterface(bytes4)'));
    */
    bytes4 constant private INTERFACE_SIGNATURE_ERC165 = 0x01ffc9a7;

    /*
        bytes4(keccak256("safeTransferFrom(address,address,uint256,uint256,bytes)")) ^
        bytes4(keccak256("safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)")) ^
        bytes4(keccak256("balanceOf(address,uint256)")) ^
        bytes4(keccak256("balanceOfBatch(address[],uint256[])")) ^
        bytes4(keccak256("setApprovalForAll(address,bool)")) ^
        bytes4(keccak256("isApprovedForAll(address,address)"));
    */
    bytes4 constant private INTERFACE_SIGNATURE_ERC1155 = 0xd9b67a26;

    function supportsInterface(bytes4 _interfaceId)
    public
    view
    returns (bool) {
         if (_interfaceId == INTERFACE_SIGNATURE_ERC165 ||
             _interfaceId == INTERFACE_SIGNATURE_ERC1155) {
            return true;
         }

         return false;
    }

/////////////////////////////////////////// ERC1155 //////////////////////////////////////////////

    /**
        @notice Transfers `_value` amount of an `_id` from the `_from` address to the `_to` address specified (with safety call).
        @dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
        MUST revert if `_to` is the zero address.
        MUST revert if balance of holder for token `_id` is lower than the `_value` sent.
        MUST revert on any other error.
        MUST emit the `TransferSingle` event to reflect the balance change (see "Safe Transfer Rules" section of the standard).
        After the above conditions are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call `onERC1155Received` on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
        @param _from    Source address
        @param _to      Target address
        @param _id      ID of the token type
        @param _value   Transfer amount
        @param _data    Additional data with no specified format, MUST be sent unaltered in call to `onERC1155Received` on `_to`
    */
    function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes calldata _data) external {

        require(_to != address(0x0), "_to must be non-zero.");
        require(_from == msg.sender || operatorApproval[_from][msg.sender] == true, "Need operator approval for 3rd party transfers.");

        // SafeMath will throw with insuficient funds _from
        // or if _id is not valid (balance will be 0)
        balances[_id][_from] = balances[_id][_from].sub(_value);
        balances[_id][_to]   = _value.add(balances[_id][_to]);

        // MUST emit event
        emit TransferSingle(msg.sender, _from, _to, _id, _value);

        // Now that the balance is updated and the event was emitted,
        // call onERC1155Received if the destination is a contract.
        if (_to.isContract()) {
            _doSafeTransferAcceptanceCheck(msg.sender, _from, _to, _id, _value, _data);
        }
    }

    /**
        @notice Transfers `_values` amount(s) of `_ids` from the `_from` address to the `_to` address specified (with safety call).
        @dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
        MUST revert if `_to` is the zero address.
        MUST revert if length of `_ids` is not the same as length of `_values`.
        MUST revert if any of the balance(s) of the holder(s) for token(s) in `_ids` is lower than the respective amount(s) in `_values` sent to the recipient.
        MUST revert on any other error.
        MUST emit `TransferSingle` or `TransferBatch` event(s) such that all the balance changes are reflected (see "Safe Transfer Rules" section of the standard).
        Balance changes and events MUST follow the ordering of the arrays (_ids[0]/_values[0] before _ids[1]/_values[1], etc).
        After the above conditions for the transfer(s) in the batch are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call the relevant `ERC1155TokenReceiver` hook(s) on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
        @param _from    Source address
        @param _to      Target address
        @param _ids     IDs of each token type (order and length must match _values array)
        @param _values  Transfer amounts per token type (order and length must match _ids array)
        @param _data    Additional data with no specified format, MUST be sent unaltered in call to the `ERC1155TokenReceiver` hook(s) on `_to`
    */
    function safeBatchTransferFrom(address _from, address _to, uint256[] calldata _ids, uint256[] calldata _values, bytes calldata _data) external {

        // MUST Throw on errors
        require(_to != address(0x0), "destination address must be non-zero.");
        require(_ids.length == _values.length, "_ids and _values array length must match.");
        require(_from == msg.sender || operatorApproval[_from][msg.sender] == true, "Need operator approval for 3rd party transfers.");

        for (uint256 i = 0; i < _ids.length; ++i) {
            uint256 id = _ids[i];
            uint256 value = _values[i];

            // SafeMath will throw with insuficient funds _from
            // or if _id is not valid (balance will be 0)
            balances[id][_from] = balances[id][_from].sub(value);
            balances[id][_to]   = value.add(balances[id][_to]);
        }

        // Note: instead of the below batch versions of event and acceptance check you MAY have emitted a TransferSingle
        // event and a subsequent call to _doSafeTransferAcceptanceCheck in above loop for each balance change instead.
        // Or emitted a TransferSingle event for each in the loop and then the single _doSafeBatchTransferAcceptanceCheck below.
        // However it is implemented the balance changes and events MUST match when a check (i.e. calling an external contract) is done.

        // MUST emit event
        emit TransferBatch(msg.sender, _from, _to, _ids, _values);

        // Now that the balances are updated and the events are emitted,
        // call onERC1155BatchReceived if the destination is a contract.
        if (_to.isContract()) {
            _doSafeBatchTransferAcceptanceCheck(msg.sender, _from, _to, _ids, _values, _data);
        }
    }

    /**
        @notice Get the balance of an account's Tokens.
        @param _owner  The address of the token holder
        @param _id     ID of the Token
        @return        The _owner's balance of the Token type requested
     */
    function balanceOf(address _owner, uint256 _id) external view returns (uint256) {
        // The balance of any account can be calculated from the Transfer events history.
        // However, since we need to keep the balances to validate transfer request,
        // there is no extra cost to also privide a querry function.
        return balances[_id][_owner];
    }


    /**
        @notice Get the balance of multiple account/token pairs
        @param _owners The addresses of the token holders
        @param _ids    ID of the Tokens
        @return        The _owner's balance of the Token types requested (i.e. balance for each (owner, id) pair)
     */
    function balanceOfBatch(address[] calldata _owners, uint256[] calldata _ids) external view returns (uint256[] memory) {

        require(_owners.length == _ids.length);

        uint256[] memory balances_ = new uint256[](_owners.length);

        for (uint256 i = 0; i < _owners.length; ++i) {
            balances_[i] = balances[_ids[i]][_owners[i]];
        }

        return balances_;
    }

    /**
        @notice Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.
        @dev MUST emit the ApprovalForAll event on success.
        @param _operator  Address to add to the set of authorized operators
        @param _approved  True if the operator is approved, false to revoke approval
    */
    function setApprovalForAll(address _operator, bool _approved) external {
        operatorApproval[msg.sender][_operator] = _approved;
        emit ApprovalForAll(msg.sender, _operator, _approved);
    }

    /**
        @notice Queries the approval status of an operator for a given owner.
        @param _owner     The owner of the Tokens
        @param _operator  Address of authorized operator
        @return           True if the operator is approved, false if not
    */
    function isApprovedForAll(address _owner, address _operator) public view returns (bool) {
        return operatorApproval[_owner][_operator];
    }

/////////////////////////////////////////// Internal //////////////////////////////////////////////

    function _doSafeTransferAcceptanceCheck(address _operator, address _from, address _to, uint256 _id, uint256 _value, bytes memory _data) internal {

        // If this was a hybrid standards solution you would have to check ERC165(_to).supportsInterface(0x4e2312e0) here but as this is a pure implementation of an ERC-1155 token set as recommended by
        // the standard, it is not necessary. The below should revert in all failure cases i.e. _to isn't a receiver, or it is and either returns an unknown value or it reverts in the call to indicate non-acceptance.


        // Note: if the below reverts in the onERC1155Received function of the _to address you will have an undefined revert reason returned rather than the one in the require test.
        // If you want predictable revert reasons consider using low level _to.call() style instead so the revert does not bubble up and you can revert yourself on the ERC1155_ACCEPTED test.
        require(ERC1155TokenReceiver(_to).onERC1155Received(_operator, _from, _id, _value, _data) == ERC1155_ACCEPTED, "contract returned an unknown value from onERC1155Received");
    }

    function _doSafeBatchTransferAcceptanceCheck(address _operator, address _from, address _to, uint256[] memory _ids, uint256[] memory _values, bytes memory _data) internal {

        // If this was a hybrid standards solution you would have to check ERC165(_to).supportsInterface(0x4e2312e0) here but as this is a pure implementation of an ERC-1155 token set as recommended by
        // the standard, it is not necessary. The below should revert in all failure cases i.e. _to isn't a receiver, or it is and either returns an unknown value or it reverts in the call to indicate non-acceptance.

        // Note: if the below reverts in the onERC1155BatchReceived function of the _to address you will have an undefined revert reason returned rather than the one in the require test.
        // If you want predictable revert reasons consider using low level _to.call() style instead so the revert does not bubble up and you can revert yourself on the ERC1155_BATCH_ACCEPTED test.
        require(ERC1155TokenReceiver(_to).onERC1155BatchReceived(_operator, _from, _ids, _values, _data) == ERC1155_BATCH_ACCEPTED, "contract returned an unknown value from onERC1155BatchReceived");
    }
}

// File: contracts/IDigitalArtCollectible.sol



pragma solidity ^0.5.0;

// DigitalArtCollectible is not quite ERC-20 compliant
interface IDigitalArtCollectible {
   function transfer(address to, uint drawingId, uint printIndex) external returns (bool success);
   function DrawingPrintToAddress(uint print) external returns (address _address);
   function buyCollectible(uint drawingId, uint printIndex) external payable;
   function alt_buyCollectible(uint drawingId, uint printIndex) external payable;
}

// File: contracts/IProxyRegistry.sol



pragma solidity ^0.5.0;

interface IProxyRegistry {
    function proxies(address) external view returns (address);
}
// File: contracts/SafeMath.sol



pragma solidity ^0.5.0;


/**
 * @title SafeMath
 * @dev Math operations with safety checks that throw on error
 */
library SafeMath {

    /**
    * @dev Multiplies two numbers, throws on overflow.
    */
    function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
        // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
        if (a == 0) {
            return 0;
        }

        c = a * b;
        assert(c / a == b);
        return c;
    }

    /**
    * @dev Integer division of two numbers, truncating the quotient.
    */
    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 a / b;
    }

    /**
    * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }

    /**
    * @dev Adds two numbers, throws on overflow.
    */
    function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
        c = a + b;
        assert(c >= a);
        return c;
    }
}

// File: contracts/Address.sol



pragma solidity ^0.5.0;


/**
 * Utility library of inline functions on addresses
 */
library Address {

    /**
     * Returns whether the target address is a contract
     * @dev This function will return false if invoked during the constructor of a contract,
     * as the code is not actually created until after the constructor finishes.
     * @param account address of the account to check
     * @return whether the target address is a contract
     */
    function isContract(address account) internal view returns (bool) {
        uint256 size;
        // XXX Currently there is no better way to check if there is a contract in an address
        // than to check the size of the code at that address.
        // See https://ethereum.stackexchange.com/a/14016/36603
        // for more details about how this works.
        // TODO Check this again before the Serenity release, because all addresses will be
        // contracts then.
        // solium-disable-next-line security/no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }

}

// File: contracts/Common.sol



pragma solidity ^0.5.0;

/**
    Note: Simple contract to use as base for const vals
*/
contract CommonConstants {

    bytes4 constant internal ERC1155_ACCEPTED = 0xf23a6e61; // bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))
    bytes4 constant internal ERC1155_BATCH_ACCEPTED = 0xbc197c81; // bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))
}

// File: contracts/IERC1155TokenReceiver.sol



pragma solidity ^0.5.0;

/**
    Note: The ERC-165 identifier for this interface is 0x4e2312e0.
*/
interface ERC1155TokenReceiver {
    /**
        @notice Handle the receipt of a single ERC1155 token type.
        @dev An ERC1155-compliant smart contract MUST call this function on the token recipient contract, at the end of a `safeTransferFrom` after the balance has been updated.
        This function MUST return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` (i.e. 0xf23a6e61) if it accepts the transfer.
        This function MUST revert if it rejects the transfer.
        Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller.
        @param _operator  The address which initiated the transfer (i.e. msg.sender)
        @param _from      The address which previously owned the token
        @param _id        The ID of the token being transferred
        @param _value     The amount of tokens being transferred
        @param _data      Additional data with no specified format
        @return           `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
    */
    function onERC1155Received(address _operator, address _from, uint256 _id, uint256 _value, bytes calldata _data) external returns(bytes4);

    /**
        @notice Handle the receipt of multiple ERC1155 token types.
        @dev An ERC1155-compliant smart contract MUST call this function on the token recipient contract, at the end of a `safeBatchTransferFrom` after the balances have been updated.
        This function MUST return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` (i.e. 0xbc197c81) if it accepts the transfer(s).
        This function MUST revert if it rejects the transfer(s).
        Return of any other value than the prescribed keccak256 generated value MUST result in the transaction being reverted by the caller.
        @param _operator  The address which initiated the batch transfer (i.e. msg.sender)
        @param _from      The address which previously owned the token
        @param _ids       An array containing ids of each token being transferred (order and length must match _values array)
        @param _values    An array containing amounts of each token being transferred (order and length must match _ids array)
        @param _data      Additional data with no specified format
        @return           `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
    */
    function onERC1155BatchReceived(address _operator, address _from, uint256[] calldata _ids, uint256[] calldata _values, bytes calldata _data) external returns(bytes4);
}

// File: contracts/IERC1155.sol



pragma solidity ^0.5.0;

import "./ERC165.sol";

/**
    @title ERC-1155 Multi Token Standard
    @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1155.md
    Note: The ERC-165 identifier for this interface is 0xd9b67a26.
 */
interface IERC1155 /* is ERC165 */ {
    /**
        @dev Either `TransferSingle` or `TransferBatch` MUST emit when tokens are transferred, including zero value transfers as well as minting or burning (see "Safe Transfer Rules" section of the standard).
        The `_operator` argument MUST be msg.sender.
        The `_from` argument MUST be the address of the holder whose balance is decreased.
        The `_to` argument MUST be the address of the recipient whose balance is increased.
        The `_id` argument MUST be the token type being transferred.
        The `_value` argument MUST be the number of tokens the holder balance is decreased by and match what the recipient balance is increased by.
        When minting/creating tokens, the `_from` argument MUST be set to `0x0` (i.e. zero address).
        When burning/destroying tokens, the `_to` argument MUST be set to `0x0` (i.e. zero address).
    */
    event TransferSingle(address indexed _operator, address indexed _from, address indexed _to, uint256 _id, uint256 _value);

    /**
        @dev Either `TransferSingle` or `TransferBatch` MUST emit when tokens are transferred, including zero value transfers as well as minting or burning (see "Safe Transfer Rules" section of the standard).
        The `_operator` argument MUST be msg.sender.
        The `_from` argument MUST be the address of the holder whose balance is decreased.
        The `_to` argument MUST be the address of the recipient whose balance is increased.
        The `_ids` argument MUST be the list of tokens being transferred.
        The `_values` argument MUST be the list of number of tokens (matching the list and order of tokens specified in _ids) the holder balance is decreased by and match what the recipient balance is increased by.
        When minting/creating tokens, the `_from` argument MUST be set to `0x0` (i.e. zero address).
        When burning/destroying tokens, the `_to` argument MUST be set to `0x0` (i.e. zero address).
    */
    event TransferBatch(address indexed _operator, address indexed _from, address indexed _to, uint256[] _ids, uint256[] _values);

    /**
        @dev MUST emit when approval for a second party/operator address to manage all tokens for an owner address is enabled or disabled (absense of an event assumes disabled).
    */
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

    /**
        @dev MUST emit when the URI is updated for a token ID.
        URIs are defined in RFC 3986.
        The URI MUST point a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema".
    */
    event URI(string _value, uint256 indexed _id);

    /**
        @notice Transfers `_value` amount of an `_id` from the `_from` address to the `_to` address specified (with safety call).
        @dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
        MUST revert if `_to` is the zero address.
        MUST revert if balance of holder for token `_id` is lower than the `_value` sent.
        MUST revert on any other error.
        MUST emit the `TransferSingle` event to reflect the balance change (see "Safe Transfer Rules" section of the standard).
        After the above conditions are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call `onERC1155Received` on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
        @param _from    Source address
        @param _to      Target address
        @param _id      ID of the token type
        @param _value   Transfer amount
        @param _data    Additional data with no specified format, MUST be sent unaltered in call to `onERC1155Received` on `_to`
    */
    function safeTransferFrom(address _from, address _to, uint256 _id, uint256 _value, bytes calldata _data) external;

    /**
        @notice Transfers `_values` amount(s) of `_ids` from the `_from` address to the `_to` address specified (with safety call).
        @dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
        MUST revert if `_to` is the zero address.
        MUST revert if length of `_ids` is not the same as length of `_values`.
        MUST revert if any of the balance(s) of the holder(s) for token(s) in `_ids` is lower than the respective amount(s) in `_values` sent to the recipient.
        MUST revert on any other error.
        MUST emit `TransferSingle` or `TransferBatch` event(s) such that all the balance changes are reflected (see "Safe Transfer Rules" section of the standard).
        Balance changes and events MUST follow the ordering of the arrays (_ids[0]/_values[0] before _ids[1]/_values[1], etc).
        After the above conditions for the transfer(s) in the batch are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call the relevant `ERC1155TokenReceiver` hook(s) on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
        @param _from    Source address
        @param _to      Target address
        @param _ids     IDs of each token type (order and length must match _values array)
        @param _values  Transfer amounts per token type (order and length must match _ids array)
        @param _data    Additional data with no specified format, MUST be sent unaltered in call to the `ERC1155TokenReceiver` hook(s) on `_to`
    */
    function safeBatchTransferFrom(address _from, address _to, uint256[] calldata _ids, uint256[] calldata _values, bytes calldata _data) external;

    /**
        @notice Get the balance of an account's Tokens.
        @param _owner  The address of the token holder
        @param _id     ID of the Token
        @return        The _owner's balance of the Token type requested
     */
    function balanceOf(address _owner, uint256 _id) external view returns (uint256);

    /**
        @notice Get the balance of multiple account/token pairs
        @param _owners The addresses of the token holders
        @param _ids    ID of the Tokens
        @return        The _owner's balance of the Token types requested (i.e. balance for each (owner, id) pair)
     */
    function balanceOfBatch(address[] calldata _owners, uint256[] calldata _ids) external view returns (uint256[] memory);

    /**
        @notice Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.
        @dev MUST emit the ApprovalForAll event on success.
        @param _operator  Address to add to the set of authorized operators
        @param _approved  True if the operator is approved, false to revoke approval
    */
    function setApprovalForAll(address _operator, bool _approved) external;

    /**
        @notice Queries the approval status of an operator for a given owner.
        @param _owner     The owner of the Tokens
        @param _operator  Address of authorized operator
        @return           True if the operator is approved, false if not
    */
    function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}

// File: contracts/ERC165.sol



pragma solidity ^0.5.0;


/**
 * @title ERC165
 * @dev https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md
 */
interface ERC165 {

    /**
     * @notice Query if a contract implements an interface
     * @param _interfaceId The interface identifier, as specified in ERC-165
     * @dev Interface identification is specified in ERC-165. This function
     * uses less than 30,000 gas.
     */
    function supportsInterface(bytes4 _interfaceId)
    external
    view
    returns (bool);
}