-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathData.py
More file actions
146 lines (125 loc) · 5.81 KB
/
Data.py
File metadata and controls
146 lines (125 loc) · 5.81 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import datetime
from decimal import Decimal
try:
from urllib import urlopen
except ImportError:
# Python 3
from urllib.request import urlopen
import json
api = None
log = None
def init(api1, log1):
global api, log
api = api1
log = log1
def get_on_order_balances():
loan_offers = api.return_open_loan_offers()
on_order_balances = {}
for CUR in loan_offers:
for offer in loan_offers[CUR]:
on_order_balances[CUR] = on_order_balances.get(CUR, 0) + Decimal(offer['amount'])
return on_order_balances
def get_max_duration(end_date, context):
if not end_date:
return ""
try:
now_time = datetime.date.today()
config_date = list(map(int, end_date.split(',')))
end_time = datetime.date(*config_date) # format YEAR,MONTH,DAY all ints, also used splat operator
diff_days = (end_time - now_time).days
if context == "order":
return diff_days # Order needs int
if context == "status":
return " - Days Remaining: " + str(diff_days) # Status needs string
except Exception as ex:
ex.message = ex.message if hasattr(ex, 'message') and ex.message else str(ex)
print("ERROR: There is something wrong with your endDate option. Error: {0}".format(ex.message))
exit(1)
def get_total_lent():
crypto_lent = api.return_active_loans()
total_lent = {}
rate_lent = {}
for item in crypto_lent["provided"]:
item_float = Decimal(item["amount"])
item_rate_float = Decimal(item["rate"])
if item["currency"] in total_lent:
crypto_lent_sum = total_lent[item["currency"]] + item_float
crypto_lent_rate = rate_lent[item["currency"]] + (item_rate_float * item_float)
total_lent[item["currency"]] = crypto_lent_sum
rate_lent[item["currency"]] = crypto_lent_rate
else:
crypto_lent_sum = item_float
crypto_lent_rate = item_rate_float * item_float
total_lent[item["currency"]] = crypto_lent_sum
rate_lent[item["currency"]] = crypto_lent_rate
return [total_lent, rate_lent]
def timestamp():
'''
Returns timestamp in UTC
'''
return datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
def stringify_total_lent(total_lent, rate_lent):
result = 'Lent: '
for key in sorted(total_lent):
average_lending_rate = Decimal(rate_lent[key] * 100 / total_lent[key])
result += '[%.4f %s @ %.4f%%] ' % (Decimal(total_lent[key]), key, average_lending_rate)
log.updateStatusValue(key, "lentSum", total_lent[key])
log.updateStatusValue(key, "averageLendingRate", average_lending_rate)
return result
def update_conversion_rates(output_currency, json_output_enabled):
if json_output_enabled:
total_lent = get_total_lent()[0]
ticker_response = api.return_ticker()
output_currency_found = False
# Set this up now in case we get an exception later and don't have a currency to use
log.updateOutputCurrency('highestBid', '1')
log.updateOutputCurrency('currency', 'BTC')
# default output currency is BTC
if output_currency == 'BTC':
output_currency_found = True
for couple in ticker_response:
currencies = couple.split('_')
ref = currencies[0]
currency = currencies[1]
if ref == 'BTC' and currency in total_lent:
log.updateStatusValue(currency, 'highestBid', ticker_response[couple]['highestBid'])
log.updateStatusValue(currency, 'couple', couple)
if not output_currency_found: # check for output currency
if ref == 'BTC' and currency == output_currency:
output_currency_found = True
log.updateOutputCurrency('highestBid', 1 / float(ticker_response[couple]['highestBid']))
log.updateOutputCurrency('currency', output_currency)
if ref == output_currency and currency == 'BTC':
output_currency_found = True
log.updateOutputCurrency('highestBid', ticker_response[couple]['highestBid'])
log.updateOutputCurrency('currency', output_currency)
if not output_currency_found: # fetch output currency rate from blockchain.info
url = "https://blockchain.info/tobtc?currency={0}&value=1".format(output_currency)
try:
highest_bid = json.loads(urlopen(url).read())
log.updateOutputCurrency('highestBid', 1 / float(highest_bid))
log.updateOutputCurrency('currency', output_currency)
except ValueError:
log.log_error("Failed to find the exchange rate for outputCurrency {0}! Using BTC as output currency"
.format(output_currency))
log.log_error("Make sure that {0} is either traded on the exchange or supported by blockchain.info: {1}"
.format(output_currency, "https://blockchain.info/api/exchange_rates_api"))
except Exception:
log.log_error("Can't connect to {0} using BTC as the output currency".format(url))
def get_lending_currencies():
currencies = []
total_lent = get_total_lent()[0]
for cur in total_lent:
currencies.append(cur)
lending_balances = api.return_available_account_balances("lending")['lending']
for cur in lending_balances:
currencies.append(cur)
return currencies
def truncate(f, n):
"""Truncates/pads a float f to n decimal places without rounding"""
# From https://stackoverflow.com/questions/783897/truncating-floats-in-python
s = '{}'.format(f)
if 'e' in s or 'E' in s:
return float('{0:.{1}f}'.format(f, n))
i, p, d = s.partition('.')
return float('.'.join([i, (d + '0' * n)[:n]]))