Explorar o código

add all_token_price

Rony %!s(int64=2) %!d(string=hai) anos
pai
achega
27f80e1f98
Modificáronse 4 ficheiros con 261 adicións e 12 borrados
  1. 127 0
      all_token_price.py
  2. 115 0
      all_token_price_bak.py
  3. 4 12
      parse_price.py
  4. 15 0
      test.py

+ 127 - 0
all_token_price.py

@@ -0,0 +1,127 @@
+import json
+import os
+from pycoingecko import CoinGeckoAPI
+from rediscluster import RedisCluster
+import redis
+import ast
+import requests
+import traceback
+
+#const
+DEFAULT_PRICE = 0.00000000000000001
+
+#redis keys
+TOKENPRICE = 'TOKENPRICE_TO_FETCH'
+R = None
+# REDIS_ONLINE = 'denet-chain-prod.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com'
+# R = RedisCluster(host=REDIS_ONLINE, port=6379, decode_responses=True, skip_full_coverage_check=True)
+env = os.environ['ENV']
+redis_host = os.environ['redis_host']
+if env == 'dev':
+    redis_password = os.environ['redis_password']
+    R = redis.Redis(
+        host=redis_host,
+        port=6379,
+        password='denet#2022',
+        decode_responses=True)
+else:
+    R = RedisCluster(host=redis_host, port=6379, decode_responses=True, skip_full_coverage_check=True)
+
+def get_swap_price(tokens):
+    print('get_swap_price')
+    ret_d = {}
+    for t in tokens:
+        segs = t.split('_')
+        chain = segs[0]
+        contract = segs[1]
+        if chain.strip().upper() == 'BSC':
+            price = get_pancakeswap_price(contract)
+            if price is not None:
+                ret_d[t] = price
+    return ret_d
+
+
+def get_pancakeswap_price(contract):
+    url = f'https://api.pancakeswap.info/api/v2/tokens/{contract}'
+    try:
+        ret = requests.get(url)
+        price = float(ret.json()['data']['price'])
+        print(price)
+        return price
+    except:
+        traceback.print_exc()
+        return None
+
+
+def get_coingecko_price(tokens):
+    print('get_coingecko_price')
+    exceptions = set()
+    ret_d = {}
+    try:
+        cg = CoinGeckoAPI()
+        coin_prices = cg.get_price(ids=list(tokens), vs_currencies='usd')
+        print(coin_prices)
+        for k, v in coin_prices.items():
+            if v.get('usd') is None:
+                exceptions.add(k)
+                continue
+            price = v['usd']
+            ret_d[k] = price
+    except:
+        traceback.print_exc()
+
+    print('get_coingecko_price exception tokens', exceptions)
+    return ret_d
+
+
+def get_tokens():
+    tokens = R.smembers(TOKENPRICE)
+    tokens = [ast.literal_eval(t) for t in tokens]
+    print(tokens)
+    realtime_price_token = set()
+    fixed_or_swap_price_token = set()
+    for t in tokens:
+        if t.get('fixed_price') is None:
+            realtime_price_token.add(t.get('coingecko_id'))
+        else:
+            fixed_or_swap_price_token.add(f"{t.get('chain')}_{t.get('contract')}")
+
+    realtime_price = get_coingecko_price(realtime_price_token) #{'coingecko_id':price}
+    swap_price_token = get_swap_price(fixed_or_swap_price_token) #{'chain_contract': price}
+    return tokens, realtime_price, swap_price_token
+
+def merge_price(tokens, realtime_price, swap_price_token):
+    ret_d = {}
+
+    exceptions = set()
+    for t in tokens:
+        chain = t.get('chain')
+        contract = t.get('contract')
+        coingecko_id = t.get('coingecko_id')
+        fixed_price = t.get('fixed_price')
+
+        now_price = realtime_price.get(coingecko_id)
+        if now_price is None:
+            now_price = swap_price_token.get(f'{chain}_{contract}')
+        if now_price is None:
+            now_price = fixed_price
+
+        if now_price is None:
+            exceptions.add(str(t))
+        else:
+            l = ret_d.get(chain, [])
+            l.append({'contract': contract, 'usdPrice': now_price})
+            ret_d[chain] = l
+    print('merge_price exceptions', exceptions)
+    return ret_d
+
+def save_price(prices):
+    for chain, token_dict in prices.items():
+        R.set(f'{TOKENPRICE}{chain.upper()}', json.dumps({'tokenPrice':token_dict}))
+        print('add', f'{TOKENPRICE}{chain.upper()}', json.dumps({'tokenPrice':token_dict}))
+
+
+if __name__ == '__main__':
+    tokens, realtime_price, swap_price_token = get_tokens()
+    prices = merge_price(tokens, realtime_price, swap_price_token)
+    save_price(prices)

+ 115 - 0
all_token_price_bak.py

