def process_kline_common(base_table, target_table, process_obj, pair): #print("base: %s, target: %s, pair: %s" % (str(base_table), str(target_table), pair)) k_last = session.query(target_table).filter( target_table.ex_pair == pair).order_by( target_table.timestamp.desc()).limit(1).first() k = process_obj(k_last) if k_last is None: # if str(base_table) == "<class 'app.models.StSwapTick'>": last_time = datetime.datetime.utcnow() - datetime.timedelta( days=365) else: last_time = k_last.timestamp #print("last time: %s" % (last_time)) ticks = session.query(base_table).filter( base_table.ex_pair == pair, base_table.timestamp >= last_time).order_by( base_table.id).all() for t in ticks: k.process_tick(t) for r in k.get_k_data(): if k_last is not None and k_last.timestamp == r['start_time']: session.query(target_table).filter_by( timestamp=k_last.timestamp, ex_pair=pair).delete() session.add(target_table(ex_pair=pair, k_open=r['k_open'], k_close=r['k_close'], \ k_high=r['k_high'], k_low=r['k_low'], timestamp=r['start_time'], \ block_num=r['block_num'], volume=r['volume']))
def _updateSinglePair(self, address, start_block_num, ex_pair): precision = 10**8 try: #cursor.execute(f"select event_arg,block_num from bl_tx_events where contract_address = '{address}' and event_name='Exchanged' and block_num > {start_block_num}") #results = cursor.fetchall() results = session.query(BlTxEvents.event_arg,BlTxEvents.block_num). \ filter( BlTxEvents.contract_address==address, BlTxEvents.event_name=='Exchanged', BlTxEvents.block_num>start_block_num).\ all() lastBlockTime = 0 lastBlockNum = 0 for r in results: exchange = json.loads(r[0]) exchange['buy_asset'] = self.addr2Token(exchange['buy_asset']) exchange['sell_asset'] = self.addr2Token( exchange['sell_asset']) if exchange['buy_asset'] == 'XWC': price = exchange['sell_amount'] / exchange['buy_amount'] volume = exchange['buy_amount'] / precision elif exchange['sell_asset'] == 'XWC': price = exchange['buy_amount'] / exchange['sell_amount'] volume = exchange['sell_amount'] / precision else: price = 0 volume = 0 exchange['buy_amount'] = exchange['buy_amount'] / precision exchange['sell_amount'] = exchange['sell_amount'] / precision exchange['fee'] = exchange['fee'] / precision if lastBlockNum == 0 or lastBlockNum != r[1]: block = session.query(BlBlock.block_time).filter( BlBlock.block_num == r[1]).first() lastBlockNum = r[1] lastBlockTime = block.block_time session.add( StSwapTick(timestamp=lastBlockTime, ex_pair=ex_pair, buy_asset=exchange['buy_asset'], sell_asset=exchange['sell_asset'], buy_amount=exchange['buy_amount'], sell_amount=exchange['sell_amount'], fee=exchange['fee'], block_num=lastBlockNum, price=price, volume=volume)) except Exception as e: print(str(e)) return
def updateLiquidity(self): lastBlock = 4953249 lastBlockRecord = session.query(StSwapStat.swap_value).filter( StSwapStat.swap_stat == 'liquidity_scan_block').first() if lastBlockRecord is not None: blockNum = int(lastBlockRecord[0]) if blockNum > lastBlock: lastBlock = blockNum currentBlock = self._xwc_api.get_block_height() for p, c in self.pairs.items(): self._updateSinglePairLiqiuidy(lastBlock, p, c, currentBlock) session.query(StSwapStat).filter( StSwapStat.swap_stat == 'liquidity_scan_block').delete() session.add( StSwapStat(swap_stat='liquidity_scan_block', swap_value=currentBlock)) session.commit() print('updateLiquidity committed')
def login(): key = request.json.get("key") password = request.json.get("password") if not all([key, password]): return render_failed("", enums.param_err) user = session.query(User).filter( or_(User.mobile == key, User.user_name == key), User.password == code.generate_md5( current_app.config.get("SALT") + password)).first() if not user: return render_failed("", enums.account_password_error) return render_success({"X-AUTH-TOKEN": code.encode_auth_token(user.id)})
def updateTick(self): last_tick = session.query( func.max(StSwapTick.block_num).label('block_num')).first() if last_tick.block_num is None: print("first time") start_block_num = 4992608 else: start_block_num = int(last_tick.block_num) print(f'update tick - start block: {start_block_num}') for k, v in self.pairs.items(): self._updateSinglePair(v, start_block_num, k) session.commit() print('tick updated')
def register(): user_name = request.json.get("user_name") password = request.json.get("password") mobile = request.json.get("mobile") sms_code = request.json.get("sms_code") if not all([user_name, password, mobile, sms_code]): return render_failed("", enums.param_err) # ts_code = ts.get(enums.register_sms_key + mobile) # if not ts_code: # return render_failed("", enums.sms_code_valid) # if ts_code.decode() != sms_code: # return render_failed("", enums.sms_code_err) exist_user = session.query(User).filter(User.mobile == mobile).first() if exist_user: return render_failed("", enums.mobile_exist) user = User( user_name=user_name, mobile=mobile, password=code.generate_md5(current_app.config.get("SALT") + password)) session.add(user) session.commit() return render_success({"X-AUTH-TOKEN": code.encode_auth_token(user.id)})
def _updateSinglePairLiqiuidy(self, startBlock, pair, contract, lastBlock): tokens = pair.split('_') tokens[0] = tokens[0].upper() tokens[1] = tokens[1].upper() #events = session.query(BlTxEvents.event_name,BlTxEvents.event_arg,BlTxEvents.block_num,BlBlock.block_time).\ #join(BlBlock, BlBlock.block_num==BlTxEvents.block_num).\ #filter(\ #BlTxEvents.block_num>startBlock, #BlTxEvents.event_name.in_(('Exchanged','LiquidityAdded','LiquidityRemoved')), #BlTxEvents.contract_address==contract).\ #order_by(BlTxEvents.block_num).all() lastRecord = session.query(StSwapLiquidity).filter( StSwapLiquidity.tp_name == pair).order_by( StSwapLiquidity.stat_time.desc()).first() currentRecord = { 'stat_time': 0, 'token1_amount': 0, 'token2_amount': 0, 'block_num': 0 } if lastRecord is not None: currentRecord['stat_time'] = lastRecord.stat_time currentRecord['token1_amount'] = lastRecord.token1_amount currentRecord['token2_amount'] = lastRecord.token2_amount today = datetime.date.today() today = datetime.datetime(today.year, today.month, today.day) if currentRecord['stat_time'] != today: currentRecord['stat_time'] = today events = self._xwc_api.get_contract_events(contract, startBlock, lastBlock - startBlock) for e in events: if currentRecord['block_num'] != e['block_num']: currentRecord['block_num'] = e['block_num'] block = session.query(BlBlock.block_time).filter( BlBlock.block_num == e['block_num']).first() blockTime = block[0] blockDay = datetime.datetime(blockTime.year, blockTime.month, blockTime.day) if currentRecord['stat_time'] == 0: currentRecord['stat_time'] = blockDay elif currentRecord['stat_time'] != blockDay and currentRecord[ 'token1_amount'] > 0 and currentRecord[ 'token2_amount'] > 0: # TODO, commit record and reset currentRecord session.query(StSwapLiquidity).filter( StSwapLiquidity.tp_name == pair, StSwapLiquidity.stat_time == currentRecord['stat_time']).delete() session.add( StSwapLiquidity( tp_name=pair, token1_name=tokens[0], token2_name=tokens[1], token1_amount=currentRecord['token1_amount'], token2_amount=currentRecord['token2_amount'], stat_time=currentRecord['stat_time'])) currentRecord['stat_time'] = blockDay if e['event_name'] == 'LiquidityAdded': liquidityChange = json.loads(e['event_arg']) currentRecord['token1_amount'] += int( liquidityChange[self.token2Addr(tokens[0])]) currentRecord['token2_amount'] += int( liquidityChange[self.token2Addr(tokens[1])]) elif e['event_name'] == 'LiquidityRemoved': liquidityChange = json.loads(e['event_arg']) currentRecord['token1_amount'] -= int( liquidityChange[self.token2Addr(tokens[0])]) currentRecord['token2_amount'] -= int( liquidityChange[self.token2Addr(tokens[1])]) elif e['event_name'] == 'Exchanged': liquidityChange = json.loads(e['event_arg']) liquidityChange['buy_asset'] = self.addr2Token( liquidityChange['buy_asset']) if tokens[0] == liquidityChange['buy_asset']: currentRecord['token1_amount'] -= int( liquidityChange['buy_amount']) currentRecord['token2_amount'] += int( liquidityChange['sell_amount']) else: currentRecord['token2_amount'] -= int( liquidityChange['buy_amount']) currentRecord['token1_amount'] += int( liquidityChange['sell_amount']) else: continue if currentRecord['token1_amount'] > 0 and currentRecord[ 'token2_amount'] > 0: session.query(StSwapLiquidity).filter( StSwapLiquidity.tp_name == pair, StSwapLiquidity.stat_time == currentRecord['stat_time']).delete() session.add( StSwapLiquidity(tp_name=pair, token1_name=tokens[0], token2_name=tokens[1], token1_amount=currentRecord['token1_amount'], token2_amount=currentRecord['token2_amount'], stat_time=currentRecord['stat_time']))