from web3Tools import * 

def newAccount():
    r = w3.provider.make_request('personal_newAccount', params = ['qwe410410'])
    return r

def personal_importRawKey(pky):#
    r = w3.provider.make_request('personal_importRawKey', params = [pky, 'qwe410410'])
    return r

def personal_listAccounts():
    r = w3.provider.make_request('personal_listAccounts', params = [])
    return r

def personal_unlockAccount(address):
    r = w3.provider.make_request('personal_unlockAccount', params = [address, 'qwe410410', 600000000])
    return r


def unlock_allAccount():
    myAccount = personal_listAccounts()['result']
    print(myAccount)
    for address in myAccount:
        printTime(address, personal_unlockAccount(address))

def personal_lockAccount(address):
    r = w3.provider.make_request('personal_lockAccount', params = [address])
    return r


def lock_allAccount():
    myAccount = personal_listAccounts()['result']
    print(myAccount)
    for address in myAccount:
        printTime(address, personal_lockAccount(address))

def get_balance():
    myAccount = personal_listAccounts()['result']
    print(len(myAccount))
    totalBalance = 0 
    for address in myAccount:
        amount = w3.eth.get_balance(w3.toChecksumAddress(address)) / 1e18
        printTime(address, amount)
        totalBalance = totalBalance + amount
    printTime(totalBalance)


def autoBalance(amount):
    myAccount = personal_listAccounts()['result'][:10]
    for address in myAccount:
        if address.lower() == '0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1'.lower():
            continue
        if w3.eth.get_balance(w3.toChecksumAddress(address))  > amount + 1e17 :
        
            tradeParams = {}
            tradeParams['from'] = address
            tradeParams['to'] = '0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1' 
            tradeParams['gas'] = hex(int(1e6))
            tradeParams['value'] = hex(int(w3.eth.get_balance(w3.toChecksumAddress(address)) - amount))
            tradeParams['gasPrice'] = hex(int(1e10))
            printTime(tradeParams['from'], '>', tradeParams['to'], tradeParams['value'])


        elif w3.eth.get_balance(w3.toChecksumAddress(address))  < amount - 1e17:
            tradeParams = {}
            tradeParams['from'] = '0x000000FB5e4fbEE939625B0099288bCF51Ed6FA1'
            tradeParams['to'] = address
            tradeParams['gas'] = hex(int(1e6))
            tradeParams['value'] = hex(int(amount - w3.eth.get_balance(w3.toChecksumAddress(address))))
            tradeParams['gasPrice'] = hex(int(1e10))          
            printTime(tradeParams['from'], '>', tradeParams['to'], tradeParams['value']) 
        try:
            result = w3.provider.make_request('eth_sendTransaction', params=[tradeParams])
            printTime(result)
        except:
            hs = 'err seed'   


def withdral(token, address):
    token = token.replace('0x', '')
    inputData = '0xbd6ddabe000000000000000000000000' + token
    params = {}
    params['method'] = 'send'
    params['params'] = {'toAddress': address, 'inputData': inputData,
                        'gasPrice': 1e10, 'gasLimit': 1e5}

    hs = requests.post(url='http://127.0.0.1:410/operation', json=params).text
    printTime(hs)


def withdralAll(address):
    withdral(WETH, address)   
    withdral(WETHW, address)   
    withdral('0x2ad7868ca212135c6119fd7ad1ce51cfc5702892', address)   
    withdral('0x11bbb41b3e8baf7f75773db7428d5acee25fec75', address)  


#print(personal_listAccounts()['result'])

on = input('withdralAll: ')
if on:
    withdralAll(v4Address)
    withdralAll(v5Address)
#print(personal_importRawKey(''))
on = input('autoBalance: ')
if on:
    autoBalance(1.5* 1e18)
get_balance()

on = input('unlock_allAccount: ')
if on:
    unlock_allAccount()