@@ -0,0 +1,115 @@
+import json
+from pycoingecko import CoinGeckoAPI
+from rediscluster import RedisCluster
+# from redis import Redis
+import ast
+import requests
+import traceback
+
+#const
+DEFAULT_PRICE = 0.00000000000000001
+
+#redis keys
+TOKENPRICE = 'TOKENPRICE_TO_FETCH'
+
+def get_swap_price(tokens):
+    ret_d = {}
+    for t in tokens:
+        segs = t.split('_')
+        chain = segs[0]
+        contract = segs[1]
+        if chain.strip().upper() == 'bsc':
+            price = get_pancakeswap_price(contract)
+            if price is not None:
+                ret_d[t] = price
+    return ret_d
+
+
+def get_pancakeswap_price(contract):
+    url = f'https://api.pancakeswap.info/api/v2/tokens/{contract}'
+    try:
+        ret = requests.get(url)
+        price = float(ret.json()['data']['price'])
+        print(price)
+        return price
+    except:
+        traceback.print_exc()
+        return None
+
+
+def get_coingecko_price(tokens):
+    exceptions = set()
+    ret_d = {}
+    try:
+        cg = CoinGeckoAPI()
+        coin_prices = cg.get_price(ids=list(tokens), vs_currencies='usd')
+        print(coin_prices)
+        for k, v in coin_prices.items():
+            if v.get('usd') is None:
+                exceptions.add(k)
+                continue
+            price = v['usd']
+            ret_d[k] = price
+    except:
+        traceback.print_exc()
+
+    print('get_coingecko_price exception tokens', exceptions)
+    return ret_d
+
+
+def get_tokens():
+    tokens = r.smembers(TOKENPRICE)
+    tokens = [ast.literal_eval(t) for t in tokens]
+    realtime_price_token = set()
+    fixed_or_swap_price_token = set()
+    for t in tokens:
+        if t.get('fixed_price') is None:
+            realtime_price_token.add(t.get('coingecko_id'))
+        else:
+            fixed_or_swap_price_token.add(f"{t.get('chain')}_{t.get('contract')}")
+
+    realtime_price = get_coingecko_price(realtime_price_token) #{'coingecko_id':price}
+    swap_price_token = get_swap_price(fixed_or_swap_price_token) #{'chain_contract': price}
+    return tokens, realtime_price, swap_price_token
+
+def merge_price(tokens, realtime_price, swap_price_token):
+    ret_d = {}
+
+    exceptions = set()
+    for t in tokens:
+        chain = t.get('chain')
+        contract = t.get('contract')
+        coingecko_id = t.get('coingecko_id')
+        fixed_price = t.get('fixed_price')
+
+        now_price = realtime_price.get(coingecko_id)
+        if now_price is None:
+            now_price = swap_price_token.get(f'{chain}_{contract}')
+        if now_price is None:
+            now_price = fixed_price
+
+        if now_price is None:
+            exceptions.add(t)
+        else:
+            l = ret_d.get(chain, [])
+            l.append({'contract': contract_address, 'usdPrice': price})
+            ret_d[chain] = l
+    print('merge_price exceptions', exceptions)
+    return ret_d
+
+def save_price(prices):
+    for chain, token_dict in prices.items():
+        r.set(f'{TOKENPRICE}{chain.upper()}', json.dumps({'tokenPrice':token_dict}))
+
+def _recover_history_data():
+    #TODO
+    #1. 读取历史数据,修改数据格式,并重新导入
+    #2. 对比历史token记录表,筛选是否有丢失数据的情况
+    #3. 修改api server接口,以新数据结构存储
+    pass
+
+if __name__ == '__main__':
+    tokens, realtime_price, swap_price_token = get_tokens()
+    prices = merge_price(tokens, realtime_price, swap_price_token)
+    save_price(prices)
+

+ 4 - 12
parse_price.py

@@ -1,13 +1,5 @@
-import requests
-from bs4 import BeautifulSoup
+from pycoingecko import CoinGeckoAPI
 
-req = requests.get('https://www.coingecko.com/zh/%E6%95%B0%E5%AD%97%E8%B4%A7%E5%B8%81/bikerush')
-soup = BeautifulSoup(req.content, 'html.parser')
-soup.prettify()
-anchors = soup.findAll('div', class_='tw-text-gray-500 text-normal dark:tw-text-white dark:tw-text-opacity-60 tw-mb-3')
-print(len(anchors))
-res = anchors[0]
-res = res.children
-for r in res:
-    print(type(r))
-    print(r)
+cg = CoinGeckoAPI()
+result = cg.get_price(ids=list(['ethereumpow']), vs_currencies='usd')
+print(result)

+ 15 - 0
test.py

@@ -0,0 +1,15 @@
+import requests
+import traceback
+
+def get_pancakeswap_price(contract):
+    url = f'https://api.pancakeswap.info/api/v2/tokens/{contract}'
+    price = None
+    try:
+        ret = requests.get(url)
+        price = round(float(ret.json()['data']['price']), 2)
+        print(price)
+    except:
+        traceback.print_exc()
+    return price
+
+get_pancakeswap_price('0x25102c5af2d4faa83ddbd36d9f6af5d9c2b84093')