|
|
|
@ -6,13 +6,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do |
|
|
|
|
alias Explorer.ExchangeRates.{Source, Token} |
|
|
|
|
alias HTTPoison.{Error, Response} |
|
|
|
|
|
|
|
|
|
import Source, only: [decode_json: 1, to_decimal: 1, headers: 0] |
|
|
|
|
|
|
|
|
|
@behaviour Source |
|
|
|
|
@headers |
|
|
|
|
|
|
|
|
|
@impl Source |
|
|
|
|
def fetch_exchange_rates do |
|
|
|
|
headers = [{"Content-Type", "application/json"}] |
|
|
|
|
|
|
|
|
|
case HTTPoison.get(source_url(), headers) do |
|
|
|
|
case HTTPoison.get(source_url(), headers()) do |
|
|
|
|
{:ok, %Response{body: body, status_code: 200}} -> |
|
|
|
|
{:ok, format_data(body)} |
|
|
|
|
|
|
|
|
@ -57,25 +58,14 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do |
|
|
|
|
configured_url || "https://api.coingecko.com/api/v3" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp decode_json(data) do |
|
|
|
|
Jason.decode!(data) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp to_decimal(nil), do: nil |
|
|
|
|
|
|
|
|
|
defp to_decimal(value) do |
|
|
|
|
Decimal.new(value) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp source_url(currency \\ "usd") do |
|
|
|
|
"#{base_url()}/coins/markets?vs_currency=#{currency}" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp get_btc_price(currency \\ "usd") do |
|
|
|
|
headers = [{"Content-Type", "application/json"}] |
|
|
|
|
url = "#{base_url()}/exchange_rates" |
|
|
|
|
|
|
|
|
|
case HTTPoison.get(url, headers) do |
|
|
|
|
case HTTPoison.get(url, headers()) do |
|
|
|
|
{:ok, %Response{body: body, status_code: 200}} -> |
|
|
|
|
data = decode_json(body) |
|
|
|
|
current_price = data["rates"][currency]["value"] |
|
|
|
|