Class: Peatio::Electrum::Blockchain

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

Overview

Constant Summary collapse

DEFAULT_FEATURES =
{ case_sensitive: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientObject (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

Note:

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.

Parameters:

  • settings (Hash) (defaults to: {})

    parameters to use.

Options Hash (settings):

  • :server (String)

    Public blockchain API endpoint.

  • :currencies (Array<Hash>)

    List of currency hashes with :id,:base_factor,:options(deprecated) keys. Custom keys could be added by defining them in Currency #options.

Returns:

  • (Hash)

    merged settings.



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.

Parameters:

  • block_number (Integer)

    the block number.

Returns:

  • (Peatio::Block)

    the block object.

Raises:

  • (Peatio::Blockchain::ClientError)

    if error was raised on blockchain API call.



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.

Parameters:

  • block_number_from (Integer)

    the block number to start from

  • block_number_to (Integer)

    the endding block number (included)

Returns:

  • (Peatio::Block)

    the block object.

Raises:

  • (Peatio::Blockchain::ClientError)

    if error was raised on blockchain API call.



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_numberInteger

Fetches current blockchain height by calling API and returns it as number.

Returns:

  • (Integer)

    the current blockchain height.

Raises:

  • (Peatio::Blockchain::ClientError)

    if error was raised on blockchain API call.



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

Note:

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.

Parameters:

  • address (String)

    the address for requesting balance.

  • currency_id (String)

    which currency balance we need to request.

Returns:

  • (BigDecimal)

    the current address balance.

Raises:

  • (Peatio::Blockchain::ClientError, Peatio::Blockchain::UnavailableAddressBalanceError)


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