-
Notifications
You must be signed in to change notification settings - Fork 48
/
updatePrices.py
288 lines (254 loc) · 10.3 KB
/
updatePrices.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import requests
import json
import os
from datetime import datetime
from sqltools import *
from common import *
from decimal import Decimal
from config import *
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
def updatePrices():
updateBTC()
updateOMNISP()
dbCommit()
def fiat2propertyid(abv):
ROWS=dbSelect("select propertyid from smartproperties where protocol='Fiat' and propertyname=%s",[abv.upper()])
if len(ROWS) == 0:
return -1
else:
return ROWS[0][0]
def getSource(sp):
try:
convert={0:{"cmcid":"1","name":"BTC","source":"coinmarketcap"},
1:{"cmcid":"83","name":"OMNI","source":"coinmarketcap"},
3:{"cmcid":"291","name":"MAID","source":"coinmarketcap"},
31:{"cmcid":"825","id":"USDt","name":"Tether USD","source":"fixed","value":1,"base":0},
#39:{"cmcid":"1125","name":"AMP","source":"coinmarketcap"},
41:{"id":"EURt","name":"Tether EUR","source":"fixed","value":1,"base":2},
#56:{"cmcid":"1172","name":"SAFEX","source":"coinmarketcap"},
58:{"cmcid":"1037","id":"AGRS","name":"IDNO Agoras","source":"coinmarketcap"},
#59:{"id":"PDC","source":"coinmarketcap"},
#66:{"cmcid":"1352","name":"GARY","source":"coinmarketcap"},
#89:{"id":"DIBC","source":"coinmarketcap"},
#90:"https://market.bitsquare.io/api/trades?market=sfsc_btc",
#149:{"cmcid":"1642","name":"ALT","source":"coinmarketcap"},
351:{"cmcid":"1612","id":"DIBC","name":"Dibcoin V2","source":"coinmarketcap"},
701:{"cmcid":"3850","id":"OTO","name":"OTOCash","source":"coinmarketcap"},
#732:"https://api.dex-trade.com/v1/public/trades?pair=HTDBTC"
}
if sp == 'cmcids':
q=[]
for key in convert.keys():
try:
q.append(convert[key]['cmcid'])
except:
pass
q = ','.join(map(str, q))
return q
else:
return convert[sp]
except KeyError:
return None
def getfixedprice(desiredvalue, base):
#base id, currency
# 0 | USD # 1 | CAD # 2 | EUR # 3 | AUD
# 4 | IDR # 5 | ILS # 6 | GBP # 7 | RON
# 8 | SEK # 9 | SGD # 10 | HKD # 11 | CHF
# 12 | CNY # 13 | TRY # 14 | NZD # 15 | NOK
# 16 | RUB # 17 | MXN # 18 | BRL # 19 | PLN
# 20 | ZAR # 21 | JPY
ROWS=dbSelect("select rate1for2 from exchangerates where protocol1='Fiat' and propertyid1=%s and protocol2='Bitcoin' and propertyid2=0 "
"order by asof desc limit 1",[int(base)])
if len(ROWS)>0:
return desiredvalue / ROWS[0][0]
else:
return 0
def upsertRate(protocol1, propertyid1, protocol2, propertyid2, rate, source, timestamp=None):
if propertyid1 < 0 or propertyid2 < 0:
printdebug(("Error, can't insert invalid propertyids", propertyid1, "for", propertyid2), 4)
return
if timestamp==None:
# if we have a record with the same exchangerate / source just update timestamp, otherwise insert new record
ROWS=dbSelect("select rate1for2, asof from exchangerates where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(protocol1, propertyid1, protocol2, propertyid2))
if len(ROWS)>0:
dbrate=ROWS[0][0]
dbtime=ROWS[0][1]
if dbrate==rate:
dbExecute("update exchangerates set asof=DEFAULT where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(protocol1, propertyid1, protocol2, propertyid2))
else:
dbExecute("update exchangerates set rate1for2=%s, asof=DEFAULT where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(rate, protocol1, propertyid1, protocol2, propertyid2))
else:
dbExecute("insert into exchangerates (protocol1, propertyid1, protocol2, propertyid2, rate1for2, source) select %s,%s,%s,%s,%s,%s",
(protocol1, propertyid1, protocol2, propertyid2, rate, source))
else:
# if we have a record with the same exchangerate / source just update timestamp, otherwise insert new record
ROWS=dbSelect("select rate1for2, asof from exchangerates where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(protocol1, propertyid1, protocol2, propertyid2))
if len(ROWS)>0:
dbrate=ROWS[0][0]
dbtime=ROWS[0][1]
if dbrate==rate:
dbExecute("update exchangerates set asof=%s where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(timestamp, protocol1, propertyid1, protocol2, propertyid2))
else:
dbExecute("update exchangerates set rate1for2=%s, asof=%s where protocol1=%s and propertyid1=%s and protocol2=%s and propertyid2=%s",
(rate, timestamp, protocol1, propertyid1, protocol2, propertyid2))
else:
dbExecute("insert into exchangerates (protocol1, propertyid1, protocol2, propertyid2, rate1for2, source, asof) select %s,%s,%s,%s,%s,%s,%s",
(protocol1, propertyid1, protocol2, propertyid2, rate, source, timestamp))
def updateBTC():
try:
source='https://apiv2.bitcoinaverage.com/frontend/constants/exchangerates/global'
r= requests.get( source, timeout=15 )
curlist=r.json()
if 'ignored_exchanges' in curlist:
curlist.pop('ignored_exchanges')
btc=curlist['rates']['BTC']['rate']
timestamp=curlist['time']
new=[]
for abv in curlist['rates']:
value = Decimal(curlist['rates'][abv]['rate']) / Decimal(btc)
value = float(int(Decimal(value) * Decimal(1e2)) / Decimal(1e2))
#get our fiat property id using internal conversion schema
fpid=fiat2propertyid(abv)
if fpid == -1:
new.append(abv)
else:
upsertRate('Fiat', fpid, 'Bitcoin', 0, value, source, timestamp)
if len(new) > 0:
#printdebug(("New Symbols not in db",new),5)
printdebug(("New Symbols not in db"),5)
except requests.exceptions.RequestException as e:
#error or timeout, skip for now
printdebug(("Error updating BTC Price",e),3)
pass
def formatData(sp, source):
trades=[]
if 'coinmarketcap' in source:
headers = {'X-CMC_PRO_API_KEY': CMCKEY}
payload = { 'id' : getSource('cmcids') }
r = requests.get( source, headers=headers, params=payload, timeout=15 )
else:
r = requests.get( source, timeout=15 )
try:
trades=r.json()
except ValueError:
trades=eval(r.content)
try:
if 'coinmarketcap' in source:
#tmap={}
#for x in trades:
# tmap[x['symbol']]=x
#trades=tmap
trades=trades['data']
else:
if sp in [39,58,59]:
trades=trades['result']
for trade in trades:
trade['rate']=trade['Price']
trade['amount']=trade['Quantity']
if sp in [89]:
for trade in trades:
trade['rate']=trade['price']
trade['amount']=trade['quantity']
if sp in [732]:
trades=trades['data']
for trade in trades:
trade['amount']=trade['volume']
if sp in [90]:
for trade in trades:
trade['rate']=trade['price']
except TypeError:
trades=[]
return trades
def updateOMNISP():
try:
#get list of smart properties we know about
ROWS=dbSelect("select propertyid from smartproperties where propertyid >0 and Protocol='Omni' order by propertyid")
#get Coinmarket Cap data
#cmcSource="https://api.coinmarketcap.com/v1/ticker/?limit=0"
cmcSource="https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest"
cmcData=formatData(0, cmcSource)
for x in ROWS:
sp=x[0]
src=getSource(sp)
if src != None:
try:
if 'source' in src and src['source'] == 'coinmarketcap':
value_usd=Decimal(cmcData[src['cmcid']]['quote']['USD']['price'])
btc_usd = Decimal(cmcData['1']['quote']['USD']['price'])
source=str(cmcSource)+str("?id=")+str(src['cmcid'])
value = value_usd / btc_usd
elif 'source' in src and src['source'] == 'fixed':
#Fix sp value
source='Fixed'
value=getfixedprice(src['value'],src['base'])
else:
source=src
trades=formatData(sp, source)
volume = 0;
sum = 0;
value = 0;
for trade in trades:
volume += float( trade['amount'] )
sum += float( trade['amount'] ) * float(trade['rate'] )
if volume != 0:
value=(sum / volume)
except Exception as e:
printdebug(("OMNISP Error processing:",e,sp,src),3)
pass
else:
#no Known source for a valuation, set to 0
value=0
source='Local'
upsertRate('Bitcoin', 0, 'Omni', sp, value, source)
except requests.exceptions.RequestException as e:
#error or timeout, skip for now
printdebug(("Error updating OMNISP Prices",e),3)
pass
def main():
USER=os.getenv("USER")
lockFile='/tmp/updatePrices.lock'+str(USER)
now=datetime.now()
if os.path.isfile(lockFile):
#open the lock file to read pid and timestamp
file=open(lockFile,'r')
pid=file.readline().replace("\n", "")
timestamp=file.readline()
file.close()
#check if the pid is still running
if os.path.exists("/proc/"+str(pid)):
print "Exiting: updatePrices already running with pid:", pid, " Last update started at ", timestamp
else:
print "Stale updatePrices found, no running pid:", pid, " Process last started at: ", timestamp
print "Removing lock file and waiting for restart"
os.remove(lockFile)
#exit program and wait for next run
exit(1)
else:
#start/create our lock file
file = open(lockFile, "w")
file.write(str(os.getpid()))
file.write(str(now))
file.close()
#set our debug level, all outputs will be controlled by this
setdebug(9)
try:
updatePrices()
printdebug(("Update Complete",now),5)
except Exception as e:
#Catch any issues and stop processing. Try to undo any incomplete changes
print "updatePrices: Problem with ", e
if dbRollback():
print "Database rolledback"
else:
print "Problem rolling database back"
os.remove(lockFile)
exit(1)
#remove the lock file and let ourself finish
os.remove(lockFile)
if __name__ == "__main__":main() ## with if