Class: BSV::Wallet::WhatsOnChainProvider

Inherits:
Object
  • Object
show all
Includes:
ChainProvider
Defined in:
lib/bsv/wallet_interface/whats_on_chain_provider.rb

Overview

ChainProvider implementation backed by the WhatsOnChain public API.

Delegates to Network::WhatsOnChain for all HTTP communication, adapting its responses to the ChainProvider contract.

Examples:

Use with WalletClient

provider = BSV::Wallet::WhatsOnChainProvider.new(network: :main)
wallet = BSV::Wallet::WalletClient.new(key, chain_provider: provider)
wallet.sync_utxos

Instance Method Summary collapse

Constructor Details

#initialize(network: :main, http_client: nil) ⇒ WhatsOnChainProvider

Returns a new instance of WhatsOnChainProvider.

Parameters:

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

    :main (default) or :test

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

    injectable HTTP client for testing



19
20
21
22
# File 'lib/bsv/wallet_interface/whats_on_chain_provider.rb', line 19

def initialize(network: :main, http_client: nil)
  woc_network = network == :main ? :mainnet : :testnet
  @woc = BSV::Network::WhatsOnChain.new(network: woc_network, http_client: http_client)
end

Instance Method Details

#get_header(_height) ⇒ String

Returns the block header at the given height.

Parameters:

  • _height (Integer)

    block height

Returns:

  • (String)

    80-byte hex-encoded block header

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/bsv/wallet_interface/whats_on_chain_provider.rb', line 33

def get_header(_height)
  raise NotImplementedError, 'WhatsOnChainProvider#get_header not yet implemented'
end

#get_heightInteger

Returns the current blockchain height.

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/bsv/wallet_interface/whats_on_chain_provider.rb', line 26

def get_height
  raise NotImplementedError, 'WhatsOnChainProvider#get_height not yet implemented'
end

#get_transaction(txid) ⇒ String

Returns the raw transaction hex for the given txid.

Delegates to Network::WhatsOnChain#fetch_transaction and serialises the result back to hex.

Parameters:

  • txid (String)

    transaction ID (hex)

Returns:

  • (String)

    raw transaction hex string



57
58
59
# File 'lib/bsv/wallet_interface/whats_on_chain_provider.rb', line 57

def get_transaction(txid)
  @woc.fetch_transaction(txid).to_hex
end

#get_utxos(address) ⇒ Array<Hash>

Returns unspent transaction outputs for the given address.

Delegates to Network::WhatsOnChain#fetch_utxos and normalises the result to plain hashes matching the ChainProvider contract.

Parameters:

  • address (String)

    BSV address

Returns:

  • (Array<Hash>)

    array of hashes with :tx_hash, :tx_pos, :value keys



44
45
46
47
48
# File 'lib/bsv/wallet_interface/whats_on_chain_provider.rb', line 44

def get_utxos(address)
  @woc.fetch_utxos(address).map do |utxo|
    { tx_hash: utxo.tx_hash, tx_pos: utxo.tx_pos, value: utxo.satoshis }
  end
end