def leaderTraderByVolume(self): token1 = self.params.get('token1', '') token2 = self.params.get('token2', '') # default 24 hours hours = self.params.get('hours', 24) if token1 == '' and token2 == '': return {} else: service = PdexService() tokenService = TokenService() data = service.lastTradingTxInHours(token1, token2, hours) if len(data) == 0: return {} result = {} tokens = tokenService.listTokens() for tx in data: if tx.get('trader_address_str') not in result: result[tx.get('trader_address_str')] = { # 'txs': 0 } # result[tx.get('trader_address_str')]['txs'] += 1 metadata = tx['metadata'] if metadata[ 'TokenIDToSellStr'] == '0000000000000000000000000000000000000000000000000000000000000004': sellToken = 'PRV-' + metadata['TokenIDToSellStr'] else: sellToken = tokens[metadata['TokenIDToSellStr']][ 'name'] + '-' + metadata['TokenIDToSellStr'] if metadata[ 'TokenIDToBuyStr'] == '0000000000000000000000000000000000000000000000000000000000000004': buyToken = 'PRV-' + metadata['TokenIDToBuyStr'] else: buyToken = tokens[metadata['TokenIDToBuyStr']][ 'name'] + '-' + metadata['TokenIDToBuyStr'] sellAmount = metadata['SellAmount'] buyAmount = tx['receive_amount'] if sellToken not in result[tx.get('trader_address_str')]: result[tx.get('trader_address_str')][sellToken] = 0 result[tx.get('trader_address_str')][sellToken] += sellAmount if buyToken not in result[tx.get('trader_address_str')]: result[tx.get('trader_address_str')][buyToken] = 0 result[tx.get('trader_address_str')][buyToken] += buyAmount temp = [] for k, v in result.items(): item = v keys = v.keys() tokenVol1 = item[keys[0]] tokenVol2 = item[keys[1]] item['volume'] = tokenVol1 * tokenVol2 item['trader'] = k temp.append(item) temp = sorted(temp, key=itemgetter('volume'), reverse=True) index = 1 for r in temp: result[index] = r index += 1 return result
def lastHoursVolumeFunc(self, token1, token2, hours, direction): if token1 == '' and token2 == '': return {} else: service = PdexService() tokenService = TokenService() data = service.lastTradingTxInHours(token1, token2, hours) if len(data) == 0: return {} result = {} tokens = tokenService.listTokens() pdeToken = service.getTokens() if not direction: for tx in data: metadata = tx['metadata'] if metadata[ 'TokenIDToSellStr'] == '0000000000000000000000000000000000000000000000000000000000000004': sellToken = 'PRV' sellTokenId = '0000000000000000000000000000000000000000000000000000000000000004' else: sellToken = tokens[ metadata['TokenIDToSellStr']]['name'] sellTokenId = metadata['TokenIDToSellStr'] if metadata[ 'TokenIDToBuyStr'] == '0000000000000000000000000000000000000000000000000000000000000004': buyToken = 'PRV' buyTokenId = '0000000000000000000000000000000000000000000000000000000000000004' else: buyToken = tokens[metadata['TokenIDToBuyStr']]['name'] buyTokenId = metadata['TokenIDToBuyStr'] sellAmount = metadata['SellAmount'] buyAmount = tx['receive_amount'] if sellToken not in result: result[sellToken] = Decimal(0) sellExchangeRate = pdeToken.get(sellTokenId).get( 'exchange_rate') sellAmount = Decimal(sellAmount / Decimal(sellExchangeRate)) result[sellToken] += sellAmount if buyToken not in result: result[buyToken] = Decimal(0) buyExchangeRate = pdeToken.get(buyTokenId).get( 'exchange_rate') buyAmount = Decimal(buyAmount / Decimal(buyExchangeRate)) result[buyToken] += buyAmount for i in result.keys(): result[i] = float(result[i]) else: for tx in data: metadata = tx['metadata'] if metadata[ 'TokenIDToSellStr'] == '0000000000000000000000000000000000000000000000000000000000000004': sellToken = 'PRV' sellTokenId = '0000000000000000000000000000000000000000000000000000000000000004' else: sellToken = tokens[ metadata['TokenIDToSellStr']]['name'] sellTokenId = metadata['TokenIDToSellStr'] if metadata[ 'TokenIDToBuyStr'] == '0000000000000000000000000000000000000000000000000000000000000004': buyToken = 'PRV' buyTokenId = '0000000000000000000000000000000000000000000000000000000000000004' else: buyToken = tokens[metadata['TokenIDToBuyStr']]['name'] buyTokenId = metadata['TokenIDToBuyStr'] sellAmount = metadata['SellAmount'] buyAmount = tx['receive_amount'] if sellToken not in result: result[sellToken] = {'sell': 0, 'buy': 0} if buyToken not in result: result[buyToken] = {'sell': 0, 'buy': 0} sellExchangeRate = pdeToken.get(sellTokenId).get( 'exchange_rate') result[sellToken]['sell'] += Decimal( sellAmount / Decimal(sellExchangeRate)) result[sellToken]['tokenId'] = sellTokenId buyExchangeRate = pdeToken.get(buyTokenId).get( 'exchange_rate') result[buyToken]['buy'] += Decimal( buyAmount / Decimal(buyExchangeRate)) result[buyToken]['tokenId'] = buyTokenId for i in result.keys(): result[i]['buy'] = float(result[i]['buy']) result[i]['sell'] = float(result[i]['sell']) return result