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

Get previous oracle chain data #776

Merged
merged 2 commits into from
Dec 14, 2022
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
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
10 changes: 7 additions & 3 deletions lib/archethic_web/controllers/api/transaction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ defmodule ArchethicWeb.API.TransactionController do
changeset = %{valid?: true} ->
timestamp = DateTime.utc_now()

uco_price = OracleChain.get_uco_price(timestamp)
uco_eur = uco_price |> Keyword.fetch!(:eur)
uco_usd = uco_price |> Keyword.fetch!(:usd)
previous_price =
timestamp
|> OracleChain.get_last_scheduling_date()
|> OracleChain.get_uco_price()

uco_eur = previous_price |> Keyword.fetch!(:eur)
uco_usd = previous_price |> Keyword.fetch!(:usd)

fee =
changeset
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ArchethicWeb.API.TransactionControllerTest do
use ArchethicCase
use ArchethicWeb.ConnCase

alias Archethic.OracleChain
alias Archethic.OracleChain.MemTable
alias Archethic.P2P
alias Archethic.P2P.Node
Expand All @@ -25,7 +26,12 @@ defmodule ArchethicWeb.API.TransactionControllerTest do

describe "transaction_fee/2" do
test "should send ok response and return fee for valid transaction body", %{conn: conn} do
MemTable.add_oracle_data("uco", %{"eur" => 0.2, "usd" => 0.2}, DateTime.utc_now())
previous_oracle_time =
DateTime.utc_now()
|> OracleChain.get_last_scheduling_date()
|> OracleChain.get_last_scheduling_date()

MemTable.add_oracle_data("uco", %{"eur" => 0.2, "usd" => 0.2}, previous_oracle_time)

conn =
post(conn, "/api/transaction_fee", %{
Expand Down