Last active
March 16, 2023 19:34
-
-
Save Reecepbcups/c7c04f199b4ae53140a141a1e0dd98cf to your computer and use it in GitHub Desktop.
Gets the value of the community pool in FIAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://docs.junonetwork.io/developer-guides/miscellaneous/get-token-prices | |
# | |
# pip install pycoingecko | |
# python3 main.py | |
# | |
# https://pypi.org/project/pycoingecko/ | |
from pycoingecko import CoinGeckoAPI | |
from requests import get | |
rest_api = "https://lcd.terrarebels.net/cosmos/distribution/v1beta1/community_pool" | |
# add `denom: coingecko_id` to here for each denom to get the price of & calulate | |
pairs: dict[str, str] = { | |
"uluna": "terra-luna", | |
"uusd": "terrausd", | |
} | |
ids = ",".join(pairs.values()) | |
currencies = "usd,eur" | |
def main(): | |
cg = Coingecko() | |
# {'terra-luna': {'usd': 0.0001267, 'eur': 0.00011934}, 'terrausd': {'usd': 0.02254546, 'eur': 0.02123563}} | |
prices = cg.get_prices() | |
# usd: 0 | |
total: dict[str, int] = {} | |
denoms = get_community_pool_amount() | |
for denom in denoms: | |
if denom["denom"] in pairs: | |
coingecko_id = pairs[denom["denom"]] | |
amount = int(float(denom["amount"]) / 1_000_000) | |
for fiat in currencies.split(","): | |
if fiat not in total: | |
total[fiat] = 0 | |
price = prices[coingecko_id][fiat] | |
total[fiat] += round(amount * price, 2) | |
print(f"Total: {'; '.join([f'{k}: {v:,}' for k, v in total.items()])}") | |
def get_community_pool_amount() -> list[dict]: | |
# query rest_api | |
pool = get(rest_api).json().get("pool", []) | |
return pool | |
class Coingecko: | |
# https://www.coingecko.com/en/api/documentation | |
def __init__(self): | |
api_key = "" | |
if len(api_key) > 0: | |
self.cg = CoinGeckoAPI(api_key=api_key) | |
else: | |
self.cg = CoinGeckoAPI() | |
def __get_symbols(self): | |
values = {} | |
for _id in ids.split(","): | |
data = self.cg.get_coin_by_id(_id) | |
symbol = data.get("symbol", "") | |
values[_id] = symbol | |
return values | |
def get_prices(self) -> dict: | |
return self.cg.get_price(ids=ids, vs_currencies=currencies) | |
def pretty_prices(self): | |
updated_coins = {} | |
symbols = self.__get_symbols() | |
for k, v in self.get_prices().items(): | |
symbol = str(symbols.get(k, k)).upper() | |
updated_coins[symbol] = {"coingecko-id": k, "prices": v} | |
return updated_coins | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment