Class: BSV::Transaction::FeeModels::LivePolicy
- Inherits:
-
BSV::Transaction::FeeModel
- Object
- BSV::Transaction::FeeModel
- BSV::Transaction::FeeModels::LivePolicy
- 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.
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
-
#arc_url ⇒ String
readonly
The ARC base URL.
-
#cache_ttl ⇒ Integer
readonly
Cache TTL in seconds.
-
#fallback_rate ⇒ Integer
readonly
Fallback sat/kB when fetch fails.
Class Method Summary collapse
-
.default(api_key: nil) ⇒ LivePolicy
Returns a LivePolicy with sensible defaults (GorillaPool ARC, 100 sat/kB fallback, 5-minute cache).
Instance Method Summary collapse
-
#compute_fee(transaction) ⇒ Integer
Compute the fee for a transaction using the latest ARC rate.
-
#current_rate ⇒ Integer
Return the current sat/kB rate, fetching from ARC if the cache has expired.
-
#initialize(arc_url:, fallback_rate: DEFAULT_FALLBACK_RATE, cache_ttl: DEFAULT_CACHE_TTL, api_key: nil, http_client: nil) ⇒ LivePolicy
constructor
A new instance of LivePolicy.
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.
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_url ⇒ String (readonly)
Returns 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_ttl ⇒ Integer (readonly)
Returns 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_rate ⇒ Integer (readonly)
Returns 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).
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.
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_rate ⇒ Integer
Return the current sat/kB rate, fetching from ARC if the cache has expired.
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 |