def get_validators() -> pd.DataFrame: """Get information about terra validators [Source: https://fcd.terra.dev/swagger] Returns ------- pd.DataFrame terra validators details """ response = _make_request("staking")["validators"] results = [] for validator in response: results.append({ "accountAddress": validator["accountAddress"], "validatorName": validator["description"].get("moniker"), "tokensAmount": denominate_number(validator["tokens"]), "votingPower": round((float(validator["votingPower"].get("weight")) * 100), 2), "commissionRate": round((float(validator["commissionInfo"].get("rate", 0)) * 100), 2), "status": validator["status"], "uptime": round((float(validator.get("upTime", 0)) * 100), 2), }) return pd.DataFrame(results).sort_values(by="votingPower")
def get_staking_account_info(address: str = "") -> Tuple[pd.DataFrame, str]: """Get staking info for provided terra account [Source: https://fcd.terra.dev/swagger] Parameters ---------- address: str terra blockchain address e.g. terra1jvwelvs7rdk6j3mqdztq5tya99w8lxk6l9hcqg Returns ------- Tuple[pd.DataFrame, str]: luna delegations and summary report for given address """ response = _make_request(f"staking/{address}") results: Dict[str, Any] = {"myDelegations": []} for field in ["availableLuna", "delegationTotal"]: results[field] = denominate_number(response.get(field, 0)) my_delegations = response.get("myDelegations") if my_delegations: for delegation in my_delegations: validator = _adjust_delegation_info(delegation) results["myDelegations"].append(validator) df = pd.DataFrame(results["myDelegations"]) try: df["validatorName"] = df["validatorName"].apply( lambda x: lambda_replace_unicode(x)) df.columns = prettify_column_names(list(df.columns)) except KeyError: df = pd.DataFrame() results["totalRewards"] = denominate_number( response.get("rewards", {}).get("total", 0)) report = f"""Overview: Address: {address} Available Luna: {results['availableLuna']} Delegated Luna: {results['delegationTotal']} Total Rewards: {results['totalRewards']}\n""" report += "\nDelegations: " if not df.empty else "\nNo delegations found\n" return df, report
def _adjust_delegation_info(delegation: dict) -> dict: """Helper method which removes redundant fields from delegation info dictionary, and denominate value fields. [Source: https://fcd.terra.dev/v1] Parameters ---------- delegation: dictionary object with delegation data e.g. Returns ------- dict adjusted dictionary with delegation data """ delegation_info = {} for key, value in delegation.items(): if key in ["amountDelegated", "totalReward"]: delegation_info[key] = denominate_number(value) elif key in ["validatorAddress", "rewards"]: continue else: delegation_info[key] = value return delegation_info