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
83
84
85
86
87
88
89
90
91
92
93
94
# 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)

  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

  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)
end