-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdefault.py
134 lines (93 loc) · 4.37 KB
/
default.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
import asyncio
import random
from web3 import AsyncHTTPProvider, AsyncWeb3
from abi import ERC20_ABI
from config import settings
from data import NETWORKS_DATA
from logs import logger
class Default:
def __init__(self, private_key: str) -> None:
self.w3 = AsyncWeb3(AsyncHTTPProvider(NETWORKS_DATA[settings.network]["rpc"]))
self.private_key = private_key
self.address = self.w3.eth.account.from_key(self.private_key).address
self.erc20_abi = ERC20_ABI
self.infinite = 115792089237316195423570985008687907853269984665640564039457584007913129639935
async def verif_tx(self, tx: str) -> bool:
try:
data = await self.w3.eth.wait_for_transaction_receipt(tx, timeout=300)
if data.get("status") is not None and data.get("status") == 1:
logger.success(f'{data.get("from")}: Успешно! Tx:{tx.hex()}')
return True
else:
logger.error(
f'{data.get("from")}: Произошла ошибка! {data.get("transactionHash").hex()} {tx.hex()}'
)
return False
except Exception as e:
logger.error(f"{tx.hex()} произошла ошибка! Error: {e}")
return False
async def random_sleep(self, sleep_time: list, description: str = ""):
random_time = random.randint(sleep_time[0], sleep_time[1])
logger.info(f"{self.address}: Спим {random_time} сек: {description}")
await asyncio.sleep(random_time)
async def get_allowance(self, token_address: str, spender: str = None) -> int:
contract_address = self.w3.to_checksum_address(
self.w3.to_checksum_address(token_address)
)
contract_instance = self.w3.eth.contract(
address=contract_address, abi=self.erc20_abi
)
return await contract_instance.functions.allowance(self.address, spender).call()
async def approve(self, token_address: str, spender: str) -> bool:
nonce = await self.w3.eth.get_transaction_count(self.address)
contract_address = self.w3.to_checksum_address(
self.w3.to_checksum_address(token_address)
)
contract_instance = self.w3.eth.contract(
address=contract_address, abi=self.erc20_abi
)
gas = await contract_instance.functions.approve(
spender,
115792089237316195423570985008687907853269984665640564039457584007913129639935,
).estimate_gas({"from": self.address, "nonce": nonce, "value": 0})
tx = {
"from": self.address,
"nonce": nonce,
"gasPrice": int(await self.w3.eth.gas_price * 1.3),
"value": 0,
"chainId": await self.w3.eth.chain_id,
"gas": gas,
}
transaction = await contract_instance.functions.approve(
spender,
115792089237316195423570985008687907853269984665640564039457584007913129639935,
).build_transaction(tx)
try:
signed_txn = self.w3.eth.account.sign_transaction(
transaction, private_key=self.private_key
)
raw_tx_hash = await self.w3.eth.send_raw_transaction(
signed_txn.rawTransaction
)
status = await self.verif_tx(raw_tx_hash)
return status
except Exception as e:
logger.error(f"{self.address} {e}")
return False
async def get_decimals(self, token_address: str) -> int:
contract_address = self.w3.to_checksum_address(token_address)
contract_instance = self.w3.eth.contract(
address=contract_address, abi=self.erc20_abi
)
return await contract_instance.functions.decimals().call()
async def token_conver_from_wei(self, amount: int, token_address: str) -> int:
decimals = await self.get_decimals(token_address=token_address)
return amount / (10**decimals)
async def get_balance(self):
return await self.w3.eth.get_balance(self.address)
async def get_token_balance(self, token_address: str) -> int:
contract_address = self.w3.to_checksum_address(token_address)
contract_instance = self.w3.eth.contract(
address=contract_address, abi=self.erc20_abi
)
return await contract_instance.functions.balanceOf(self.address).call()