import time

from walrus import *
from loguru import logger
class RedisIf:
    def __init__(self, host="127.0.0.1", port=6379, password="", db=0):
        self.db = Database(host=host, port=port, db=db)

        self.zbalance = self.db.ZSet("balancev2d")
        '''
        #realize cap progress
        self.rv = self.db.Hash("rv")
        #address and balance progress
        self.addr = self.db.Hash("addr")
        #block volume progress
        self.bv = self.db.Hash("bv")
        #daily volume progress
        self.dv = self.db.Hash("dv")
        '''
        '''
        #stat tx progress
        self.tx = self.db.Hash("tx")

        #ETH daily contract progress
        self.eth_dc = self.db.Hash("ethdc")

        #btc stats fee
        self.btc_stats = self.db.Hash("btcstats")

        #btc stats volume
        self.btc_volume = self.db.Hash("btcvolume")

        # btc stats cdd
        self.btc_cdd = self.db.Hash("btccdd")

        # btc stats cdd days
        self.btc_cdd_days = self.db.Hash("btccdddays")
        '''
        self.btc_block_time = self.db.Hash("btcblocktimev2d")
        '''
        self.btc_sopr = self.db.Hash("btcsopr")
        '''
        self.btc_data = self.db.Hash("btc_datav2d")

        self.active_address = self.db.Set("active_addressv2d")
        self.send_address = self.db.Set("send_addressv2d")
        self.receive_address = self.db.Set("receive_addressv2d")

    def get_btc_data(self, key):
        value = None
        if self.btc_data[key] is not None:
            value = self.btc_data[key]
        return value

    def set_btc_data(self, key, value):
        self.btc_data[key] = value

    def reset_btc_data(self):
        self.btc_data.clear()
        self.zbalance.clear()
        self.btc_block_time.clear()

    '''
    def get_last_btc_sopr(self):
        last_sopr_buy = None
        last_asopr_buy = None
        last_easopr_buy = None
        last_lth_sopr_buy = None
        last_sth_sopr_buy = None
        last_asol = None
        last_eaasol = None

        if self.btc_sopr["last_asol"] is not None:
            last_asol = self.btc_sopr["last_asol"]
            #last_asol = float(self.btc_sopr["last_asol"].decode("utf-8"))
        if self.btc_sopr["last_eaasol"] is not None:
            last_eaasol = self.btc_sopr["last_eaasol"]
            #last_eaasol = float(self.btc_sopr["last_eaasol"].decode("utf-8"))


        if self.btc_sopr["last_sopr_buy"] is not None:
            last_sopr_buy = self.btc_sopr["last_sopr_buy"]
            #last_sopr_buy = float(self.btc_sopr["last_sopr_buy"].decode("utf-8"))
        if self.btc_sopr["last_asopr_buy"] is not None:
            last_asopr_buy = self.btc_sopr["last_asopr_buy"]
            #last_asopr_buy = float(self.btc_sopr["last_asopr_buy"].decode("utf-8"))
        if self.btc_sopr["last_easopr_buy"] is not None:
            last_easopr_buy = self.btc_sopr["last_easopr_buy"]
            #last_easopr_buy = float(self.btc_sopr["last_easopr_buy"].decode("utf-8"))
        if self.btc_sopr["last_lth_sopr_buy"] is not None:
            last_lth_sopr_buy = self.btc_sopr["last_lth_sopr_buy"]
            #last_lth_sopr_buy = float(self.btc_sopr["last_lth_sopr_buy"].decode("utf-8"))
        if self.btc_sopr["last_sth_sopr_buy"] is not None:
            last_sth_sopr_buy = self.btc_sopr["last_sth_sopr_buy"]
            #last_sth_sopr_buy = float(self.btc_sopr["last_sth_sopr_buy"].decode("utf-8"))

        last_sopr_sell = None
        last_asopr_sell = None
        last_easopr_sell = None
        last_lth_sopr_sell = None
        last_sth_sopr_sell = None
        if self.btc_sopr["last_sopr_sell"] is not None:
            last_sopr_sell = self.btc_sopr["last_sopr_sell"]
            # last_sopr_sell = float(self.btc_sopr["last_sopr_sell"].decode("utf-8"))
        if self.btc_sopr["last_asopr_sell"] is not None:
            last_asopr_sell = self.btc_sopr["last_asopr_sell"]
            # last_asopr = float(self.btc_sopr["last_asopr"].decode("utf-8"))
        if self.btc_sopr["last_easopr_sell"] is not None:
            last_easopr_sell = self.btc_sopr["last_easopr_sell"]
            # last_easopr_sell = float(self.btc_sopr["last_easopr_sell"].decode("utf-8"))
        if self.btc_sopr["last_lth_sopr_sell"] is not None:
            last_lth_sopr_sell = self.btc_sopr["last_lth_sopr_sell"]
            # last_lth_sopr_sell = float(self.btc_sopr["last_lth_sopr_sell"].decode("utf-8"))
        if self.btc_sopr["last_sth_sopr_sell"] is not None:
            last_sth_sopr_sell = self.btc_sopr["last_sth_sopr_sell"]
            # last_sth_sopr_sell = float(self.btc_sopr["last_sth_sopr_sell"].decode("utf-8"))

        return last_asol, last_eaasol, last_sopr_buy, last_asopr_buy, last_easopr_buy, last_lth_sopr_buy, last_sth_sopr_buy, last_sopr_sell, last_asopr_sell, last_easopr_sell, last_lth_sopr_sell, last_sth_sopr_sell

    def set_last_btc_sopr(self, last_asol, last_eaasol, last_sopr_buy, last_asopr_buy, last_easopr_buy, last_lth_sopr_buy, last_sth_sopr_buy, last_sopr_sell, last_asopr_sell, last_easopr_sell, last_lth_sopr_sell, last_sth_sopr_sell):
        self.btc_sopr["last_asol"] = last_asol
        self.btc_sopr["last_eaasol"] = last_eaasol

        self.btc_sopr["last_sopr_buy"] = last_sopr_buy
        self.btc_sopr["last_asopr_buy"] = last_asopr_buy
        self.btc_sopr["last_easopr_buy"] = last_easopr_buy
        self.btc_sopr["last_lth_sopr_buy"] = last_lth_sopr_buy
        self.btc_sopr["last_sth_sopr_buy"] = last_sth_sopr_buy
        self.btc_sopr["last_sopr_sell"] = last_sopr_sell
        self.btc_sopr["last_asopr_sell"] = last_asopr_sell
        self.btc_sopr["last_easopr_sell"] = last_easopr_sell
        self.btc_sopr["last_lth_sopr_sell"] = last_lth_sopr_sell
        self.btc_sopr["last_sth_sopr_sell"] = last_sth_sopr_sell
    '''
    def get_block_time(self, height):
        block_time = None
        height_str = str(height)
        if self.btc_block_time[height_str] is not None:
            block_time = int(self.btc_block_time[height_str].decode("utf-8"))
            #block_time = int(self.btc_block_time[height_str].decode("utf-8"))

        return block_time

    def set_block_time(self, height, ts):
        height_str = str(height)
        self.btc_block_time[height_str] = ts

    '''
    def get_last_btc_cdd_days(self):
        last_cdd = None
        last_acdd = None
        last_eacdd = None
        last_cdd_day1= None
        last_cdd_day7 = None
        last_cdd_day30 = None
        last_cdd_day60 = None
        last_cdd_day90 = None
        last_cdd_day180 = None
        last_cdd_day365 = None
        last_cdd_day730 = None

        last_date = None
        last_height = None
        last_date_str = None

        if self.btc_cdd["last_cdd"] is not None:
            last_cdd = float(self.btc_cdd["last_cdd"].decode("utf-8"))
        if self.btc_cdd["last_acdd"] is not None:
            last_acdd = float(self.btc_cdd["last_acdd"].decode("utf-8"))
        if self.btc_cdd["last_eacdd"] is not None:
            last_eacdd = float(self.btc_cdd["last_eacdd"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day1"] is not None:
            last_cdd_day1 = float(self.btc_cdd_days["last_cdd_day1"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day7"] is not None:
            last_cdd_day7 = float(self.btc_cdd_days["last_cdd_day7"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day30"] is not None:
            last_cdd_day30 = float(self.btc_cdd_days["last_cdd_day30"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day60"] is not None:
            last_cdd_day60 = float(self.btc_cdd_days["last_cdd_day60"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day90"] is not None:
            last_cdd_day90 = float(self.btc_cdd_days["last_cdd_day90"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day180"] is not None:
            last_cdd_day180 = float(self.btc_cdd_days["last_cdd_day180"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day365"] is not None:
            last_cdd_day365 = float(self.btc_cdd_days["last_cdd_day365"].decode("utf-8"))
        if self.btc_cdd_days["last_cdd_day730"] is not None:
            last_cdd_day730 = float(self.btc_cdd_days["last_cdd_day730"].decode("utf-8"))
        if self.btc_cdd_days["last_date"] is not None:
            last_date = int(self.btc_cdd_days["last_date"].decode("utf-8"))
        if self.btc_cdd_days["last_height"] is not None:
            last_height = int(self.btc_cdd_days["last_height"].decode("utf-8"))
        if self.btc_cdd_days["last_date_str"] is not None:
            last_date_str = self.btc_cdd_days["last_date_str"].decode("utf-8")
        return last_cdd, last_acdd, last_eacdd, last_cdd_day1, last_cdd_day7, last_cdd_day30, last_cdd_day60, last_cdd_day90, last_cdd_day180, last_cdd_day365, last_cdd_day730, last_date, last_height, last_date_str

    def set_last_btc_cdd_days(self, cdd, acdd, eacdd, day1, day7, day30, day60, day90, day180, day365, day730, dt, height, dtstr):
        self.btc_cdd["last_cdd"] = cdd
        self.btc_cdd["last_acdd"] = acdd
        self.btc_cdd["last_eacdd"] = eacdd
        self.btc_cdd_days["last_cdd_day1"] = day1
        self.btc_cdd_days["last_cdd_day7"] = day7
        self.btc_cdd_days["last_cdd_day30"] = day30
        self.btc_cdd_days["last_cdd_day60"] = day60
        self.btc_cdd_days["last_cdd_day90"] = day90
        self.btc_cdd_days["last_cdd_day180"] = day180
        self.btc_cdd_days["last_cdd_day365"] = day365
        self.btc_cdd_days["last_cdd_day730"] = day730
        self.btc_cdd_days["last_date"] = dt
        self.btc_cdd_days["last_height"] = height
        self.btc_cdd_days["last_date_str"] = dtstr
    '''
    '''
    def get_last_btc_cdd(self):
        last_cdd = None
        last_date = None
        last_height = None
        last_date_str = None
        if self.btc_cdd["last_cdd"] is not None:
            last_cdd = float(self.btc_cdd["last_cdd"].decode("utf-8"))
        if self.btc_cdd["last_date"] is not None:
            last_date = int(self.btc_cdd["last_date"].decode("utf-8"))
        if self.btc_cdd["last_height"] is not None:
            last_height = int(self.btc_cdd["last_height"].decode("utf-8"))
        if self.btc_cdd["last_date_str"] is not None:
            last_date_str = self.btc_cdd["last_date_str"].decode("utf-8")
        return last_cdd, last_date, last_height, last_date_str

    def set_last_btc_cdd(self, cdd, dt, height, dtstr):
        self.btc_cdd["last_cdd"] = cdd
        self.btc_cdd["last_date"] = dt
        self.btc_cdd["last_height"] = height
        self.btc_cdd["last_date_str"] = dtstr
    
    def get_last_btc_volume(self):
        last_volume = None
        last_date = None
        last_height = None
        last_date_str = None
        if self.btc_volume["last_volume"] is not None:
            last_volume = float(self.btc_volume["last_volume"].decode("utf-8"))
        if self.btc_volume["last_date"] is not None:
            last_date = int(self.btc_volume["last_date"].decode("utf-8"))
        if self.btc_volume["last_height"] is not None:
            last_height = int(self.btc_volume["last_height"].decode("utf-8"))
        if self.btc_volume["last_date_str"] is not None:
            last_date_str = self.btc_volume["last_date_str"].decode("utf-8")
        return last_volume, last_date, last_height, last_date_str

    def set_last_btc_volume(self, volume, dt, height, dtstr):
        self.btc_volume["last_volume"] = volume
        self.btc_volume["last_date"] = dt
        self.btc_volume["last_height"] = height
        self.btc_volume["last_date_str"] = dtstr
    '''
    '''
    def get_last_btc_stats(self):
        last_fees = None
        last_date = None
        last_height = None
        last_date_str = None
        last_volume = None
        if self.btc_stats["last_fees"] is not None:
            last_fees = float(self.btc_stats["last_fees"].decode("utf-8"))
        if self.btc_volume["last_volume"] is not None:
            last_volume = float(self.btc_volume["last_volume"].decode("utf-8"))
        if self.btc_stats["last_date"] is not None:
            last_date = int(self.btc_stats["last_date"].decode("utf-8"))
        if self.btc_stats["last_height"] is not None:
            last_height = int(self.btc_stats["last_height"].decode("utf-8"))
        if self.btc_stats["last_date_str"] is not None:
            last_date_str = self.btc_stats["last_date_str"].decode("utf-8")
        return last_fees, last_volume, last_date, last_height, last_date_str

    def set_last_btc_stats(self, fees, volume, dt, height, dtstr):
        self.btc_stats["last_fees"] = fees
        self.btc_volume["last_volume"] = volume
        self.btc_stats["last_date"] = dt
        self.btc_stats["last_height"] = height
        self.btc_stats["last_date_str"] = dtstr


    def get_last_eth_dc(self):
        last_date = None
        last_height = None
        last_date_str = None
        if self.eth_dc["last_date"] is not None:
            last_date = int(self.eth_dc["last_date"].decode("utf-8"))
        if self.eth_dc["last_height"] is not None:
            last_height = int(self.eth_dc["last_height"].decode("utf-8"))
        if self.eth_dc["last_date_str"] is not None:
            last_date_str = self.eth_dc["last_date_str"].decode("utf-8")
        return last_date, last_height, last_date_str

    def set_last_eth_dc(self, dt, height, dtstr):
        self.eth_dc["last_date"] = dt
        self.eth_dc["last_height"] = height
        self.eth_dc["last_date_str"] = dtstr
    '''
    '''
    def get_last_dv(self):
        last_dv = None
        last_date = None
        last_height = None
        last_date_str = None
        if self.dv["last_dv"] is not None:
            last_dv = float(self.dv["last_dv"].decode("utf-8"))
        if self.dv["last_date"] is not None:
            last_date = int(self.dv["last_date"].decode("utf-8"))
        if self.dv["last_height"] is not None:
            last_height = int(self.dv["last_height"].decode("utf-8"))
        if self.dv["last_date_str"] is not None:
            last_date_str = self.dv["last_date_str"].decode("utf-8")
        return last_dv, last_date, last_height, last_date_str

    def set_last_dv(self, dv, dt, height, dtstr):
        self.dv["last_dv"] = dv
        self.dv["last_date"] = dt
        self.dv["last_height"] = height
        self.dv["last_date_str"] = dtstr

    def get_last_bv(self):
        last_height = None
        if self.bv["last_height"] is not None:
            last_height = int(self.bv["last_height"].decode("utf-8"))
        return last_height

    def set_last_bv(self, height):
        self.bv["last_height"] = height
    '''
    '''
    def get_last_ind(self):
        last_csupply = None
        last_mintusd = None
        last_sumcsupply = None
        last_sumcdd = None
        last_sumeacdd = None
        last_rprofit = None
        last_rloss = None
        last_marketcap = None
        last_rcap = None
        last_mvrv = None

        last_earcap = None
        if self.tx["last_csupply"] is not None:
            last_csupply = float(self.tx["last_csupply"].decode("utf-8"))
        if self.tx["last_mintusd"] is not None:
            last_mintusd = float(self.tx["last_mintusd"].decode("utf-8"))
        if self.tx["last_sumcsupply"] is not None:
            last_sumcsupply = float(self.tx["last_sumcsupply"].decode("utf-8"))
        if self.tx["last_sumcdd"] is not None:
            last_sumcdd = float(self.tx["last_sumcdd"].decode("utf-8"))
        if self.tx["last_sumeacdd"] is not None:
            last_sumeacdd = float(self.tx["last_sumeacdd"].decode("utf-8"))
        if self.tx["last_rprofit"] is not None:
            last_rprofit = float(self.tx["last_rprofit"].decode("utf-8"))
        if self.tx["last_rloss"] is not None:
            last_rloss = float(self.tx["last_rloss"].decode("utf-8"))
        if self.tx["last_marketcap"] is not None:
            last_marketcap = float(self.tx["last_marketcap"].decode("utf-8"))
        if self.tx["last_rcap"] is not None:
            last_rcap = float(self.tx["last_rcap"].decode("utf-8"))
        if self.tx["last_earcap"] is not None:
            last_earcap = float(self.tx["last_earcap"].decode("utf-8"))
        if self.tx["last_mvrv"] is not None:
            last_mvrv = float(self.tx["last_mvrv"].decode("utf-8"))


        return last_csupply, last_mintusd, last_sumcsupply, last_sumcdd, last_sumeacdd, last_rprofit, last_rloss, last_marketcap, last_rcap, last_earcap, last_mvrv

    def set_last_ind(self, last_csupply, last_mintusd, last_sumcsupply, last_sumcdd, last_sumeacdd, last_rprofit, last_rloss, last_marketcap, last_rcap, last_earcap, last_mvrv):
        self.tx["last_csupply"] = last_csupply
        self.tx["last_mintusd"] = last_mintusd
        self.tx["last_sumcsupply"] = last_sumcsupply
        self.tx["last_sumcdd"] = last_sumcdd
        self.tx["last_sumeacdd"] = last_sumeacdd
        self.tx["last_rprofit"] = last_rprofit
        self.tx["last_rloss"] = last_rloss
        self.tx["last_marketcap"] = last_marketcap
        self.tx["last_rcap"] = last_rcap
        self.tx["last_earcap"] = last_earcap
        self.tx["last_mvrv"] = last_mvrv
    

    def get_last_tx(self):
        last_profit = None
        last_fees = None
        last_newaddr_cnt = None
        last_newaddr_vol = None
        last_active_addr_cnt = None
        last_tx_addr_cnt = None
        last_rx_addr_cnt = None
        last_vol_change = None
        last_vol = None
        last_avol = None
        last_date = None
        last_height = None
        last_date_str = None
        last_txs = None
        last_eatxs = None
        if self.tx["last_profit_rate"] is not None:
            last_profit = int(self.tx["last_profit"].decode("utf-8"))
        if self.tx["last_fees"] is not None:
            last_fees = int(self.tx["last_fees"].decode("utf-8"))
        if self.tx["last_txs"] is not None:
            last_txs = int(self.tx["last_txs"].decode("utf-8"))
        if self.tx["last_eatxs"] is not None:
            last_eatxs = int(self.tx["last_eatxs"].decode("utf-8"))
        if self.tx["last_newaddr_cnt"] is not None:
            last_newaddr_cnt = int(self.tx["last_newaddr_cnt"].decode("utf-8"))
        if self.tx["last_newaddr_vol"] is not None:
            last_newaddr_vol = float(self.tx["last_newaddr_vol"].decode("utf-8"))
        if self.tx["last_active_addr_cnt"] is not None:
            last_active_addr_cnt = int(self.tx["last_active_addr_cnt"].decode("utf-8"))
        if self.tx["last_tx_addr_cnt"] is not None:
            last_tx_addr_cnt = int(self.tx["last_tx_addr_cnt"].decode("utf-8"))
        if self.tx["last_rx_addr_cnt"] is not None:
            last_rx_addr_cnt = int(self.tx["last_rx_addr_cnt"].decode("utf-8"))
        if self.tx["last_vol_change"] is not None:
            last_vol_change = float(self.tx["last_vol_change"].decode("utf-8"))
        if self.tx["last_vol"] is not None:
            last_vol = float(self.tx["last_vol"].decode("utf-8"))
        if self.tx["last_avol"] is not None:
            last_avol = float(self.tx["last_avol"].decode("utf-8"))
        if self.tx["last_date"] is not None:
            last_date = int(self.tx["last_date"].decode("utf-8"))
        if self.tx["last_height"] is not None:
            last_height = int(self.tx["last_height"].decode("utf-8"))
        if self.tx["last_date_str"] is not None:
            last_date_str = self.tx["last_date_str"].decode("utf-8")
        return last_profit, last_fees, last_txs, last_eatxs, last_newaddr_cnt, last_newaddr_vol, last_active_addr_cnt, last_tx_addr_cnt, last_rx_addr_cnt, last_vol_change, last_vol, last_avol, last_date, last_height, last_date_str

    def set_last_tx(self, last_profit, last_fees, last_txs, last_eatxs, newaddr_cnt, newaddr_vol, active_addr_cnt, tx_addr_cnt, rx_addr_cnt, vol_change, vol, avol, dt, height, dtstr):
        self.tx["last_profit"] = last_profit
        self.tx["last_fees"] = last_fees
        self.tx["last_txs"] = last_txs
        self.tx["last_eatxs"] = last_eatxs
        self.tx["last_newaddr_cnt"] = newaddr_cnt
        self.tx["last_newaddr_vol"] = newaddr_vol
        self.tx["last_active_addr_cnt"] = active_addr_cnt
        self.tx["last_tx_addr_cnt"] = tx_addr_cnt
        self.tx["last_rx_addr_cnt"] = rx_addr_cnt
        self.tx["last_vol_change"] = vol_change
        self.tx["last_vol"] = vol
        self.tx["last_avol"] = avol
        self.tx["last_date"] = dt
        self.tx["last_height"] = height
        self.tx["last_date_str"] = dtstr
    '''
    '''
    def get_last_addr(self):
        last_daily_cnt = None
        last_date = None
        last_height = None
        last_date_str = None
        if self.addr["last_daily_cnt"] is not None:
            last_daily_cnt = int(self.addr["last_daily_cnt"].decode("utf-8"))
        if self.addr["last_date"] is not None:
            last_date = int(self.addr["last_date"].decode("utf-8"))
        if self.addr["last_height"] is not None:
            last_height = int(self.addr["last_height"].decode("utf-8"))
        if self.addr["last_date_str"] is not None:
            last_date_str = self.addr["last_date_str"].decode("utf-8")
        return last_daily_cnt, last_date, last_height, last_date_str

    def set_last_addr(self, daily_cnt, dt, height, dtstr):
        self.addr["last_daily_cnt"] = daily_cnt
        self.addr["last_date"] = dt
        self.addr["last_height"] = height
        self.addr["last_date_str"] = dtstr
    '''

    def is_active_address(self, address):
        result = address in self.active_address
        if not result:
            self.active_address.add(address)
        return result

    def reset_active_address(self):
        self.active_address.clear()

    def get_active_address_cnt(self):
        return len(self.active_address)

    def is_send_address(self, address):
        result = address in self.send_address
        if not result:
            self.send_address.add(address)
        return result

    def reset_send_address(self):
        self.send_address.clear()

    def get_send_address_cnt(self):
        return len(self.send_address)

    def is_receive_address(self, address):
        result = address in self.receive_address
        if not result:
            self.receive_address.add(address)
        return result

    def reset_receive_address(self):
        self.receive_address.clear()

    def get_receive_address_cnt(self):
        return len(self.receive_address)

    def save_addr(self, address, balance):
        new_balance = balance
        if address in self.zbalance:
            new_balance = self.zbalance.score(address) + balance
            #print("update", self.zbalance.score(address), balance, new_balance)
            #time.sleep(10)
            if new_balance < 0.01:
                del self.zbalance[address]
                #print("check exist", address, address in self.zbalance)
                #time.sleep(10)
                return
        self.zbalance.add({address: new_balance})

    '''
    def delete_addr(self, config):
        self.addr.clear()
        self.zbalance.clear()
    '''
    def is_in_addr(self, address):
        return address in self.zbalance

    def get_addr_cnt(self):
        return len(self.zbalance)

    '''
    def delete_rv(self, config):
        self.rv.clear()

    def get_last_rv(self):
        last_rv = None
        last_date = None
        last_height = None
        last_date_str = None
        if self.rv["last_rv"] is not None:
            last_rv = float(self.rv["last_rv"].decode("utf-8"))
        if self.rv["last_date"] is not None:
            last_date = int(self.rv["last_date"].decode("utf-8"))
        if self.rv["last_height"] is not None:
            last_height = int(self.rv["last_height"].decode("utf-8"))
        if self.rv["last_date_str"] is not None:
            last_date_str = self.rv["last_date_str"].decode("utf-8")
        return last_rv, last_date, last_height, last_date_str

    def set_last_rv(self, rv, dt, height, dtstr):
        self.rv["last_rv"] = rv
        self.rv["last_date"] = dt
        self.rv["last_height"] = height
        self.rv["last_date_str"] = dtstr
    '''

    def get_all_address(self):
        return self.zbalance.keys()

    def delete_address_data(self, config):
        self.zbalance.clear()

    '''
    def query_from_address(self, start_balance=0, end_balance=0, address="", limit=0):
        if len(address) > 0:
            results = []
            result = {}
            result["address"] = address
            balance = self.zbalance.score(address)
            print(balance)
            if balance is not None:
                result["balance"] = balance
            results.append(result)
            return results

        match_result = None
        if start_balance > 0:
            if end_balance > 0:
                match_result = self.zbalance.range_by_score(start_balance, end_balance, 0, -1, True, False)
            else:
                match_result = self.zbalance.range_by_score(0, start_balance, 0, -1, True, False)
        else:
            if end_balance > 0:
                match_result = self.zbalance.range_by_score(end_balance, 21000000, 0, -1, True, False)

        results = []
        if match_result is not None:
            #print(match_result)
            for addr, balance2 in match_result:
                address = addr.decode('utf-8')
                result = {}
                result["address"] = address
                result["balance"] = balance2
                results.append(result)
                if limit > 0 and len(results) >= limit:
                    break
        return results
    '''











