lecoos / web3-flashbots

MIT License
1 stars 1 forks source link

I get error AttributeError: 'Flashbots' object has no attribute 'retrieve_caller_fn' when I test. #1

Open elvise7777 opened 2 years ago

elvise7777 commented 2 years ago

Traceback (most recent call last): File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/elvise/Downloads/flashloan/flashbotsTest.py", line 56, in w3.flashbots.send_bundle(bundle, target_block_number=target_block) File "/Users/elvise/Downloads/flashloan/venv/lib/python3.9/site-packages/web3/method.py", line 142, in get return obj.retrieve_caller_fn(self) AttributeError: 'Flashbots' object has no attribute 'retrieve_caller_fn'

lecoos commented 2 years ago

@elvise7777 When I looked at the method.py file of the web3 python module, I found the description of this type of error. Directly calls to methods are not supported. Methods must be called from an module instance, usually attached to a web3 instance. So did you creat a web3 instance successfully? If you comfirm it, don't forget add these two pieces of code after your web3 instance. w3.middleware_onion.add(construct_sign_and_send_raw_middleware(sender)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) Good luck : )

elvise7777 commented 2 years ago

I import ModuleV2 from web3.module and solve the problem. But the bundle not execute. Did you test the flashbots successfully on goerli? Thank you for your response.

@elvise7777 When I looked at the method.py file of the web3 python module, I found the description of this type of error. Directly calls to methods are not supported. Methods must be called from an module instance, usually attached to a web3 instance. So did you creat a web3 instance successfully? If you comfirm it, don't forget add these two pieces of code after your web3 instance. w3.middleware_onion.add(construct_sign_and_send_raw_middleware(sender)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) Good luck : )

lecoos commented 2 years ago

@elvise7777 Sorry for taking so long to reply to you. I tested flashbots successfully on goerli include create contracts and swap tokens and even arbitrage. You can adjust the gas price and coinbase trasfer amount to a higher value. If it still fails, you can use flashbots simulate api to check the specific reason. You can read the document of troubleshooting here to learn more. Wish you all the best~

hongwei520 commented 2 years ago

@lecoos hi~ How to set or switch to the url of the testnet in the goerli testnet test? Is the url of the testnet set as flashbots.DEFAULT_FLASHBOTS_RELAY='https://relay-goerli.flashbots.net' Thank you

lecoos commented 2 years ago

@hongwei520
You can try to modify in three places.

  1. _DEFAULT_FLASHBOTSRELAY in init.py of flashbots pyp.
  2. _get_defaultendpoint() in provider.py of flashbots pyp.
  3. flashbot args of web3 middleware in your python code.

I modified all these for my test : )

Good luck

hongwei520 commented 2 years ago

@lecoos Thank you. After I execute the script, I finally see the transaction record. I used the eip1559.py in the example at the beginning, but there are many problems. So if I execute the script on the main network, do I need to submit the bundle through a loop?

lecoos commented 2 years ago

@hongwei520 It depends on the purpose of your transactions. If you just want your transactions to be on-chain, like deploy, you can submit the bundle through a loop. While if your transactions are time sensitive, like arbitrage, you better determine an accurate target block number instead of using loop. Of course, you can send some same bundles to improve success rate, but flashbots relayer may reject you due to the preventions of DoS, so draw a fine line : )

hongwei520 commented 2 years ago

@lecoos If I set the current block number + 1, and then send multiple bundles, the transaction will have a higher chance of succeeding in the current block number + 1 block. Is it like this?

lecoos commented 2 years ago

@hongwei520 Actually this method will improve the success rate only if your network is not smooth. If the flashbots relayer could receive your bundle normaly, the chance only depends on your transactions.

hongwei520 commented 2 years ago

@lecoos Well, that is, I just need to send one. I also encountered a problem when using the example. It does not necessarily wait for the result and will error out TransactionNotFound. I know that the transaction may not be packaged, but I don't know how to judge it. Is this transaction pending or failed because I have waited for a long time and still haven't seen the transaction record and re-executing the script returns notfound and the hash of the transaction is the same as before. This is why

lecoos commented 2 years ago

@hongwei520 I just test the example script eip1559.py successfully on goerli. If you confirm your code is correct, you can use flashbots simulate function to check for the specific reason.

hongwei520 commented 2 years ago

@lecoos alright, thank you very much

hongwei520 commented 2 years ago

@lecoos I tested it successfully on the testnet but failed on the mainnet. For the code I just did change the chainId to 1. I tested using web3.eth.sendRawTransaction to send the signed transaction directly and it was successful but using the bundle to send to the relayer It seems that the relay did not receive the package I submitted. My code is very simple 'https://rentry.co/pnyk7' Please where do I need to modify to adapt to the main network? Thank you

lecoos commented 2 years ago

@hongwei520

  1. Change the relay url to "http://relay.flashbots.net" in flashbots pyp file and flashbot web3-middleware (line 6 in your code).
  2. Check the transaction fee is sufficient to be on-chain by flashbots if you don't transfer bribe by coinbase.transfer in the contract.
hongwei520 commented 2 years ago

@lecoos The test is successful, there is no problem with the code, it seems to be a problem of cost, it seems that the gas price was as high as 60gwei during the test period yesterday, so it failed

hongwei520 commented 2 years ago

@lecoos If I don't bribe the miner via coinbase.transfer, but add a transfer transaction to the bundle, would that work? If it works, then how do I just specify the miner as the recipient?

lecoos commented 2 years ago

@hongwei520 You can write a contract to implement transfer function, and specify coinbase as the recipent in contract. But there is no different from coinbase.transfer. 😂

hongwei520 commented 2 years ago

@lecoos Is it possible to add a tip without a contract, like adding it to a bundle

lecoos commented 2 years ago

@hongwei520 No such method now, cuz you can't specify miner except coinbase in contract.

hongwei520 commented 2 years ago

@lecoos Understood thank you

elvise7777 commented 2 years ago

@lecoos I sent bundle to miners in block 14588272 on mainnet, but do not get the receipt. The maxFeePerGas was 49 and the maxPriorityFeePerGas was 3 which was higher than other transactions in block. I never send bundle successfully on mainnet.

lecoos commented 2 years ago

@elvise7777 Can you post your simple code, pay attention to privacy protection.

elvise7777 commented 2 years ago

tx2: TxParams = { "from": self.ACCOUNT_FROM.address, "to": self.CONTRACT_AD, "value": 0, "nonce": nonce, "gas": 1700000, "maxFeePerGas": Web3.toWei(gasPrice, "gwei"), "maxPriorityFeePerGas": Web3.toWei(priorityGas, "gwei"), "chainId": 1, "data": encodeBytesData }

bundle = [ { "signer": self.ACCOUNT_FROM, "transaction": tx2 } ]

results = [] for target_block in [block + k + 1 for k in range(3)]: results.append( self.w3.flashbots.send_bundle(bundle, target_block_number=target_block) )

print(f"Bundle sent to miners in block {block}") results[-1].wait()

try: receipt = results[-1].receipts() print(f"Bundle was executed in block {receipt[0].blockNumber}") return receipt except TransactionNotFound: print("Bundle was not executed") return "Bundle was not executed"

elvise7777 commented 2 years ago

@lecoos I send bribe by contract. Does the miners not package the transaction which may be reverted by contract even pay the higher gas. Thank u, sir.

lecoos commented 2 years ago

@elvise7777 If you don't set the revertingTxHashes field, the miners will not pack your reverted transactions even you pay a high gas. You can use simulate call to check the failed reason like this: simulation = w3.flashbots.simulate(bundled_transactions=bundle, block_tag=w3.eth.block_number)

elvise7777 commented 2 years ago

@lecoos I simulate the bundle successfully. simulation = self.w3.flashbots.simulate(bundle, block)

elvise7777 commented 2 years ago

@lecoos Does the successful simulation mean my bundle will be executed in the current block?

lecoos commented 2 years ago

@elvise7777 Nope. Simulation could only be used to simulate a bundle against a specific history block number, or simulate a bundle at the top of the next block. So you can't predict your bundle execution by the result of simulation, just for finding failed reason.

elvise7777 commented 2 years ago

@lecoos Got it. It's hard to do arbitrage on mainnet. Thanks a lot.

elvise7777 commented 2 years ago

@lecoos Do you know why it take a long time almost 20s before send to miner?

image

lecoos commented 2 years ago

@elvise7777 The flashbots relayer will prerun transactions, package txns into mega bundle and other processes. 20s should be reasonable if it was at the peak trading period. I think you can increase the gas price so that your txns are at the forefront of prerun process, to shorten the on-chain time.

elvise7777 commented 2 years ago

@lecoos Thanks a lot. I only send one arb transaction successful, after a loooooong time. Sending bribe through contract or increasing the gas price, which way is better?

lecoos commented 2 years ago

@elvise7777 Gas price. And you should notice base fee if you use EIP1559.

elvise7777 commented 2 years ago

@lecoos Thanks a lot.