Skip to content

Commit

Permalink
Merge pull request #3875 from santiment/fetch-realtime-price-santiment
Browse files Browse the repository at this point in the history
Fetch realtime price santiment
  • Loading branch information
tspenov authored Jul 25, 2023
2 parents 3137edc + abf007a commit 6729c08
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/sanbase/external_services/coinmarketcap/ticker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ defmodule Sanbase.ExternalServices.Coinmarketcap.Ticker do
end
end

# Fetch quotes for single project
@spec fetch_data_by_slug(String.t()) :: {:error, String.t()} | {:ok, [%__MODULE__{}]}
def fetch_data_by_slug(slug) do
"v1/cryptocurrency/quotes/latest?slug=#{slug}&convert=USD,BTC"
|> get()
|> case do
{:ok, %Tesla.Env{status: 200, body: body}} ->
{:ok, parse_json(body)}

{:ok, %Tesla.Env{status: status}} ->
error = "Failed fetching #{slug} information. Status: #{status}"

Logger.warning(error)
{:error, error}

{:error, error} ->
error_msg = "Error fetching #{slug} information. Error message #{inspect(error)}"

Logger.error(error_msg)

{:error, error_msg}
end
end

@spec parse_json(String.t()) :: [%__MODULE__{}] | no_return
defp parse_json(json) do
%{"data" => data} =
Expand All @@ -108,6 +132,12 @@ defmodule Sanbase.ExternalServices.Coinmarketcap.Ticker do
data =
data
|> Enum.map(fn project_data ->
project_data =
case project_data do
{_, %{"id" => _id} = data} -> data
%{"id" => _id} = data -> data
end

%{
"id" => id,
"name" => name,
Expand Down
4 changes: 4 additions & 0 deletions lib/sanbase/external_services/coinmarketcap/ticker_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ defmodule Sanbase.ExternalServices.Coinmarketcap.TickerFetcher do
Logger.info("[CMC] Fetching realtime data from coinmarketcap")
# Fetch current coinmarketcap data for many tickers
{:ok, tickers} = Ticker.fetch_data(opts)
# Add a special case for SAN token since it might be out of top 5K projects
{:ok, san_tickers} = Ticker.fetch_data_by_slug("santiment")

tickers = tickers ++ san_tickers

# Create a map where the coinmarketcap_id is key and the values is the list of
# santiment slugs that have that coinmarketcap_id
Expand Down

0 comments on commit 6729c08

Please sign in to comment.