contract ERC1155SaleNonceHolder is OwnableOperatorRole {
    /// @notice Token nonces.
    /// @dev keccak256(token, owner, tokenId) => nonce
    mapping(bytes32 => uint256) public nonces;

    /// @notice The amount of selled tokens for the tokenId.
    /// @dev keccak256(token, owner, tokenId, nonce) => completed amount
    mapping(bytes32 => uint256) public completed;

    /// @notice Get nonce value for the token.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @return The nonce value.
    function getNonce(address token, uint256 tokenId, address owner) view public returns (uint256) {
        return nonces[getNonceKey(token, tokenId, owner)];
    }

    /// @notice Sets new nonce value for the token. Can only be called by the operator.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @param nonce - The new value for the nonce.
    function setNonce(address token, uint256 tokenId, address owner, uint256 nonce) public onlyOperator {
        nonces[getNonceKey(token, tokenId, owner)] = nonce;
    }

    /// @notice Encode the token info to use as a key for `nonces` mapping.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @return Encoded key for the token.
    function getNonceKey(address token, uint256 tokenId, address owner) pure public returns (bytes32) {
        return keccak256(abi.encodePacked(token, tokenId, owner));
    }

    /// @notice Get the amount of selled tokens.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @param nonce - The nonce value of the sale.
    /// @return Selled tokens count for the sale with specific nonce.
    function getCompleted(address token, uint256 tokenId, address owner, uint256 nonce) view public returns (uint256) {
        return completed[getCompletedKey(token, tokenId, owner, nonce)];
    }

    /// @notice Sets the new amount of selled tokens. Can be called only by the contract operator.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @param nonce - The nonce value of the sale.
    /// @param _completed - The new completed value to set.
    function setCompleted(address token, uint256 tokenId, address owner, uint256 nonce, uint256 _completed) public onlyOperator {
        completed[getCompletedKey(token, tokenId, owner, nonce)] = _completed;
    }

    /// @notice Encode order key to use as a key of `completed` mapping.
    /// @param token - Token's contract address.
    /// @param tokenId - Token id.
    /// @param owner - The address of the token owner.
    /// @param nonce - The nonce value of the sale.
    /// @return Encoded key.
    function getCompletedKey(address token, uint256 tokenId, address owner, uint256 nonce) pure public returns (bytes32) {
        return keccak256(abi.encodePacked(token, tokenId, owner, nonce));
    }
}
/// @title ERC1155SaleNonceHolder
/// @notice The contract manages nonce values for the sales.
function getCompleted(address token, uint256 tokenId, address owner, uint256 nonce) view public returns (uint256) {
    return completed[getCompletedKey(token, tokenId, owner, nonce)];
}

/// @notice Get the amount of selled tokens.
/// @param token - Token's contract address.
/// @param tokenId - Token id.
/// @param owner - The address of the token owner.
/// @param nonce - The nonce value of the sale.
/// @return Selled tokens count for the sale with specific nonce.