Skip to content

Commit

Permalink
Get previous oracle uco price
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Dec 14, 2022
1 parent ce0ccff commit cb9315f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
18 changes: 6 additions & 12 deletions lib/archethic/oracle_chain/mem_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ defmodule Archethic.OracleChain.MemTable do
date
|> DateTime.to_unix()

case :ets.lookup(:archethic_oracle, {timestamp, type}) do
[] ->
case :ets.prev(:archethic_oracle, {timestamp, type}) do
:"$end_of_table" ->
{:error, :not_found}

key = {time, _} ->
[{_, data}] = :ets.lookup(:archethic_oracle, key)
{:ok, data, DateTime.from_unix!(time)}
end
case :ets.prev(:archethic_oracle, {timestamp, type}) do
:"$end_of_table" ->
{:error, :not_found}

[{_, data}] ->
{:ok, data, date}
key = {time, _} ->
[{_, data}] = :ets.lookup(:archethic_oracle, key)
{:ok, data, DateTime.from_unix!(time)}
end
end

Expand Down
5 changes: 3 additions & 2 deletions test/archethic/oracle_chain/mem_table_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ defmodule Archethic.OracleChain.MemTableLoaderTest do
}
})

assert {:ok, %{"eur" => 0.02}, _} = MemTable.get_oracle_data("uco", DateTime.utc_now())
assert {:ok, %{"eur" => 0.02}, _} =
MemTable.get_oracle_data("uco", DateTime.utc_now() |> DateTime.add(1000))
end

test "should load an oracle summary transaction and the related changes" do
Expand All @@ -43,7 +44,7 @@ defmodule Archethic.OracleChain.MemTableLoaderTest do
})

assert {:ok, %{"eur" => 0.07}, _} =
MemTable.get_oracle_data("uco", DateTime.from_unix!(1_614_677_925))
MemTable.get_oracle_data("uco", DateTime.from_unix!(1_614_677_927))
end
end
end

0 comments on commit cb9315f

Please sign in to comment.