-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelper.py
260 lines (231 loc) · 9.8 KB
/
helper.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
from multiprocessing import pool
import requests
import json, collections
import shutil
from os.path import exists, abspath, dirname
from os import mkdir
import cardano_cli_helper as cli
import time
def getBlockfrostAPIData(requestString: str, apiKey: str):
header = {"project_id":apiKey}
# Get call from blockfrost.io
for i in range(10):
data = requests.get(requestString, headers=header)
if (data.status_code == 200):
break
print('Request failed, retrying...', i+1)
time.sleep(6)
if (data.status_code != 200):
print('Error: Request failed: ', requestString)
return False
try:
dataJson = json.loads(data.text)
return dataJson
except:
print('Error: Request return not in JSON format')
return False
def getRecipientsFromStakeAddr(stakeAddressesOfRecipients: list, recipientList: list, blockfrostURL: str, apiKey: str):
# Convert staking address list to sending address list
for stakeAddr in stakeAddressesOfRecipients:
requestString = blockfrostURL + 'accounts/' + stakeAddr + '/addresses'
addressJson = getBlockfrostAPIData(requestString, apiKey)
# Use first address from the returned list
try:
delegatorAddr = addressJson[0]['address']
recipientList.append(delegatorAddr)
except:
print('Error: Key address not found.')
return recipientList
def getSenderAddressFromTxHash(txHash: str, blockfrostURL: str, apiKey: str):
txHash=txHash.split('#')[0] # Drop the TxId
requestString = blockfrostURL + 'txs/' + txHash + '/utxos'
txhashJson = getBlockfrostAPIData(requestString, apiKey)
try:
senderAddress = txhashJson['inputs'][0]['address']
return senderAddress
except:
print('Error: Key inputs or address not found.')
return False
def appendLogJson(logfile: str, data: dict):
try:
with open(logfile, 'r') as jsonlog:
old_data = json.load(jsonlog)
except:
if exists(logfile):
print('Logfile not loaded properly. Backing up and creating a new one.')
shutil.copyfile(logfile, logfile + '.bk')
else:
parent_folder = dirname(abspath(logfile))
print(parent_folder, 'parent')
if not exists(parent_folder):
mkdir(parent_folder)
print('Logfile does not exist. Creating a new one.')
old_data = {}
if old_data == {}:
newDict = data
else:
newDict = {**old_data, **data}
reverseNewDict = collections.OrderedDict(reversed(newDict.items())) # if you want reversed sorted
with open(logfile, 'w') as jsonlog:
json.dump(reverseNewDict, jsonlog, indent=4, sort_keys=False)
print('Log appended')
def parseConfigSendAssets(configFile, stakeAddressesOfRecipients, recipientList):
finRecipientObjList = []
try:
with open(configFile, 'r') as jsonConfig:
print('Opened config file...')
config = json.load(jsonConfig)
myPaymentAddrFile = config['myPaymentAddrFile']
myPaymentAddrSignKeyFile = config['myPaymentAddrSignKeyFile']
tokenPolicyID = config['tokenPolicyID']
noOfTokensToSend = config['noOfTokensToSend']
minADAToSendWithToken = config['minADAToSendWithToken']
sentTokensLogFile = config['sentTokensLogFile']
delegatorsLogFile = config['delegatorsLogFile']
minFee = config['minFee']
blockfrostURL = config['blockFrostURL']
blockFrostProjID = config['blockFrostProjID']
recipientList = getRecipientsFromStakeAddr(stakeAddressesOfRecipients, recipientList, blockfrostURL, blockFrostProjID)
for recipientAddr in recipientList:
finRecipientObjList.append(cli.Recipient(recipientAddr, 0, minADAToSendWithToken, noOfTokensToSend))
print('Config file parsed succesfully!')
return myPaymentAddrFile, \
myPaymentAddrSignKeyFile, \
tokenPolicyID, \
sentTokensLogFile, \
delegatorsLogFile, \
minFee, \
finRecipientObjList
except:
print('Configuration file misformated or does not exist.')
return False
def parseConfigMonitorAddr(configFile):
try:
with open(configFile, 'r') as jsonConfig:
print('Opened config file...')
config = json.load(jsonConfig)
network = config["network"]
myPaymentAddrFile = config['myPaymentAddrFile']
myPaymentAddrSignKeyFile = config['myPaymentAddrSignKeyFile']
tokenPolicyID = config['tokenPolicyID']
tokenPriceLovelace = config['tokenPriceLovelace']
stakingTokenRatio = config['stakingTokenRatio']
minADAToSendWithToken = config['minADAToSendWithToken']
minFee = config['minFee']
incomingTxhashLogFile = config['incomingTxhashLogFile']
sentTxhashLogFile = config['sentTxhashLogFile']
sentTokensLogFile = config['sentTokensLogFile']
delegatorsLogFile = config['delegatorsLogFile']
blockFrostURL = config['blockFrostURL']
blockFrostProjID = config['blockFrostProjID']
print('Config file parsed succesfully!')
return network, \
myPaymentAddrFile, \
myPaymentAddrSignKeyFile, \
tokenPolicyID, \
tokenPriceLovelace, \
stakingTokenRatio, \
minADAToSendWithToken, \
minFee, \
incomingTxhashLogFile, \
sentTxhashLogFile, \
sentTokensLogFile, \
delegatorsLogFile, \
blockFrostURL, \
blockFrostProjID
except:
print('Configuration file misformated or does not exist.')
return False
def parseConfigGetDelegators(configFile):
try:
with open(configFile, 'r') as jsonConfig:
print('Opened config file...')
config = json.load(jsonConfig)
delegatorsLogFile = config['delegatorsLogFile']
blockFrostURL = config['blockFrostURL']
blockFrostProjID = config['blockFrostProjID']
poolID = config['poolID']
koiosURL = config['koiosURL']
print('Config file parsed succesfully!')
return delegatorsLogFile, \
blockFrostURL, \
blockFrostProjID, \
koiosURL, \
poolID
except:
print('Configuration file misformated or does not exist.')
return False
def calculateSoldTokensToSend(lovelace_received, minADAToSendWithToken, minFee, tokenPriceLovelace):
# Calculates how many tokens to send when selling them at a specific price
tokens_to_send = int((lovelace_received - minADAToSendWithToken - minFee) / tokenPriceLovelace)
if tokens_to_send<0:
tokens_to_send = 0
lovelace_amount_to_refund = lovelace_received - (tokens_to_send * tokenPriceLovelace) - minFee
return tokens_to_send, lovelace_amount_to_refund
def calculateEarnedTokensToSend(lovelace_received, minADAToSendWithToken, minFee, totalStakedAmount, stakingTokenRatio):
# Calculates how many tokens to send for a specific delegator
tokens_to_send = round(totalStakedAmount * stakingTokenRatio)
if tokens_to_send == 0:
lovelace_amount_to_refund = lovelace_received - minFee
else:
lovelace_amount_to_refund = minADAToSendWithToken
# Sanity Check
if lovelace_received < minADAToSendWithToken + minFee:
tokens_to_send = 0
lovelace_amount_to_refund = lovelace_received - minFee
return tokens_to_send, lovelace_amount_to_refund
def getDelegatorsBlockfrost(pool_id, blockfrostURL: str, apiKey: str):
# Get list of delegators from a Pool
requestString = blockfrostURL + 'pools/' + pool_id + '/delegators'
print (requestString)
delegatorsList = getBlockfrostAPIData(requestString, apiKey)
delegators = {}
try:
for deleg in delegatorsList:
delegators[deleg['address']] = int(deleg['live_stake'])
except:
print('Error: Not a proper JSON return.')
return delegators
def getDelegatorsKoios(pool_id, URL: str, epoch: int):
# Get list of delegators from a Pool
requestString = URL + 'pool_delegators'
params = {"_pool_bech32":pool_id, "_epoch_no": str(epoch)}
print ('requestString', requestString)
# Get call from api.koios.rest
data = requests.get(requestString, params=params)
if (data.status_code != 200):
print('Error: Request failed: ', requestString)
return False
try:
dataJson = json.loads(data.text)
except:
print('Error: Request return not in JSON format')
return False
delegatorsList = dataJson
delegators = {}
try:
for deleg in delegatorsList:
delegators[deleg['stake_address']] = int(deleg['amount'])
except:
print('Error: Not a proper JSON return.')
return delegators
def getCurrentEpoch(blockfrostURL: str, apiKey: str):
# Get current epoch
requestString = blockfrostURL + 'epochs/latest'
data = getBlockfrostAPIData(requestString, apiKey)
epoch = 0
try:
epoch = data['epoch']
except:
print('Error: Not a proper JSON return.')
return epoch
def getStakeFromAddress(paymentAddr: str, blockfrostURL: str, apiKey: str):
# Get Staking address of payment address
requestString = blockfrostURL + 'addresses/' + paymentAddr
data = getBlockfrostAPIData(requestString, apiKey)
try:
stakeAddr = data['stake_address']
return stakeAddr
except:
print('Error: could not find stake_address key in response')
return False