Class: Sibit::BestOf

Inherits:
Object
  • Object
show all
Defined in:
lib/sibit/bestof.rb

Overview

API best of.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(list, log: Loog::NULL, verbose: false) ⇒ BestOf

Constructor.



17
18
19
20
21
# File 'lib/sibit/bestof.rb', line 17

def initialize(list, log: Loog::NULL, verbose: false)
  @list = list
  @log = log
  @verbose = verbose
end

Instance Method Details

#balance(address) ⇒ Object

Gets the balance of the address, in satoshi.



31
32
33
34
35
# File 'lib/sibit/bestof.rb', line 31

def balance(address)
  best_of('balance') do |api|
    api.balance(address)
  end
end

#block(hash) ⇒ Object

This method should fetch a block and return as a hash.



92
93
94
95
96
# File 'lib/sibit/bestof.rb', line 92

def block(hash)
  best_of('block') do |api|
    api.block(hash)
  end
end

#feesObject

Get recommended fees, in satoshi per byte. The method returns a hash: { S: 12, M: 45, L: 100, XL: 200 }



53
54
55
# File 'lib/sibit/bestof.rb', line 53

def fees
  best_of('fees', &:fees)
end

#height(hash) ⇒ Object

Get the height of the block.



38
39
40
41
42
# File 'lib/sibit/bestof.rb', line 38

def height(hash)
  best_of('height') do |api|
    api.height(hash)
  end
end

#latestObject

Latest block hash.



65
66
67
# File 'lib/sibit/bestof.rb', line 65

def latest
  best_of('latest', &:latest)
end

#next_of(hash) ⇒ Object

Get the hash of the next block.



45
46
47
48
49
# File 'lib/sibit/bestof.rb', line 45

def next_of(hash)
  best_of('next_of') do |api|
    api.next_of(hash)
  end
end

#price(currency = 'USD') ⇒ Object

Current price of BTC in USD (float returned).



24
25
26
27
28
# File 'lib/sibit/bestof.rb', line 24

def price(currency = 'USD')
  best_of('price') do |api|
    api.price(currency)
  end
end

#push(hex) ⇒ Object

Push this transaction (in hex format) to the network. This one does not vote: pushing is a side effect, so it hands the transaction to each API in turn and stops at the first that accepts it.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sibit/bestof.rb', line 72

def push(hex)
  return @list.push(hex) unless @list.is_a?(Array)
  errors = []
  @list.each do |api|
    return api.push(hex)
  rescue Sibit::NotSupportedError
    nil
  rescue Sibit::Error => e
    errors << e
    @log.debug("The API #{api.class.name} failed at push(): #{e.message}") if @verbose
  end
  errors.each { |e| @log.debug(Backtrace.new(e).to_s) }
  raise(
    Sibit::Error,
    "No APIs out of #{@list.length} managed to succeed at push(): " \
    "#{@list.map { |a| a.class.name }.join(', ')}"
  )
end

#utxos(keys) ⇒ Object

Fetch all unspent outputs per address.



58
59
60
61
62
# File 'lib/sibit/bestof.rb', line 58

def utxos(keys)
  best_of('utxos') do |api|
    api.utxos(keys)
  end
end