From 14aec95a40ea16a97891f8e4a1e1c14c54a988b9 Mon Sep 17 00:00:00 2001 From: Vamsi Krishna B Date: Thu, 22 Jul 2021 22:27:34 +0530 Subject: [PATCH] Added TX Fee --- config/dev.exs | 2 +- lib/zcash_explorer_web.ex | 3 +- .../templates/page/index.html.eex | 68 +---- .../templates/transaction/tx.html.eex | 58 ++++- lib/zcash_explorer_web/views/block_view.ex | 1 - lib/zcash_explorer_web/views/page_view.ex | 4 +- .../views/transaction_view.ex | 239 ++++++++++-------- 7 files changed, 192 insertions(+), 183 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 6227de0..917d0ba 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -30,12 +30,12 @@ config :zcash_explorer, ZcashExplorerWeb.Endpoint, ] ] - config :zcash_explorer, Zcashex, zcashd_hostname: "localhost", zcashd_port: "38232", zcashd_username: "zcashrpc", zcashd_password: "changeme" + # ## SSL Support # # In order to use HTTPS in development, a self-signed diff --git a/lib/zcash_explorer_web.ex b/lib/zcash_explorer_web.ex index 408c865..779dbdb 100644 --- a/lib/zcash_explorer_web.ex +++ b/lib/zcash_explorer_web.ex @@ -30,6 +30,7 @@ defmodule ZcashExplorerWeb do def view do quote do import Phoenix.LiveView.Helpers + use Phoenix.View, root: "lib/zcash_explorer_web/templates", namespace: ZcashExplorerWeb @@ -38,8 +39,6 @@ defmodule ZcashExplorerWeb do import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] - - # Include shared imports and aliases for views unquote(view_helpers()) end diff --git a/lib/zcash_explorer_web/templates/page/index.html.eex b/lib/zcash_explorer_web/templates/page/index.html.eex index 5910013..467f44b 100644 --- a/lib/zcash_explorer_web/templates/page/index.html.eex +++ b/lib/zcash_explorer_web/templates/page/index.html.eex @@ -2,7 +2,7 @@
- +
@@ -26,9 +26,9 @@

Blocks

- + <%= live_render(@conn, ZcashExplorerWeb.BlockCountLive) %> - +
View blocks View blocks @@ -40,16 +40,16 @@
- +

Mempool Transactions

- + <%= live_render(@conn, ZcashExplorerWeb.MempoolInfoLive) %> - +
- + - +

Blockchain Size

- + <%= live_render(@conn, ZcashExplorerWeb.BlockChainSizeLive) %> - +
-
-
-
- - - - -
-

Difficulty

-
-
- - <%= live_render(@conn, ZcashExplorerWeb.DifficultyLive) %> - - -
-
- - - -
-
-
- - - - -
-

Network Sols/Sec

-
-
- - <%= live_render(@conn, ZcashExplorerWeb.NetworkSolpsLive) %> - - -
-
-
@@ -149,4 +103,4 @@ <%= live_render(@conn, ZcashExplorerWeb.RecentTransactionsLive) %> - \ No newline at end of file + diff --git a/lib/zcash_explorer_web/templates/transaction/tx.html.eex b/lib/zcash_explorer_web/templates/transaction/tx.html.eex index fba9da9..c0755e2 100644 --- a/lib/zcash_explorer_web/templates/transaction/tx.html.eex +++ b/lib/zcash_explorer_web/templates/transaction/tx.html.eex @@ -36,7 +36,7 @@ - +
Tx Type @@ -122,16 +122,50 @@
<%= @tx.locktime %> +
+
+
+
+ Transaction fee +
+
+ <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "coinbase" do %> + 0.0 ZEC + <% end %> + <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "transparent" do %> + <%= transparent_tx_fee(@tx) %> + <% end %> + + <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "shielded" do %> + <%= format_zec(get_shielded_pool_value(@tx)) %> + <% end %> + + <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "shielding" do %> + <%= shielding_tx_fee(@tx) %> + <% end %> + + <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "deshielding" do %> + <%= deshielding_tx_fees(@tx) %> + <% end %> + + <%= if ZcashExplorerWeb.BlockView.tx_type(@tx) == "mixed" do %> + <%= mixed_tx_fees(@tx) %> + <% end %> + + + + +
- +

Public transfers -

