Skip to content

Commit

Permalink
Merge pull request #19 from protofire/staging-harmony
Browse files Browse the repository at this point in the history
Adds staking rewards to transaction and staking transaction pages details
  • Loading branch information
leonimella authored Dec 19, 2024
2 parents 8e6f0f6 + fbb29f8 commit f3c800a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@ defmodule BlockScoutWeb.API.V2.StakingTransactionController do
def staking_transaction(conn, %{"staking_transactions_hash_param" => staking_transaction_hash_string} = params) do
necessity_by_association = @staking_transaction_necessity_by_association

with {:ok, transaction, _transaction_hash} <-
with {:ok, transaction, transaction_hash} <-
validate_transaction(staking_transaction_hash_string, params,
necessity_by_association: necessity_by_association,
api?: true
) do
case {transaction.type == :collect_rewards, Chain.staking_transaction_rewards(transaction_hash)} do
{true, %{data: hex_reward_value}} ->
reward =
hex_reward_value
|> Chain.hash_to_lower_case_string()
|> (fn "0x" <> reward_hex -> String.to_integer(reward_hex, 16) end).()
|> Integer.to_string()

conn
|> put_status(200)
|> render(:staking_transaction_reward, %{
transaction: transaction |> maybe_preload_ens_to_transaction(),
reward: reward
})

_ ->
conn
|> put_status(200)
|> render(:staking_transaction, %{transaction: transaction |> maybe_preload_ens_to_transaction()})
end

conn
|> put_status(200)
|> render(:staking_transaction, %{transaction: transaction |> maybe_preload_ens_to_transaction()})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,34 @@ defmodule BlockScoutWeb.API.V2.TransactionController do
necessity_by_association_with_actions
end

with {:ok, transaction, _transaction_hash} <-
with {:ok, transaction, transaction_hash} <-
validate_transaction(transaction_hash_string, params,
necessity_by_association: necessity_by_association,
api?: true
),
preloaded <-
Chain.preload_token_transfers(transaction, @token_transfers_in_tx_necessity_by_association, @api_true, false) do
conn
|> put_status(200)
|> render(:transaction, %{transaction: preloaded |> maybe_preload_ens_to_transaction()})
case {Chain.hash_to_lower_case_string(transaction.input), Chain.transaction_to_reward_log(transaction_hash)} do
# CollectReward Input
{"0x6d6b2f77" <> _, %{data: hex_reward_value}} ->
reward =
hex_reward_value
|> Chain.hash_to_lower_case_string()
|> (fn "0x" <> reward_hex -> String.to_integer(reward_hex, 16) end).()
|> Integer.to_string()

conn
|> put_status(200)
|> render(:transaction_staking_reward, %{
transaction: transaction |> maybe_preload_ens_to_transaction(),
reward: reward
})

_ ->
conn
|> put_status(200)
|> render(:transaction, %{transaction: preloaded |> maybe_preload_ens_to_transaction()})
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ defmodule BlockScoutWeb.API.V2.StakingTransactionView do
}
end

def render("staking_transaction_reward.json", %{transaction: transaction, reward: reward}) do
Map.put(prepare_transaction(transaction), :claimed_reward, reward)
end

defp prepare_transaction(transaction) do
%{
hash: transaction.hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ defmodule BlockScoutWeb.API.V2.TransactionView do
}
end

def render("transaction_staking_reward.json", %{conn: conn, transaction: transaction, reward: reward}) do
block_height = Chain.block_height(@api_true)
{decoded_transactions, _, _} = decode_transactions([transaction], true)

Map.put(
prepare_transaction(transaction, conn, true, block_height, nil, decoded_transactions),
:claimed_reward,
reward
)
end

@doc """
Decodes list of logs
"""
Expand Down
44 changes: 44 additions & 0 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2900,6 +2900,28 @@ defmodule Explorer.Chain do
|> select_repo(options).all()
end

@spec transaction_to_reward_log(Hash.Full.t(), [paging_options | necessity_by_association_option | api?]) :: [Log.t()]
def transaction_to_reward_log(transaction_hash, options \\ []) when is_list(options) do
log_with_transactions =
from(log in Log,
inner_join: transaction in Transaction,
on:
transaction.block_hash == log.block_hash and transaction.block_number == log.block_number and
transaction.hash == log.transaction_hash
)

{:ok, claim_rewards_event} =
Chain.string_to_transaction_hash("0xef0a8d7b9b8cbde3c16f5ea86afc300881518ffc9f6e8c8f6984e0037eec16fb")

query =
log_with_transactions
|> where([_, transaction], transaction.hash == ^transaction_hash)
|> where([log], log.first_topic == ^claim_rewards_event)

query
|> select_repo(options).one()
end

@spec staking_transaction_to_logs(Hash.Full.t(), [paging_options | necessity_by_association_option | api?]) :: [
StakingLog.t()
]
Expand Down Expand Up @@ -2927,6 +2949,27 @@ defmodule Explorer.Chain do
|> select_repo(options).all()
end

def staking_transaction_rewards(transaction_hash, options \\ []) when is_list(options) do
log_with_transactions =
from(log in StakingLog,
inner_join: transaction in StakingTransaction,
on:
transaction.block_hash == log.block_hash and transaction.block_number == log.block_number and
transaction.hash == log.transaction_hash
)

{:ok, claim_rewards_event} =
Chain.string_to_transaction_hash("0xef0a8d7b9b8cbde3c16f5ea86afc300881518ffc9f6e8c8f6984e0037eec16fb")

query =
log_with_transactions
|> where([_, transaction], transaction.hash == ^transaction_hash)
|> where([log], log.first_topic == ^claim_rewards_event)

query
|> select_repo(options).one()
end

@doc """
Finds all `t:Explorer.Chain.TokenTransfer.t/0`s for `t:Explorer.Chain.Transaction.t/0`.
Expand Down Expand Up @@ -5307,6 +5350,7 @@ defmodule Explorer.Chain do

def update_epoch_if_not_exists(block, epoch) do
changeset = Block.changeset(block, %{epoch: epoch})

case Repo.update(changeset) do
{:ok, _} -> {:ok, block}
_ -> {:error, "There was an error updating the epoch."}
Expand Down

0 comments on commit f3c800a

Please sign in to comment.