Class: Peatio::Coinpaymentnew::Blockchain

Inherits:
Blockchain::Abstract
  • Object
show all
Defined in:
lib/peatio/coinpaymentnew/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/coinpaymentnew/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/coinpaymentnew/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
# File 'lib/peatio/coinpaymentnew/blockchain.rb', line 20

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

  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.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) }
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#latest_block_numberObject



34
35
36
37
38
# File 'lib/peatio/coinpaymentnew/blockchain.rb', line 34

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



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

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

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

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