+
Inputs (<%= vin_count(@tx.vin) %>) @@ -141,7 +175,7 @@ Inputs (<%= vin_count(@tx.vin) %>)
@@ -179,7 +213,7 @@ Inputs (<%= vin_count(@tx.vin) %>)
- +
@@ -195,7 +229,7 @@ Outputs (<%= vout_count(@tx.vout) %>) @@ -240,7 +274,7 @@ Shielded transfers
- +
@@ -260,8 +294,8 @@ Shielded transfers
<%= if ZcashExplorerWeb.BlockView.contains_sprout(@tx) do %> - Yes - - <%= ZcashExplorerWeb.BlockView.get_joinsplit_count(@tx) %> JoinSplits + Yes - + <%= ZcashExplorerWeb.BlockView.get_joinsplit_count(@tx) %> <% else %> No <% end %> @@ -288,4 +322,4 @@ Shielded transfers
- \ No newline at end of file + diff --git a/lib/zcash_explorer_web/views/block_view.ex b/lib/zcash_explorer_web/views/block_view.ex index 3dae2f5..a9bcee3 100644 --- a/lib/zcash_explorer_web/views/block_view.ex +++ b/lib/zcash_explorer_web/views/block_view.ex @@ -1,7 +1,6 @@ defmodule ZcashExplorerWeb.BlockView do use ZcashExplorerWeb, :view - def mined_time(nil) do "Not yet mined" end diff --git a/lib/zcash_explorer_web/views/page_view.ex b/lib/zcash_explorer_web/views/page_view.ex index c9911cf..f55ba24 100644 --- a/lib/zcash_explorer_web/views/page_view.ex +++ b/lib/zcash_explorer_web/views/page_view.ex @@ -2,7 +2,7 @@ defmodule ZcashExplorerWeb.PageView do use ZcashExplorerWeb, :view def price() do - {:ok, price } = Cachex.get(:price_cache, "price") - price + {:ok, price} = Cachex.get(:price_cache, "price") + price end end diff --git a/lib/zcash_explorer_web/views/transaction_view.ex b/lib/zcash_explorer_web/views/transaction_view.ex index 628ef45..e162b30 100644 --- a/lib/zcash_explorer_web/views/transaction_view.ex +++ b/lib/zcash_explorer_web/views/transaction_view.ex @@ -24,22 +24,19 @@ defmodule ZcashExplorerWeb.TransactionView do "" end - # functions to get the label for + # functions to get the label for # shielding tx without vjoinsplit def get_shielded_pool_label(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) > 0 - and length(tx.vout) == 0 - and length(tx.vShieldedOutput) > 0 - and (tx.valueBalance) < 0.0 - do - IO.inspect("38") + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) > 0 and + length(tx.vout) == 0 and + length(tx.vShieldedOutput) > 0 and + tx.valueBalance < 0.0 do + IO.inspect("38") "Transferred to shielded pool" end - # handle when vjoinsplit is present ( indicated legacy transaction) # 1 def get_shielded_pool_label(tx) @@ -48,28 +45,24 @@ defmodule ZcashExplorerWeb.TransactionView do "Transferred to shielded pool" end - # handle when and vjoinsplit is present and vin == 0 .. + # handle when and vjoinsplit is present and vin == 0 .. def get_shielded_pool_label(tx) when tx.vjoinsplit != nil and length(tx.vjoinsplit) > 0 and length(tx.vin) == 0 do - IO.inspect("54") + IO.inspect("") "Transferred from shielded pool" end - -# handle mixed tx + # handle mixed tx def get_shielded_pool_label(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) > 0 - and length(tx.vShieldedOutput) > 0 - and tx.valueBalance < 0.0 - do - IO.inspect("68") + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) > 0 and + length(tx.vShieldedOutput) > 0 and + tx.valueBalance < 0.0 do + IO.inspect("68") "Transferred to shielded pool" end - # handle public tx def get_shielded_pool_label(tx) when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) > 0 do @@ -77,8 +70,7 @@ defmodule ZcashExplorerWeb.TransactionView do "Transferred from/to shielded pool" end - - # 3 with value balance + # 3 with value balance def get_shielded_pool_label(tx) when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) == 0 and length(tx.vout) > 0 and tx.valueBalance > 0 do @@ -90,120 +82,151 @@ defmodule ZcashExplorerWeb.TransactionView do def get_shielded_pool_label(tx) when tx.vjoinsplit != nil and length(tx.vjoinsplit) == 0 and length(tx.vin) == 0 and length(tx.vout) == 0 and tx.valueBalance > 0 do - IO.inspect("93") + IO.inspect("93") "Transferred from shielded pool" end - - - # mixed - def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) > 0 - and length(tx.vShieldedOutput) > 0 - and tx.valueBalance < 0.0 - do - abs(tx.valueBalance) + # mixed + def get_shielded_pool_value(tx) + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) > 0 and + length(tx.vShieldedOutput) > 0 and + tx.valueBalance < 0.0 do + abs(tx.valueBalance) end - - - def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) > 0 - and length(tx.vout) == 0 - and length(tx.vShieldedOutput) > 0 - and (tx.valueBalance) < 0.0 - do - abs(tx.valueBalance) + def get_shielded_pool_value(tx) + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) > 0 and + length(tx.vout) == 0 and + length(tx.vShieldedOutput) > 0 and + tx.valueBalance < 0.0 do + abs(tx.valueBalance) end - - # 1 def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) > 0 - and length(tx.vin) > 0 do + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) > 0 and + length(tx.vin) > 0 do Map.get(tx, :vjoinsplit) |> Enum.reduce(0, fn x, acc -> Map.get(x, :vpub_old) + acc end) end - - # 3 + # 3 def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) == 0 - and length(tx.vout) > 0 - and tx.valueBalance > 0 do + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) == 0 and + length(tx.vout) > 0 and + tx.valueBalance > 0 do abs(tx.valueBalance) end - - # 4 + # 4 def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) > 0 - and length(tx.vin) == 0 - and length(tx.vout) == 0 - do - - val = tx - |> Map.get(:vjoinsplit) - |> List.flatten() - |> Enum.reduce(0, fn x, acc -> Map.get(x, :vpub_new) + acc end) - |> Kernel.+(0.0) + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) > 0 and + length(tx.vin) == 0 and + length(tx.vout) == 0 do + val = + tx + |> Map.get(:vjoinsplit) + |> List.flatten() + |> Enum.reduce(0, fn x, acc -> Map.get(x, :vpub_new) + acc end) + |> Kernel.+(0.0) + abs(val) end - - - # 4 + # 4 Deshielding legacy def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) > 0 - and length(tx.vin) == 0 - and length(tx.vout) > 0 - do - - val = tx - |> Map.get(:vout) - |> List.flatten() - |> Enum.reduce(0, fn x, acc -> Map.get(x, :value) + acc end) - |> Kernel.+(0.0) + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) > 0 and + length(tx.vin) == 0 and + length(tx.vout) > 0 do + IO.inspect(177) + + val = + tx + |> Map.get(:vjoinsplit) + |> List.flatten() + |> Enum.reduce(0, fn x, acc -> Map.get(x, :vpub_new) + acc end) + |> Kernel.+(0.0) + abs(val) end - - - # 4 + # 4 def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) == 0 - and length(tx.vout) == 0 - and tx.valueBalance > 0 do + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) == 0 and + length(tx.vout) == 0 and + tx.valueBalance > 0 do abs(tx.valueBalance) end - # handle public tx def get_shielded_pool_value(tx) - when - tx.vjoinsplit != nil - and length(tx.vjoinsplit) == 0 - and length(tx.vin) > 0 do + when tx.vjoinsplit != nil and + length(tx.vjoinsplit) == 0 and + length(tx.vin) > 0 do 0.00 end + def tx_in_total(tx) when is_map(tx) do + IO.inspect(tx.vin) + tx.vin |> Enum.reduce(0, fn x, acc -> x.value + acc end) + end + def tx_out_total(tx) when is_map(tx) do + tx.vout |> Enum.reduce(0, fn x, acc -> x.value + acc end) + end + + def transparent_tx_fee(public_tx) do + fee = tx_in_total(public_tx) - tx_out_total(public_tx) + fee |> format_zec() + end + + def vjoinsplit_vpub_old_total(tx) when is_map(tx) do + tx.vjoinsplit |> Enum.reduce(0, fn x, acc -> x.vpub_old + acc end) + end + + def vjoinsplit_vpub_new_total(tx) when is_map(tx) do + tx.vjoinsplit |> Enum.reduce(0, fn x, acc -> x.vpub_new + acc end) + end + + def shielding_tx_fee(tx) when is_map(tx) and length(tx.vjoinsplit) > 0 do + fee = tx_in_total(tx) - vjoinsplit_vpub_old_total(tx) + fee |> format_zec() + end + + def shielding_tx_fee(tx) when is_map(tx) and length(tx.vjoinsplit) == 0 do + fee = tx_in_total(tx) - abs(tx.valueBalance) + fee |> format_zec() + end + + def deshielding_tx_fees(tx) when is_map(tx) and length(tx.vjoinsplit) > 0 do + fee = vjoinsplit_vpub_new_total(tx) - tx_out_total(tx) + fee |> format_zec() + end + + def deshielding_tx_fees(tx) when is_map(tx) and length(tx.vjoinsplit) == 0 do + fee = tx.valueBalance - tx_out_total(tx) + fee |> format_zec() + end + + def mixed_tx_fees(tx) + when is_map(tx) and + length(tx.vjoinsplit) == 0 and + length(tx.vShieldedOutput) > 0 and + length(tx.vShieldedSpend) == 0 and + tx.valueBalance < 0 and + length(tx.vin) > 0 and + length(tx.vout) > 0 do + fee = tx_in_total(tx) - abs(tx.valueBalance) - tx_out_total(tx) + fee |> format_zec() + end end