Class: BSV::Network::WhatsOnChain
- Inherits:
-
Object
- Object
- BSV::Network::WhatsOnChain
- 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
-
.default(testnet: false, **opts) ⇒ WhatsOnChain
Returns a WhatsOnChain instance using the provider default.
Instance Method Summary collapse
-
#fetch_transaction(txid) ⇒ BSV::Transaction::Transaction
Fetch a raw transaction by its txid and parse it.
-
#fetch_utxos(address) ⇒ Array<UTXO>
Fetch unspent transaction outputs for an address.
-
#initialize(network: :mainnet, http_client: nil, protocol: nil) ⇒ WhatsOnChain
constructor
A new instance of WhatsOnChain.
Constructor Details
#initialize(network: :mainnet, http_client: nil, protocol: nil) ⇒ WhatsOnChain
Returns a new instance of WhatsOnChain.
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.
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.
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.
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 |