Class: BSV::Transaction::FeeModels::LivePolicy

Inherits:
BSV::Transaction::FeeModel show all
Defined in:
lib/bsv/transaction/fee_models/live_policy.rb

Overview

Dynamic fee model that fetches the live mining fee rate from an ARC policy endpoint.

The fetched rate is cached for a configurable TTL (default 5 minutes) so repeated calls to #compute_fee do not repeatedly query the API. If a fetch fails, the model falls back to a configurable default rate.

Examples:

model = BSV::Transaction::FeeModels::LivePolicy.new(
  arc_url: 'https://arcade.gorillapool.io',
  fallback_rate: 50
)
fee = model.compute_fee(transaction)

Constant Summary collapse

DEFAULT_CACHE_TTL =

5 minutes in seconds

300
DEFAULT_ARC_URL =
BSV::MAINNET_URL
DEFAULT_FALLBACK_RATE =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arc_url:, fallback_rate: DEFAULT_FALLBACK_RATE, cache_ttl: DEFAULT_CACHE_TTL, api_key: nil, http_client: nil) ⇒ LivePolicy

Returns a new instance of LivePolicy.

Parameters:

  • arc_url (String)

    ARC base URL (e.g. ‘arcade.gorillapool.io’)

  • fallback_rate (Integer) (defaults to: DEFAULT_FALLBACK_RATE)

    sat/kB to use when fetch fails (default: 100)

  • cache_ttl (Integer) (defaults to: DEFAULT_CACHE_TTL)

    seconds to cache a fetched rate (default: 300)

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

    optional Bearer token for ARC authentication

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

    injectable HTTP client for testing



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 52

def initialize(arc_url:, fallback_rate: DEFAULT_FALLBACK_RATE, cache_ttl: DEFAULT_CACHE_TTL, api_key: nil, http_client: nil)
  super()
  @arc_url = arc_url.chomp('/')
  @fallback_rate = fallback_rate
  @cache_ttl = cache_ttl
  @api_key = api_key
  @http_client = http_client
  @cached_rate = nil
  @cached_at = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#arc_urlString (readonly)

Returns the ARC base URL.

Returns:

  • (String)

    the ARC base URL



27
28
29
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 27

def arc_url
  @arc_url
end

#cache_ttlInteger (readonly)

Returns cache TTL in seconds.

Returns:

  • (Integer)

    cache TTL in seconds



33
34
35
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 33

def cache_ttl
  @cache_ttl
end

#fallback_rateInteger (readonly)

Returns fallback sat/kB when fetch fails.

Returns:

  • (Integer)

    fallback sat/kB when fetch fails



30
31
32
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 30

def fallback_rate
  @fallback_rate
end

Class Method Details

.default(api_key: nil) ⇒ LivePolicy

Returns a LivePolicy with sensible defaults (GorillaPool ARC, 100 sat/kB fallback, 5-minute cache).

Parameters:

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

    optional ARC API key

Returns:



43
44
45
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 43

def self.default(api_key: nil)
  new(arc_url: DEFAULT_ARC_URL, fallback_rate: DEFAULT_FALLBACK_RATE, api_key: api_key)
end

Instance Method Details

#compute_fee(transaction) ⇒ Integer

Compute the fee for a transaction using the latest ARC rate.

Parameters:

  • transaction (Transaction)

    the transaction to compute the fee for

Returns:

  • (Integer)

    the fee in satoshis



68
69
70
71
72
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 68

def compute_fee(transaction)
  rate = current_rate
  size = transaction.estimated_size
  (size / 1000.0 * rate).ceil
end

#current_rateInteger

Return the current sat/kB rate, fetching from ARC if the cache has expired.

Returns:

  • (Integer)

    satoshis per kilobyte



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bsv/transaction/fee_models/live_policy.rb', line 78

def current_rate
  @mutex.synchronize do
    return @cached_rate if cache_valid?

    rate = fetch_rate
    if rate
      @cached_rate = rate
      @cached_at = Time.now
      rate
    elsif @cached_rate
      @cached_rate
    else
      @fallback_rate
    end
  end
end