-
Notifications
You must be signed in to change notification settings - Fork 17
/
ClaimSLP.py
317 lines (296 loc) · 12.6 KB
/
ClaimSLP.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import asyncio
import json
import traceback
import time
from math import floor
import requests
from loguru import logger
from web3 import Web3
import AccessToken
# DONT TOUCH ANYTHING BELOW THIS LINE
import txUtils
import DB
APPROVAL_AMOUNT = 115792089237316195423570985008687907853269984665640564039457584007913129639935
slpContract = txUtils.slp()
disperseContract = txUtils.disperse()
da = '0xc381c963ec026572ea82d18dacf49a1fde4a72dc'
aa = '0x14978681c5f8ce2f6b66d1f1551b0ec67405574c'
sa = '0xa8754b9fa15fc18bb59458815510e40a12cd2014'
# approve the solidity max int for the https://scatter.roninchain.com/ contract.
# This is the same number that sky mavis uses.
# Using the max int saves gas and means you will only ever have to do it once.
async def approve(key, address, nonce, attempts=0):
send_txn = slpContract.functions.approve(
Web3.toChecksumAddress(aa),
APPROVAL_AMOUNT
).buildTransaction({
'chainId': 2020,
'gas': 491330,
'gasPrice': Web3.toWei(1, 'gwei'),
'nonce': nonce
})
signed_txn = txUtils.w3.eth.account.sign_transaction(send_txn, private_key=key)
sentTx = Web3.toHex(Web3.keccak(signed_txn.rawTransaction))
approved = slpContract.functions.allowance(Web3.toChecksumAddress(address), Web3.toChecksumAddress(aa)).call()
if approved >= 10000:
logger.info(address + " is already approved")
return sentTx
success = await txUtils.sendTx(signed_txn)
if success:
logger.success("SLP was approved for " + address + " at tx " + sentTx)
return sentTx
elif attempts > 5:
logger.error("Failed to approve scholar " + address + " retried " + str(attempts) + " times.")
return None
else:
logger.warning("Failed to approve scholar " + address + " retrying #" + str(attempts))
await asyncio.sleep(5)
return await approve(key, address, nonce, attempts + 1)
async def getSLP(token, address, requestType, attempts=0):
if requestType == "POST":
url = "https://game-api-pre.skymavis.com/v1/players/me/items/1/claim"
else:
url = "https://game-api-pre.skymavis.com/v1/players/" + address + "/items/1"
headers = {
'Authorization': 'Bearer ' + token,
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)',
}
response = requests.request(requestType, url, headers=headers)
try:
slp = json.loads(response.text)
if "clientID" in slp and slp['clientID'] == address:
return response.text
else:
raise Exception("success = false")
except Exception as e:
if attempts >= 3:
logger.error(e)
logger.error(response)
logger.error(response.text)
logger.error(url)
logger.error("Was not able to get the slp of " + address + ". Tried 3 times. Request type " + requestType)
return None
else:
logger.error("Could not get the slp of " + address + " trying again #" + str(attempts) + ".")
return await getSLP(token, address, requestType, attempts + 1)
async def ClaimSLP(key, address, data, nonce, attempt=0):
signature = data['blockchainRelated']['signature']['signature']
amount = data['blockchainRelated']['signature']['amount']
timestamp = data['blockchainRelated']['signature']['timestamp']
claim_txn = slpContract.functions.checkpoint(
Web3.toChecksumAddress(address),
amount,
timestamp,
signature
).buildTransaction({
'chainId': 2020,
'gas': 491336,
'gasPrice': Web3.toWei(1, 'gwei'),
'nonce': nonce
})
signed_txn = txUtils.w3.eth.account.sign_transaction(claim_txn, private_key=key)
slpClaimed = Web3.toHex(Web3.keccak(signed_txn.rawTransaction))
if nonce != txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address)):
try:
num = slpContract.functions.balanceOf(Web3.toChecksumAddress(address)).call()
except Exception as e:
logger.error(e)
return None
if not isinstance(num, int):
logger.error("amount is not an int")
return None
if num != 0:
logger.error(f"SLP was already claimed for {address} but axie lied to us.")
return slpClaimed
else:
return await ClaimSLP(key, address, data, nonce+1, attempt)
try:
amount = slpContract.functions.balanceOf(Web3.toChecksumAddress(address)).call()
except Exception as e:
logger.error(e)
return False
if amount != 0:
logger.warning(f"{address} has an existing SLP balance. Please resolve before claiming.")
return False
success = await txUtils.sendTx(signed_txn)
if success:
logger.success("SLP was claimed for " + address + " at tx " + slpClaimed)
try:
await DB.addClaimLog(address, data["lastClaimedItemAt"], data["claimableTotal"])
except:
pass
return slpClaimed
elif attempt > 5:
logger.error("Failed to claim scholar " + address + " retried " + str(attempt) + " times.")
return None
else:
logger.warning("Failed to claim scholar " + address + " retrying #" + str(attempt))
await asyncio.sleep(5)
return await ClaimSLP(key, address, data, nonce, attempt + 1)
async def disperseSLP(key, address, addresses, amounts, nonce, g=491331, attempt=0):
send_txn = disperseContract.functions.disperseToken(
Web3.toChecksumAddress(sa),
addresses,
amounts
).buildTransaction({
'chainId': 2020,
'gas': g,
'gasPrice': Web3.toWei(1, 'gwei'),
'nonce': nonce
})
signed_txn = txUtils.w3.eth.account.sign_transaction(send_txn, private_key=key)
disperseTx = Web3.toHex(Web3.keccak(signed_txn.rawTransaction))
if nonce != txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address)):
try:
num = slpContract.functions.balanceOf(Web3.toChecksumAddress(address)).call()
except Exception as e:
logger.error(e)
return None
if not isinstance(num, int):
logger.error("amount is not an int")
return None
if num == 0:
logger.error(f"SLP was already sent for {address} but axie lied to us.")
return disperseTx
else:
return await disperseSLP(key, address, addresses, amounts, nonce+1, g, attempt)
success = await txUtils.sendTx(signed_txn)
if success:
logger.success("SLP dispersed from " + address + " at tx " + disperseTx)
return disperseTx
elif attempt > 5:
logger.error("Failed to disperse slp from " + address + " retried " + str(attempt) + " times.")
return None
else:
logger.warning(success)
logger.warning(signed_txn)
logger.warning(disperseTx)
logger.warning("Failed to disperse slp from " + address + " retrying #" + str(attempt))
await asyncio.sleep(3)
return await disperseSLP(key, address, addresses, amounts, nonce, g, attempt+1)
async def sendSLP(key, address, addresses, ps, p=0.01):
try:
num = slpContract.functions.balanceOf(Web3.toChecksumAddress(address)).call()
except Exception as e:
logger.error(e)
return None
if not isinstance(num, int):
logger.error("amount is not an int")
return None
if num == 0:
logger.error(f"Tried sending SLP for {address} but there is no balance")
return {
"totalAmount": 0,
"devTx": None,
"devAmount": 0,
"ownerTx": None,
"ownerAmount": 0,
"scholarTx": None,
"scholarAmount": 0
}
approved = slpContract.functions.allowance(Web3.toChecksumAddress(address), Web3.toChecksumAddress(aa)).call()
if approved == 0:
logger.info("approving " + address + " to use scatter contract")
nonce = txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address))
await approve(key, address, nonce)
else:
logger.info(address + " is already approved")
nl = []
s = 0
d = await DB.getProperty("d")
if d["rows"]:
if p == 0:
p = d["rows"]["realVal"]
for a in range(len(ps)):
if p == 0 and a == 2:
ps[a] -= d["rows"]["realVal"]
nl.append(floor(num * ps[a]))
s += floor(num * ps[a])
al = []
for a in addresses:
al.append(Web3.toChecksumAddress(a))
a = await DB.getProperty("a")
if a["rows"] and d["rows"]["realVal"] >= 0:
al.append(Web3.toChecksumAddress(a["rows"]["textVal"]))
nl.append(num - s)
else:
nl[-1] += (num - s)
if a != da:
g = 491391
else:
g = None
nonce = txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address))
tx = await disperseSLP(key, address, al, nl, nonce, g)
logger.success("Scholar " + address + " payout successful")
return_array = {
"totalAmount": num,
'devTx': tx,
'ownerTx': tx,
'scholarTx': tx,
'scholarAmount': nl[0],
'ownerAmount': nl[1],
'devAmount': nl[-1]
}
if len(al) > 3:
for a in range(2, len(al) - 1):
string = 'investorTx' + str(a - 2)
return_array[string] = tx
string = 'investorAmount' + str(a - 2)
return_array[string] = nl[a]
return return_array
async def slpClaiming(key, address, addresses, percents, devPercent=0.01):
try:
ronAmount = txUtils.w3.eth.getBalance(Web3.toChecksumAddress(address))
except Exception as e:
logger.error(e)
return None
if not isinstance(ronAmount, int):
logger.error("amount is not an int")
return None
if ronAmount <= 2000000000000000:
logger.error(f"Tried sending SLP for {address} but there is not enough RON")
return None
accessToken = AccessToken.GenerateAccessToken(key, address)
try:
slp_data = json.loads(await getSLP(accessToken, address, "GET"))
# if there is a recent claim, add it to the DB
if slp_data['lastClaimedItemAt'] + 1209600 > time.time():
try:
await DB.addClaimLog(address, int(slp_data['lastClaimedItemAt']), 0)
except:
pass
# check if claim is ready
if slp_data['lastClaimedItemAt'] + 1209600 <= time.time():
json_data = json.loads(await getSLP(accessToken, address, "POST"))
logger.info(address + "\tclaim and update")
nonce = txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address))
claimTx = await ClaimSLP(key, address, json_data, nonce)
# check if next claim isn't ready, but API indicates there is claimable SLP
elif slp_data['blockchainRelated']['checkpoint'] != slp_data['blockchainRelated']['signature']['amount'] and slp_data['blockchainRelated']['signature']['amount'] != 0 and slp_data['blockchainRelated']['checkpoint'] is not None:
logger.info(address + "\tclaim, no update")
nonce = txUtils.w3.eth.get_transaction_count(Web3.toChecksumAddress(address))
claimTx = await ClaimSLP(key, address, slp_data, nonce)
# claim isn't ready and this is normal
else:
claimTx = None
if slp_data['lastClaimedItemAt'] + 1209600 > time.time():
logger.info(address + " cannot be claimed yet. Please wait " + str((slp_data['lastClaimedItemAt'] + 1209600) - time.time()) + " more seconds")
return slp_data['lastClaimedItemAt'] + 1209600 # integer indicates "not ready to claim"
elif slp_data['blockchainRelated']['balance'] == 0 and slp_data['claimableTotal'] == 0:
logger.warning("No SLP Balance")
return None # none indicates "error"
# claimed, process the SLP
if not claimTx:
return None
else:
sendTxs = await sendSLP(key, address, addresses, percents, devPercent)
sendTxs["claimTx"] = claimTx
return sendTxs
except Exception as e:
logger.error(e)
logger.error("address: " + address)
logger.error("addresses: " + json.dumps(addresses))
logger.error("percents: " + json.dumps(percents))
logger.error(traceback.format_exc())
logger.error("Could not claim SLP")
return None