-
Notifications
You must be signed in to change notification settings - Fork 71
/
liskpool.py
249 lines (187 loc) · 7.74 KB
/
liskpool.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
import requests
import json
import sys
import time
import argparse
ENABLE_VERSION_1 = True
if sys.version_info[0] < 3:
print ('python2 not supported, please use python3')
sys.exit (0)
# Parse command line args
parser = argparse.ArgumentParser(description='DPOS delegate pool script')
parser.add_argument('-c', metavar='config.json', dest='cfile', action='store',
default='config.json',
help='set a config file (default: config.json)')
parser.add_argument('-y', dest='alwaysyes', action='store_const',
default=False, const=True,
help='automatic yes for log saving (default: no)')
parser.add_argument('--min-payout', type=float, dest='minpayout', action='store',
default=None,
help='override the minpayout value from config file')
args = parser.parse_args ()
# Load the config file
try:
conf = json.load (open (args.cfile, 'r'))
except:
print ('Unable to load config file.')
sys.exit ()
if 'logfile' in conf:
LOGFILE = conf['logfile']
else:
LOGFILE = 'poollogs.json'
fees = 0.0
if 'feededuct' in conf and conf['feededuct']:
fees = 0.1
# Override minpayout from command line arg
if args.minpayout != None:
conf['minpayout'] = args.minpayout
# Fix the node address if it ends with a /
if conf['node'][-1] == '/':
conf['node'] = conf['node'][:-1]
if conf['nodepay'][-1] == '/':
conf['nodepay'] = conf['nodepay'][:-1]
def loadLog ():
try:
data = json.load (open (LOGFILE, 'r'))
except:
data = {
"lastpayout": 0,
"accounts": {},
"skip": []
}
return data
def saveLog (log):
json.dump (log, open (LOGFILE, 'w'), indent=4, separators=(',', ': '))
def createPaymentLine (to, amount):
if conf['coin'].lower() != 'lisk':
data = { "secret": conf['secret'], "amount": int (amount * 100000000), "recipientId": to }
if conf['secondsecret'] != None:
data['secondSecret'] = conf['secondsecret']
nodepay = conf['nodepay']
if ENABLE_VERSION_1:
nodepay = 'http://localhost:6990'
return 'curl -k -H "Content-Type: application/json" -X PUT -d \'' + json.dumps (data) + '\' ' + nodepay + "/api/transactions\n\nsleep 0.5\n"
else:
pline = 'lisk transaction:broadcast `lisk transaction:create --type=0 %.8f %s -p=pass:"%s"' % (amount, to, conf['secret'])
if conf['secondsecret'] != None:
pline += ' -s=pass:"%s"' % conf['secondsecret']
pline += '`\n'
return pline
def estimatePayouts (log):
if conf['coin'].lower () == 'ark' or conf['coin'].lower () == 'kapu' :
uri = conf['node'] + '/api/delegates/forging/getForgedByAccount?generatorPublicKey=' + conf['pubkey']
d = requests.get (uri)
lf = log['lastforged']
rew = int (d.json ()['rewards'])
log['lastforged'] = rew
rew = rew - lf
elif conf['coin'].lower () == 'lisk' and ENABLE_VERSION_1:
uri = conf['node'] + '/api/delegates/' + conf['address'] + '/forging_statistics?&fromTimestamp=' + str (log['lastpayout']) + '000&toTimestamp=' + str (int (time.time ())) + '000'
d = requests.get (uri)
rew = d.json ()['data']['rewards']
else:
uri = conf['node'] + '/api/delegates/forging/getForgedByAccount?generatorPublicKey=' + conf['pubkey'] + '&start=' + str (log['lastpayout']) + '&end=' + str (int (time.time ()))
d = requests.get (uri)
rew = d.json ()['rewards']
forged = (int (rew) / 100000000) * conf['percentage'] / 100
print ('To distribute: %f %s' % (forged, conf['coin']))
if forged < 0.1:
return ([], log, 0.0)
print ('Getting voters...')
if conf['coin'].lower () == 'lisk' and ENABLE_VERSION_1:
votes = requests.get (conf['node'] + '/api/voters?address=' + conf['address']).json ()['data']['votes']
d = []
for offset in range(0,votes,100):
dpart = requests.get (conf['node'] + '/api/voters?address=' + conf['address'] + '&offset='+str(offset)+'&limit=100').json ()['data']['voters']
d += dpart
print ('Getting voters...', offset + 100, ' of ', votes)
d = { "accounts": d }
else:
d = requests.get (conf['node'] + '/api/delegates/voters?publicKey=' + conf['pubkey']).json ()
weight = 0.0
payouts = []
for x in d['accounts']:
if x['balance'] == '0' or x['address'] in conf['skip']:
continue
if 'private' in conf and conf['private'] and not (x['address'] in conf['whitelist']):
continue
weight += float (x['balance']) / 100000000
print ('Total weight is: %f' % weight)
for x in d['accounts']:
if int (x['balance']) == 0 or x['address'] in conf['skip']:
continue
if 'private' in conf and conf['private'] and not (x['address'] in conf['whitelist']):
continue
payouts.append ({ "address": x['address'], "balance": (float (x['balance']) / 100000000 * forged) / weight})
#print (float (x['balance']) / 100000000, payouts [x['address']], x['address'])
return (payouts, log, forged)
def pool ():
log = loadLog ()
(topay, log, forged) = estimatePayouts (log)
f = open ('payments.sh', 'w')
if ENABLE_VERSION_1 and conf['coin'].lower() != 'lisk':
SUFFIX = conf['coin'][0]
if conf['coin'] == 'OXY' or conf['coin'] == 'OXYCOIN':
SUFFIX = 'X'
#if conf['coin'] == 'LISK':
# SUFFIX = 'L'
f.write ("echo Starting dpos-api-fallback\n")
f.write ("node dpos-api-fallback/dist/index.js start -n " + conf['nodepay'] + " -s " + SUFFIX + "&\n")
f.write ("DPOSFALLBACK_PID=$!\n")
f.write ("sleep 4\n")
i = 1
n = len(topay)
for x in topay:
# Create the row if not present
if not (x['address'] in log['accounts']) and x['balance'] != 0.0:
log['accounts'][x['address']] = { 'pending': 0.0, 'received': 0.0 }
# Check if the voter has a pending balance
pending = 0
if x['address'] in log['accounts']:
pending = log['accounts'][x['address']]['pending']
# If below minpayout, put in the accoutns pending and skip
if (x['balance'] + pending - fees) < conf['minpayout'] and x['balance'] > 0.0:
log['accounts'][x['address']]['pending'] += x['balance']
continue
# If above, update the received balance and write the payout line
log['accounts'][x['address']]['received'] += (x['balance'] + pending)
if pending > 0:
log['accounts'][x['address']]['pending'] = 0
f.write ('echo ['+str(i)+'/'+str(n)+'] Sending ' + str (x['balance'] - fees) + ' \(+' + str (pending) + ' pending\) to ' + x['address'] + '\n')
f.write (createPaymentLine (x['address'], x['balance'] + pending - fees))
i += 1
# Handle pending balances
for y in log['accounts']:
# If the pending is above the minpayout, create the payout line
if log['accounts'][y]['pending'] - fees > conf['minpayout']:
f.write ('echo Sending pending ' + str (log['accounts'][y]['pending']) + ' to ' + y + '\n')
f.write (createPaymentLine (y, log['accounts'][y]['pending'] - fees))
log['accounts'][y]['received'] += log['accounts'][y]['pending']
log['accounts'][y]['pending'] = 0.0
# Donations
if 'donations' in conf:
for y in conf['donations']:
f.write ('echo Sending donation ' + str (conf['donations'][y]) + ' to ' + y + '\n')
f.write (createPaymentLine (y, conf['donations'][y]))
# Donation percentage
if 'donationspercentage' in conf:
for y in conf['donationspercentage']:
am = (forged * conf['donationspercentage'][y]) / 100
f.write ('echo Sending donation ' + str (conf['donationspercentage'][y]) + '% \(' + str (am) + 'LSK\) to ' + y + '\n')
f.write (createPaymentLine (y, am))
if ENABLE_VERSION_1 and conf['coin'].lower() != 'lisk':
f.write ("kill $DPOSFALLBACK_PID\n")
f.close ()
# Update last payout
log['lastpayout'] = int (time.time ())
for acc in log['accounts']:
print (acc, '\tPending:', log['accounts'][acc]['pending'], '\tReceived:', log['accounts'][acc]['received'])
if args.alwaysyes:
print ('Saving...')
saveLog (log)
else:
yes = input ('save? y/n: ')
if yes == 'y':
saveLog (log)
if __name__ == "__main__":
pool ()