Class: BSV::Network::WhatsOnChain

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/network/whats_on_chain.rb

Overview

WhatsOnChain chain data provider for reading transactions and UTXOs from the BSV network.

Any object responding to #fetch_utxos(address) and #fetch_transaction(txid) can serve as a chain data provider; this class implements that contract by delegating to Protocols::WoCREST.

The HTTP client is injectable for testability. It must respond to #request(uri, request) and return an object with #code and #body.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network: :mainnet, http_client: nil, protocol: nil) ⇒ WhatsOnChain

Returns a new instance of WhatsOnChain.

Parameters:

  • network (Symbol) (defaults to: :mainnet)

    :main, :mainnet, :test, :testnet, :stn (legacy compat)

  • http_client (#request, nil) (defaults to: nil)

    injectable HTTP client

  • protocol (BSV::Network::Protocols::WoCREST, nil) (defaults to: nil)

    pre-configured protocol



29
30
31
32
33
34
35
36
# File 'lib/bsv/network/whats_on_chain.rb', line 29

def initialize(network: :mainnet, http_client: nil, protocol: nil)
  if protocol
    @protocol = protocol
  else
    provider = Providers::WhatsOnChain.default(network: network, http_client: http_client)
    @protocol = provider.protocol_for(:get_tx)
  end
end

Class Method Details

.default(testnet: false, **opts) ⇒ WhatsOnChain

Returns a WhatsOnChain instance using the provider default.

Parameters:

  • testnet (Boolean) (defaults to: false)

    when true, uses the testnet endpoint

  • opts (Hash)

    forwarded to the underlying protocol (e.g. api_key:, http_client:)

Returns:



21
22
23
24
# File 'lib/bsv/network/whats_on_chain.rb', line 21

def self.default(testnet: false, **opts)
  provider = Providers::WhatsOnChain.default(testnet: testnet, **opts)
  new(protocol: provider.protocol_for(:get_tx))
end

Instance Method Details

#fetch_transaction(txid) ⇒ BSV::Transaction::Transaction

Fetch a raw transaction by its txid and parse it.

Parameters:

  • txid (String)

    transaction ID (hex)

Returns:



58
59
60
61
62
63
# File 'lib/bsv/network/whats_on_chain.rb', line 58

def fetch_transaction(txid)
  result = @protocol.call(:get_tx, txid)
  raise_on_error(result)

  BSV::Transaction::Transaction.from_hex(result.data)
end

#fetch_utxos(address) ⇒ Array<UTXO>

Fetch unspent transaction outputs for an address.

Parameters:

  • address (String)

    BSV address

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bsv/network/whats_on_chain.rb', line 41

def fetch_utxos(address)
  result = @protocol.call(:get_utxos_all, address)
  raise_on_error(result)

  result.data.map do |entry|
    UTXO.new(
      tx_hash: entry[:tx_hash],
      tx_pos: entry[:tx_pos],
      satoshis: entry[:satoshis],
      height: entry[:height]
    )
  end
end