-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEzBSC.py
166 lines (138 loc) · 6.91 KB
/
EzBSC.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
from modules import utils
from web3 import Web3
from eth_account import Account
import time
class Wallet(object):
def __init__(self, mnemonic: str) -> None:
Account.enable_unaudited_hdwallet_features()
self.__private_key = Account.from_mnemonic(mnemonic)._private_key.hex()
self.__web3 = Web3(Web3.HTTPProvider(utils.BSC))
self.__private_wallet = self.__web3.eth.account.from_key(self.__private_key).address
def bnb_balance(self) -> float:
return float(self.__web3.from_wei(
self.__web3.eth.get_balance(
self.__web3.to_checksum_address(self.__private_wallet)
),
'ether'
))
def token_balance_by_address(self, token_address: str) -> float:
token_data = self.__web3.eth.contract(
address=self.__web3.to_checksum_address(token_address),
abi=utils.TOKENNAME_ABI
)
balance = token_data.functions.balanceOf(
self.__web3.to_checksum_address(self.__private_wallet)
).call()
if token_data.functions.decimals().call() == 8:
return float(self.__web3.from_wei(balance, 'ether') * (10 ** 10))
return float(self.__web3.from_wei(
balance,
utils.get_token_decimal(token_data.functions.decimals().call())
))
def swap_bnb_to_token_by_address(
self,
token_address: str,
bnb_quantity: float,
transaction_revert_time: int = 100000,
gas_amount: int = 300000,
gas_price: int = 5
) -> str | None:
token_to_buy = self.__web3.to_checksum_address(token_address)
spend = self.__web3.to_checksum_address(utils.WBNB_CONTRACT)
contract = self.__web3.eth.contract(
address=self.__web3.to_checksum_address(utils.PANCAKE_SWAP_ROUTER_ADDRESS),
abi=utils.PANCAKE_ABI
)
nonce = self.__web3.eth.get_transaction_count(self.__web3.to_checksum_address(self.__private_wallet))
pancakeswap2_txn = contract.functions.swapExactETHForTokensSupportingFeeOnTransferTokens(
0,
[spend, token_to_buy],
self.__private_wallet,
(int(time.time()) + transaction_revert_time)
).build_transaction({
'from': self.__private_wallet,
'value': self.__web3.to_wei(float(bnb_quantity), 'ether'),
'gas': gas_amount,
'gasPrice': self.__web3.to_wei(gas_price, 'gwei'),
'nonce': nonce,
})
tx_token_swap = False
try:
signed_txn = self.__web3.eth.account.sign_transaction(pancakeswap2_txn, self.__private_key)
tx_token_swap = self.__web3.eth.send_raw_transaction(signed_txn.rawTransaction)
except Exception as error:
print(error)
return None
if tx_token_swap:
time.sleep(15)
tx_hash = str(self.__web3.to_hex(tx_token_swap))
return tx_hash
return None
def swap_token_to_bnb_by_address(self, token_address: str, token_quantity: float, gas_price: int = 5) -> str | None:
contract = self.__web3.eth.contract(
address=self.__web3.to_checksum_address(
utils.PANCAKE_SWAP_ROUTER_ADDRESS),
abi=utils.PANCAKE_ABI
)
spend = self.__web3.to_checksum_address(utils.WBNB_CONTRACT)
sell_token_contract = self.__web3.eth.contract(address=self.__web3.to_checksum_address(token_address),
abi=utils.TOKENNAME_ABI)
token_quantity = utils.adjust_quantity(token_quantity, sell_token_contract.functions.decimals().call())
try:
approve = sell_token_contract.functions.approve(utils.PANCAKE_SWAP_ROUTER_ADDRESS,
token_quantity).build_transaction({
'from': self.__private_wallet,
'gasPrice': self.__web3.to_wei(gas_price, 'gwei'),
'nonce': self.__web3.eth.get_transaction_count(self.__web3.to_checksum_address(self.__private_wallet)),
})
except Exception as error:
print(error)
return None
try:
signed_txn = self.__web3.eth.account.sign_transaction(approve, private_key=self.__private_wallet)
tx_token_approve = self.__web3.eth.send_raw_transaction(signed_txn.rawTransaction)
except Exception as error:
print(error)
return None
if tx_token_approve:
time.sleep(5)
pancakeswap2_txn = contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
token_quantity, 0,
[self.__web3.to_checksum_address(token_address), spend],
self.__private_wallet,
(int(time.time()) + 1000000)
).build_transaction({
'from': self.__private_wallet,
'gasPrice': self.__web3.to_wei(gas_price, 'gwei'),
'nonce': self.__web3.eth.get_transaction_count(self.__web3.to_checksum_address(self.__private_wallet)),
})
tx_token_swap = False
try:
signed_txn = self.__web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=self.__private_key)
tx_token_swap = self.__web3.eth.send_raw_transaction(signed_txn.rawTransaction)
except Exception as error:
print(error)
return None
if tx_token_swap:
time.sleep(15)
tx_hash = str(self.__web3.to_hex(tx_token_swap))
return tx_hash
return None
class TokenInfo(object):
def __init__(self, token_address: str) -> None:
self.__web3 = Web3(Web3.HTTPProvider(utils.BSC))
self.token_address = token_address
self.__token_contract = self.__web3.eth.contract(address=self.__web3.to_checksum_address(token_address),
abi=utils.TOKENNAME_ABI)
def bnb_price(self) -> float:
bnb_token = Web3.to_checksum_address(utils.WBNB_CONTRACT)
token_decimals = self.__token_contract.functions.decimals().call()
amount_in = self.__web3.to_wei(1, utils.get_token_decimal(token_decimals))
router = self.__web3.eth.contract(address=Web3.to_checksum_address(utils.PANCAKE_SWAP_ROUTER_ADDRESS),
abi=utils.PANCAKE_ABI)
amount_out = router.functions.getAmountsOut(amount_in,
[Web3.to_checksum_address(self.token_address), bnb_token]).call()
amount_out = self.__web3.from_wei(amount_out[1], 'ether')
return amount_out
def symbol(self):
return self.__token_contract.functions.symbol().call()