parent
0371d2acd6
commit
e485c1d7d9
@ -0,0 +1,46 @@ |
||||
defmodule Explorer.ExchangeRates.Source.DefiLlama do |
||||
@moduledoc """ |
||||
Adapter for fetching exchange rates from https://defillama.com/ |
||||
|
||||
""" |
||||
|
||||
alias Explorer.ExchangeRates.Source |
||||
|
||||
@behaviour Source |
||||
|
||||
@impl Source |
||||
def format_data(_), do: [] |
||||
|
||||
@spec history_url(non_neg_integer()) :: String.t() |
||||
def history_url(_previous_days) do |
||||
"#{base_url()}/historicalChainTvl" |
||||
end |
||||
|
||||
@impl Source |
||||
def source_url do |
||||
"" |
||||
end |
||||
|
||||
@impl Source |
||||
def source_url(_) do |
||||
"" |
||||
end |
||||
|
||||
@impl Source |
||||
def headers do |
||||
[] |
||||
end |
||||
|
||||
defp base_url do |
||||
base_free_url() |
||||
end |
||||
|
||||
defp base_free_url do |
||||
config(:base_url) || "https://api.llama.fi/v2" |
||||
end |
||||
|
||||
@spec config(atom()) :: term |
||||
defp config(key) do |
||||
Application.get_env(:explorer, __MODULE__, [])[key] |
||||
end |
||||
end |
@ -0,0 +1,18 @@ |
||||
defmodule Explorer.Market.History.Source.TVL do |
||||
@moduledoc """ |
||||
Interface for a source that allows for fetching of TVL history. |
||||
""" |
||||
|
||||
@typedoc """ |
||||
Record of market values for a specific date. |
||||
""" |
||||
@type record :: %{ |
||||
date: Date.t(), |
||||
tvl: Decimal.t() |
||||
} |
||||
|
||||
@doc """ |
||||
Fetch history for a specified amount of days in the past. |
||||
""" |
||||
@callback fetch_tvl(previous_days :: non_neg_integer()) :: {:ok, [record()]} | :error |
||||
end |
@ -0,0 +1,49 @@ |
||||
defmodule Explorer.Market.History.Source.TVL.DefiLlama do |
||||
@moduledoc """ |
||||
Adapter for fetching current market from DefiLlama. |
||||
""" |
||||
|
||||
alias Explorer.ExchangeRates.Source |
||||
alias Explorer.ExchangeRates.Source.DefiLlama, as: ExchangeRatesSourceDefiLlama |
||||
alias Explorer.Market.History.Source.Price.CryptoCompare |
||||
alias Explorer.Market.History.Source.TVL, as: SourceTVL |
||||
|
||||
@behaviour SourceTVL |
||||
|
||||
@impl SourceTVL |
||||
def fetch_tvl(previous_days) do |
||||
coin_id = Application.get_env(:explorer, Explorer.ExchangeRates.Source.DefiLlama, [])[:coin_id] |
||||
|
||||
if coin_id do |
||||
url = |
||||
ExchangeRatesSourceDefiLlama.history_url(previous_days) <> |
||||
"/" <> coin_id |
||||
|
||||
case Source.http_request(url, ExchangeRatesSourceDefiLlama.headers()) do |
||||
{:ok, data} -> |
||||
result = |
||||
data |
||||
|> format_data() |
||||
|
||||
{:ok, result} |
||||
|
||||
_ -> |
||||
:error |
||||
end |
||||
else |
||||
{:ok, []} |
||||
end |
||||
end |
||||
|
||||
@spec format_data(term()) :: SourceTVL.record() | nil |
||||
defp format_data(nil), do: nil |
||||
|
||||
defp format_data(data) do |
||||
Enum.map(data, fn %{"date" => date, "tvl" => tvl} -> |
||||
%{ |
||||
tvl: Decimal.new(to_string(tvl)), |
||||
date: CryptoCompare.date(date) |
||||
} |
||||
end) |
||||
end |
||||
end |
@ -0,0 +1,9 @@ |
||||
defmodule Explorer.Repo.Migrations.AddTvlToMarketHistoryTable do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
alter table(:market_history) do |
||||
add(:tvl, :decimal) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue