|
| 1 | +module GraphNodes |
| 2 | + class Transactions < ActiveInteraction::Base |
| 3 | + string :node_id, default: nil |
| 4 | + string :sort, default: "block_timestamp.desc" |
| 5 | + integer :page, default: 1 |
| 6 | + integer :page_size, default: FiberGraphChannel.default_per_page |
| 7 | + |
| 8 | + string :type_hash, default: nil |
| 9 | + decimal :min_token_amount, default: nil |
| 10 | + decimal :max_token_amount, default: nil |
| 11 | + |
| 12 | + string :address_hash, default: nil |
| 13 | + string :status, default: nil |
| 14 | + |
| 15 | + integer :start_date, default: nil |
| 16 | + integer :end_date, default: nil |
| 17 | + |
| 18 | + validates :status, inclusion: { in: %w[open closed], allow_nil: true } |
| 19 | + |
| 20 | + def execute |
| 21 | + channels = FiberGraphChannel.with_deleted |
| 22 | + channels = channels.where(node1: node_id).or(channels.where(node2: node_id)) |
| 23 | + return FiberGraphChannel.none if channels.empty? |
| 24 | + |
| 25 | + channels = filter_by_address(channels) |
| 26 | + channels = filter_by_type_hash(channels) |
| 27 | + |
| 28 | + wrap_result(channels) |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + def filter_by_address(channels) |
| 34 | + return channels unless address_hash |
| 35 | + |
| 36 | + address = Address.find_address!(address_hash) |
| 37 | + channels.includes(:fiber_account_books).where(fiber_account_books: { address: }) |
| 38 | + end |
| 39 | + |
| 40 | + def filter_by_type_hash(channels) |
| 41 | + return channels unless type_hash |
| 42 | + |
| 43 | + if type_hash.eql?("0x0") |
| 44 | + channels = channels.where(udt_id: nil) |
| 45 | + channels = channels.where(capacity: min_token_amount..) if min_token_amount |
| 46 | + channels = channels.where(capacity: ..max_token_amount) if max_token_amount |
| 47 | + else |
| 48 | + channels = channels.includes(:udt).where(udt: { type_hash: }) |
| 49 | + channels = channels.includes(:funding_cell).where(funding_cell: { udt_amount: min_token_amount.. }) if min_token_amount |
| 50 | + channels = channels.includes(:funding_cell).where(funding_cell: { udt_amount: ..max_token_amount }) if max_token_amount |
| 51 | + end |
| 52 | + |
| 53 | + channels |
| 54 | + end |
| 55 | + |
| 56 | + def wrap_result(channels) |
| 57 | + transactions = channels.flat_map do |channel| |
| 58 | + list = [] |
| 59 | + list << transaction_data(channel.open_transaction_info, channel, true) |
| 60 | + if channel.closed_transaction |
| 61 | + list << transaction_data(channel.closed_transaction_info, channel, false) |
| 62 | + end |
| 63 | + list |
| 64 | + end |
| 65 | + |
| 66 | + transactions = sort_transactions(transactions) |
| 67 | + transactions = filter_by_status(transactions) |
| 68 | + transactions = filter_by_block_timestamp(transactions) |
| 69 | + |
| 70 | + Kaminari.paginate_array(transactions).page(page).per(page_size) |
| 71 | + end |
| 72 | + |
| 73 | + def transaction_data(tx_info, channel, is_open) |
| 74 | + { is_open: is_open, is_udt: channel.udt.present? }.merge(tx_info) |
| 75 | + end |
| 76 | + |
| 77 | + def sort_transactions(transactions) |
| 78 | + direction = sort == "block_timestamp.desc" ? -1 : 1 |
| 79 | + transactions.sort_by { |tx| tx[:block_timestamp] * direction } |
| 80 | + end |
| 81 | + |
| 82 | + def filter_by_block_timestamp(transactions) |
| 83 | + return transactions unless start_date || end_date |
| 84 | + |
| 85 | + transactions.select do |tx| |
| 86 | + ts = tx[:block_timestamp].to_i |
| 87 | + (start_date.nil? || ts >= start_date) && (end_date.nil? || ts <= end_date) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def filter_by_status(transactions) |
| 92 | + case status |
| 93 | + when "open" |
| 94 | + transactions.select { |t| t[:is_open] } |
| 95 | + when "closed" |
| 96 | + transactions.reject { |t| t[:is_open] } |
| 97 | + else |
| 98 | + transactions |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments