flashApi.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import secrets
  3. import time
  4. import traceback
  5. from eth_account import Account
  6. from eth_account.signers.local import LocalAccount
  7. from flashbots import flashbot
  8. from web3 import Web3, HTTPProvider, WebsocketProvider, IPCProvider
  9. from web3.exceptions import TransactionNotFound
  10. from web3.types import TxParams
  11. def getTranRawGas(toAddress, data, nonce, gasPrice , accountList ,myAddress, private):
  12. try:
  13. params = {}
  14. params['to'] = w3.toChecksumAddress(toAddress)
  15. params['from'] = w3.toChecksumAddress(myAddress)
  16. params['value'] = int(0)
  17. params['gas'] = int(5e6)
  18. params['gasPrice'] = int(gasPrice)
  19. params['nonce'] = nonce
  20. params['data'] = data
  21. params['chainId'] = 10001
  22. params['accessList'] = accountList
  23. signed_txn = w3.eth.account.signTransaction(params, private_key=private)
  24. return signed_txn
  25. except BaseException as err:
  26. #print(traceback.format_exc())
  27. print(err)
  28. def getTranRaw(toAddress, data, nonce, accountList ,myAddress, private):
  29. try:
  30. params = {}
  31. params['to'] = w3.toChecksumAddress(toAddress)
  32. params['from'] = w3.toChecksumAddress(myAddress)
  33. params['value'] = int(0)
  34. params['gas'] = int(5e6)
  35. params['maxPriorityFeePerGas'] = int(1e9)
  36. params['maxFeePerGas'] = int(100 * 1e9)
  37. params['type'] = 2
  38. params['nonce'] = nonce
  39. params['data'] = data
  40. params['chainId'] = 10001
  41. params['accessList'] = accountList
  42. signed_txn = w3.eth.account.signTransaction(params, private_key=private)
  43. return signed_txn
  44. except BaseException as err:
  45. #print(traceback.format_exc())
  46. print(err)
  47. def getTran(rawTransaction):
  48. try:
  49. w3.eth.sendRawTransaction(rawTransaction)
  50. txhash = w3.toHex(w3.sha3(rawTransaction))
  51. print('https://www.oklink.com/en/ethw/tx/' + str(txhash))
  52. return txhash
  53. except BaseException as err:
  54. #print(traceback.format_exc())
  55. print(err)
  56. def simulate(toAdd, dataArray, blockNumber):
  57. try:
  58. nonce = w3.eth.get_transaction_count(myAddress, blockNumber)
  59. print('nonce :', nonce)
  60. i = 0
  61. bundle = []
  62. for data in dataArray:
  63. raw = getTranRaw(toAdd, data, nonce + i, [], myAddress, prvkry)
  64. i = i + 1
  65. bundle.append({"signed_transaction": raw.rawTransaction})
  66. r = w3.flashbots.simulate(bundled_transactions = bundle, block_tag = blockNumber + 1, block_timestamp = int(time.time() + 600))
  67. return r
  68. except BaseException as err:
  69. #print(err)
  70. return False
  71. prvkry = '0xd18f844b8ff00c723592257d57c41b2ebef0b197c8daf4f8a5909ffd18165b8d'
  72. myAddress = '0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1'
  73. sender: LocalAccount = Account.from_key(prvkry)
  74. # account to receive the transfer
  75. ETH_ACCOUNT_SIGNATURE: LocalAccount = Account.from_key(prvkry)
  76. url ='http://127.0.0.1:18545'
  77. w3 = Web3(IPCProvider('/ethereum_pow_800/data/geth.ipc'))
  78. flashbot(w3, ETH_ACCOUNT_SIGNATURE, url)