def get_asset_info(asset, at_dt=None): mongo_db = config.mongo_db asset_info = mongo_db.tracked_assets.find_one({'asset': asset}) if asset not in (config.XLT, config.LTC) and at_dt and asset_info['_at_block_time'] > at_dt: #get the asset info at or before the given at_dt datetime for e in reversed(asset_info['_history']): #newest to oldest if e['_at_block_time'] <= at_dt: asset_info = e break else: #asset was created AFTER at_dt asset_info = None if asset_info is None: return None assert asset_info['_at_block_time'] <= at_dt #modify some of the properties of the returned asset_info for LTC and XLT if asset == config.LTC: if at_dt: start_block_index, end_block_index = util.get_block_indexes_for_dates(end_dt=at_dt) asset_info['total_issued'] = util_litecoin.get_ltc_supply(normalize=False, at_block_index=end_block_index) asset_info['total_issued_normalized'] = util_litecoin.normalize_quantity(asset_info['total_issued']) else: asset_info['total_issued'] = util_litecoin.get_ltc_supply(normalize=False) asset_info['total_issued_normalized'] = util_litecoin.normalize_quantity(asset_info['total_issued']) elif asset == config.XLT: #BUG: this does not take end_dt (if specified) into account. however, the deviation won't be too big # as XLT doesn't deflate quickly at all, and shouldn't matter that much since there weren't any/much trades # before the end of the burn period (which is what is involved with how we use at_dt with currently) asset_info['total_issued'] = util.call_jsonrpc_api("get_xlt_supply", abort_on_error=True)['result'] asset_info['total_issued_normalized'] = util_litecoin.normalize_quantity(asset_info['total_issued']) if not asset_info: raise Exception("Invalid asset: %s" % asset) return asset_info
def compile_asset_pair_market_info(): """Compiles the pair-level statistics that show on the View Prices page of litetokenswallet, for instance""" #loop through all open orders, and compile a listing of pairs, with a count of open orders for each pair mongo_db = config.mongo_db end_dt = datetime.datetime.utcnow() start_dt = end_dt - datetime.timedelta(days=1) start_block_index, end_block_index = util.get_block_indexes_for_dates(start_dt=start_dt, end_dt=end_dt) open_orders = util.call_jsonrpc_api("get_orders", { 'filters': [ {'field': 'give_remaining', 'op': '>', 'value': 0}, {'field': 'get_remaining', 'op': '>', 'value': 0}, {'field': 'fee_required_remaining', 'op': '>=', 'value': 0}, {'field': 'fee_provided_remaining', 'op': '>=', 'value': 0}, ], 'status': 'open', 'show_expired': False, }, abort_on_error=True)['result'] pair_data = {} asset_info = {} def get_price(base_quantity_normalized, quote_quantity_normalized): return float(D(quote_quantity_normalized / base_quantity_normalized )) #COMPOSE order depth, lowest ask, and highest bid column data for o in open_orders: (base_asset, quote_asset) = util.assets_to_asset_pair(o['give_asset'], o['get_asset']) pair = '%s/%s' % (base_asset, quote_asset) base_asset_info = asset_info.get(base_asset, mongo_db.tracked_assets.find_one({ 'asset': base_asset })) if base_asset not in asset_info: asset_info[base_asset] = base_asset_info quote_asset_info = asset_info.get(quote_asset, mongo_db.tracked_assets.find_one({ 'asset': quote_asset })) if quote_asset not in asset_info: asset_info[quote_asset] = quote_asset_info pair_data.setdefault(pair, {'open_orders_count': 0, 'lowest_ask': None, 'highest_bid': None, 'completed_trades_count': 0, 'vol_base': 0, 'vol_quote': 0}) #^ highest ask = open order selling base, highest bid = open order buying base #^ we also initialize completed_trades_count, vol_base, vol_quote because every pair inited here may # not have cooresponding data out of the trades_data_by_pair aggregation below pair_data[pair]['open_orders_count'] += 1 base_quantity_normalized = util_litecoin.normalize_quantity(o['give_quantity'] if base_asset == o['give_asset'] else o['get_quantity'], base_asset_info['divisible']) quote_quantity_normalized = util_litecoin.normalize_quantity(o['give_quantity'] if quote_asset == o['give_asset'] else o['get_quantity'], quote_asset_info['divisible']) order_price = get_price(base_quantity_normalized, quote_quantity_normalized) if base_asset == o['give_asset']: #selling base if pair_data[pair]['lowest_ask'] is None or order_price < pair_data[pair]['lowest_ask']: pair_data[pair]['lowest_ask'] = order_price elif base_asset == o['get_asset']: #buying base if pair_data[pair]['highest_bid'] is None or order_price > pair_data[pair]['highest_bid']: pair_data[pair]['highest_bid'] = order_price #COMPOSE volume data (in XLT and LTC), and % change data #loop through all trade volume over the past 24h, and match that to the open orders trades_data_by_pair = mongo_db.trades.aggregate([ {"$match": { "block_time": {"$gte": start_dt, "$lte": end_dt } } }, {"$project": { "base_asset": 1, "quote_asset": 1, "base_quantity_normalized": 1, #to derive base volume "quote_quantity_normalized": 1 #to derive quote volume }}, {"$group": { "_id": {"base_asset": "$base_asset", "quote_asset": "$quote_asset"}, "vol_base": {"$sum": "$base_quantity_normalized"}, "vol_quote": {"$sum": "$quote_quantity_normalized"}, "count": {"$sum": 1}, }} ]) trades_data_by_pair = [] if not trades_data_by_pair['ok'] else trades_data_by_pair['result'] for e in trades_data_by_pair: pair = '%s/%s' % (e['_id']['base_asset'], e['_id']['quote_asset']) pair_data.setdefault(pair, {'open_orders_count': 0, 'lowest_ask': None, 'highest_bid': None}) #^ initialize an empty pair in the event there are no open orders for that pair, but there ARE completed trades for it pair_data[pair]['completed_trades_count'] = e['count'] pair_data[pair]['vol_base'] = e['vol_base'] pair_data[pair]['vol_quote'] = e['vol_quote'] #compose price data, relative to LTC and XLT mps_xlt_ltc, xlt_ltc_price, ltc_xlt_price = get_price_primatives() for pair, e in pair_data.iteritems(): base_asset, quote_asset = pair.split('/') _24h_vol_in_ltc = None _24h_vol_in_xlt = None #derive asset price data, expressed in LTC and XLT, for the given volumes if base_asset == config.XLT: _24h_vol_in_xlt = e['vol_base'] _24h_vol_in_ltc = util_litecoin.round_out(e['vol_base'] * xlt_ltc_price) if xlt_ltc_price else 0 elif base_asset == config.LTC: _24h_vol_in_xlt = util_litecoin.round_out(e['vol_base'] * ltc_xlt_price) if ltc_xlt_price else 0 _24h_vol_in_ltc = e['vol_base'] else: #base is not XLT or LTC price_summary_in_xlt, price_summary_in_ltc, price_in_xlt, price_in_ltc, aggregated_price_in_xlt, aggregated_price_in_ltc = \ get_xlt_ltc_price_info(base_asset, mps_xlt_ltc, xlt_ltc_price, ltc_xlt_price, with_last_trades=0, start_dt=start_dt, end_dt=end_dt) if price_in_xlt: _24h_vol_in_xlt = util_litecoin.round_out(e['vol_base'] * price_in_xlt) if price_in_ltc: _24h_vol_in_ltc = util_litecoin.round_out(e['vol_base'] * price_in_ltc) if _24h_vol_in_xlt is None or _24h_vol_in_ltc is None: #the base asset didn't have price data against LTC or XLT, or both...try against the quote asset instead price_summary_in_xlt, price_summary_in_ltc, price_in_xlt, price_in_ltc, aggregated_price_in_xlt, aggregated_price_in_ltc = \ get_xlt_ltc_price_info(quote_asset, mps_xlt_ltc, xlt_ltc_price, ltc_xlt_price, with_last_trades=0, start_dt=start_dt, end_dt=end_dt) if _24h_vol_in_xlt is None and price_in_xlt: _24h_vol_in_xlt = util_litecoin.round_out(e['vol_quote'] * price_in_xlt) if _24h_vol_in_ltc is None and price_in_ltc: _24h_vol_in_ltc = util_litecoin.round_out(e['vol_quote'] * price_in_ltc) pair_data[pair]['24h_vol_in_{}'.format(config.XLT.lower())] = _24h_vol_in_xlt #might still be None pair_data[pair]['24h_vol_in_{}'.format(config.LTC.lower())] = _24h_vol_in_ltc #might still be None #get % change stats -- start by getting the first trade directly before the 24h period starts prev_trade = mongo_db.trades.find({ "base_asset": base_asset, "quote_asset": quote_asset, "block_time": {'$lt': start_dt}}).sort('block_time', pymongo.DESCENDING).limit(1) latest_trade = mongo_db.trades.find({ "base_asset": base_asset, "quote_asset": quote_asset}).sort('block_time', pymongo.DESCENDING).limit(1) if not prev_trade.count(): #no previous trade before this 24hr period pair_data[pair]['24h_pct_change'] = None else: prev_trade = prev_trade[0] latest_trade = latest_trade[0] prev_trade_price = get_price(prev_trade['base_quantity_normalized'], prev_trade['quote_quantity_normalized']) latest_trade_price = get_price(latest_trade['base_quantity_normalized'], latest_trade['quote_quantity_normalized']) pair_data[pair]['24h_pct_change'] = ((latest_trade_price - prev_trade_price) / prev_trade_price) * 100 pair_data[pair]['last_updated'] = end_dt #print "PRODUCED", pair, pair_data[pair] mongo_db.asset_pair_market_info.update( {'base_asset': base_asset, 'quote_asset': quote_asset}, {"$set": pair_data[pair]}, upsert=True) #remove any old pairs that were not just updated mongo_db.asset_pair_market_info.remove({'last_updated': {'$lt': end_dt}}) logging.info("Recomposed 24h trade statistics for %i asset pairs: %s" % (len(pair_data), ', '.join(pair_data.keys())))
def decorate_message(message, for_txn_history=False): #insert custom fields in certain events... #even invalid actions need these extra fields for proper reporting to the client (as the reporting message # is produced via PendingActionViewModel.calcText) -- however make it able to deal with the queried data not existing in this case assert '_category' in message mongo_db = config.mongo_db if for_txn_history: message['_command'] = 'insert' #history data doesn't include this block_index = message[ 'block_index'] if 'block_index' in message else message[ 'tx1_block_index'] message['_block_time'] = get_block_time(block_index) message['_tx_index'] = message[ 'tx_index'] if 'tx_index' in message else message.get( 'tx1_index', None) if message['_category'] in [ 'bet_expirations', 'order_expirations', 'bet_match_expirations', 'order_match_expirations' ]: message[ '_tx_index'] = 0 #add tx_index to all entries (so we can sort on it secondarily in history view), since these lack it if message['_category'] in ['credits', 'debits']: #find the last balance change on record bal_change = mongo_db.balance_changes.find_one( { 'address': message['address'], 'asset': message['asset'] }, sort=[("block_time", pymongo.DESCENDING)]) message['_quantity_normalized'] = abs( bal_change['quantity_normalized']) if bal_change else None message['_balance'] = bal_change['new_balance'] if bal_change else None message['_balance_normalized'] = bal_change[ 'new_balance_normalized'] if bal_change else None if message['_category'] in [ 'orders', ] and message['_command'] == 'insert': get_asset_info = mongo_db.tracked_assets.find_one( {'asset': message['get_asset']}) give_asset_info = mongo_db.tracked_assets.find_one( {'asset': message['give_asset']}) message['_get_asset_divisible'] = get_asset_info[ 'divisible'] if get_asset_info else None message['_give_asset_divisible'] = give_asset_info[ 'divisible'] if give_asset_info else None if message['_category'] in [ 'order_matches', ] and message['_command'] == 'insert': forward_asset_info = mongo_db.tracked_assets.find_one( {'asset': message['forward_asset']}) backward_asset_info = mongo_db.tracked_assets.find_one( {'asset': message['backward_asset']}) message['_forward_asset_divisible'] = forward_asset_info[ 'divisible'] if forward_asset_info else None message['_backward_asset_divisible'] = backward_asset_info[ 'divisible'] if backward_asset_info else None if message['_category'] in [ 'orders', 'order_matches', ]: message['_ltc_below_dust_limit'] = ( ('forward_asset' in message and message['forward_asset'] == config.LTC and message['forward_quantity'] <= config.ORDER_LTC_DUST_LIMIT_CUTOFF) or ('backward_asset' in message and message['backward_asset'] == config.LTC and message['backward_quantity'] <= config.ORDER_LTC_DUST_LIMIT_CUTOFF)) if message['_category'] in ['dividends', 'sends', 'callbacks']: asset_info = mongo_db.tracked_assets.find_one( {'asset': message['asset']}) message['_divisible'] = asset_info['divisible'] if asset_info else None if message['_category'] in [ 'issuances', ]: message['_quantity_normalized'] = util_litecoin.normalize_quantity( message['quantity'], message['divisible']) return message
'credits', 'debits', ]: actionName = 'credit' if msg[ 'category'] == 'credits' else 'debit' address = msg_data['address'] asset_info = mongo_db.tracked_assets.find_one( {'asset': msg_data['asset']}) if asset_info is None: logging.warn( "Credit/debit of %s where asset ('%s') does not exist. Ignoring..." % (msg_data['quantity'], msg_data['asset'])) continue quantity = msg_data['quantity'] if msg[ 'category'] == 'credits' else -msg_data['quantity'] quantity_normalized = util_litecoin.normalize_quantity( quantity, asset_info['divisible']) #look up the previous balance to go off of last_bal_change = mongo_db.balance_changes.find_one( { 'address': address, 'asset': asset_info['asset'] }, sort=[("block_index", pymongo.DESCENDING), ("_id", pymongo.DESCENDING)]) if last_bal_change \ and last_bal_change['block_index'] == cur_block_index: #modify this record, as we want at most one entry per block index for each (address, asset) pair last_bal_change['quantity'] += quantity last_bal_change[
def parse_issuance(db, message, cur_block_index, cur_block): if message['status'] != 'valid': return def modify_extended_asset_info(asset, description): """adds an asset to asset_extended_info collection if the description is a valid json link. or, if the link is not a valid json link, will remove the asset entry from the table if it exists""" if util.is_valid_url(description, suffix='.json', allow_no_protocol=True): db.asset_extended_info.update( {'asset': asset}, { '$set': { 'info_url': description, 'info_status': 'needfetch', 'fetch_info_retry': 0, # retry ASSET_MAX_RETRY times to fetch info from info_url 'info_data': {}, 'errors': [] } }, upsert=True) #^ valid info_status settings: needfetch, valid, invalid, error #additional fields will be added later in events, once the asset info is pulled else: db.asset_extended_info.remove({'asset': asset}) #remove any saved asset image data imagePath = os.path.join(config.DATA_DIR, config.SUBDIR_ASSET_IMAGES, asset + '.png') if os.path.exists(imagePath): os.remove(imagePath) tracked_asset = db.tracked_assets.find_one({'asset': message['asset']}, { '_id': 0, '_history': 0 }) #^ pulls the tracked asset without the _id and history fields. This may be None if message['locked']: #lock asset assert tracked_asset is not None db.tracked_assets.update({'asset': message['asset']}, { "$set": { '_at_block': cur_block_index, '_at_block_time': cur_block['block_time_obj'], '_change_type': 'locked', 'locked': True, }, "$push": { '_history': tracked_asset } }, upsert=False) logging.info("Locking asset %s" % (message['asset'], )) elif message['transfer']: #transfer asset assert tracked_asset is not None db.tracked_assets.update({'asset': message['asset']}, { "$set": { '_at_block': cur_block_index, '_at_block_time': cur_block['block_time_obj'], '_change_type': 'transferred', 'owner': message['issuer'], }, "$push": { '_history': tracked_asset } }, upsert=False) logging.info("Transferring asset %s to address %s" % (message['asset'], message['issuer'])) elif message[ 'quantity'] == 0 and tracked_asset is not None: #change description db.tracked_assets.update({'asset': message['asset']}, { "$set": { '_at_block': cur_block_index, '_at_block_time': cur_block['block_time_obj'], '_change_type': 'changed_description', 'description': message['description'], }, "$push": { '_history': tracked_asset } }, upsert=False) modify_extended_asset_info(message['asset'], message['description']) logging.info("Changing description for asset %s to '%s'" % (message['asset'], message['description'])) else: #issue new asset or issue addition qty of an asset if not tracked_asset: #new issuance tracked_asset = { '_change_type': 'created', '_at_block': cur_block_index, #the block ID this asset is current for '_at_block_time': cur_block['block_time_obj'], #^ NOTE: (if there are multiple asset tracked changes updates in a single block for the same # asset, the last one with _at_block == that block id in the history array is the # final version for that asset at that block 'asset': message['asset'], 'owner': message['issuer'], 'description': message['description'], 'divisible': message['divisible'], 'locked': False, 'total_issued': message['quantity'], 'total_issued_normalized': util_litecoin.normalize_quantity(message['quantity'], message['divisible']), '_history': [] #to allow for block rollbacks } db.tracked_assets.insert(tracked_asset) logging.info("Tracking new asset: %s" % message['asset']) modify_extended_asset_info(message['asset'], message['description']) else: #issuing additional of existing asset assert tracked_asset is not None db.tracked_assets.update({'asset': message['asset']}, { "$set": { '_at_block': cur_block_index, '_at_block_time': cur_block['block_time_obj'], '_change_type': 'issued_more', }, "$inc": { 'total_issued': message['quantity'], 'total_issued_normalized': util_litecoin.normalize_quantity(message['quantity'], message['divisible']) }, "$push": { '_history': tracked_asset } }, upsert=False) logging.info("Adding additional %s quantity for asset %s" % (util_litecoin.normalize_quantity( message['quantity'], message['divisible']), message['asset'])) return True