Add realtime to emission rewards

wsa-implement-emission-reward-tile
William Sanches 6 years ago
parent 1e6e2251b2
commit 57fab18a39
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 15
      apps/block_scout_web/assets/js/pages/address/transactions.js
  2. 35
      apps/block_scout_web/lib/block_scout_web/channels/reward_channel.ex
  3. 3
      apps/block_scout_web/lib/block_scout_web/channels/user_socket.ex
  4. 17
      apps/block_scout_web/lib/block_scout_web/notifier.ex
  5. 5
      apps/block_scout_web/lib/block_scout_web/realtime_event_handler.ex
  6. 1
      apps/explorer/lib/explorer/chain.ex
  7. 2
      apps/explorer/lib/explorer/chain/events/publisher.ex
  8. 2
      apps/explorer/lib/explorer/chain/events/subscriber.ex

@ -34,6 +34,11 @@ export function reducer (state, action) {
return Object.assign({}, state, { items: [ action.msg.transactionHtml, ...state.items ] })
}
case 'RECEIVED_NEW_REWARD': {
if (state.channelDisconnected) return state
return Object.assign({}, state, { items: [ action.msg.rewardHtml, ...state.items ] })
}
default:
return state
}
@ -62,7 +67,6 @@ if ($('[data-page="address-transactions"]').length) {
})
const addressChannel = subscribeChannel(`addresses:${addressHash}`)
addressChannel.onError(() => store.dispatch({ type: 'CHANNEL_DISCONNECTED' }))
addressChannel.on('transaction', (msg) => {
store.dispatch({
@ -70,4 +74,13 @@ if ($('[data-page="address-transactions"]').length) {
msg: humps.camelizeKeys(msg)
})
})
const rewardsChannel = subscribeChannel(`rewards:${addressHash}`)
rewardsChannel.onError(() => store.dispatch({ type: 'CHANNEL_DISCONNECTED' }))
rewardsChannel.on('new_reward', (msg) => {
store.dispatch({
type: 'RECEIVED_NEW_REWARD',
msg: humps.camelizeKeys(msg)
})
})
}

@ -0,0 +1,35 @@
defmodule BlockScoutWeb.RewardChannel do
@moduledoc """
Establishes pub/sub channel for live updates of block reward events.
"""
use BlockScoutWeb, :channel
alias BlockScoutWeb.TransactionView
alias Explorer.Chain
alias Phoenix.View
intercept(["new_reward"])
def join("rewards:" <> address_hash, _params, socket) do
{:ok, hash} = Chain.string_to_address_hash(address_hash)
{:ok, address} = Chain.hash_to_address(hash)
{:ok, %{}, assign(socket, :current_address, address)}
end
def handle_out("new_reward", %{emission_funds: emission_funds, validator: validator}, socket) do
Gettext.put_locale(BlockScoutWeb.Gettext, socket.assigns.locale)
rendered_reward =
View.render_to_string(
TransactionView,
"_emission_reward_tile.html",
current_address: socket.assigns.current_address,
emission_funds: emission_funds,
validator: validator
)
push(socket, "new_reward", %{reward_html: rendered_reward})
{:noreply, socket}
end
end

@ -4,8 +4,9 @@ defmodule BlockScoutWeb.UserSocket do
channel("addresses:*", BlockScoutWeb.AddressChannel)
channel("blocks:*", BlockScoutWeb.BlockChannel)
channel("transactions:*", BlockScoutWeb.TransactionChannel)
channel("exchange_rate:*", BlockScoutWeb.ExchangeRateChannel)
channel("rewards:*", BlockScoutWeb.RewardChannel)
channel("transactions:*", BlockScoutWeb.TransactionChannel)
transport(:websocket, Phoenix.Transports.WebSocket, timeout: 45_000)
# transport :longpoll, Phoenix.Transports.LongPoll

@ -21,6 +21,12 @@ defmodule BlockScoutWeb.Notifier do
Enum.each(address_coin_balances, &broadcast_address_coin_balance/1)
end
def handle_event({:chain_event, :block_rewards, :realtime, rewards}) do
if Application.get_env(:block_scout_web, BlockScoutWeb.Chain)[:has_emission_funds] do
broadcast_rewards(rewards)
end
end
def handle_event({:chain_event, :blocks, :realtime, blocks}) do
Enum.each(blocks, &broadcast_block/1)
end
@ -116,6 +122,17 @@ defmodule BlockScoutWeb.Notifier do
})
end
defp broadcast_rewards(rewards) do
preloaded_rewards = Repo.preload(rewards, [:address, :block])
Enum.each(preloaded_rewards, fn reward ->
Endpoint.broadcast("rewards:#{to_string(reward.address_hash)}", "new_reward", %{
emission_funds: Enum.at(preloaded_rewards, 1),
validator: Enum.at(preloaded_rewards, 0)
})
end)
end
defp broadcast_internal_transaction(internal_transaction) do
Endpoint.broadcast("internal_transactions:new_internal_transaction", "new_internal_transaction", %{
internal_transaction: internal_transaction

@ -14,12 +14,13 @@ defmodule BlockScoutWeb.RealtimeEventHandler do
@impl true
def init([]) do
Subscriber.to(:addresses, :realtime)
Subscriber.to(:address_coin_balances, :realtime)
Subscriber.to(:addresses, :realtime)
Subscriber.to(:block_rewards, :realtime)
Subscriber.to(:blocks, :realtime)
Subscriber.to(:internal_transactions, :realtime)
Subscriber.to(:transactions, :realtime)
Subscriber.to(:token_transfers, :realtime)
Subscriber.to(:transactions, :realtime)
# Does not come from the indexer
Subscriber.to(:exchange_rate)
{:ok, []}

@ -64,6 +64,7 @@ defmodule Explorer.Chain do
:addresses
| :address_coin_balances
| :blocks
| :block_rewards
| :exchange_rate
| :internal_transactions
| :logs

@ -3,7 +3,7 @@ defmodule Explorer.Chain.Events.Publisher do
Publishes events related to the Chain context.
"""
@allowed_events ~w(addresses address_coin_balances blocks internal_transactions token_transfers transactions)a
@allowed_events ~w(addresses address_coin_balances blocks block_rewards internal_transactions token_transfers transactions)a
def broadcast(_data, false), do: :ok

@ -3,7 +3,7 @@ defmodule Explorer.Chain.Events.Subscriber do
Subscribes to events related to the Chain context.
"""
@allowed_broadcast_events ~w(addresses address_coin_balances blocks internal_transactions token_transfers transactions)a
@allowed_broadcast_events ~w(addresses address_coin_balances blocks block_rewards internal_transactions token_transfers transactions)a
@allowed_broadcast_types ~w(catchup realtime)a

Loading…
Cancel
Save