-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.py
130 lines (116 loc) · 3.55 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import urllib.request
import datetime
from typing import Any, Callable
import Params
import time
def load_json_(url : str) -> Any:
return json.loads(urllib.request.urlopen(urllib.request.Request(url)).read().decode('utf-8'))
def cached_call(cache : dict[str, Any], key : str, f : Callable[[],Any]):
r = cache.get(key)
if r == None:
r = f()
cache[key] = r
return r
query_cache : dict[str, Any] = {}
def load_json(url : str) -> Any:
r = query_cache.get(url)
while r == None:
try:
time.sleep(1)
print("loading url: ", url)
r = load_json_(url)
query_cache[url] = r
except Exception as e:
print("retrying: ", url, e)
pass
return r
def parse_percent(s : str) -> float:
return float(s[:-1])/100
def parse_start_time(s: str) -> datetime.datetime:
return datetime.datetime.strptime(s[:19],"%Y-%m-%dT%H:%M:%S")
def days_from_now(n : int) -> datetime.datetime:
return datetime.datetime.now() + datetime.timedelta(days=7)
def write_csv(name : str, lines : list[str]) -> None:
with open(name, "w+") as f:
f.write("\n".join(lines))
def read_csv(name : str) -> list[list[str]]:
with open(name, "r") as f:
return [x.strip().split(",") for x in f.readlines()]
#Sorts pools into categories based on their constituent assets
def categorize(l : list[str]) -> str:
(base, asset) = based(l)
if base == "OSMO":
if asset[-4:] == "OSMO":
return "LST"
if asset[:4] == "OSMO":
return "LST"
if asset in Params.Stables:
return "OSMO_STABLE"
if asset in Params.Majors:
return "OSMO_MAJOR"
else:
return "OSMO_MINOR"
# Note: This will recognise LSDs as composability pool too. Would need to revise if any (except stOSMO/OSMO) were incentivised.
elif base[:4] == asset[:4]:
if base[:4] in Params.Stables:
return "COMPOSABILITY"
if asset[:4] in Params.Stables:
return "COMPOSABILITY"
else:
return "LST"
elif base[-4:] == asset[-4:]:
if base[:4] in Params.Stables:
return "COMPOSABILITY"
if asset[:4] in Params.Stables:
return "COMPOSABILITY"
else:
return "LST"
elif base in Params.Stables:
return "STABLE_STABLE"
else:
return "NO_CATEGORY_MATCHED"
#Defines what the base pair of the pool is for display and categorisation purposes
def based(l : list[str]) -> tuple[str, str, str]:
a = l[0]
b = l[1]
try:
c = l[2]
except IndexError:
c = "None"
if a == "OSMO":
return ("OSMO",b)
elif b == "OSMO":
return ("OSMO",a)
elif a == "USDC":
return ("USDC",b)
elif b == "USDC":
return ("USDC",a)
elif c == "USDC":
return ("USDC", "3pool")
elif a == "BUSD":
return ("BUSD",b)
elif b == "BUSD":
return ("BUSD",a)
elif a == "USDT":
return ("USDT",b)
elif b == "USDT":
return ("USDT",a)
elif a == "DAI":
if c == "IST":
return ("DAI","3pool")
return ("DAI",b)
elif b == "DAI":
return ("DAI",a)
elif c == "DAI":
return ("DAI", "3pool")
elif a == "ATOM":
return ("ATOM",b)
elif b == "ATOM":
return ("ATOM",a)
elif a[-4:] == b[-4:]:
ordering = (a,b)
shorter, longer = sorted(ordering, key=len)
return (shorter, longer)
print("assets not based? : ", l)
return (a,b)