Class: Peatio::Bitcoincash::Blockchain

Inherits:
Peatio::Blockchain::Abstract
  • Object
show all
Includes:
CashAddressFormat
Defined in:
lib/peatio/bitcoincash/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: true}.freeze

Instance Method Summary collapse

Methods included from CashAddressFormat

#normalize_address

Constructor Details

#initialize(custom_features = {}) ⇒ Blockchain

Returns a new instance of Blockchain.



10
11
12
13
# File 'lib/peatio/bitcoincash/blockchain.rb', line 10

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

Instance Method Details

#configure(settings = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/peatio/bitcoincash/blockchain.rb', line 15

def configure(settings = {})
  # Clean client state during configure.
  @client = nil

  @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
end

#fetch_block!(block_number) ⇒ Object



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

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

  client.json_rpc(:getblock, [block_hash, true])
    .fetch('tx').each_with_object([]) do |tx, txs_array|
    txs = build_transaction(client.json_rpc(:getrawtransaction, [tx, true])).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



36
37
38
39
40
# File 'lib/peatio/bitcoincash/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
53
54
# File 'lib/peatio/bitcoincash/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] == normalize_address(address) }

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

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

#transaction_sources(transaction) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/peatio/bitcoincash/blockchain.rb', line 56

def transaction_sources(transaction)
  transaction_hash = client.json_rpc(:getrawtransaction, [transaction.hash, 1])
  transaction_hash['vin'].each_with_object([]) do |vin, source_addresses|
    next if vin['txid'].blank?

    vin_transaction = client.json_rpc(:getrawtransaction, [vin['txid'], 1])
    source = vin_transaction['vout'].find { |hash| hash['n'] == vin['vout'] }
    source_addresses << source['scriptPubKey']['addresses'][0]
  end.compact.uniq
end