-
Notifications
You must be signed in to change notification settings - Fork 3
/
init_database.py
157 lines (148 loc) · 5.16 KB
/
init_database.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# pylint: disable=broad-except, unspecified-encoding, too-many-locals
"""
╔╗ ┬┌┬┐┌─┐┬ ┬┌─┐┬─┐┌─┐┌─┐ ╔╦╗┌─┐─┐ ┬ ╦ ╦─┐ ┬
╠╩╗│ │ └─┐├─┤├─┤├┬┘├┤ └─┐ ║║├┤ ┌┴┬┘ ║ ║┌┴┬┘
╚═╝┴ ┴ └─┘┴ ┴┴ ┴┴└─└─┘└─┘ ═╩╝└─┘┴ └─ ╚═╝┴ └─
Bitshares Decentralized Exchange User Experience
Initalize database with rpc queried data
"""
# STANDARD MOUDLES
import contextlib
import json
import time
import asyncio
# BITSHARES DEX UX MODULES
from database import Sql
from rpc import get_max_object, rpc_get_objects, wss_handshake
from sql_utils import name_id_lookup
BATCH = 100
def assets():
"""
initialize the assets database
"""
sql = Sql()
rpc = await wss_handshake()
max_object = await get_max_object(rpc, "1.3.")
query = "SELECT id FROM assets;" #'SELECT * FROM nodes'
# 'SELECT * FROM 1.3'
max_object_sql = sql.execute(query)
# print(max_object_sql)
max_object_sql = int(
max(max_object_sql, key=lambda x: int(x["id"][4:]))["id"][4:]
if max_object_sql
else -1
)
get_objs = {}
for batch_n in range(max_object_sql + 1, max_object + BATCH, BATCH):
get_objs = {
**get_objs,
**await rpc_get_objects(
rpc, [f"1.3.{str(n)}" for n in range(batch_n, batch_n + 100)]
),
}
time.sleep(0.5)
inserts = []
for token, get_obj in get_objs.items():
desc = ""
with contextlib.suppress(Exception):
desc = json.loads(get_obj["options"]["description"])["short_name"]
inserts.append(
(
"INSERT INTO assets VALUES (?,?,?,?,?,?,?,?,?) ",
(
token,
get_obj["dynamic_asset_data_id"],
get_obj.get("for_liquidity_pool", ""),
get_obj.get("bitasset_data_id", ""),
get_obj["symbol"],
get_obj["precision"],
get_obj["options"]["market_fee_percent"],
get_obj["options"]["extensions"].get(
"taker_fee_percent",
get_obj["options"]["market_fee_percent"],
),
desc,
),
)
)
if inserts:
queries = []
for update in inserts:
dml = {"query": update[0], "values": update[1]}
queries.append(dml)
sql.execute(queries)
def pools():
"""
initialize the pools database
"""
sql = Sql()
rpc = await wss_handshake()
max_object = await get_max_object(rpc, "1.19.")
query = "SELECT id FROM pools;"
max_object_sql = sql.execute(query)
# print(max_object_sql)
max_object_sql = int(
max(max_object_sql, key=lambda x: int(x["id"][5:]))["id"][5:]
if max_object_sql
else -1
)
get_objs = {}
for batch_n in range(max_object_sql + 1, max_object + BATCH, BATCH):
get_objs = {
**get_objs,
**await rpc_get_objects(
rpc, [f"1.19.{str(n)}" for n in range(batch_n, batch_n + 100)]
),
}
time.sleep(0.5)
inserts = []
for token, get_obj in get_objs.items():
query = [
{
"query": "SELECT symbol, precision FROM assets WHERE id=?",
"values": (get_obj["asset_a"],),
}
]
asset_a_data = sql.execute(query)[0]
query = [
{
"query": "SELECT symbol, precision FROM assets WHERE id=?",
"values": (get_obj["asset_b"],),
}
]
asset_b_data = sql.execute(query)[0]
asset_a_name = asset_a_data["symbol"]
asset_b_name = asset_b_data["symbol"]
asset_a_prec = asset_a_data["precision"]
asset_b_prec = asset_b_data["precision"]
share_asset_name = name_id_lookup(sql, get_obj["share_asset"])
inserts.append(
(
"INSERT INTO pools VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ",
(
token,
get_obj["asset_a"],
get_obj["asset_b"],
asset_a_name,
asset_b_name,
float(int(get_obj["balance_a"]) / 10**asset_a_prec),
float(int(get_obj["balance_b"]) / 10**asset_b_prec),
get_obj["share_asset"],
share_asset_name,
get_obj["taker_fee_percent"],
get_obj["withdrawal_fee_percent"],
int(get_obj["virtual_value"]) / 10 ** (asset_a_prec + asset_b_prec),
f"{asset_a_name}:{asset_b_name}",
f"{asset_a_name}:{asset_b_name}:{share_asset_name}",
),
)
)
if inserts:
queries = []
for update in inserts:
dml = {"query": update[0], "values": update[1]}
queries.append(dml)
sql.execute(queries)
if __name__ == "__main__":
assets()
pools()