forked from OsmosisIncentivesCommittee/OsmoIncentives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
95 lines (79 loc) · 2.32 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
import json
import urllib.request
import datetime
from typing import Any, Callable
import Params
import time
def load_json_(url : str) -> Any:
time.sleep(1)
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:
print("loading url: ", url)
r = load_json_(url)
query_cache[url] = r
except:
print("retrying: ", url)
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()]
def categorize(l : list[str]) -> str:
(base, asset) = based(l)
if base == "OSMO":
if asset in Params.Stables:
return "OSMO_STABLE"
if asset in Params.Majors:
return "OSMO_MAJOR"
else:
return "OSMO_MINOR"
elif base in Params.Stables:
if asset in Params.Majors:
return "MAJOR_STABLE"
if asset in Params.Stables:
return "STABLESWAP"
return "OTHERS"
def based(l : list[str]) -> tuple[str, str]:
a = l[0]
b = l[1]
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 a == "DAI":
return ("DAI",b)
elif b == "DAI":
return ("DAI",a)
elif a == "EEUR":
return ("EEUR", b)
elif b == "EEUR":
return ("EEUR", a)
elif a == "ATOM":
return ("ATOM",b)
elif b == "ATOM":
return ("ATOM",a)
print("assets not based? : ", l)
return ("","")