簡體   English   中英

我在創建 bsc 令牌狙擊機器人時遇到問題

[英]I have a problem creating a bsc token sniper bot

我正在嘗試制作一個在 binance 智能鏈中工作的令牌狙擊機器人,我也希望它有一個小的 tkinter 應用程序,您可以在其中設置某些功能

import tkinter as tk
import requests
import web3
import time

API_ENDPOINT = "https://bsc-api.binance.org"
CONTRACT_ADDRESS = "0x1234567890abcdef"

def get_current_price():
    url = f"{API_ENDPOINT}/v1/ticker/price?symbol={CONTRACT_ADDRESS}"
    response = requests.get(url)
    price = float(response.json()["price"])
    return price

def snipe_token(threshold):
    # Connect to the Binance Smart Chain
    w3 = web3.Web3(web3.HTTPProvider("https://bsc-dataseed.binance.org:443"))

    # Get the current price of the token
    price = get_current_price()

    # If the price is below the threshold, make the purchase
    if price < threshold:
        # Unlock the account
        w3.eth.defaultAccount = "acc adress"
        w3.personal.unlockAccount(w3.eth.defaultAccount, "password")

        # Load the contract and make the purchase
        contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=abi)
        tx_hash = contract.functions.buy().transact()

        # Wait for the transaction to be mined
        tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

        # Print the transaction details
        print(f"Successfully purchased token for {price} BNB")
        print(f"Transaction hash: {tx_hash}")
    else:
        print(f"Price is above threshold ({threshold} BNB)")

def sell_token(price):
    # Connect to the Binance Smart Chain
    w3 = web3.Web3(web3.HTTPProvider("https://bsc-dataseed.binance.org:443"))

    # Unlock the account
    w3.eth.defaultAccount = "0x1234567890abcdef"
    w3.personal.unlockAccount(w3.eth.defaultAccount, "password123")

    # Load the contract and make the sale
    contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=abi)
    tx_hash = contract.functions.sell(price).transact()

    # Wait for the transaction to be mined
    tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

    # Print the transaction details
    print(f"Successfully sold token for {price} BNB")
    print(f"Transaction hash: {tx_hash}")

def check_price():
    # Get the current price of the token
    price = get_current_price()

    # Print the current price
    print(f"Current price: {price} BNB")
    return price

# Create the main window
root = tk.Tk()
root.title("Token Sniper")

# Create the threshold entry field
threshold_label = tk.Label(root, text="Snipe threshold (BNB):")
threshold_entry = tk.Entry(root)

# Create the start button
def start_clicked():
    # Get the snipe threshold from the entry field
    threshold = float(threshold_entry.get())

    # Run the snipe loop
    while True:
        # Check the current price of the token
        price = check_price()

        # Set the sell threshold (in BNB)
        sell_threshold = 0.02

        # If the price is above the sell threshold, sell the token
        if price > sell_threshold:
            sell_token(price)
        else:
            snipe_token(threshold)

        # Wait one minute before checking again
        time.sleep(1)
start_button = tk.Button(root, text="Start", command=start_clicked)

# Arrange the widgets in the window
threshold_label.grid(row=0, column=0)
threshold_entry.grid(row=0, column=1)
start_button.grid(row=1, column=0, columnspan=2)

# Run the main loop
root.mainloop()

它甚至說我還沒有安裝 web3 庫,但事實並非如此,我找不到一個庫可以做我想讓我的程序做的事情,而不是 web3,因為它接縫 web3 不想遵守

乍一看,我發現您錯誤地配置了與 BSC 的連接。 HTTPProvider方法位於web3庫的Web3 class。

import web3


# Set the HTTP provider endpoint for the BSC
bsc_provider = web3.Web3.HTTPProvider("https://bsc-dataseed.binance.org/")

# Create a web3 object using the BSC provider
w3 = web3.Web3(bsc_provider)

# Check the connection
print(w3.is_connected())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM