Orchard Pool

This commit is contained in:
Vamsi Krishna B 2022-05-31 15:26:57 +05:30
parent 7024d17d6c
commit 921420e8bd
7 changed files with 62 additions and 127 deletions

View File

@ -31,7 +31,6 @@ defmodule ZcashExplorer.Application do
{Cachex,
name: :app_cache,
warmers: [
warmer(module: ZcashExplorer.Price.PriceWarmer, state: {}),
warmer(module: ZcashExplorer.Metrics.MetricsWarmer, state: {}),
warmer(module: ZcashExplorer.Metrics.MempoolInfoWarmer, state: {}),
warmer(module: ZcashExplorer.Metrics.NetworkSolpsWarmer, state: {}),

View File

@ -1,24 +0,0 @@
defmodule ZcashExplorer.Price.PriceService do
@moduledoc false
def fetch_current_price() do
case fetch_price() do
nil ->
{:error, "Unable to fetch ZEC price in USD"}
price ->
{:ok, price}
end
end
defp fetch_price() do
url()
|> HTTPoison.get!([{"User-agent", "curl/7.83.1"}])
|> Map.get(:body)
|> Poison.decode!()
end
defp url() do
"https://api.coingecko.com/api/v3/simple/price?ids=zcash&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true"
end
end

View File

@ -1,28 +0,0 @@
defmodule ZcashExplorer.Price.PriceWarmer do
@moduledoc """
Price warmer which caches Zec prices every 30s.
"""
alias ZcashExplorer.Price.PriceService
use Cachex.Warmer
@doc """
Returns the interval for this warmer.
"""
def interval,
do: :timer.seconds(30)
@doc """
Executes this cache warmer.
"""
def execute(_state) do
PriceService.fetch_current_price() |> handle_result
end
# ignores the warmer result in case of error
defp handle_result({:error, _reason}),
do: :ignore
defp handle_result({:ok, price}) do
{:ok, [{"price", price}]}
end
end

View File

@ -0,0 +1,48 @@
defmodule ZcashExplorerWeb.OrchardPoolLive do
use ZcashExplorerWeb, :live_view
import Phoenix.LiveView.Helpers
@impl true
def render(assigns) do
~L"""
<p class="text-2xl font-semibold text-gray-900">
<%= orchard_value(@blockchain_info["valuePools"]) %> ZEC
</p>
"""
end
@impl true
def mount(_params, _session, socket) do
if connected?(socket), do: Process.send_after(self(), :update, 15000)
case Cachex.get(:app_cache, "metrics") do
{:ok, info} ->
{:ok, %{"build" => build}} = Cachex.get(:app_cache, "info")
info = Map.put(info, "build", build)
{:ok, assign(socket, :blockchain_info, info)}
{:error, _reason} ->
{:ok, assign(socket, :blockchain_info, "loading...")}
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 15000)
{:ok, info} = Cachex.get(:app_cache, "metrics")
{:ok, %{"build" => build}} = Cachex.get(:app_cache, "info")
info = Map.put(info, "build", build)
{:noreply, assign(socket, :blockchain_info, info)}
end
defp orchard_value(value_pools) do
value_pools |> get_value_pools |> Map.get("orchard")
end
defp get_value_pools(value_pools) do
Enum.map(value_pools, fn %{"id" => name, "chainValue" => value} -> {name, value} end)
|> Map.new()
end
end

View File

@ -1,69 +0,0 @@
defmodule ZcashExplorerWeb.PriceLive do
use ZcashExplorerWeb, :live_view
import Phoenix.LiveView.Helpers
@impl true
def render(assigns) do
~L"""
<dd class="ml-16 pb-6 flex items-baseline sm:pb-7">
<p class="text-2xl font-semibold text-gray-900">
$<%= @price["zcash"]["usd"] %>
</p>
<%= if @price["zcash"]["usd_24h_change"] > 0 do %>
<p class="ml-2 flex items-baseline text-sm font-semibold text-green-600">
<!-- Heroicon name: solid/arrow-sm-up -->
<svg class="self-center flex-shrink-0 h-5 w-5 text-green-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M5.293 9.707a1 1 0 010-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 01-1.414 1.414L11 7.414V15a1 1 0 11-2 0V7.414L6.707 9.707a1 1 0 01-1.414 0z" clip-rule="evenodd" />
</svg>
<span class="sr-only">
Increased by
</span>
<%= Float.ceil(@price["zcash"]["usd_24h_change"], 4) %> %
</p>
<% end %>
<%= if @price["zcash"]["usd_24h_change"] < 0 do %>
<p class="ml-2 flex items-baseline text-sm font-semibold text-red-600">
<!-- Heroicon name: solid/arrow-sm-down -->
<svg class="self-center flex-shrink-0 h-5 w-5 text-red-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M14.707 10.293a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 111.414-1.414L9 12.586V5a1 1 0 012 0v7.586l2.293-2.293a1 1 0 011.414 0z" clip-rule="evenodd" />
</svg>
<span class="sr-only">
Decreased by
</span>
<%= Float.ceil(@price["zcash"]["usd_24h_change"], 4) %>%
</p>
<% end %>
<div class="absolute bottom-0 inset-x-0 bg-gray-50 px-4 py-4 sm:px-6">
<div class="text-sm">
<a href="https://www.coingecko.com/en/coins/zcash" target="_blank" rel="noreferrer" class="font-medium text-indigo-600 hover:text-indigo-500"> View more<span class="sr-only"> View detailed price charts of Zcash.</span></a>
</div>
</div>
</dd>
"""
end
@impl true
def mount(_params, _session, socket) do
if connected?(socket), do: Process.send_after(self(), :update, 3000)
case Cachex.get(:app_cache, "price") do
{:ok, price} ->
{:ok, assign(socket, :price, price)}
{:error, _reason} ->
{:ok, assign(socket, :price, "updating...")}
end
end
@impl true
def handle_info(:update, socket) do
Process.send_after(self(), :update, 3000)
{:ok, price} = Cachex.get(:app_cache, "price")
{:noreply, assign(socket, :price, price)}
end
end

View File

@ -30,6 +30,7 @@ defmodule ZcashExplorerWeb.Router do
live "/index/recent_blocks", RecentBlocksLive
live "/index/recent_transactions", RecentTransactionsLive
live "/live/raw_mempool", RawMempoolLive
live "/live/orchard_pool", OrchardPoolLive
live "/live/nodes", NodesLive
live "/vkdetails", VkLive
live "/blockchain-info-live", BlockChainInfoLive

View File

@ -2,16 +2,24 @@
<div>
<dl class="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-4">
<div class="relative bg-white pt-5 px-4 pb-12 sm:pt-6 sm:px-6 shadow rounded-lg overflow-hidden">
<dt>
<div class="absolute bg-indigo-500 rounded-md p-3">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z" />
<div class="absolute bg-green-500 rounded-md p-3">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"></path>
</svg>
</div>
<p class="ml-16 text-sm font-medium text-gray-500 truncate">Price</p>
<p class="ml-16 text-sm font-medium text-gray-500 truncate">Blockchain Size</p>
</dt>
<%= live_render(@conn, ZcashExplorerWeb.PriceLive) %>
<dd class="ml-16 pb-6 flex items-baseline sm:pb-7">
<%= live_render(@conn, ZcashExplorerWeb.OrchardPoolLive) %>
<div class="absolute bottom-0 inset-x-0 bg-gray-50 px-4 py-4 sm:px-6">
<div class="text-sm">
<a href="/blockchain-info" class="font-medium text-indigo-600 hover:text-indigo-500">View blockchain info<span class="sr-only"> View blockchain info</span></a>
</div>
</div>
</dd>
</div>