buy and sell in one transaction in Web3, to check for honeypots

Question:

How can i turn this buy function into a transaction that buys and sell in a single transaction?

The reason behind this is to check if a token is a honeypot and will not allow me sell the token, but i dont know how to go on from here. Maybe there is better way to check if a address is a honeypot(www.honeypot.is is not working good), but Nonetheless can it be done with web3?

contract = web3.eth.contract(address=uniswap_factory, abi=uniswap_factory_abi)
contractbuy = web3.eth.contract(address=panRouterContractAddress, abi=panabi)


def buy(token, wbnb_amount):
    spend = web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c")  # wbnb contract address
    tokenToBuy = web3.toChecksumAddress(token)
    nonce = web3.eth.get_transaction_count(sender_address)
    pancakeswap2_txn = contractbuy.functions.swapExactETHForTokens(
        0,  # set to 0, or specify minimum amount of token you want to receive - consider decimals!!!
        [spend, tokenToBuy],
        sender_address,
        (int(time.time()) + 10000)
    ).buildTransaction({
        'from': sender_address,
        'value': web3.toWei(wbnb_amount, 'ether'),  # This is the Token(BNB) amount you want to Swap from
        '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)
    transaction_tx = web3.toHex(tx_token)

    return transaction_tx

Asked By: SomaJuice

||

Answers:

In conclusion,you are not able to achieve this.
The reason is that function you called from contract by web3 will be encoded to bytecode and put into the data field of the transaction,and basically the bytecode is able to interact one function of a contact once a time.
Sure,you are able to wrap the buy and sell function into your self-defined contract,but you just can not check the honeypot.

Answered By: Ghosts381937

I am not sure I understand your question but you can check honeypot contract with Web3.
You can find code in github

It works for me.

Answered By: Alchemist