-
Notifications
You must be signed in to change notification settings - Fork 48
/
updateFees.py
143 lines (127 loc) · 3.98 KB
/
updateFees.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
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()
try:
TESTNET = config.TESTNET
except:
TESTNET = False
if TESTNET:
BITGO_API_URL = "https://test.bitgo.com/api/v1"
else:
BITGO_API_URL = "https://www.bitgo.com/api/v1"
def updatePrices():
updateFEES()
dbCommit()
def updateFEES():
#Get bitgo fees
faster=[]
fast=[]
normal=[]
#Get BitGo Fee's
try:
source = BITGO_API_URL + '/tx/fee'
r= requests.get( source, timeout=15 )
feelist=r.json()
#q=[]
#for x in feelist['feeByBlockTarget']:
# if feelist['feeByBlockTarget'][x] not in q:
# q.append(feelist['feeByBlockTarget'][x])
#q.sort(reverse=True)
#faster.append(q[0])
#fast.append(q[1])
#normal.append(q[2])
faster.append(feelist['feeByBlockTarget']['1'])
fast.append(feelist['feeByBlockTarget']['2'])
normal.append(feelist['feeByBlockTarget']['4'])
except Exception as e:
#error or timeout, skip for now
printdebug(("Error getting BitGo fees",e),3)
pass
if not TESTNET:
##Get Bitcoinfees21 Fee's
#try:
# source='https://bitcoinfees.earn.com/api/v1/fees/recommended'
# r= requests.get( source, timeout=15 )
# feelist=r.json()
# fr=int(feelist['fastestFee']*1000)
# f=int(feelist['halfHourFee']*1000)
# n=int(feelist['hourFee']*1000)
# faster.append(fr)
# fast.append(f)
# normal.append(n)
#except Exception as e:
# #error or timeout, skip for now
# printdebug(("Error getting bitcoinfees21 fees",e),3)
# pass
#Get mempool.space Fee's
try:
source='https://mempool.space/api/v1/fees/recommended'
r= requests.get( source, timeout=15 )
feelist=r.json()
fr=int(feelist['fastestFee']*1000)
f=int(feelist['halfHourFee']*1000)
n=int(feelist['hourFee']*1000)
faster.append(fr)
fast.append(f)
normal.append(n)
except Exception as e:
#error or timeout, skip for now
printdebug(("Error getting bitcoinfees21 fees",e),3)
pass
fr=int(sum(faster)/len(faster))
ff=int(sum(fast)/len(fast))
nf=int(sum(normal)/len(normal))
data=json.dumps({'faster':fr,'fast':ff,'normal':nf})
dbExecute("with upsert as "
"(update settings set value=%s, updated_at=DEFAULT where key='feeEstimates' returning *) "
"insert into settings (key, value) select 'feeEstimates',%s "
"where not exists (select * from upsert)",
(data, data))
def main():
USER=os.getenv("USER")
lockFile='/tmp/updateFees.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: updateFees already running with pid:", pid, " Last update started at ", timestamp
else:
print "Stale updateFees 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()
except Exception as e:
#Catch any issues and stop processing. Try to undo any incomplete changes
print "updateFees: 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