Skip to content

Utils

This module contains methods that are backend independent.

get_token_from_registry(asset: str) -> TokenInfo | None

Get token from the Cardano Token Registry.

Parameters:

Name Type Description Default
asset str

The policy + name of the asset.

required

Returns:

Type Description
TokenInfo | None

Either the token information if the asset exists, or None if it doesn't.

Source code in src/charli3_dendrite/backend/utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def get_token_from_registry(asset: str) -> TokenInfo | None:
    """Get token from the Cardano Token Registry.

    Args:
        asset: The policy + name of the asset.

    Returns:
        Either the token information if the asset exists, or None if it doesn't.
    """
    response = requests.get(
        f"https://raw.githubusercontent.com/cardano-foundation/cardano-token-registry/master/mappings/{asset}.json",
        timeout=15,
    )

    if response.status_code != requests.status_codes.codes.OK:
        return None

    response = response.json()

    ticker = "" if "ticker" not in response else response["ticker"]["value"]
    if ticker == "":
        ticker = bytes.fromhex(asset[56:]).decode(encoding="latin_1")
    name = "" if "name" not in response else response["name"]["value"]
    policy_id = asset[:56]
    policy_name = asset[56:]
    decimals = 0 if "decimals" not in response else response["decimals"]["value"]
    logo = "" if "logo" not in response else response["logo"]["value"]
    return TokenInfo(
        ticker=ticker,
        name=name,
        policy_id=policy_id,
        policy_name=policy_name,
        decimals=decimals,
        logo=logo,
    )