Class: Peatio::Dash::Blockchain

Inherits:
Blockchain::Abstract
  • Object
show all
Defined in:
lib/peatio/dash/blockchain.rb

Overview

TODO: Processing of unconfirmed transactions from mempool isn’t supported now.

Constant Summary collapse

DEFAULT_FEATURES =
{case_sensitive: true, cash_addr_format: false}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(custom_features = {}) ⇒ Blockchain

Returns a new instance of Blockchain.



9
10
11
12
# File 'lib/peatio/dash/blockchain.rb', line 9

def initialize(custom_features={})
  @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
  @settings = {}
end

Instance Method Details

#configure(settings = {}) ⇒ Object



14
15
16
17
18
# File 'lib/peatio/dash/blockchain.rb', line 14

def configure(settings={})
  # Clean client state during configure.
  @client = nil
  @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
end

#fetch_block!(block_number) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/peatio/dash/blockchain.rb', line 20

def fetch_block!(block_number)
  block_hash = client.json_rpc(:getblockhash, [block_number])

  block_txs = client.json_rpc(:getblock, [block_hash, 2])
                    .fetch("tx").each_with_object([]) do |tx, txs_array|
    txs = build_transaction(tx).map do |ntx|
      Peatio::Transaction.new(ntx.merge(block_number: block_number))
    end
    txs_array.append(*txs)
  end

  Peatio::Block.new(block_number, block_txs)
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#latest_block_numberObject



36
37
38
39
40
# File 'lib/peatio/dash/blockchain.rb', line 36

def latest_block_number
  client.json_rpc(:getblockcount)
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#load_balance_of_address!(address, _currency_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/peatio/dash/blockchain.rb', line 42

def load_balance_of_address!(address, _currency_id)
  address_with_balance = client.json_rpc(:listaddressgroupings)
                               .flatten(1)
                               .find {|addr| addr[0] == address }

  raise Peatio::Blockchain::UnavailableAddressBalanceError, address if address_with_balance.blank?

  address_with_balance[1].to_d
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end