-
Notifications
You must be signed in to change notification settings - Fork 21
/
uco_price.ex
127 lines (104 loc) · 3.12 KB
/
uco_price.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
defmodule Archethic.OracleChain.Services.UCOPrice do
@moduledoc """
Define Oracle behaviors to support UCO Price feed oracle
"""
require Logger
alias Archethic.OracleChain.Services.Impl
alias Archethic.OracleChain.Services.ProviderCacheSupervisor
alias Archethic.Utils
@behaviour Impl
@precision_digits 5
@pairs ["usd", "eur"]
@impl Impl
def cache_child_spec do
Supervisor.child_spec({ProviderCacheSupervisor, providers: providers(), fetch_args: @pairs},
id: CacheSupervisor
)
end
defp providers do
Application.get_env(:archethic, __MODULE__) |> Keyword.fetch!(:providers)
end
@impl Impl
@spec fetch() :: {:ok, %{required(String.t()) => any()}} | {:error, any()}
def fetch do
# retrieve prices from configured providers and filter results marked as errors
# Here stream looks like : [%{"eur"=>[0.44], "usd"=[0.32]}, ..., %{"eur"=>[0.42, 0.43], "usd"=[0.35]}]
prices =
providers()
|> ProviderCacheSupervisor.get_values()
|> Enum.reduce(%{}, &agregate_providers_data/2)
|> Enum.reduce(%{}, fn {currency, values}, acc ->
price =
values
|> Utils.median()
|> Archethic.Cldr.Number.to_string!(
currency: currency,
currency_symbol: "",
fractional_digits: @precision_digits
)
|> String.to_float()
Map.put(acc, currency, price)
end)
if prices == %{} do
{:error, "no data fetched from any service"}
else
{:ok, prices}
end
end
defp agregate_providers_data(provider_results, acc) do
provider_results
|> Enum.reduce(acc, fn
{currency, values}, acc when values != [] ->
Map.update(acc, String.downcase(currency), values, fn
previous_values ->
previous_values ++ values
end)
_, acc ->
acc
end)
end
@impl Impl
@spec verify?(%{required(String.t()) => any()}) :: boolean
def verify?(prices_prior = %{}) do
case fetch() do
{:error, reason} ->
Logger.error("Cannot fetch UCO price - reason: #{reason}.")
false
{:ok, prices_now} ->
Enum.all?(@pairs, fn pair ->
compare_price(
Map.fetch!(prices_prior, pair),
Map.get(prices_now, pair, Map.get(prices_prior, pair, 0.0))
)
end)
end
end
defp compare_price(price_prior, price_now) do
deviation_threshold = 0.01
deviation =
[price_prior, price_now]
|> Utils.standard_deviation()
|> Float.round(3)
if deviation < deviation_threshold do
true
else
Logger.warning(
"UCO price deviated from #{deviation} % - previous price: #{price_prior} - new price: #{price_now} "
)
false
end
end
@impl Impl
@spec parse_data(map()) :: {:ok, map()} | :error
def parse_data(service_data) when is_map(service_data) do
valid? =
Enum.all?(service_data, fn
{key, val} when key in @pairs and is_float(val) ->
true
_ ->
false
end)
if valid?, do: {:ok, service_data}, else: :error
end
def parse_data(_), do: {:error, :invalid_data}
end