Class: BSV::MCP::Tools::FetchUtxos

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/bsv/mcp/tools/fetch_utxos.rb

Overview

Fetches unspent transaction outputs (UTXOs) for a BSV address using the WhatsOnChain API.

The http_client dependency is injectable for testing — pass a compatible mock object to new_woc.

Class Method Summary collapse

Class Method Details

.call(address:, network: nil, server_context: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bsv/mcp/tools/fetch_utxos.rb', line 51

def self.call(address:, network: nil, server_context: nil)
  net_sym = Helpers.resolve_network_sym(network, server_context)
  provider = BSV::Network::Providers::WhatsOnChain.default(network: net_sym)
  utxo_result = provider.call(:get_utxos_all, address)

  unless utxo_result.success?
    code = utxo_result.[:status_code]
    msg = utxo_result.message
    msg = "#{msg} (HTTP #{code})" if code
    return Helpers.error_response(msg)
  end

  utxos = utxo_result.data.map do |entry|
    BSV::Network::UTXO.new(
      tx_hash: entry[:tx_hash], tx_pos: entry[:tx_pos],
      satoshis: entry[:satoshis], height: entry[:height]
    )
  end

  result = {
    address: address,
    network: net_sym.to_s,
    utxos: utxos.map { |u| Helpers.utxo_to_h(u) }
  }

  ::MCP::Tool::Response.new(
    [::MCP::Content::Text.new(result.to_json)],
    structured_content: result
  )
end