import time
from flask import Flask
from flask import request
import requests
from web3Tools import *
PARAMS_ERROR = {
    'rst': False,
    'msg': 'params error.'
}

app = Flask(__name__)

def pong(data):
    printTime('ok',data)
    return time.time()

def sendTransactionWithAddress_Gas_Nonce(params):
    toAddress = params['toAddress']
    fromAddress = params['fromAddress']
    inputData = params['inputData']
    gasPrice = params['gasPrice']
    nonce = params['nonce']
    gasLimit = params['gasLimit']
    hs = w3.new.sendTransaction(fromAddress, toAddress, inputData, nonce, gasPrice, gasLimit)
    return str(hs)


def sendTransactionAutoAddressWithGas(params):
    toAddress = params['toAddress']
    inputData = params['inputData']
    gasPrice = params['gasPrice']
    gasLimit = params['gasLimit']
    global addressIndex
    if addressIndex >= len(myAddressArray):
        addressIndex = 0
    fromAddress = myAddressArray[addressIndex]
    nonce = int(w3.new.getTransactionCount(fromAddress),16)
    hs = w3.new.sendTransaction(fromAddress, toAddress, inputData, nonce, gasPrice, gasLimit)
    addressIndex = addressIndex + 1
    return str(hs)

def sendTransaction(params):
    global addressIndex
    if addressIndex >= len(myAddressArray):
        addressIndex = 0
    fromAddress = myAddressArray[addressIndex]
    nonce = int(w3.new.getTransactionCount(fromAddress),16)
    addressIndex = addressIndex + 1
    params['from'] = fromAddress
    params['nonce'] = hex(int(nonce))
    params['chainId'] = hex(10001)
    result = w3.provider.make_request('eth_sendTransaction', params=[params])
    try:
        if 'error' in result:
            raise NameError(result['error'])
        if 'result' in result:
            hs =  result['result']
            printTime('https://www.oklink.com/en/ethw/tx/' + str(hs))
            return str(hs)

    except BaseException as err:
        printTime('sendTransaction err', params['from'], params['nonce'], err)
        return False



@app.route('/operation', methods=['POST'])
def operation():
    request_data = request.json
    printTime(request_data)
    method = request_data['method']
    try:
        # 解析参数
        params = request_data['params']
        if params is None:
            return PARAMS_ERROR
        if method == 'PING':
            r = pong(params)
            
        elif method == 'send':
            r = sendTransactionAutoAddressWithGas(params)

        elif method == 'sendAddress':
            r = sendTransactionWithAddress_Gas_Nonce(params)

        elif method == 'sendTransaction':
            r = sendTransaction(params)
        else:
            r = 0
        printTime('-' * 60)
        return str(r)
    except BaseException as err:
        printTime('-' * 60)
        printTime(err)
        return False



if __name__ == '__main__':
    myAddressArray = ['0x1c5c6857fd3fefdfa235370289620bd3fe20c883', '0x5160c5060c148b71e0245fb8436ee0cb1960561e', '0x90afc8617f6c9d295049e76033dda58dc2c4cc08', '0x454408d88e9b4d2bfd17cd09244929dddaed6d92', '0x10464a0e69e5bbd01b9bdb7f2943a103a885ff29', '0xe44e4cdc95e6007d272d8d75e60345225879a8c6', '0x50637fb9dfd3a099023b8932f71e3416034e5a6b', '0xef42cfb845967a841c93e9657ee25eb27ccbace1', '0x48fdc21b2849aeb9f23ffde1a96acbbe0d0194cb', '0x841561ca4d4505464d392fb25713d7f5aec12893', '0x1712b216fdd1943e5fcd75ec945588ddb4765c0f', '0x7514b72dc60cdca974654b6f6074adf1d5f6b0b1', '0xbcaa29727bf3f0e5d6235c2c20e2fe994390d81b', '0x648bac1b5b01942eef0c18acb648e9ba16a62123', '0x5261d48e1d3d01154c7dfbacceb00e3cc8d74840', '0xfe0bd6ce1819be31213ae4aca63f16dbaf19923d', '0x6373fab021646e1e4cc83c84791af2d61ba881c3', '0x7b02a8771111ff820d749cd06f3e4c550a8aab93', '0x6ae50742b3e93e25560be5ee29bc4c9fa66b83f0', '0x3e160a4b1771cbef907d826a6d95489091aac230', '0xa3437a046ef34c52a5a8631af3bd027315b29a9d', '0xc760145514070e3b711b421d382cd8aad9da90e5', '0x0753168e162b294be3e4216e459cbc7d3191adbf', '0x867c32e59dd70ca4ef9f1163df728debfe09279d', '0xa51e7b6c32dc44589a713b35c4b8cc6bb1acfe74', '0x1395e54e8ed39524930b1742c1d173ab73dda8de', '0x67b6fc8977ff9cb5ae260623570d1ef731fc7453', '0x2dd1e0b679ac05ce757b9d978b3d04958fceeb47', '0xbf735653f5caeb61e627042e6c2bf4df9810df56', '0x439ae758fb7c0a54d23d9340e01fc667a2c94a7f', '0x4e10b98c70658f0b95c932e8c2d73604573e7302', '0x000000fb5e4fbee939625b0099288bcf51ed6fa1']
    myAddressArray = myAddressArray[:10]
    printTime(len(myAddressArray), myAddressArray)
    addressIndex = 0
    app.run(host='0.0.0.0', port=410)