-
Notifications
You must be signed in to change notification settings - Fork 2
/
cr_utils.py
171 lines (134 loc) · 4.51 KB
/
cr_utils.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json
import requests
from decimal import Decimal
from binance.client import Client
from cfg import *
def update_fiat_rates(fiat, buy, sell):
"""Update USDT/fiat rate"""
if fiat == "USD":
fiat = "USD (Ecuador)"
with open(USDT_FIAT_RATES_JSON, "r") as file:
rates = json.load(file)
buy = buy.replace(",", ".")
sell = sell.replace(",", ".")
rates[fiat] = [buy, sell]
with open(USDT_FIAT_RATES_JSON, "w") as file:
json.dump(rates, file)
def get_fiat_rate(fiat):
"""Get USDT/fiat rate. Returns [buy, sell]"""
with open(USDT_FIAT_RATES_JSON, "r") as file:
rates = json.load(file)
buy, sell = rates[fiat]
buy = Decimal(buy)
sell = Decimal(sell)
return buy, sell
def update_crypto_rates():
rates = {}
for cr in ["BTC", "ETH"]:
rates[cr] = requests.get(f"https://api.binance.com/api/v3/ticker/price?symbol={cr}USDT").json()["price"]
with open(CRYPTO_RATES_JSON, "w") as file:
json.dump(rates, file)
def crypt_to_fiat_rate(fiat, crypt, buy) -> Decimal:
if crypt != "USDT":
with open(CRYPTO_RATES_JSON, "r") as file:
rates = json.load(file)
crypt_to_usdt = Decimal(rates[crypt])
else:
crypt_to_usdt = Decimal(1)
usdt_to_fiat = Decimal(get_fiat_rate(fiat)[int(not buy)])
rate = crypt_to_usdt * usdt_to_fiat
return rate
def crypt_to_usdt_rate(crypt) -> Decimal:
if crypt == "USDT":
return Decimal(1)
with open(CRYPTO_RATES_JSON, "r") as file:
rates = json.load(file)
return Decimal(rates[crypt])
def crypt_to_crypt_rate(cr1, cr2) -> Decimal:
cr1_to_usdt = crypt_to_usdt_rate(cr1)
cr2_to_usdt = crypt_to_usdt_rate(cr2)
return cr1_to_usdt / cr2_to_usdt
def rate(cur1, cur2, buy):
if cur1 in CRYPTO_CURRENCIES and cur2 in CRYPTO_CURRENCIES:
return crypt_to_crypt_rate(cur1, cur2)
elif cur1 in CRYPTO_CURRENCIES and cur2 in FIAT_CURRENCIES:
return crypt_to_fiat_rate(cur2, cur1, buy)
elif cur1 in FIAT_CURRENCIES and cur2 in CRYPTO_CURRENCIES:
return Decimal(1) / crypt_to_fiat_rate(cur1, cur2, not buy)
def save_escrow_chats(new_chat=None):
try:
with open(CHATS_JSON, "r") as file:
chats = json.load(file)
except Exception:
chats = {}
if new_chat:
chats[new_chat] = True
with open(CHATS_JSON, "w") as file:
json.dump(chats, file)
def get_chat():
with open(CHATS_JSON, "r") as file:
chats = json.load(file)
for chat, free in zip(chats.keys(), chats.values()):
if free:
chats[chat] = False
with open(CHATS_JSON, "w") as file:
json.dump(chats, file)
return int(chat)
return None
def mark_as_free(chat):
with open(CHATS_JSON, "r") as file:
chats = json.load(file)
chats[str(chat)] = True
with open(CHATS_JSON, "w") as file:
json.dump(chats, file)
def get_bin_balance():
client = Client(API_KEY, SECRET_KEY)
res = client.get_account()
bals = res["balances"]
balances = UID_DIVIDER
for balance in balances:
balances[balance] = Decimal(0)
for balance in bals:
try:
balances[balance["asset"]] += Decimal(balance["Free"])
except KeyError:
pass
return balances
def get_dep_addresses(dict_):
client = Client(API_KEY, SECRET_KEY)
btc = client.get_deposit_address("BTC", "BTC")["address"]
eth = client.get_deposit_address("ETH", "ETH")["address"]
usd = client.get_deposit_address("USDT", "TRX")["address"]
dict_["BTC"] = [btc, "BTC"]
dict_["ETH"] = [eth, "ETH"]
dict_["USDT"] = [usd, "TRX"]
return dict_
def get_new_confirmed_uids():
client = Client(API_KEY, SECRET_KEY)
hist = client.get_deposit_history()
i = 0
with open("dep_history.json", "r") as file:
old_hist = json.load(file)
new_confirmed_uids = []
while True:
if i >= len(hist):
break
if hist[i] in old_hist:
break
dep = hist[i]
i += 1
coin = dep["coin"]
amount = dep["amount"]
uid = get_uid(amount, coin)
new_confirmed_uids.append(uid)
with open("dep_history.json", "w") as file:
json.dump(hist, file)
return new_confirmed_uids
def get_uid(amount, currency):
amount = str(amount)
amount = amount[amount.find(".") + 1:]
divider = UID_DIVIDER[currency]
uid = int(amount[divider - 2:divider])
return uid
if __name__ == '__main__':
print(get_new_confirmed_uids())