marktoda / uniswap-v3-manager

Uniswap V3 Position manager - automatically readjusts positions to match a given price width window
25 stars 8 forks source link

Execution Reverted Error When Attempting to Swap Tokens Using Web3.py #13

Open hello07world opened 4 months ago

hello07world commented 4 months ago

I am encountering an "execution reverted" error when trying to perform a token swap using the PancakeSwap V3 router via a Web3.py script. The issue does not occur when swapping directly on the PancakeSwap interface, suggesting there might be a discrepancy or an issue with how the transaction is being handled or constructed in the script.

Steps to Reproduce Set up a Web3.py environment with connection to Binance Smart Chain (BSC). Use the following code snippet to perform a token swap:

def buy_token(web3, router_contract, my_address, private_key, token_address, amount_in_usdt, slippage):
    amount_in = int(float(amount_in_usdt) * 10**6)
    fee = 10000
    amount_out_min = int(amount_in * 1 - (slippage / 100))
    deadline = int(time.time()) + 10 * 60

    txn = router_contract.functions.exactInputSingle({
        'tokenIn': web3.to_checksum_address("0x55d398326f99059ff775485246999027b3197955"), # USDT Address
        'tokenOut': web3.to_checksum_address(token_address), # My Token Address
        'fee': fee,
        'recipient': my_address,
        'deadline': deadline,
        'amountIn': amount_in,
        'amountOutMinimum': amount_out_min,
        'sqrtPriceLimitX96': 0
    }).build_transaction({
        'from': my_address,
        'value': 0,
        'gas': 300000,
        'gasPrice': web3.eth.gas_price,
        'nonce': web3.eth.get_transaction_count(my_address),
    })

    signed_txn = web3.eth.account.sign_transaction(txn, private_key)
    try:
        tx_result = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
        return web3.to_hex(tx_result)
    except Exception as e:
        return str(e)

Additional Information I have verified that the smart contract has sufficient allowances, and the account has enough USDT to perform the swap.