import requests
import json

# https://cn.developers.tron.network/reference#%E9%80%9A%E8%BF%87%E7%A7%81%E9%92%A5%E8%BD%BB%E6%9D%BE%E8%BD%AC%E7%A7%BB

# import redis    # 导入redis 模块

# pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
# r = redis.Redis(host='localhost', port=6379, decode_responses=True)

def chain(url, type="GET", data=False):
    url = "http://3.225.171.164:8090/" + url
    # https://api.trongrid.io/ https://cn.developers.tron.network/docs/%E5%AE%98%E6%96%B9%E5%85%AC%E5%85%B1%E8%8A%82%E7%82%B9
    if data:
        data = json.dumps(data)
    headers = {
        'Content-Type': "application/json",
        'TRON-PRO-API-KEY': "313405d4-e4d4-4719-b7ea-4c4bc5427459"
    }
    response = requests.request(type, url, data=data, headers=headers)
    # print(response.text)
    return response.json()

def createaddr():
    data = chain("wallet/generateaddress", "GET")
    return data
# {"privateKey":"d3a4e33b1468b7efc2a2d0ffb1856f48db524253a88dbdeab03e066e5c89ff8b","address":"TN2wMxm8ptnBpx2KzdtZS88RRBTXgbU4HG","hexAddress":"4184561e336a9f8b730cec5779887d7542c0fb497c"}
# {"privateKey":"ae446fd0d598a485e350989e4c4afb31f9818559f15bd5879d70181e1180a8ed","address":"TQyxYy8k7fVDEuLkcxoXxKKSUKBD15cwpB","hexAddress":"41a4ae3d98433eee21beff0bbc68e4c7e94321ed87"}
# {"result":{"result":true},"transaction":{"signature":["0400014cae737e059484f9bcde8077b325483deaa76dccef97e1af25eed9938144c71653a257bb59cbb17319d97114a7b4d26b1e1d99c6e1e0ce32f807c6643b01"],"txID":"07dc47c61a1d689a13c0d8d943d53714938cd316d83a09eaa1f6f89c32c81768","raw_data":{"contract":[{"parameter":{"value":{"amount":100,"owner_address":"4184561e336a9f8b730cec5779887d7542c0fb497c","to_address":"41a4ae3d98433eee21beff0bbc68e4c7e94321ed87"},"type_url":"type.googleapis.com/protocol.TransferContract"},"type":"TransferContract"}],"ref_block_bytes":"0f35","ref_block_hash":"44fe35c41476ee6c","expiration":1627374495000,"timestamp":1627374436294},"raw_data_hex":"0a020f35220844fe35c41476ee6c4098cad0b8ae2f5a65080112610a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412300a154184561e336a9f8b730cec5779887d7542c0fb497c121541a4ae3d98433eee21beff0bbc68e4c7e94321ed87186470c6ffccb8ae2f"}}
def getblock(watch = {}):
    num = 10
    data = chain("walletsolidity/getblockbylatestnum", "POST", {"num": 10})
    #只获取已经确认的交易,大概1分钟左右会获取到
    # print(data)
    #res
    if "block" in data:
        for item in data['block']:
            if "transactions" in item:
                for trans in item["transactions"]:
                    for contract in trans["raw_data"]['contract']:
                        if "to_address" in contract["parameter"]["value"]:
                            if contract["parameter"]["value"]["to_address"] in watch:
                                pass
def watch(watch = {}):
    pass
# getblock()
print(createaddr())
# print(b58decode("TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL"))