Example #1
0
    def __post_init__(self):
        kwargs = DictObject(self.raw_data)
        _maxs, _circ = self.max_supply, self.circulating_supply

        self.max_supply = conv_dec(kwargs.get('maxSupply', self.max_supply))
        self.max_supply = conv_dec(kwargs.get('max_supply', self.max_supply))

        self.circulating_supply = conv_dec(
            kwargs.get('circulatingSupply', self.circulating_supply))
        self.circulating_supply = conv_dec(
            kwargs.get('circulating_supply', self.circulating_supply))

        self.supply = Decimal(kwargs.get('supply', self.supply))

        meta = self.metadata
        try:
            meta = {} if empty(meta, itr=True) else (
                json.loads(meta) if isinstance(meta, str) else meta)
        except json.JSONDecodeError:
            log.warning("Failed to decode Token metadata as JSON: %s", meta)
            meta = {}
        self.metadata = TokenMetadata.from_dict(meta)
Example #2
0
            assetObject.asset_id) else assetObject.asset_id
        # Map the asset's symbol (e.g. HIVE) to the Asset object, so it can be matched by both asset ID and symbol.
        new_assets[assetObject.symbol] = assetObject

    return new_assets


######
# For each network in CHAIN_ASSETS and KNOWN_ASSETS, make sure every asset type can be matched by both asset ID
# (i.e. IDs starting with "@@0000"), and their symbol (e.g. "HIVE").
# noinspection PyTypeChecker
for chain in list(CHAIN):  # type: CHAIN
    KNOWN_ASSETS[chain.value] = add_known_asset_symbols(
        KNOWN_ASSETS.get(chain.value, {}))
    CHAIN_ASSETS[chain.name] = add_known_asset_symbols(
        CHAIN_ASSETS.get(chain.name, {}))


@dataclass
class Amount(DictDataClass):
    asset: Asset
    amount: Decimal

    @property
    def symbol(self) -> str:
        return self.asset.symbol

    @property
    def precision(self) -> int:
        return self.asset.precision
    def _node_table_row(cls, node: NodeStatus) -> Tuple[str, NodeTableRow]:
        statuses = {
            0: Fore.RED + "DEAD",
            1: Fore.LIGHTRED_EX + "UNSTABLE",
            2: Fore.YELLOW + "Unreliable",
            3: Fore.GREEN + "Online",
        }

        # Decide on the node's status based on how many test stages the
        status = statuses[len(node.raw)]

        # Calculate the average response time of this node by totalling the timing seconds, and dividing them
        # by the amount of individual timing events
        avg_res = 'error'
        if len(node.timing) > 0:
            time_total = 0.0
            for time_type, time in node.timing.items():
                time_total += time
            avg_res = time_total / len(node.timing)
            avg_res = '{:.2f}'.format(avg_res)

        # Calculate the average tries required per successful call by summing up the total amount of tries,
        # and dividing that by the length of the 'tries' dict (individual calls / tests that were tried)
        avg_tries = 'error'
        if len(node['tries']) > 0:
            tries_total = 0
            for tries_type, tries in node.tries.items():
                tries_total += tries
            avg_tries = tries_total / len(node.tries)
            avg_tries = '{:.2f}'.format(avg_tries)

        if node.time_behind:
            if node.time_behind.total_seconds() >= 60:
                status = f"{Fore.LIGHTRED_EX}Out-of-sync"

        # If there were any moderate errors while testing the node, change the status from green to yellow, and
        # change the status to the error state
        if 'err_reason' in node and not empty(node.err_reason, True, True):
            status = Fore.YELLOW + node.err_reason
        host = str(node.host)
        # Replace the long http:// | https:// URI prefix with a short, clean character in brackets
        host = host.replace('https://', '(S)')
        host = host.replace('http://', '(H)')

        # Select the appropriate coloured host type symbol based on the node's detected 'srvtype'
        def_stype = f"{Fore.RED}(?){Fore.RESET}"
        host_stypes = DictObject(jussi=f"{Fore.GREEN}(J){Fore.RESET}",
                                 appbase=f"{Fore.BLUE}(A){Fore.RESET}",
                                 legacy=f"{Fore.MAGENTA}(L){Fore.RESET}")

        # If plugin scanning was enabled, generate and append the working vs. total plugin stat column
        # to the fmt_str row.
        f_plugins = ''
        if settings.plugins:
            plg, ttl_plg = len(node.plugins), len(TEST_PLUGINS_LIST)

            f_plugins = f'{plg} / {ttl_plg}'
            if plg <= (ttl_plg // 3):
                f_plugins = f'{Fore.RED}{f_plugins}'
            elif plg <= (ttl_plg // 2):
                f_plugins = f'{Fore.LIGHTRED_EX}{f_plugins}'
            elif plg < ttl_plg:
                f_plugins = f'{Fore.YELLOW}{f_plugins}'
            elif plg == ttl_plg:
                f_plugins = f'{Fore.GREEN}{f_plugins}'

            f_plugins += Fore.RESET

        return node.host, cls.NodeTableRow(server=host,
                                           server_type=host_stypes.get(
                                               node.srvtype, def_stype),
                                           ssl='https://' in node.host,
                                           status=status,
                                           head_block=node.current_block,
                                           block_time=node.block_time,
                                           version=node.version,
                                           network=node.network,
                                           res_time=avg_res,
                                           avg_retries=avg_tries,
                                           api_tests=f_plugins)