Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt #1

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config :archethic_fas,
uco: 6887,
matic: 3890,
bnb: 1839,
bitcoin: 1,
btc: 1,
eth: 1027
}

Expand Down
30 changes: 21 additions & 9 deletions lib/archethic_fas/quotes/cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,40 @@ defmodule ArchethicFAS.Quotes.Cache do
@spec get_latest() :: {:ok, %{Currency.t() => float()}} | {:error, String.t()}
def get_latest() do
case :ets.lookup(@table, :latest) do
[{:latest, value}] -> value
[] -> {:error, "Value not cached yet"}
[{:latest, value}] ->
value

[] ->
case hydrate() do
{:ok, value} ->
value

{:error, reason} ->
Logger.warning("Hydrating failed: #{inspect(reason)}")
{:error, "Value not cached yet"}
end
end
end

def hydrate do
GenServer.call(__MODULE__, :hydrate)
end

def init([]) do
:ets.new(@table, [:named_table, :set, :public])

{:ok, :no_state}
end

def handle_info(:hydrate, state) do
def handle_call(:hydrate, _from, state) do
case Quotes.fetch_latest(Currency.list()) do
{:ok, result} ->
:ets.insert(@table, {:latest, {:ok, result}})
{:reply, {:ok, result}, state}

{:error, reason} ->
Logger.warning("Hydrating failed: #{inspect(reason)}")

:ets.insert(@table, {:latest, {:error, reason}})
{:error, _reason} = e ->
:ets.delete(@table, :latest)
{:reply, e, state}
end

{:noreply, state}
end
end
4 changes: 2 additions & 2 deletions lib/archethic_fas/quotes/currency.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule ArchethicFAS.Quotes.Currency do

@ucids Application.compile_env!(:archethic_fas, :ucids)

@type t :: :eth | :bnb | :matic | :bitcoin | :uco
@type t :: :eth | :bnb | :matic | :btc | :uco

@doc """
Return all handled currencies
Expand All @@ -22,7 +22,7 @@ defmodule ArchethicFAS.Quotes.Currency do
def cast("eth"), do: {:ok, :eth}
def cast("bnb"), do: {:ok, :bnb}
def cast("matic"), do: {:ok, :matic}
def cast("bitcoin"), do: {:ok, :bitcoin}
def cast("btc"), do: {:ok, :btc}
def cast("uco"), do: {:ok, :uco}
def cast(_), do: :error

Expand Down
4 changes: 2 additions & 2 deletions lib/archethic_fas/quotes/scheduler.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ArchethicFAS.Quotes.Scheduler do
@moduledoc """
Responsible of calling MFA every N ms
Responsible of hydrating the cache each minute
"""

alias ArchethicFAS.Quotes.Cache
Expand All @@ -18,7 +18,7 @@ defmodule ArchethicFAS.Quotes.Scheduler do
end

def handle_info(:tick, state) do
send(Cache, :hydrate)
Cache.hydrate()

Process.send_after(self(), :tick, 60_000)
{:noreply, state}
Expand Down
2 changes: 1 addition & 1 deletion test/archethic_fas/quotes/currency_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ArchethicFAS.Quotes.CurrencyTest do
assert {:ok, :eth} = Currency.cast("eth")
assert {:ok, :bnb} = Currency.cast("bnb")
assert {:ok, :matic} = Currency.cast("matic")
assert {:ok, :bitcoin} = Currency.cast("bitcoin")
assert {:ok, :btc} = Currency.cast("btc")
assert {:ok, :uco} = Currency.cast("uco")
end

Expand Down
6 changes: 3 additions & 3 deletions test/archethic_fas/quotes/provider/coin_market_cap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ defmodule ArchethicFAS.Quotes.Provider.CoinMarketCapTest do
assert {:ok, map} = CoinMarketCap.fetch_latest([:matic])
assert [:matic] = Map.keys(map)
assert Enum.all?(Map.values(map), &is_float/1)
assert {:ok, map} = CoinMarketCap.fetch_latest([:bitcoin])
assert [:bitcoin] = Map.keys(map)
assert {:ok, map} = CoinMarketCap.fetch_latest([:btc])
assert [:btc] = Map.keys(map)
assert Enum.all?(Map.values(map), &is_float/1)
assert {:ok, map} = CoinMarketCap.fetch_latest([:bnb])
assert [:bnb] = Map.keys(map)
assert Enum.all?(Map.values(map), &is_float/1)
assert {:ok, map} = CoinMarketCap.fetch_latest([:uco])
assert [:uco] = Map.keys(map)
assert Enum.all?(Map.values(map), &is_float/1)
assert {:ok, map} = CoinMarketCap.fetch_latest([:eth, :matic, :bitcoin, :bnb, :uco])
assert {:ok, map} = CoinMarketCap.fetch_latest([:eth, :matic, :btc, :bnb, :uco])
assert 5 = map_size(map)
assert Enum.all?(Map.values(map), &is_float/1)
end
Expand Down
Loading