From 539fe1b4126c5623c500e91cf36d2599e89367dd Mon Sep 17 00:00:00 2001 From: Rabbit Date: Tue, 3 Dec 2024 15:30:14 +0800 Subject: [PATCH] feat: fiber graph channel add closed_transaction_info --- app/models/fiber_graph_channel.rb | 24 +++++++++++++++---- app/models/suggest_query.rb | 7 +++++- .../v2/fiber/graph_channels/index.jbuilder | 2 +- .../api/v2/fiber/graph_nodes/show.jbuilder | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/models/fiber_graph_channel.rb b/app/models/fiber_graph_channel.rb index d83c16d88..42e03ee64 100644 --- a/app/models/fiber_graph_channel.rb +++ b/app/models/fiber_graph_channel.rb @@ -12,16 +12,30 @@ class FiberGraphChannel < ApplicationRecord scope :open_channels, -> { where(closed_transaction_id: nil) } - def outpoint_info - open_transaction.as_json(only: %i[tx_hash block_number block_timestamp transaction_fee]).merge( + def open_transaction_info + open_transaction.as_json(only: %i[tx_hash block_number block_timestamp]).merge( { - funding_capacity: funding_cell.capacity, - funding_udt_amount: funding_cell.udt_amount, - funding_address: funding_cell.address_hash, + capacity: funding_cell.capacity, + udt_amount: funding_cell.udt_amount, + address: funding_cell.address_hash, }, ) end + def closed_transaction_info + return Hash.new unless closed_transaction + + closed_transaction.as_json(only: %i[tx_hash block_number block_timestamp]).merge( + close_accounts: closed_transaction.outputs.map do |cell| + { + capacity: cell.capacity, + udt_amount: cell.udt_amount, + address: cell.address_hash, + } + end, + ) + end + def udt_info udt&.as_json(only: %i[full_name symbol decimal icon_file]) end diff --git a/app/models/suggest_query.rb b/app/models/suggest_query.rb index 6803b0299..b004e963f 100644 --- a/app/models/suggest_query.rb +++ b/app/models/suggest_query.rb @@ -157,7 +157,12 @@ def find_bitcoin_address end def find_fiber_graph_nodes - normalized_key = QueryKeyUtils.hex_string?(query_key) ? query_key.delete_prefix(Settings.default_hash_prefix) : query_key + normalized_key = + if QueryKeyUtils.start_with_default_hash_prefix?(query_key) + query_key.delete_prefix(Settings.default_hash_prefix) + else + query_key + end fiber_graph_nodes = FiberGraphNode.where("alias = :query_key or peer_id = :query_key or node_id = :query_key", query_key: normalized_key) FiberGraphNodeSerializer.new(fiber_graph_nodes) if fiber_graph_nodes.present? end diff --git a/app/views/api/v2/fiber/graph_channels/index.jbuilder b/app/views/api/v2/fiber/graph_channels/index.jbuilder index f130eb241..a53cbb927 100644 --- a/app/views/api/v2/fiber/graph_channels/index.jbuilder +++ b/app/views/api/v2/fiber/graph_channels/index.jbuilder @@ -1,6 +1,6 @@ json.data do json.fiber_graph_channels @channels do |channel| - json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :outpoint_info) + json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :open_transaction_info, :closed_transaction_info) json.funding_tx_block_number channel.funding_tx_block_number.to_s json.funding_tx_index channel.funding_tx_index.to_s json.last_updated_timestamp channel.last_updated_timestamp.to_s diff --git a/app/views/api/v2/fiber/graph_nodes/show.jbuilder b/app/views/api/v2/fiber/graph_nodes/show.jbuilder index 5c24bc39f..d0dadbc14 100644 --- a/app/views/api/v2/fiber/graph_nodes/show.jbuilder +++ b/app/views/api/v2/fiber/graph_nodes/show.jbuilder @@ -6,7 +6,7 @@ json.data do json.udt_cfg_infos @node.udt_cfg_infos json.fiber_graph_channels @graph_channels do |channel| - json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :outpoint_info) + json.(channel, :channel_outpoint, :node1, :node2, :chain_hash, :open_transaction_info, :closed_transaction_info) json.funding_tx_block_number channel.funding_tx_block_number.to_s json.funding_tx_index channel.funding_tx_index.to_s json.last_updated_timestamp channel.last_updated_timestamp.to_s