0

I am trying to sell my tokens via python web3. But sometimes I get an error "execution reverted: Pancake: K". I researched this error and it has to do with the slippage but pancake swap docs tell me a slippage option is on their site. But I want to sell this token via python. How can I add a slippage tolerance option?

This is my web3.py code:

spend = web3.toChecksumAddress("0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c")
balance = web3.eth.get_balance(sender_address)
humanReadable = web3.fromWei(balance, 'ether')
contract_id = web3.toChecksumAddress(contractaddress)
sellTokenContract = web3.eth.contract(contract_id, abi=sellAbi)
balance = sellTokenContract.functions.balanceOf(sender_address).call()
symbol = sellTokenContract.functions.symbol().call()
readable = web3.fromWei(balance, 'ether')
    tokenValue = web3.toWei(readable, 'ether')
    tokenValue2 = web3.fromWei(tokenValue, 'ether')
    start = time.time()
    approve = sellTokenContract.functions.approve(panRouterContractAddress, balance).buildTransaction({
        'from': sender_address,
        'gasPrice': web3.toWei('5', 'gwei'),
        'nonce': web3.eth.get_transaction_count(sender_address),
    })

    signed_txn = web3.eth.account.sign_transaction(approve, private_key=config.private)
    tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
    print("Approved: " + web3.toHex(tx_token))
    time.sleep(3)
    print(f"Swapping {tokenValue2} {symbol} for BNB")


    pancakeswap2_txn = contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
        tokenValue,0, 
        [contract_id,spend],
        sender_address,
        (int(time.time()) + 10000)
    ).buildTransaction({
        'from': sender_address,
        'gasPrice': web3.toWei(str(gwei), 'gwei'),
        'nonce': web3.eth.get_transaction_count(sender_address),

    })

    signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=config.private)
    tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
    print(f"Sold {symbol}: " + web3.toHex(tx_token))
0

A slippage tolerance option is already added to your code.

In the func

swapExactTokensForETHSupportingFeeOnTransferTokens(amountIn, amountOutMin, path, to, deadline)

amountOutMin is yout slippage tolerance.

When amountOutMin equals 0 it means you will find the best price and execute transaction. If you specify amountOutMin for some number and price will go under your number -> transaction cancels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.