-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
125 lines (100 loc) · 4.21 KB
/
functions.py
File metadata and controls
125 lines (100 loc) · 4.21 KB
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
import logging
from datetime import datetime, timedelta
import requests
from pytonconnect import TonConnect
from pytonconnect.storage import FileStorage
from config import *
from tc_storage import SimpleStorage
# def get_wallets_dict() -> dict[str, str]:
# with open("wallets.csv") as f:
# all_wallets = {}
# for elem in f.read().split('\n')[1:]:
# elem = elem.split(';')
# all_wallets[int(elem[0])] = elem[2]
# return all_wallets
def add_wallet(user_id: int, username: str, wallet_address: str) -> None:
"""adds new wallet to the storage"""
with open("wallets.csv", 'a') as f:
f.write(f"\n{user_id};{username};{wallet_address}")
def remove_wallet(user_id: int) -> bool:
"""removes data about user with the given user_id"""
with open("wallets.csv") as f:
wallets = f.read()
line_start_index = wallets.find(str(user_id) + ';')
if line_start_index != -1:
line_end_index = wallets.find('\n', line_start_index)
with open("wallets.csv", 'w') as f:
if line_end_index != -1:
f.write(wallets[:line_start_index] + wallets[line_end_index + 1:])
else:
f.write(wallets[:line_start_index - 1])
return True
return False
def get_wallet(user_id: int) -> str | None:
"""returns user's wallet address by the given user_id"""
with open("wallets.csv") as f:
wallets = f.read()
line_start_index = wallets.find(str(user_id) + ';')
if line_start_index != -1:
line_end_index = wallets.find('\n', line_start_index)
if line_end_index != -1:
return wallets[line_start_index:line_end_index].split(';')[2]
else:
return wallets[line_start_index:].split(';')[2]
return None
def check_wallet(wallet_address) -> bool:
"""checks weather the wallet with the given address is already connected"""
with open("wallets.csv") as f:
return wallet_address in f.read()
async def get_connector(user_id: str, wallet_name: str = 'Tonkeeper') -> tuple[str, TonConnect]:
"""starts wallet connect proccess and returns tuple(connection_link, connector)"""
storage = SimpleStorage(user_id)
connector = TonConnect(manifest_url=TONCONNECT_MANIFEST_URL, storage=storage)
await connector.restore_connection()
if connector.connected:
await connector.disconnect()
def status_changed(wallet_info):
unsubscribe()
def status_error(e):
pass
unsubscribe = connector.on_status_change(status_changed, status_error)
wallets_list = connector.get_wallets()
for wallet in wallets_list:
if wallet["name"] == wallet_name:
return ((await connector.connect(wallet)) + "&ret=back", connector)
async def disconnect_wallet(user_id: str) -> None:
storage = SimpleStorage(user_id)
connector = TonConnect(manifest_url=TONCONNECT_MANIFEST_URL, storage=storage)
await connector.restore_connection()
if connector.connected:
await connector.disconnect()
def get_traded_volume(wallet_address: str, start_date: datetime = None, end_date: datetime = None) -> float:
"""returns total traded volume in TON pools"""
url = f"https://api.ston.fi/v1/wallets/{wallet_address}/operations"
if start_date is None and end_date is None:
end_date = datetime.now()
elif end_date is None:
end_date = start_date + timedelta(days=30)
if start_date is None:
start_date = end_date - timedelta(days=30)
params = {
"since": start_date.strftime("%Y-%m-%dT%H:%M:%S"),
"until": end_date.strftime("%Y-%m-%dT%H:%M:%S"),
"op_type": "Swap",
}
try:
operations = requests.get(url, params).json()["operations"]
except Exception:
return 0
total_volume = 0
for operation in operations:
operation = operation["operation"]
if operation["asset0_address"] == PTON_ADDRESS:
pton_index = 0
elif operation["asset1_address"] == PTON_ADDRESS:
pton_index = 1
else:
continue
operation_volume = abs(int(operation[f"asset{pton_index}_amount"]))
total_volume += operation_volume
return round(total_volume / 1e9, 2)