Spaces:
Sleeping
Sleeping
import requests | |
import time | |
from web3 import Web3 | |
from time import gmtime, strftime | |
from st_keyup import st_keyup | |
import datetime | |
import streamlit as st | |
import json | |
# Initialise les valeurs des booléens | |
if "bool1" not in st.session_state: | |
st.session_state.bool1 = False | |
if "bool2" not in st.session_state: | |
st.session_state.bool2 = False | |
if "bool3" not in st.session_state: | |
st.session_state.bool3 = False | |
if "bool4" not in st.session_state: | |
st.session_state.bool4 = False | |
if "bool5" not in st.session_state: | |
st.session_state.bool5 = False | |
if "bool6" not in st.session_state: | |
st.session_state.bool6 = False | |
if "bool_abi" not in st.session_state: | |
st.session_state.bool_abi = False | |
# Initialise les valeurs des prix et montants | |
if "price1" not in st.session_state: | |
st.session_state.price1 = "" | |
if "price2" not in st.session_state: | |
st.session_state.price2 = "" | |
if "price3" not in st.session_state: | |
st.session_state.price3 = "" | |
if "price4" not in st.session_state: | |
st.session_state.price4 = "" | |
if "price5" not in st.session_state: | |
st.session_state.price5 = "" | |
if "price6" not in st.session_state: | |
st.session_state.price6 = "" | |
if "amount1" not in st.session_state: | |
st.session_state.amount1 = "" | |
if "amount2" not in st.session_state: | |
st.session_state.amount2 = "" | |
if "amount3" not in st.session_state: | |
st.session_state.amount3 = "" | |
if "amount4" not in st.session_state: | |
st.session_state.amount4 = "" | |
if "amount5" not in st.session_state: | |
st.session_state.amount5 = "" | |
if "amount6" not in st.session_state: | |
st.session_state.amount6 = "" | |
if "abi" not in st.session_state: | |
st.session_state.abi = "" | |
# Initialise les valeurs des prix et montants | |
if "trx1" not in st.session_state: | |
st.session_state.trx1 = "no trx" | |
if "trx2" not in st.session_state: | |
st.session_state.trx2 = "no trx" | |
if "trx3" not in st.session_state: | |
st.session_state.trx3 = "no trx" | |
if "trx4" not in st.session_state: | |
st.session_state.trx4 = "no trx" | |
if "trx5" not in st.session_state: | |
st.session_state.trx5 = "no trx" | |
if "trx6" not in st.session_state: | |
st.session_state.trx6 = "no trx" | |
# Fonction pour modifier la valeur du booléen 1 | |
def toggle_bool1(): | |
st.session_state.bool1 = not st.session_state.bool1 | |
time.sleep(1) | |
# Fonction pour modifier la valeur du booléen 2 | |
def toggle_bool2(): | |
st.session_state.bool2 = not st.session_state.bool2 | |
time.sleep(1) | |
# Fonction pour modifier la valeur du booléen 3 | |
def toggle_bool3(): | |
st.session_state.bool3 = not st.session_state.bool3 | |
time.sleep(1) | |
def toggle_bool4(): | |
st.session_state.bool4 = not st.session_state.bool4 | |
time.sleep(1) | |
def toggle_bool5(): | |
st.session_state.bool5 = not st.session_state.bool5 | |
time.sleep(1) | |
def toggle_bool6(): | |
st.session_state.bool6 = not st.session_state.bool6 | |
time.sleep(0.1) | |
st.write( | |
""" | |
<style> | |
body { | |
background-color: green !important; | |
color: white; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
st.title('🔗💬 TrollTrade - Buy/Sell BSC') | |
st.write('TrollTrade is a powerful trading bot designed to help users of the PancakeSwap platform improve their trading strategy. ') | |
prompt_address_user_container = st.empty() #situation de l'espace de trading | |
prompt_address_user_container.write("Wallet Adress : 0xD7eA51d434D0897DE93400b43341FB3115585111") | |
prompt_address_userBNB = st.text_input('Give your Wallet Address') | |
prompt_address_user = st.text_input('Give your Private Key') | |
st.write("----------------") | |
st.title('🔗💬 let s go trade...') | |
prompt_token_address = st.text_input('Choose the BSC Token Adress') | |
tokenAdress_container = st.empty() #situation de l'espace de trading | |
tokenAdress_container.write("Token Adress : 0x922722e9ef614ec9a3e94b78496e92abfbb5a624") | |
st.write("----------------") | |
output_container2 = st.empty() | |
output_container = st.empty() | |
output_container3 = st.empty() | |
output_container_symbole = st.empty() | |
output_container2.write("Time : " + strftime("%Y-%m-%d %H:%M:%S")) | |
output_container_symbole.write("Symbole : ") | |
output_container.write("Price : ") | |
trade7_container = st.empty() #ammount of token | |
trade8_container = st.empty() #ammount of token BNB | |
trade7_container.write("Ammount Token : ") | |
trade8_container.write("Ammount BNB: ") | |
output_container.write("Price : ") | |
output_container2.write("Time : " + strftime("%Y-%m-%d %H:%M:%S")) | |
st.write("----------------") | |
st.write("When Sell?") | |
# Crée des champs de saisie pour les prix et les montants | |
col1, col2, col3 = st.columns(3) | |
with col1: | |
st.write("Trade 1") | |
st.session_state.price1 = st.text_input("Price 1", st.session_state.price1) | |
st.session_state.amount1 = st.text_input("Amount 1", st.session_state.amount1) | |
st.write("Boolean 1:") | |
if st.button("START/STOP - 1"): | |
toggle_bool1() | |
# Réexécute uniquement cette partie du code | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool1 else "False")) | |
with col2: | |
st.write("Trade 2") | |
st.session_state.price2 = st.text_input("Price 2", st.session_state.price2) | |
st.session_state.amount2 = st.text_input("Amount 2", st.session_state.amount2) | |
st.write("Boolean 2:") | |
if st.button("START/STOP - 2"): | |
toggle_bool2() | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool2 else "False")) | |
with col3: | |
st.write("Trade 3") | |
st.session_state.price3 = st.text_input("Price 3", st.session_state.price3) | |
st.session_state.amount3 = st.text_input("Amount 3", st.session_state.amount3) | |
st.write("Boolean 2:") | |
if st.button("START/STOP - 3"): | |
toggle_bool3() | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool3 else "False")) | |
st.write("----------------") | |
st.write("When Buy ?") | |
col4 ,col5 ,col6 = st.columns(3) | |
with col4: | |
st.write("Trade 4") | |
st.session_state.price4 = st.text_input("Price 4", st.session_state.price4) | |
st.session_state.amount4= st.text_input("Amount 4 ", st.session_state.amount4) | |
st.write("Boolean 4:") | |
if st.button("START/STOP - 4"): | |
toggle_bool4() | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool4 else "False")) | |
with col5: | |
st.write("Trade 5") | |
st.session_state.price5 = st.text_input("Price 5", st.session_state.price5) | |
st.session_state.amount5 = st.text_input("Amount 5", st.session_state.amount5) | |
st.write("Boolean 5 :") | |
if st.button("START/STOP - 5"): | |
toggle_bool5() | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool5 else "False")) | |
with col6: | |
st.write("Trade 6") | |
st.session_state.price6 = st.text_input("Price 6", st.session_state.price6) | |
st.session_state.amount6 = st.text_input("Amount 6", st.session_state.amount6) | |
st.write("Boolean 6 :") | |
if st.button("START/STOP -6"): | |
toggle_bool6() | |
st.experimental_rerun() | |
st.write("Trade : " + ("True" if st.session_state.bool6 else "False")) | |
st.write("----------------") | |
# Affiche les informations sur les trades dans un tableau | |
table_data = [("Trade Sell 1", st.session_state.price1, st.session_state.amount1, "True" if st.session_state.bool1 else "False", st.session_state.trx1), | |
("Trade Sell 2", st.session_state.price2, st.session_state.amount2, "True" if st.session_state.bool2 else "False", st.session_state.trx2), | |
("Trade Sell 3", st.session_state.price3, st.session_state.amount3, "True" if st.session_state.bool3 else "False", st.session_state.trx3), | |
("Trade Buy 1", st.session_state.price4, st.session_state.amount4, "True" if st.session_state.bool4 else "False", st.session_state.trx4), | |
("Trade Buy 2", st.session_state.price5, st.session_state.amount5, "True" if st.session_state.bool5 else "False", st.session_state.trx5), | |
("Trade Buy 3", st.session_state.price6, st.session_state.amount6, "True" if st.session_state.bool6 else "False", st.session_state.trx6)] | |
st.write("Table of trades:") | |
with st.container(): | |
col1, col2, col3, col4, col5 = st.columns(5) | |
col1.write("Trade") | |
col2.write("Price") | |
col3.write("Amount") | |
col4.write("Status") | |
col5.write("TRX") | |
for data in table_data: | |
with st.container(): | |
col1.write(data[0]) | |
col2.write(data[1]) | |
col3.write(data[2]) | |
col4.write(data[3]) | |
col5.write(data[4]) | |
url = "https://api.dexscreener.com/latest/dex/tokens/" | |
w3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443')) | |
def modify_price(pricex, symbol, total, BNB): | |
price = pricex | |
print("edit price") | |
trade7_container.write("Ammount Token : " + str(total) + " " + symbol) | |
trade8_container.write("Ammount BNB : " + str(BNB) + " " + "BNB") | |
output_container.write("Price : " + pricex + " USD") | |
output_container_symbole.write("Symbole : " + symbol) | |
output_container2.write("Time : " + strftime("%Y-%m-%d %H:%M:%S")) | |
time.sleep(1) | |
def analyser_amount_token(myaddress, token): | |
bsc = "https://bsc-dataseed.binance.org:443" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
contract_a = your_contract_adress_retrieval_function(token) | |
if 'bool_abi' not in st.session_state: | |
st.session_state.bool_abi = False | |
bool_abii = False | |
abi = "" | |
else: | |
bool_abii = st.session_state.bool_abi | |
abi = get_abi(token, bool_abii) | |
if abi == "": | |
return 0 | |
contract = web3.eth.contract(address=contract_a, abi=abi) | |
totalSupply = contract.functions.totalSupply().call() | |
address = web3.toChecksumAddress(myaddress) | |
balance12 = contract.functions.balanceOf(myaddress).call() | |
print(balance12) | |
balance1 = web3.fromWei(balance12,"ether") | |
return balance1 | |
def analyser_amount_BNB(myaddress): | |
bsc = "https://bsc-dataseed.binance.org/" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
balance = web3.eth.get_balance(myaddress) | |
print(balance) | |
result = web3.fromWei(balance, "ether") | |
print(result) | |
return result | |
def analyser_prix(adress_token): | |
url2 = url + adress_token | |
while True: | |
try: | |
response = requests.get(url2) | |
price = response.json()['pairs'][0]['priceUsd'] | |
priceBNB = response.json()['pairs'][0]['priceNative'] | |
price_change_5m = response.json()['pairs'][0]['priceChange']['m5'] | |
price_change_1h = response.json()['pairs'][0]['priceChange']['h1'] | |
price_change_6h = response.json()['pairs'][0]['priceChange']['h6'] | |
price_change_24h = response.json()['pairs'][0]['priceChange']['h24'] | |
symbole = response.json()['pairs'][0]["baseToken"]["symbol"] | |
# call the function and pass the initial value of x | |
if prompt_address_userBNB.strip(): | |
addressUser = prompt_address_userBNB.strip() | |
else: | |
addressUser = "0xD7eA51d434D0897DE93400b43341FB3115585111" | |
amount_user = analyser_amount_token(addressUser,adress_token) | |
amount_BNB = analyser_amount_BNB(addressUser) | |
modify_price(price, symbole,amount_user,amount_BNB) | |
#modify_token_address(prompt_token_address) | |
print("Price : " + str(price )+ " - Price 5m : " + str(price_change_5m) + " - Price 1h : " + str(price_change_1h) + " - Price 6H " + str(price_change_6h) + " - Price 1J " + str(price_change_24h)) | |
time.sleep(1) | |
return price | |
except requests.exceptions.RequestException as e: | |
print("Erreur lors de la récupération des données: ", e) | |
time.sleep(1) | |
def analyser_prixBNB(adress_token): | |
url2 = url + adress_token | |
print(url2) | |
while True: | |
try: | |
response = requests.get(url2) | |
priceBNB = response.json()['pairs'][0]['priceNative'] | |
return priceBNB | |
except requests.exceptions.RequestException as e: | |
print("Erreur lors de la récupération des données: ", e) | |
time.sleep(1) | |
def get_abi(token, bool_abi_h): | |
# Vérifier si l'ABI est déjà stocké dans les variables de session | |
print(bool_abi_h) | |
print("abi") | |
if bool_abi_h: | |
print("IF") | |
abih = st.session_state['abi'] | |
return abih | |
else: | |
print("ELSE") | |
# Récupérer l'ABI depuis la source appropriée | |
abi = your_abi_retrieval_function(token) | |
print(abi) | |
st.session_state['abi'] = abi | |
print("yasdadasd") | |
return abi | |
# Retourner l'ABI | |
def your_contract_adress_retrieval_function(token): | |
bsc = "https://bsc-dataseed.binance.org:443" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
url_eth = "https://api.bscscan.com/api" | |
contract_address = web3.toChecksumAddress(token) | |
return contract_address | |
tempa = 0 | |
def your_abi_retrieval_function(token): | |
if 'bool_abi' not in st.session_state: | |
st.session_state.bool_abi = True | |
if not st.session_state.bool_abi: | |
st.session_state.bool_abi = True | |
print("returnabi_1") | |
print(st.session_state.bool_abi) | |
token_v = token | |
print(token_v) | |
bsc = "https://bsc-dataseed.binance.org:443" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
url_eth = "https://api.bscscan.com/api" | |
contract_address = web3.toChecksumAddress(token_v) | |
API_ENDPOINT = url_eth+"?module=contract&action=getabi&address="+str(contract_address) | |
r = requests.get(url = API_ENDPOINT) | |
response = r.json() | |
abi=json.loads(response["result"]) | |
print("returnabi_2") | |
return abi | |
st.experimental_rerun() | |
else: | |
abi3 = st.session_state['abi'] | |
return abi3 | |
def reset_value(temp): | |
numtrade = temp | |
if numtrade == 1: | |
st.session_state.amount1 = 0 | |
st.session_state.price1 = 0 | |
if numtrade == 2: | |
st.session_state.amount2 = 0 | |
st.session_state.price2 = 0 | |
if numtrade == 3: | |
st.session_state.amount3 = 0 | |
st.session_state.price3 = 0 | |
if numtrade == 4: | |
st.session_state.amount4 = 0 | |
st.session_state.price3 = 0 | |
if numtrade == 5: | |
st.session_state.amount5 = 0 | |
st.session_state.price3 = 0 | |
if numtrade == 6: | |
st.session_state.amount6 = 0 | |
st.session_state.price3 = 0 | |
def sellToken(amountToken, sender_address, tokenToSell, prixNative): | |
bsc = "https://bsc-dataseed.binance.org/" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
print(web3.is_connected()) | |
# Adresse du contrat du routeur PancakeSwap | |
panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E' | |
panabi = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]' | |
# Adresse du contrat de BNB (WBNB) | |
receive = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c") | |
# Configuration du contrat PancakeSwap | |
contract = web3.eth.contract(address=panRouterContractAddress, abi=panabi) | |
nonce = web3.eth.get_transaction_count(sender_address) | |
start = time.time() | |
# Conversion des adresses en format checksum | |
checksum_address_sender = web3.toChecksumAddress(sender_address) | |
checksum_address_tokenToSell = web3.toChecksumAddress(tokenToSell) | |
# Calcul du montant total du token | |
totaltoken = 1000000000000000000 * int(amountToken) | |
# Calcul du montant total de BNB à recevoir | |
totalBNB = (float(prixNative) * float(amountToken) * 0.99 - float(0.000737585)) | |
# Création de la transaction pour l'échange de tokens contre BNB | |
pancakeswap2_txn = contract.functions.swapExactTokensForETH( | |
totaltoken, # Montant du token à vendre | |
0, # Montant minimum de BNB à recevoir (0 pour ignorer) | |
[checksum_address_tokenToSell, receive], # Adresses du contrat du token et du contrat BNB | |
checksum_address_sender, # Adresse de l'acheteur | |
(int(time.time()) + 10000) # Timestamp + 10000 (durée de validité de la transaction) | |
).build_transaction({ | |
'from': checksum_address_sender, | |
'gas': 250000, | |
'gasPrice': web3.toWei('5', 'gwei'), | |
'nonce': nonce, | |
}) | |
# Signature et envoi de la transaction | |
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key="xxx") | |
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction) | |
print("TRX : " + web3.to_hex(tx_token)) | |
trxfinal = web3.to_hex(tx_token) | |
return trxfinal | |
def buyToken(amountToken, sender_address, tokenToBuy, prixNative): | |
bsc = "https://bsc-dataseed.binance.org/" | |
web3 = Web3(Web3.HTTPProvider(bsc)) | |
print(web3.is_connected()) | |
#pancakeswap router #pancakeswap router abi | |
panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E' | |
panabi = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]' | |
#balance = web3.eth.get_balance(sender_address) | |
#humanReadable = web3.from_wei(balance,'ether') | |
#Contract Address of Token we want to buy | |
spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c") #wbnb contract | |
#Setup the PancakeSwap contract | |
contract = web3.eth.contract(address=panRouterContractAddress, abi=panabi) | |
nonce = web3.eth.get_transaction_count(sender_address) | |
start = time.time() | |
print(sender_address) | |
checksum_address_sender = web3.toChecksumAddress(sender_address) | |
checksum_address_tokenToBuy = web3.toChecksumAddress(tokenToBuy) | |
print(checksum_address_sender) | |
totaltoken = 1000000000000000000 * int(amountToken) | |
totalBNB = (float(prixNative) * float(amountToken) * 1.01 + float(0.000737585)) | |
pancakeswap2_txn = contract.functions.swapETHForExactTokens( | |
totaltoken, # set to 0, or specify minimum amount of tokeny you want to receive - consider decimals!!! | |
# set to 0, or specify minimum amount of tokeny you want to receive - consider decimals!!! | |
[spend,checksum_address_tokenToBuy], | |
checksum_address_sender, | |
(int(time.time()) + 10000) | |
).build_transaction({ | |
'from': checksum_address_sender, | |
'value': web3.toWei(totalBNB,'ether'),#This is the Token(BNB) amount you want to Swap from | |
'gas': 250000, | |
'gasPrice': web3.toWei('5','gwei'), | |
'nonce': nonce, | |
}) | |
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key="privatekey") | |
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction) | |
print("TRX : " + web3.to_hex(tx_token)) | |
trxfinal = web3.to_hex(tx_token) | |
return trxfinal | |
while True: | |
time.sleep(1) | |
if prompt_token_address: | |
price_now = analyser_prix(prompt_token_address) | |
print(st.session_state.bool1) | |
prompt_addres_userBNB = "0xD7eA51d434D0897DE93400b43341FB3115585111" | |
prixNative = analyser_prixBNB(prompt_token_address) | |
if st.session_state.bool1: | |
print("CHECK PRICE TRADE 1") | |
if price_now > st.session_state.price1: | |
print("Trade Sell 1 : ") | |
#st.session_state.trx1 = sellToken(st.session_state.amount1, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool1() | |
reset_value(1) | |
st.write("Trade 1 done") | |
st.experimental_rerun() | |
if st.session_state.bool2: | |
print("CHECK PRICE TRADE 2") | |
if price_now > st.session_state.price2: | |
print("Trade Sell 2 : ") | |
#st.session_state.trx2 = sellToken(st.session_state.amount2, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool2() | |
st.write("Trade 2 done") | |
reset_value(2) | |
st.experimental_rerun() | |
if st.session_state.bool3: | |
print("CHECK PRICE TRADE 3") | |
if price_now > st.session_state.price3: | |
print("Trade Sell 3 : ") | |
#st.session_state.trx3 = sellToken(st.session_state.amount3, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool3() | |
reset_value(3) | |
st.experimental_rerun() | |
if st.session_state.bool4: | |
print("CHECK PRICE TRADE 4") | |
if price_now < st.session_state.price4 and st.session_state.price4 > 0: | |
print("Trade BUY 4 : ") | |
#st.session_state.trx4 = buyToken(st.session_state.amount4, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool4() | |
reset_value(4) | |
st.experimental_rerun() | |
if st.session_state.bool5: | |
print("CHECK PRICE TRADE 5") | |
if price_now < st.session_state.price5 and st.session_state.price5 > 0: | |
print("Trade BUY 5 : ") | |
#st.session_state.trx5 = buyToken(st.session_state.amount5, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool5() | |
reset_value(5) | |
st.experimental_rerun() | |
if st.session_state.bool6: | |
print("CHECK PRICE TRADE 6") | |
if price_now < st.session_state.price6 and st.session_state.price6 > 0: | |
print("Trade BUY 6 : ") | |
#st.session_state.trx6 = buyToken(st.session_state.amount6, prompt_addres_userBNB, prompt_token_address, prixNative) | |
toggle_bool6() | |
reset_value(6) | |
st.experimental_rerun() | |
time.sleep(1) | |