Get the price of a token from a variety of sources
In your webapp, you may want to show the current price of a token in the ecosystem or other ecosystems. Here are a few ways to get these prices to display. These should NOT be used for logic. If you wish to use token prices for logic, you will need to use an Oracle from multiple sources.
Coingecko
Coingecko is the most popular source for Crypto price feeds. It is free to use, and allows 1 query per 6 seconds by default. This should only be used for decoration, and no logic should be built off of it.
Python
# https://pypi.org/project/pycoingecko/# pip install pycoingeckofrom pycoingecko import CoinGeckoAPIids ="juno-network,bitcoin"currencies ="usd,eur"defmain(): cg =Coingecko()print(cg.pretty_prices())classCoingecko:# https://www.coingecko.com/en/api/documentationdef__init__(self): api_key =""iflen(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]= symbolreturn valuesdefget_prices(self) ->dict:return self.cg.get_price(ids=ids, vs_currencies=currencies)defpretty_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_coinsif__name__=="__main__":main()