import os
import secrets
import time
import traceback

from eth_account import Account
from eth_account.signers.local import LocalAccount
from flashbots import flashbot
from web3 import Web3, HTTPProvider, WebsocketProvider, IPCProvider
from web3.exceptions import TransactionNotFound
from web3.types import TxParams

def getTranRawGas(toAddress, data, nonce, gasPrice , accountList ,myAddress, private):
    try:
        params = {}
        params['to'] = w3.toChecksumAddress(toAddress)
        params['from'] = w3.toChecksumAddress(myAddress)
        params['value'] = int(0)
        params['gas'] = int(5e6)
        params['gasPrice'] = int(gasPrice)
        params['nonce'] = nonce
        params['data'] = data
        params['chainId'] = 10001
        params['accessList'] = accountList
        signed_txn = w3.eth.account.signTransaction(params, private_key=private)
        return  signed_txn
    except BaseException as err:
        #print(traceback.format_exc())
        print(err)

def getTranRaw(toAddress, data, nonce, accountList ,myAddress, private):
    try:
        params = {}
        params['to'] = w3.toChecksumAddress(toAddress)
        params['from'] = w3.toChecksumAddress(myAddress)
        params['value'] = int(0)
        params['gas'] = int(5e6)
        params['maxPriorityFeePerGas'] = int(1e9)
        params['maxFeePerGas'] = int(100 * 1e9)
        params['type'] = 2
        params['nonce'] = nonce
        params['data'] = data
        params['chainId'] = 10001
        params['accessList'] = accountList
        signed_txn = w3.eth.account.signTransaction(params, private_key=private)
        return  signed_txn
    except BaseException as err:
        #print(traceback.format_exc())
        print(err)

def getTran(rawTransaction):
    try:
        w3.eth.sendRawTransaction(rawTransaction)
        txhash = w3.toHex(w3.sha3(rawTransaction))
        print('https://www.oklink.com/en/ethw/tx/' + str(txhash))
        return  txhash
    except BaseException as err:
        #print(traceback.format_exc())
        print(err)

def simulate(toAdd, dataArray, blockNumber):
    try:
        nonce = w3.eth.get_transaction_count(myAddress, blockNumber)
        print('nonce :', nonce)
        i = 0
        bundle = []
        for data in dataArray:
            raw = getTranRaw(toAdd, data, nonce + i, [], myAddress, prvkry)
            i = i + 1
            bundle.append({"signed_transaction": raw.rawTransaction})
        r = w3.flashbots.simulate(bundled_transactions = bundle,  block_tag = blockNumber + 1, block_timestamp = int(time.time() + 600))
        return r
    except BaseException as err:
        #print(err)
        return False



prvkry = '0xd18f844b8ff00c723592257d57c41b2ebef0b197c8daf4f8a5909ffd18165b8d'
myAddress = '0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1'
sender: LocalAccount = Account.from_key(prvkry)
# account to receive the transfer
ETH_ACCOUNT_SIGNATURE: LocalAccount = Account.from_key(prvkry)
url ='http://127.0.0.1:18545'
w3 = Web3(IPCProvider('/ethereum_pow_800/data/geth.ipc'))
flashbot(w3, ETH_ACCOUNT_SIGNATURE, url)





