Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in code #7

Open
LudovicoPiccolo opened this issue Nov 20, 2021 · 1 comment
Open

Error in code #7

LudovicoPiccolo opened this issue Nov 20, 2021 · 1 comment

Comments

@LudovicoPiccolo
Copy link

If i try to sell a crypto i see:

996341397394100562
0.996341397394100562
Enter the Contract Address of token you want to sell: 0x4d496efc21754481fe7a9f3f0f758785ade8e1d3

Balance: 9435079356.573655643061248581 SANINU

Enter amount of SANINU you want to sell: 700000

Approved: 0x59a9466c83ab9412cd96614de95937bf8572c3dc6b086c8043eda0e6ab597be9

Swapping 700000 SANINU for BNB

Traceback (most recent call last):
File "sell2.py", line 78, in
pancakeswap2_txn = contract.functions.swapExactTokensForETH(
File "/usr/local/lib/python3.8/dist-packages/web3/contract.py", line 1081, in buildTransaction
return build_transaction_for_function(
File "/usr/local/lib/python3.8/dist-packages/web3/contract.py", line 1650, in build_transaction_for_function
prepared_transaction = fill_transaction_defaults(web3, prepared_transaction)
File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call
File "/usr/local/lib/python3.8/dist-packages/web3/_utils/transactions.py", line 102, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "/usr/local/lib/python3.8/dist-packages/web3/_utils/transactions.py", line 66, in
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "/usr/local/lib/python3.8/dist-packages/web3/eth.py", line 735, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "/usr/local/lib/python3.8/dist-packages/web3/module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 187, in request_blocking
return self.formatted_response(response,
File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 167, in formatted_response
apply_error_formatters(error_formatters, response)
File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 67, in apply_error_formatters
formatted_resp = pipe(response, error_formatters)
File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "/usr/local/lib/python3.8/dist-packages/web3/_utils/method_formatters.py", line 569, in raise_solidity_error_on_revert
raise ContractLogicError(response['error']['message'])
web3.exceptions.ContractLogicError: execution reverted: Pancake: K

Can you help me or fix the code?

Thanks

@LudovicoPiccolo
Copy link
Author

I have created a loop that decreases the amount until the transaction is successful:

def estimateGas( txn):
try:
gas = web3.eth.estimateGas({
"from": txn['from'],
"to": txn['to'],
"value": txn['value'],
"data": txn['data']})
gas = gas + (gas / 10) # Adding 1/10 from gas to gas!
except:
gas = False
return gas

while True:
pancakeswap2_txn = contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmountok,
0,
# Set to 0 or specify min number of tokens - setting to 0 just buys X amount of token at its current price for whatever BNB specified
[tokenToBuy, spend],
my_wallet,
(int(time.time()) + transactionRevertTime)
).buildTransaction({
'from': my_wallet,
'gas': 400000,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,
})

if estimateGas(pancakeswap2_txn) == False:
    tokenAmountok=int((tokenAmountok*0.99));
else:
    pancakeswap2_txn.update({ 'gas' : int(estimateGas(pancakeswap2_txn))})
    break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant