Skip to content

Commit

Permalink
Get previous oracle chain data (#776)
Browse files Browse the repository at this point in the history
* Fix transaction fee API with the previous price
* Get previous oracle uco price
  • Loading branch information
samuelmanzanera committed Dec 14, 2022
1 parent c2c13c6 commit 3bc00ce
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 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
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

0 comments on commit 3bc00ce

Please sign in to comment.