Class: Peatio::Electrum::Blockchain
- Inherits:
-
Blockchain::Abstract
- Object
- Blockchain::Abstract
- Peatio::Electrum::Blockchain
- Defined in:
- lib/peatio/electrum/blockchain.rb
Overview
See the abstract class here: github.com/openware/peatio-core/blob/master/lib/peatio/blockchain/abstract.rb
Constant Summary collapse
- DEFAULT_FEATURES =
{ case_sensitive: true }.freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#configure(settings = {}) ⇒ Hash
Merges given configuration parameters with defined during initialization and returns the result.
-
#fetch_block!(block_number) ⇒ Peatio::Block
Fetches blockchain block by calling API and builds block object from response payload.
-
#fetch_multi_blocks!(block_number_from, block_number_to) ⇒ Peatio::Block
Fetches multiple blocks from the blockchain and builds an aggregated object from response payload.
-
#initialize(custom_features = {}) ⇒ Blockchain
constructor
You could override default features by passing them to initializer.
-
#latest_block_number ⇒ Integer
Fetches current blockchain height by calling API and returns it as number.
-
#load_balance_of_address!(address, _currency_id) ⇒ BigDecimal
Fetches address balance of specific currency.
Constructor Details
#initialize(custom_features = {}) ⇒ Blockchain
You could override default features by passing them to initializer.
13 14 15 16 |
# File 'lib/peatio/electrum/blockchain.rb', line 13 def initialize(custom_features = {}) @features = DEFAULT_FEATURES.merge(custom_features) @settings = {} end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/peatio/electrum/blockchain.rb', line 9 def client @client end |
Instance Method Details
#configure(settings = {}) ⇒ Hash
Be careful with your blockchain state after configure. Clean everything what could be related to other blockchain configuration. E.g. client state.
Merges given configuration parameters with defined during initialization and returns the result.
33 34 35 36 37 |
# File 'lib/peatio/electrum/blockchain.rb', line 33 def configure(settings = {}) @settings.merge!(settings.slice(*SUPPORTED_SETTINGS)) @client = Client.new(@settings[:server]) @currencies_ids = @settings[:currencies].pluck(:id) end |
#fetch_block!(block_number) ⇒ Peatio::Block
Fetches blockchain block by calling API and builds block object from response payload.
46 47 48 49 50 51 |
# File 'lib/peatio/electrum/blockchain.rb', line 46 def fetch_block!(block_number) block = fetch_multi_blocks!(block_number, block_number + 1).first return Peatio::Block.new(block_number, []) if block.nil? block end |
#fetch_multi_blocks!(block_number_from, block_number_to) ⇒ Peatio::Block
Fetches multiple blocks from the blockchain and builds an aggregated object from response payload.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/peatio/electrum/blockchain.rb', line 61 def fetch_multi_blocks!(block_number_from, block_number_to) unless client.is_synchronized raise Peatio::Blockchain::ClientError, 'Electrum is synchronizing' end txs = [] blocks = [] current_height = block_number_from client.history(nil, true, false, true, block_number_from, block_number_to)['transactions'].each do |tx| if tx['height'] != current_height blocks << Peatio::Block.new(current_height, txs) unless txs.empty? txs = [] current_height = tx['height'] end fee = tx['fee'] (tx['outputs'] || []).each_with_index do |out, i| @currencies_ids.each do |currency_id| txs << Peatio::Transaction.new( hash: tx['txid'], txout: i, to_address: out['address'], amount: out['value'].to_d, status: 'success', block_number: tx['height'], currency_id: currency_id, fee_currency_id: currency_id, fee: fee ) end end end blocks << Peatio::Block.new(current_height, txs) unless txs.empty? blocks end |
#latest_block_number ⇒ Integer
Fetches current blockchain height by calling API and returns it as number.
103 104 105 106 107 |
# File 'lib/peatio/electrum/blockchain.rb', line 103 def latest_block_number client.get_local_height rescue Client::Error => e raise Peatio::Blockchain::ClientError, e end |
#load_balance_of_address!(address, _currency_id) ⇒ BigDecimal
Optional. Don’t override this method if your blockchain
Fetches address balance of specific currency.
doesn’t provide functionality to get balance by address.
if error was raised on blockchain API call ClientError is raised. if blockchain API call was successful but we can’t detect balance for address Error is raised.
121 122 123 |
# File 'lib/peatio/electrum/blockchain.rb', line 121 def load_balance_of_address!(address, _currency_id) client.get_address_balance(address)['confirmed'] end |