Class: Peatio::Ripple::Blockchain

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

Constant Summary collapse

UndefinedCurrencyError =

Flow: get block_number from params fetch transactions from this block build transactions from prev step

Class.new(StandardError)
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.



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

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

Instance Method Details

#configure(settings = {}) ⇒ Object



20
21
22
23
24
# File 'lib/peatio/ripple/blockchain.rb', line 20

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

#fetch_block!(ledger_index) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/peatio/ripple/blockchain.rb', line 26

def fetch_block!(ledger_index)
  ledger = client.json_rpc(:ledger,
                               [
                                 {
                                   ledger_index: ledger_index || 'validated',
                                   transactions: true,
                                   expand: true
                                 }
                               ]).dig('ledger')

  return if ledger.blank?
  ledger.fetch('transactions').each_with_object([]) do |tx, txs_array|
    next unless valid_transaction?(tx)

    txs = build_transaction(tx).map do |ntx|
      Peatio::Transaction.new(ntx.merge(block_number: ledger_index))
    end
    txs_array.append(*txs)
  end.yield_self { |txs_array| Peatio::Block.new(ledger_index, txs_array) }
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#latest_block_numberObject



49
50
51
52
53
# File 'lib/peatio/ripple/blockchain.rb', line 49

def latest_block_number
  client.json_rpc(:ledger, [{ ledger_index: 'validated' }]).fetch('ledger_index')
rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#load_balance_of_address!(address, currency_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/peatio/ripple/blockchain.rb', line 55

def load_balance_of_address!(address, currency_id)
  currency = settings[:currencies].find { |c| c[:id] == currency_id.to_s }
  raise UndefinedCurrencyError unless currency

  client.json_rpc(:account_info,
                  [account: normalize_address(address), ledger_index: 'validated', strict: true])
                  .fetch('account_data')
                  .fetch('Balance')
                  .to_d
                  .yield_self { |amount| convert_from_base_unit(amount, currency) }

rescue Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end