Class: BSV::MCP::Tools::CheckBalance

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

Overview

Returns the total confirmed and unconfirmed balance for a BSV address or WIF private key, along with the individual UTXOs.

Accepts either a WIF-encoded private key (from which the address is derived) or a plain P2PKH address string. The tool auto-detects the input type via a try/rescue on PrivateKey.from_wif.

Class Method Summary collapse

Class Method Details

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bsv/mcp/tools/check_balance.rb', line 58

def self.call(address_or_wif:, network: nil, server_context: nil)
  net_sym = Helpers.resolve_network_sym(network, server_context)
  address = resolve_address(address_or_wif, net_sym)

  woc = BSV::Network::WhatsOnChain.new(network: net_sym)
  utxos = woc.fetch_utxos(address)

  balance = utxos.sum(&:satoshis)
  result = {
    address: address,
    network: net_sym.to_s,
    balance_satoshis: balance,
    utxo_count: utxos.length,
    utxos: utxos.map { |u| Helpers.utxo_to_h(u) }
  }

  ::MCP::Tool::Response.new(
    [::MCP::Content::Text.new(result.to_json)],
    structured_content: result
  )
rescue ArgumentError => e
  Helpers.error_response(e.message)
rescue BSV::Network::ChainProviderError => e
  Helpers.error_response("#{e.message} (HTTP #{e.status_code})")
end