Coverage for src/utils/token_util.py: 0%

61 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-03 19:06 +0000

1from datetime import datetime 

2from time import sleep 

3 

4import pandas as pd 

5 

6from src.api import spl, hive 

7 

8 

9def get_all_tokens(account_name): 

10 all_assets = {} 

11 for token in spl.get_balances(account_name): 

12 all_assets[token["token"]] = token["balance"] 

13 return all_assets 

14 

15 

16def calculate_prices(df, all_tokens, hive_in_dollar): 

17 # Exclude this list because they are not on hive engine. 

18 # Currently, calculate value on hive engine because to be conservative lower values iso of internal SPL market. 

19 exclude_list = [ 

20 'BLDSTONE', 

21 'CREDITS', 

22 'DEC-B', # has no value any more unable to get out of splinterlands 

23 'GLADIUS', 

24 'GOLD', 

25 'GP', 

26 'GRAIN', 

27 'LEGENDARY', 

28 'MERITS', 

29 'TC', 

30 'PURCHASED_ENERGY', 

31 'PWRSTONE', 

32 'VOUCHER-TOTAL' # already done via VOUCHER and VOUCHER-G 

33 'SPSP-OUT' 

34 ] 

35 

36 for token in all_tokens: 

37 if token not in exclude_list: 

38 if token == 'SPSP': 

39 token_market = hive.get_market_with_retry('SPS') 

40 elif token == 'DICE': 

41 token_market = hive.get_market_with_retry('SLDICE') 

42 elif token == 'VOUCHER-G': 

43 token_market = hive.get_market_with_retry('VOUCHER') 

44 else: 

45 token_market = hive.get_market_with_retry(token) 

46 

47 if token_market: 

48 quantity = all_tokens[token] 

49 hive_value = float(token_market["highestBid"]) 

50 value = round(hive_value * hive_in_dollar * quantity, 2) 

51 if quantity: 

52 df[str(token.lower()) + '_qty'] = quantity 

53 df[str(token.lower()) + '_value'] = value 

54 

55 if token == 'CREDITS': 

56 df[str(token.lower()) + '_qty'] = all_tokens[token] 

57 df[str(token.lower()) + '_value'] = round(all_tokens[token] * 0.001, 2) 

58 

59 return df 

60 

61 

62def get_token_value(account): 

63 hive_in_dollar = float(spl.get_prices()['hive']) 

64 all_tokens = get_all_tokens(account) 

65 df = pd.DataFrame({'date': datetime.today().strftime('%Y-%m-%d'), 

66 'account_name': account}, 

67 index=[0]) 

68 df = calculate_prices(df, all_tokens, hive_in_dollar) 

69 df = get_liquidity_pool(df, account, hive_in_dollar) 

70 return df 

71 

72 

73def get_dec_last_price(): 

74 df = pd.DataFrame(hive.get_market_with_retry('DEC'), index=[0]) 

75 return float(df.lastPrice.iloc[0]) 

76 

77 

78def get_liquidity_pool(df, account, hive_in_dollar): 

79 token_pair = "DEC:SPS" 

80 my_shares = hive.get_liquidity_positions(account, token_pair) 

81 dec = 0 

82 sps = 0 

83 value_usd = 0 

84 if my_shares: 

85 dec_qty, sps_qty, total_shares = hive.get_quantity(token_pair) 

86 share_pct = my_shares / total_shares 

87 dec = share_pct * dec_qty 

88 sps = share_pct * sps_qty 

89 

90 # TODO analyze if sleep is enough else we get an service temporary unavailable 

91 sleep(1) 

92 dec_last_price = get_dec_last_price() 

93 value_hive = dec_last_price * dec 

94 value_hive = value_hive * 2 # liquidity pool contain equal amount of dec and sps therefor times 2 

95 value_usd = value_hive * hive_in_dollar 

96 df['liq_pool_dec_qty'] = dec 

97 df['liq_pool_sps_qty'] = sps 

98 df['liq_pool_value'] = value_usd 

99 

100 return df