Mahfq / Snipe_new_token-Bot

14 stars 3 forks source link

Modification code #1

Open 0xAurelou opened 1 year ago

0xAurelou commented 1 year ago

Hello,

je voulais juste apporter quelques modification sur le code afin d'en ameliorer la qualite,

Tout d'abord l utilisation d'environnement virtuel:

# Installation

Assurez vous d'avoir python3 d'installez au prealable, si ce n'est pas le cas referez vous a "Comment utiliser le Snipe_new_token_Bot"

Créez un environnement virtuel avec ``python3 -m venv .venv``

Activez votre envrionnement virtuel avec ``source .venv/bin/activate``

Installez le paquet web3 en faisant ``pip3 install -r requirements.txt``

contenu du fichier requirements.txt: web3

ca permet d'installer le paquet dans un environnement et non directmenet sur ta machine, c'est sympa sinon tu te retrouves vite avec une pollution sur ton pc.

Refacto du code Python:

J ai juste refacto ton code en petite fonction afin que ca soit plus lisibile:

#!/usr/bin/python3

from web3 import Web3
import json

# add your blockchain connection information
# add your blockchain connection information
with open('secret.json', 'r') as file:
    data = json.load(file)

provider_url = data['provider']
web3 = Web3(Web3.HTTPProvider(provider_url))

def connect_to_ethereum():
    """
    Connects to the Ethereum network using the provided Infura URL.
    """
    if web3.is_connected():
        print("Connecté à Ethereum")

def get_erc20_contract_info(tx_hash, abi):
    """
    Retrieves and prints information about the ERC20 contract from the given transaction hash.
    """
    info_transac = web3.eth.get_transaction_receipt(tx_hash)
    address = info_transac['contractAddress']
    contract = web3.eth.contract(address=address, abi=abi)
    try:
        name = contract.functions.name().call()
        symbol = contract.functions.symbol().call()
        total_supply = contract.functions.totalSupply().call()
        is_erc20 = True
    except:
        is_erc20 = False

    if is_erc20:
        print()
        print(address, "Ce contrat est ERC20")
        print('Nom du contrat:', name)
        print('Symbole:', symbol)
        print('Total Supply:', total_supply)
        print()

def process_block_transactions(block_number, abi):
    """
    Processes transactions in the given block number.
    """
    block = web3.eth.get_block(block_number, full_transactions=True)
    transactions = block['transactions']
    print("Nombre de transactions dans le bloc", block_number, ":", len(transactions))

    for tx in transactions:
        to_address = tx['to']
        if to_address is None or to_address == "0x0":
            get_erc20_contract_info(tx.hash.hex(), abi)

def check_last_block():
    """
    Main function to monitor new blocks and process their transactions.
    """
    # Smart contract ERC20 ABI
    abi = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_initialSupply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')

    latest_block_number = web3.eth.block_number
    while True:
        block_number = web3.eth.block_number
        if block_number != latest_block_number:
            latest_block_number = block_number
            process_block_transactions(latest_block_number, abi)

if __name__ == '__main__':
    connect_to_ethereum()
    check_last_block()

Voila quelques changements qui te permettront d avoir une meilleur qualite de code, n'hesite pas a me donner les acces de push sur le repos pour que je puisse creer une branche ou je met mes modifications.

Bonne journee a toi,

Aurelou

0xAurelou commented 1 year ago

N'hesite pas si il y a des choses que tu ne comprends pas 😁