ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.97k stars 1.69k forks source link

Transactions not executing despite same function call on poocoin.app #2263

Closed DanielMS93 closed 2 years ago

DanielMS93 commented 2 years ago

What was wrong?

I am trying to use the swap functionality of the pancake router here: https://bscscan.com/address/0x10ed43c718714eb63d5aa57b78b54704e256024e

However, none of my transactions are completing when i use the below code. I have tried multiple to_tokens so fairly confident is it not an issue with the token but i just dont understand why the transaction will not complete

Please include any of the following that are applicable:


    bsc = "https://bsc-dataseed1.defibit.io/"
    web3 = Web3(Web3.HTTPProvider(bsc))
    return web3

def setup_contract():
    pancake_router_contract_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"

    pancake_router_contract_abi = [
        {
            "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"},
    ]

    contract = web3.eth.contract(
        address=pancake_router_contract_address,
        abi=pancake_router_contract_abi,
    )

    return contract

if __name__ == "__main__":
    web3 = instantiate_web3()

    balance = get_balance(WALLET_ADDRESS)

    contract = setup_contract()

    to_token = web3.toChecksumAddress("0xc748673057861a797275cd8a068abb95a902e8de")  # moontrust
    from_token = web3.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c')  # wbnb

    nonce = web3.eth.get_transaction_count(WALLET_ADDRESS)

    start_time = time.time()

    min_amount_out = 0
    purchase_qty = 0.001  # in BNB

    tx = contract.functions.swapExactETHForTokens(
        min_amount_out,
        [from_token, to_token],
        WALLET_ADDRESS,
        (int(time.time() + 10000))
    )

    tx = tx.buildTransaction(
        {
            "from": web3.toChecksumAddress(WALLET_ADDRESS),
            "value": web3.toWei(purchase_qty, 'ether'),
            "gas": 500000,
            "gasPrice": web3.eth.gas_price,
            "nonce": nonce
        }
    )

    sign_tx = web3.eth.account.sign_transaction(tx, private_key=WALLET_PRIVATE_KEY)

    send_tx = web3.eth.send_raw_transaction(sign_tx.rawTransaction)
    print(f"{web3.toHex(send_tx)}")```

---
**Note:** We prefer to use issues to track our work. If you think you've encountered a bug in web3py or 
have a feature request, you're in the right place. If you have implementation or usage questions, 
please refer to our [documentation](https://web3py.readthedocs.io/en/latest/) and/or join the conversation 
on [discord](https://discord.gg/GHryRvPB84).
fselmo commented 2 years ago

@DanielMS93 being that this is the eth module, it is intended to work with Ethereum first. If it works with bsc that is because the calls are similar. I'm going to close this as web3.py doesn't attempt to keep up with every chain out there. This doesn't appear to be an issue with the library itself. I would like to point you instead to the discord where there is a great community of users all learning from each other and this is where usage questions should be asked.

Best of luck.