---
# System prepended metadata

title: Untitled

---

```
load("render.star", "render")
load("schema.star", "schema")
load("http.star", "http")
load("encoding/base64.star", "base64")
load("cache.star", "cache")
load("humanize.star", "humanize")

DEFAULT_TOKEN = "ethereum"

CG_PRICE_URL = "https://api.coingecko.com/api/v3/coins/{}"

def main(config):

    user_defined_token = config.get("token") or DEFAULT_TOKEN

    include_eth = config.get("include_eth")

    include_btc = config.get("include_btc")

    interval = int(config.get("interval","5")) * 1000

    cg_price_query = CG_PRICE_URL.format(user_defined_token)
    
    resp = http.get(cg_price_query)

    TOKEN_ICON = resp.json()["image"]["thumb"]
    thumbimage = http.get(TOKEN_ICON).body()
    #Unfortunately need to encode/decode to avoid error
    thumbimage_enc = base64.encode(thumbimage)
    TOKEN_ICON = base64.decode(thumbimage_enc)

    usd_rate_cached = cache.get(user_defined_token + "_usd_rate")
    if usd_rate_cached != None:
        print("Hit! Displaying cached data.")
        print(usd_rate_cached)
        usd_rate = usd_rate_cached
    else:
        print("Miss! Calling CoinGecko API.")
        if resp.status_code != 200:
            fail("CoinGecko request failed with status %d", resp.status_code)
        usd_rate = resp.json()["market_data"]["current_price"]["usd"]
        usd_rate = humanize.ftoa(usd_rate,4)
        print(usd_rate)
        cache.set(user_defined_token + "_usd_rate", usd_rate, ttl_seconds = 300)
    
    eth_rate_cached = cache.get(user_defined_token + "_eth_rate")
    if eth_rate_cached != None:
        print("Hit! Displaying cached data.")
        print(eth_rate_cached)
        eth_rate = eth_rate_cached
    else:
        print("Miss! Calling CoinGecko API.")
        if resp.status_code != 200:
            fail("CoinGecko request failed with status %d", resp.status_code)
        eth_rate = resp.json()["market_data"]["current_price"]["eth"]
        eth_rate = humanize.ftoa(eth_rate,4)
        print(eth_rate)
        cache.set(user_defined_token + "_eth_rate", eth_rate, ttl_seconds = 300)

    btc_rate_cached = cache.get(user_defined_token + "_btc_rate")
    if btc_rate_cached != None:
        print("Hit! Displaying cached data.")
        print(btc_rate_cached)
        btc_rate = btc_rate_cached
    else:
        print("Miss! Calling CoinGecko API.")
        if resp.status_code != 200:
            fail("CoinGecko request failed with status %d", resp.status_code)
        btc_rate = resp.json()["market_data"]["current_price"]["btc"]
        btc_rate = humanize.ftoa(btc_rate,4)
        print(btc_rate)
        cache.set(user_defined_token + "_btc_rate", btc_rate, ttl_seconds = 300)
    
    #Build the Display based on User Toggles
    vector = []
    if include_eth and include_btc:
        vector = [
            render.Text("$%g" % float(usd_rate)),
            render.Text("Ξ%g" % float(eth_rate)),
            render.Text("B%g" % float(btc_rate))
        ]
    elif include_eth:
        vector = [
            render.Text("$%g" % float(usd_rate)),
            render.Text("Ξ%g" % float(eth_rate))
        ]
    else:
        vector = [
            render.Text("$%g" % float(usd_rate))
        ]
    
    return render.Root(
        child = render.Box(
            render.Row(
                expanded = True,
                main_align = "space_between",
                cross_align = "center",
                children = [
                    render.Image(src = TOKEN_ICON),
                    render.Animation(
                        children = vector
                    )
                ],

            ),
        ),delay=5000
    )

def get_schema():
    return schema.Schema(
        version = "1",
        fields = [
            schema.Text(
                id = "token",
                name = "Token?",
                desc = "Insert Token Name from CoinGecko (ex. governance-ohm)",
                icon = "coin-vertical"
            ),
            schema.Toggle(
                id = "include_eth",
                name = "Show ETH Price",
                desc = "Display ETH Price",
                icon = "ethereum",
                default = False
            ),
            schema.Toggle(
                id = "include_btc",
                name = "Show BTC Price",
                desc = "Display BTC Price",
                icon = "btc",
                default = False
            ),
            schema.Text(
                id = "interval",
                name = "Time Interval (sec)",
                desc = "Price Rotation (default 5)",
                icon = "timer"
            )
        ]
    )
```