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

Add transaction fee api, fix archethic-foundation/archethic-node#196 #200

Merged
1 commit merged into from
Jan 21, 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
34 changes: 34 additions & 0 deletions lib/archethic_web/controllers/api/transaction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ defmodule ArchEthicWeb.API.TransactionController do
alias ArchEthic.TransactionChain.Transaction
alias ArchEthic.TransactionChain.TransactionData

alias ArchEthic.Mining
alias ArchEthic.OracleChain

alias ArchEthicWeb.API.TransactionPayload
alias ArchEthicWeb.ErrorView

Expand Down Expand Up @@ -86,4 +89,35 @@ defmodule ArchEthicWeb.API.TransactionController do
send_resp(conn, 404, "Not Found")
end
end

def transaction_fee(conn, tx) do
case TransactionPayload.changeset(tx) do
changeset = %{valid?: true} ->
uco_price = OracleChain.get_uco_price(DateTime.utc_now())
uco_eur = uco_price |> Keyword.fetch!(:eur)
uco_usd = uco_price |> Keyword.fetch!(:usd)

fee =
changeset
|> TransactionPayload.to_map()
|> Transaction.from_map()
|> Mining.get_transaction_fee(uco_usd)

conn
|> put_status(:ok)
|> json(%{
"fee" => fee / 100_000_000,
"rates" => %{
"usd" => uco_usd,
"eur" => uco_eur
}
})

changeset ->
conn
|> put_status(:bad_request)
|> put_view(ErrorView)
|> render("400.json", changeset: changeset)
end
end
end
1 change: 1 addition & 0 deletions lib/archethic_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ defmodule ArchEthicWeb.Router do
)

post("/transaction", ArchEthicWeb.API.TransactionController, :new)
post("/transaction_fee", ArchEthicWeb.API.TransactionController, :transaction_fee)

forward(
"/graphiql",
Expand Down
96 changes: 96 additions & 0 deletions test/archethic_web/controllers/api/transaction_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
defmodule ArchEthicWeb.API.TransactionControllerTest do
use ArchEthicCase
use ArchEthicWeb.ConnCase

alias ArchEthic.OracleChain.MemTable
alias ArchEthic.P2P
alias ArchEthic.P2P.Node
alias ArchEthic.Crypto

setup do
P2P.add_and_connect_node(%Node{
ip: {127, 0, 0, 1},
port: 3000,
first_public_key: Crypto.last_node_public_key(),
last_public_key: Crypto.last_node_public_key(),
network_patch: "AAA",
geo_patch: "AAA",
available?: true,
authorized?: true,
authorization_date: DateTime.utc_now()
})

:ok
end

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())

conn =
post(conn, "/api/transaction_fee", %{
"address" => "009e059e8171643b959284fe542909f3b32198b8fc25b3e50447589b84341c1d67",
"data" => %{
"ledger" => %{
"nft" => %{"transfers" => []},
"uco" => %{
"transfers" => [
%{
"amount" => trunc(100_000_000),
"to" => "0098fe10e8633bce19c59a40a089731c1f72b097c5a8f7dc71a37eb26913aa4f80"
}
]
}
}
},
"originSignature" =>
"3045022024f8d254671af93f8b9c11b5a2781a4a7535d2e89bad69d6b1f142f8f4bcf489022100c364e10f5f846b2534a7ace4aeaa1b6c8cb674f842b9f8bc78225dfa61cabec6",
"previousPublicKey" =>
"000071e1b5d4b89eddf2322c69bbf1c5591f7361b24cb3c4c464f6b5eb688fe50f7a",
"previousSignature" =>
"9b209dd92c6caffbb5c39d12263f05baebc9fe3c36cb0f4dde04c96f1237b75a3a2973405c6d9d5e65d8a970a37bafea57b919febad46b0cceb04a7ffa4b6b00",
"type" => "transfer",
"version" => 1
})

assert %{
"fee" => 0.50001315,
"rates" => %{
"eur" => 0.2,
"usd" => 0.2
}
} = json_response(conn, 200)
end

test "should send bad_request response for invalid transaction body", %{conn: conn} do
conn = post(conn, "/api/transaction_fee", %{})

assert %{
"errors" => %{
"address" => [
"can't be blank"
],
"data" => [
"can't be blank"
],
"originSignature" => [
"can't be blank"
],
"previousPublicKey" => [
"can't be blank"
],
"previousSignature" => [
"can't be blank"
],
"type" => [
"can't be blank"
],
"version" => [
"can't be blank"
]
},
"status" => "invalid"
} = json_response(conn, 400)
end
end
end