Class: BSV::Transaction::ChainTrackers::Chaintracks

Inherits:
BSV::Transaction::ChainTracker show all
Defined in:
lib/bsv/transaction/chain_trackers/chaintracks.rb

Overview

Chain tracker that verifies merkle roots using the Chaintracks API (Arcade/GorillaPool).

Queries the Chaintracks v2 block header endpoint to retrieve the merkle root for a given block height and compares it with the provided root.

Examples:

tracker = BSV::Transaction::ChainTrackers::Chaintracks.new
tracker.valid_root_for_height?('abcd...', 800_000)

With API key

tracker = BSV::Transaction::ChainTrackers::Chaintracks.new(api_key: 'my-key')
tracker.current_height

Constant Summary collapse

MAINNET_URL =
BSV::MAINNET_URL
TESTNET_URL =
BSV::TESTNET_URL

Instance Method Summary collapse

Constructor Details

#initialize(url: MAINNET_URL, api_key: nil, http_client: nil) ⇒ Chaintracks

Returns a new instance of Chaintracks.

Parameters:

  • url (String) (defaults to: MAINNET_URL)

    base URL for the Chaintracks API

  • api_key (String, nil) (defaults to: nil)

    optional Bearer API key

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

    injectable HTTP client for testing



29
30
31
32
33
34
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 29

def initialize(url: MAINNET_URL, api_key: nil, http_client: nil)
  super()
  @url = url.chomp('/')
  @api_key = api_key
  @http_client = http_client
end

Instance Method Details

#current_heightInteger

Return the current blockchain height.

Returns:

  • (Integer)


55
56
57
58
59
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 55

def current_height
  response = get('/chaintracks/v2/tip', not_found_returns_nil: false)
  data = JSON.parse(response.body)
  data['height']
end

#valid_root_for_height?(root, height) ⇒ Boolean

Verify that a merkle root is valid for the given block height.

Parameters:

  • root (String)

    merkle root as a hex string

  • height (Integer)

    block height

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/bsv/transaction/chain_trackers/chaintracks.rb', line 41

def valid_root_for_height?(root, height)
  response = get("/chaintracks/v2/header/height/#{height}")
  return false if response.nil?

  data = JSON.parse(response.body)
  merkle_root = data['merkleRoot']
  return false unless merkle_root

  merkle_root.downcase == root.downcase
end