-
Notifications
You must be signed in to change notification settings - Fork 259
/
bera_tools.py
454 lines (424 loc) · 23.6 KB
/
bera_tools.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# -*- coding: utf-8 -*-
# Time :2024/1/22 00:36
# Author :ym
# File :bera_tools.py
import json
import random
import time
from typing import Union
import requests
from eth_account import Account
from eth_typing import Address, ChecksumAddress
from faker import Faker
from requests import Response
from solcx import compile_source, set_solc_version
from web3 import Web3
from config.abi_config import erc_20_abi, honey_abi, bex_abi, bend_abi, bend_borrows_abi, ooga_booga_abi, bera_name_abi
from config.address_config import bex_swap_address, usdc_address, honey_address, honey_swap_address, \
bex_approve_liquidity_address, weth_address, bend_address, bend_borrows_address, wbear_address, zero_address, \
ooga_booga_address, bera_name_address
from config.other_config import emoji_list
class BeraChainTools(object):
def __init__(self, private_key, client_key='', solver_provider='', rpc_url='https://artio.rpc.berachain.com/'):
# if solver_provider not in ["yescaptcha", "2captcha", "ez-captcha", ""]:
if solver_provider not in ["yescaptcha", "2captcha"]:
raise ValueError("solver_provider must be 'yescaptcha' or '2captcha' or 'ez-captcha' ")
self.solver_provider = solver_provider
self.private_key = private_key
self.client_key = client_key
self.rpc_url = rpc_url
self.fake = Faker()
self.account = Account.from_key(self.private_key)
self.session = requests.session()
self.w3 = Web3(Web3.HTTPProvider(self.rpc_url))
self.bex_contract = self.w3.eth.contract(address=bex_swap_address, abi=bex_abi)
self.honey_swap_contract = self.w3.eth.contract(address=honey_swap_address, abi=honey_abi)
self.usdc_contract = self.w3.eth.contract(address=usdc_address, abi=erc_20_abi)
self.weth_contract = self.w3.eth.contract(address=weth_address, abi=erc_20_abi)
self.honey_contract = self.w3.eth.contract(address=honey_address, abi=erc_20_abi)
self.bend_contract = self.w3.eth.contract(address=bend_address, abi=bend_abi)
self.bend_borrows_contract = self.w3.eth.contract(address=bend_borrows_address, abi=bend_borrows_abi)
self.ooga_booga_contract = self.w3.eth.contract(address=ooga_booga_address, abi=ooga_booga_abi)
self.bera_name_contract = self.w3.eth.contract(address=bera_name_address, abi=bera_name_abi)
def get_2captcha_google_token(self) -> Union[bool, str]:
if self.client_key == '':
raise ValueError('2captcha_client_key is null ')
params = {'key': self.client_key, 'method': 'userrecaptcha', 'version': 'v3', 'action': 'submit',
'min_score': 0.5,
'googlekey': '6LfOA04pAAAAAL9ttkwIz40hC63_7IsaU2MgcwVH',
'pageurl': 'https://artio.faucet.berachain.com/',
'json': 1}
response = requests.get(f'https://2captcha.com/in.php?', params=params).json()
if response['status'] != 1:
raise ValueError(response)
task_id = response['request']
for _ in range(60):
response = requests.get(
f'https://2captcha.com/res.php?key={self.client_key}&action=get&id={task_id}&json=1').json()
if response['status'] == 1:
return response['request']
else:
time.sleep(3)
return False
def get_2captcha_turnstile_token(self) -> Union[bool, str]:
if self.client_key == '':
raise ValueError('2captcha_client_key is null ')
params = {'key': self.client_key, 'method': 'turnstile',
'sitekey': '0x4AAAAAAARdAuciFArKhVwt',
'pageurl': 'https://artio.faucet.berachain.com/',
'json': 1}
response = requests.get(f'https://2captcha.com/in.php?', params=params).json()
if response['status'] != 1:
raise ValueError(response)
task_id = response['request']
for _ in range(60):
response = requests.get(
f'https://2captcha.com/res.php?key={self.client_key}&action=get&id={task_id}&json=1').json()
if response['status'] == 1:
return response['request']
else:
time.sleep(3)
return False
def get_yescaptcha_google_token(self) -> Union[bool, str]:
if self.client_key == '':
raise ValueError('yes_captcha_client_key is null ')
json_data = {"clientKey": self.client_key,
"task": {"websiteURL": "https://artio.faucet.berachain.com/",
"websiteKey": "6LfOA04pAAAAAL9ttkwIz40hC63_7IsaU2MgcwVH",
"type": "RecaptchaV3TaskProxylessM1S7", "pageAction": "submit"}, "softID": 109}
response = self.session.post(url='https://api.yescaptcha.com/createTask', json=json_data).json()
if response['errorId'] != 0:
raise ValueError(response)
task_id = response['taskId']
time.sleep(5)
for _ in range(30):
data = {"clientKey": self.client_key, "taskId": task_id}
response = requests.post(url='https://api.yescaptcha.com/getTaskResult', json=data).json()
if response['status'] == 'ready':
return response['solution']['gRecaptchaResponse']
else:
time.sleep(2)
return False
def get_yescaptcha_turnstile_token(self) -> Union[bool, str]:
if self.client_key == '':
raise ValueError('yes_captcha_client_key is null ')
json_data = {"clientKey": self.client_key,
"task": {"websiteURL": "https://artio.faucet.berachain.com/",
"websiteKey": "0x4AAAAAAARdAuciFArKhVwt",
"type": "TurnstileTaskProxylessM1"}, "softID": 109}
response = self.session.post(url='https://api.yescaptcha.com/createTask', json=json_data).json()
if response['errorId'] != 0:
raise ValueError(response)
task_id = response['taskId']
time.sleep(5)
for _ in range(30):
data = {"clientKey": self.client_key, "taskId": task_id}
response = requests.post(url='https://api.yescaptcha.com/getTaskResult', json=data).json()
if response['status'] == 'ready':
return response['solution']['token']
else:
time.sleep(2)
return False
def get_ez_captcha_google_token(self) -> Union[bool, str]:
if self.client_key == '':
raise ValueError('ez-captcha is null ')
json_data = {
"clientKey": self.client_key,
"task": {"websiteURL": "https://artio.faucet.berachain.com/",
"websiteKey": "6LfOA04pAAAAAL9ttkwIz40hC63_7IsaU2MgcwVH",
"type": "ReCaptchaV3TaskProxyless", }, 'appId': '34119'}
response = self.session.post(url='https://api.ez-captcha.com/createTask', json=json_data).json()
if response['errorId'] != 0:
raise ValueError(response)
task_id = response['taskId']
time.sleep(5)
for _ in range(30):
data = {"clientKey": self.client_key, "taskId": task_id}
response = requests.post(url='https://api.ez-captcha.com/getTaskResult', json=data).json()
if response['status'] == 'ready':
return response['solution']['gRecaptchaResponse']
else:
time.sleep(2)
return False
def get_nonce(self):
return self.w3.eth.get_transaction_count(self.account.address)
def get_solver_provider(self):
provider_dict = {
# 'yescaptcha': self.get_yescaptcha_google_token,
'yescaptcha': self.get_yescaptcha_turnstile_token,
'2captcha': self.get_2captcha_turnstile_token,
'ez-captcha': self.get_ez_captcha_google_token,
}
if self.solver_provider not in list(provider_dict.keys()):
raise ValueError("solver_provider must be 'yescaptcha' or '2captcha' or 'ez-captcha' ")
return provider_dict[self.solver_provider]()
def claim_bera(self, proxies=None) -> Response:
"""
bera领水
:param proxies: http代理
:return: object
"""
google_token = self.get_solver_provider()
if not google_token:
raise ValueError('获取google token 出错')
user_agent = self.fake.chrome()
headers = {'authority': 'artio-80085-ts-faucet-api-2.berachain.com', 'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9', 'authorization': f'Bearer {google_token}',
'cache-control': 'no-cache', 'content-type': 'text/plain;charset=UTF-8',
'origin': 'https://artio.faucet.berachain.com', 'pragma': 'no-cache',
'referer': 'https://artio.faucet.berachain.com/', 'user-agent': user_agent}
# print(headers)
params = {'address': self.account.address}
# if proxies is not None:
# proxies = {"http": f"http://{proxies}", "https": f"http://{proxies}"}
response = requests.post('https://artio-80085-faucet-api-cf.berachain.com/api/claim', params=params,
headers=headers, data=json.dumps(params), proxies=proxies)
return response
def approve_token(self, spender: Union[Address, ChecksumAddress], amount: int,
approve_token_address: Union[Address, ChecksumAddress]) -> Union[bool, str]:
"""
授权代币
:param spender: 授权给哪个地址
:param amount: 授权金额
:param approve_token_address: 需要授权的代币地址
:return: hash
"""
approve_contract = self.w3.eth.contract(address=approve_token_address, abi=erc_20_abi)
allowance_balance = approve_contract.functions.allowance(self.account.address, spender).call()
if allowance_balance < amount:
txn = approve_contract.functions.approve(spender, amount).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
return True
def bex_swap(self, amount_in: int, asset_in_address: Union[Address, ChecksumAddress],
asset_out_address: Union[Address, ChecksumAddress]) -> str:
"""
bex 交换
:param amount_in: 输入数量
:param asset_in_address: 输入 token 地址
:param asset_out_address: 输出 token 地址
:return:
"""
if asset_in_address == wbear_address:
balance = self.w3.eth.get_balance(self.account.address)
assert balance != 0
assert balance >= amount_in
else:
asset_in_token_contract = self.w3.eth.contract(address=asset_in_address, abi=erc_20_abi)
balance = asset_in_token_contract.functions.balanceOf(self.account.address).call()
assert balance != 0
assert balance >= amount_in
allowance_balance = asset_in_token_contract.functions.allowance(self.account.address,
bex_swap_address).call()
if allowance_balance < amount_in:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(bex_swap_address, int("0x" + "f" * 64, 16), "{asset_in_address}")')
headers = {'authority': 'artio-80085-dex-router.berachain.com', 'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9', 'cache-control': 'no-cache',
'origin': 'https://artio.bex.berachain.com', 'pragma': 'no-cache',
'referer': 'https://artio.bex.berachain.com/', 'user-agent': self.fake.chrome()}
params = {'quoteAsset': asset_out_address, 'baseAsset': asset_in_address, 'amount': amount_in,
'swap_type': 'given_in'}
response = self.session.get('https://artio-80085-dex-router.berachain.com/dex/route', params=params,
headers=headers)
assert response.status_code == 200
swaps_list = response.json()['steps']
swaps = list()
for index, info in enumerate(swaps_list):
swaps.append(dict(
poolId=self.w3.to_checksum_address(info['pool']),
assetIn=self.w3.to_checksum_address(info['assetIn']),
amountIn=int(info['amountIn']),
assetOut=self.w3.to_checksum_address(info['assetOut']),
amountOut=0 if index + 1 != len(swaps_list) else int(int(info['amountOut']) * 0.5),
userData=b''))
if asset_in_address.lower() == wbear_address.lower():
swaps[0]['assetIn'] = zero_address
txn = self.bex_contract.functions.batchSwap(kind=0, swaps=swaps, deadline=99999999).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'value': amount_in if asset_in_address == wbear_address else 0,
'gasPrice': int(self.w3.eth.gas_price * 1.2), 'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def bex_add_liquidity(self, amount_in: int, pool_address: Union[Address], asset_in_address: Union[Address]) -> str:
"""
bex 增加流动性
:param amount_in: 输入数量
:param pool_address: 交互的pool 地址
:param asset_in_address: 需要加流动性的token地址
:return:
"""
asset_in_token_contract = self.w3.eth.contract(address=asset_in_address, abi=erc_20_abi)
token_balance = asset_in_token_contract.functions.balanceOf(self.account.address).call()
assert token_balance != 0
assert token_balance >= amount_in
allowance_balance = asset_in_token_contract.functions.allowance(self.account.address,
bex_approve_liquidity_address).call()
if allowance_balance < amount_in:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(bex_approve_liquidity_address, int("0x" + "f" * 64, 16), "{asset_in_address}")')
txn = self.bex_contract.functions.addLiquidity(pool=pool_address, receiver=self.account.address,
assetsIn=[asset_in_address],
amountsIn=[amount_in]).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def honey_mint(self, amount_usdc_in: int) -> str:
"""
honey mint
:param amount_usdc_in: 输入数量
:return:
"""
usdc_balance = self.usdc_contract.functions.balanceOf(self.account.address).call()
assert usdc_balance != 0
assert usdc_balance >= amount_usdc_in
allowance_balance = self.usdc_contract.functions.allowance(self.account.address, honey_swap_address).call()
if allowance_balance < amount_usdc_in:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(honey_swap_address, int("0x" + "f" * 64, 16), "{usdc_address}")')
txn = self.honey_swap_contract.functions.mint(to=self.account.address, collateral=usdc_address,
amount=amount_usdc_in, ).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def honey_redeem(self, amount_honey_in: int) -> str:
"""
honey redeem
:param amount_honey_in: 输入数量
:return:
"""
honey_balance = self.honey_contract.functions.balanceOf(self.account.address).call()
assert honey_balance != 0
assert honey_balance >= amount_honey_in
allowance_balance = self.honey_contract.functions.allowance(self.account.address, honey_swap_address).call()
if allowance_balance < amount_honey_in:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(honey_swap_address, int("0x" + "f" * 64, 16), "{honey_address}")')
txn = self.honey_swap_contract.functions.redeem(to=self.account.address, amount=amount_honey_in,
collateral=usdc_address).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def bend_deposit(self, amount_in: int, amount_in_token_address: Union[Address]) -> str:
"""
bend deposit
:param amount_in: 数量
:param amount_in_token_address: 代币地址
:return:
"""
amount_in_token_contract = self.w3.eth.contract(address=amount_in_token_address, abi=erc_20_abi)
token_balance = amount_in_token_contract.functions.balanceOf(self.account.address).call()
assert token_balance != 0
assert token_balance >= amount_in
allowance_balance = amount_in_token_contract.functions.allowance(self.account.address,
bend_address).call()
if allowance_balance < amount_in:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(bend_address, int("0x" + "f" * 64, 16), "{amount_in_token_address}")')
txn = self.bend_contract.functions.supply(asset=amount_in_token_address, amount=amount_in,
onBehalfOf=self.account.address, referralCode=0).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def bend_borrow(self, amount_out: int, asset_token_address: Union[Address]) -> str:
"""
bend borrow
:param amount_out: 数量
:param asset_token_address: 借款代币地址
:return:
"""
txn = self.bend_contract.functions.borrow(asset=asset_token_address, amount=amount_out,
interestRateMode=2, referralCode=0,
onBehalfOf=self.account.address).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def bend_repay(self, repay_amount: int, asset_token_address: Union[Address]) -> str:
"""
bend 还款
:param repay_amount:还款数量
:param asset_token_address: repay 代币地址
:return:
"""
allowance_balance = self.honey_contract.functions.allowance(self.account.address, bend_address).call()
if allowance_balance < repay_amount:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(bend_address, int("0x" + "f" * 64, 16), "{honey_address}")')
txn = self.bend_contract.functions.repay(asset=asset_token_address, amount=repay_amount,
interestRateMode=2, onBehalfOf=self.account.address).build_transaction(
{'gas': 500000 + random.randint(1, 10000), 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce()})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def honey_jar_mint(self):
allowance_balance = self.honey_contract.functions.allowance(self.account.address, ooga_booga_address).call()
if allowance_balance / 1e18 < 4.2:
raise ValueError(
f'需要授权\nplease run : \nbera.approve_token(ooga_booga_address, int("0x" + "f" * 64, 16), "{honey_address}")')
has_mint = self.ooga_booga_contract.functions.hasMinted(self.account.address).call()
if has_mint:
return True
signed_txn = self.w3.eth.account.sign_transaction(
dict(
chainId=80085,
nonce=self.get_nonce(),
gasPrice=int(self.w3.eth.gas_price * 1.15),
gas=134500 + random.randint(1, 10000),
to=self.w3.to_checksum_address(ooga_booga_address),
data='0xa6f2ae3a',
),
self.account.key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def deploy_contract(self, contract_source_code, solc_version):
"""
部署合约
运行前需要安装你指定的版本
from solcx import install_solc
install_solc('0.4.18')
:param contract_source_code: 合约代码
:param solc_version: 编译器版本
:return:
"""
""
set_solc_version(solc_version)
compiled_sol = compile_source(contract_source_code)
contract_id, contract_interface = compiled_sol.popitem()
txn = dict(
chainId=80085,
gas=2000000,
gasPrice=int(self.w3.eth.gas_price * 1.15),
nonce=self.get_nonce(),
data=contract_interface['bin'])
# 签署交易
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()
def create_bera_name(self):
random_str = ''.join(random.choice(emoji_list) for _ in range(random.randint(5, 20)))
random_chars = list(random_str)
random.shuffle(random_chars)
shuffled_str = ''.join(random_chars)
txn = self.bera_name_contract.functions.mintNative(chars=list(shuffled_str), duration=1,
whois=self.account.address,
metadataURI='https://beranames.com/api/metadata/69',
to=self.account.address).build_transaction(
{'gas': 2000000, 'gasPrice': int(self.w3.eth.gas_price * 1.15),
'nonce': self.get_nonce(), 'value': int(608614232209737)})
signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=self.private_key)
order_hash = self.w3.eth.send_raw_transaction(signed_txn.rawTransaction)
return order_hash.hex()