0

I have been trying to solve this issue for a while now and would really appreciate any help i can get.

i am essentially trying to create a loop that doesnt stop after one succesful tx. End goal is to produce a loop that will run the function which sends a tx to the smart contract. I have the function all sorted and its a simple tx to a smart contract on pancakeswap router.

Ive now tried a While True: loop, as well as the Scheduler but can never get it to run the function more than once, as once the statement is 'True' it simply stops. I just want to send tx every 3 seconds to a smart contract its that simple ahha!!

I would really appreciate any guidance

Thank you!

Example Code:

Pancakeswaptx = contract.functions.swapExactETHForTokens(
    0,
    [spend,tokentobuy],
    my_address,
    (int(start)+ 10000)
    ).buildTransaction({
    'from': my_address,
    'value': web3.toWei(0.001,'ether'),
    'gas': 250000,
    'gasPrice': web3.toWei('5','gwei'), 
    'nonce': web3.eth.get_transaction_count(my_address),
    })

def runtx():
    signed_tx = web3.eth.account.signTransaction(Pancakeswaptx, privatekey)
    transaction = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
    print(web3.toHex(transaction))

while True: 
    time.sleep(1)
    runtx()

Note: i get the following error after 1 iteration and succesful tx, both when i use the loop and scheduler:

ValueError: {'code': -32000, 'message': 'already known'}
7
  • show code (remove private infos) please, otherwise noone can help
    – Majd TL
    Oct 29, 2021 at 9:46
  • @MajdTL, added some example code using the 'while True' loop
    – user84407
    Oct 29, 2021 at 9:57
  • I think you are providing the exact same transaction everytime. The nonce part is especially problematic. Try incrementing the nonce for every transaction. Oct 29, 2021 at 14:51
  • @Itération122442 surely the nonce would just update after each tx as its just counting the tx count? so when i run it again after a tx itll be one higher
    – user84407
    Oct 29, 2021 at 21:06
  • Nonce goes up when the transaction is mined. I doubt it is mined in less than 1 second, as per your script. Oct 30, 2021 at 9:44

1 Answer 1

0

if you think first tx sholud mine

def runtx():
signed_tx = web3.eth.account.signTransaction(Pancakeswaptx, privatekey)
transaction = web3.eth.send_raw_transaction(signed_tx.rawTransaction)    
wait_tx = web3.eth.get_transaction_receipt(transaction)
if wait_tx:
    nonce = nonce + 1

if transaction mine is nor important

def runtx():
signed_tx = web3.eth.account.signTransaction(Pancakeswaptx, privatekey)
transaction = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(web3.toHex(transaction))
nonce = nonce + 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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