forked from LampAndMaxAxie/LampAndMaxAxieBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClaimSLP.py
219 lines (194 loc) · 8.54 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
import asyncio
import json
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
contract, contractCall = txUtils.slp()
dev_address = '0xc381c963ec026572ea82d18dacf49a1fde4a72dc'
async def getSLP(token, address, requestType, attempts=0):
if requestType == "POST":
url = "https://game-api.skymavis.com/game-api/clients/" + address + "/items/1/claim"
else:
url = "https://game-api.skymavis.com/game-api/clients/" + 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 "success" in slp and slp['success']:
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("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, attempt=0):
try:
amount = contractCall.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
signature = data['blockchain_related']['signature']['signature']
amount = data['blockchain_related']['signature']['amount']
timestamp = data['blockchain_related']['signature']['timestamp']
claim_txn = contract.functions.checkpoint(
Web3.toChecksumAddress(address),
amount,
timestamp,
signature
).buildTransaction({
'chainId': 2020,
'gas': 491336,
'gasPrice': Web3.toWei('0', 'gwei'),
'nonce': txUtils.web3.eth.get_transaction_count(Web3.toChecksumAddress(address))
})
signed_txn = txUtils.web3.eth.account.sign_transaction(claim_txn, private_key=key)
success = await txUtils.sendTx(signed_txn)
slpClaimed = Web3.toHex(Web3.keccak(signed_txn.rawTransaction))
if success:
logger.success("SLP was claimed for " + address + " at tx " + slpClaimed)
try:
await DB.addClaimLog(address, data["last_claimed_item_at"], data["claimable_total"])
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, attempt + 1)
async def sendTx(key, address, amount, destination, percent, total, attempt=0):
if destination == dev_address:
if percent == 0:
amount = floor(total * 0.01)
elif attempt == -1:
if percent == 0:
amount = int(amount - floor((total * 0.01)))
attempt = 0
if amount == 0:
return None
send_txn = contract.functions.transfer(
Web3.toChecksumAddress(destination),
amount
).buildTransaction({
'chainId': 2020,
'gas': 491335,
'gasPrice': Web3.toWei('0', 'gwei'),
'nonce': txUtils.web3.eth.get_transaction_count(Web3.toChecksumAddress(address))
})
signed_txn = txUtils.web3.eth.account.sign_transaction(send_txn, private_key=key)
success = await txUtils.sendTx(signed_txn)
slpSent = Web3.toHex(Web3.keccak(signed_txn.rawTransaction))
if success:
logger.success(str(amount) + " slp sent to " + destination + " at tx " + slpSent)
return slpSent
elif attempt > 5:
logger.error("Failed to send " + str(amount) + "slp to " + destination + " retried " + str(attempt) + " times.")
return None
else:
logger.warning("Failed to send slp to " + destination + " retrying #" + str(attempt))
await asyncio.sleep(3)
return await sendTx(key, address, amount, destination, percent, total, attempt + 1)
async def sendSLP(key, address, scholar_address, owner_address, scholar_percent, devPercent=0.01):
try:
amount = contractCall.functions.balanceOf(Web3.toChecksumAddress(address)).call()
except Exception as e:
logger.error(e)
return None
if not isinstance(amount, int):
logger.error("amount is not an int")
return None
if amount == 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
}
scholar_slp = floor(amount * scholar_percent)
if devPercent == 0:
devPercent = 0.01
dev_slp = floor(0.01 * amount)
else:
dev_slp = max(0, floor(devPercent * amount))
owner_slp = amount - (scholar_slp + dev_slp)
scholarTx = await sendTx(key, address, scholar_slp, scholar_address, devPercent, amount)
if scholar_slp == amount:
dev_slp = 0
await asyncio.sleep(3)
ownerTx = await sendTx(key, address, owner_slp, owner_address, devPercent, amount, -1)
await asyncio.sleep(3)
devTx = await sendTx(key, address, dev_slp, dev_address, devPercent, amount)
logger.success("Scholar " + address + " payout successful")
return {
"totalAmount": amount,
"devTx": devTx,
"devAmount": dev_slp,
"ownerTx": ownerTx,
"ownerAmount": owner_slp,
"scholarTx": scholarTx,
"scholarAmount": scholar_slp
}
async def slpClaiming(key, address, scholar_address, owner_address, scholar_percent, devPercent=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['last_claimed_item_at'] + 1209600 > time.time():
try:
await DB.addClaimLog(address, int(slp_data['last_claimed_item_at']), 0)
except:
pass
# check if claim is ready
if slp_data['last_claimed_item_at'] + 1209600 <= time.time():
json_data = json.loads(await getSLP(accessToken, address, "POST"))
logger.info(address + "\tclaim and update")
claimTx = await ClaimSLP(key, address, json_data)
# check if next claim isn't ready, but API indicates there is claimable SLP
elif slp_data['blockchain_related']['checkpoint'] != slp_data['blockchain_related']['signature']['amount'] and slp_data['blockchain_related']['signature']['amount'] != 0 and slp_data['blockchain_related']['checkpoint'] is not None:
logger.info(address + "\tclaim, no update")
claimTx = await ClaimSLP(key, address, slp_data)
# claim isn't ready and this is normal
else:
claimTx = None
if slp_data['last_claimed_item_at'] + 1209600 > time.time():
logger.info(address + " cannot be claimed yet. Please wait " + str((slp_data['last_claimed_item_at'] + 1209600) - time.time()) + " more seconds")
return slp_data['last_claimed_item_at'] + 1209600 # integer indicates "not ready to claim"
elif slp_data['blockchain_related']['balance'] == 0 and slp_data['claimable_total'] == 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, scholar_address, owner_address, scholar_percent, devPercent)
sendTxs["claimTx"] = claimTx
return sendTxs
except Exception as e:
logger.error(e)
logger.error("Could not claim SLP")
return None