Class: Peatio::Eos::Blockchain

Inherits:
Blockchain::Abstract
  • Object
show all
Defined in:
lib/ultex/eos/blockchain.rb

Defined Under Namespace

Classes: MissingTokenNameError, UndefinedCurrencyError

Constant Summary collapse

DEFAULT_FEATURES =
{case_sensitive: true, cash_addr_format: false}.freeze
TOKEN_STANDARD =
"eosio.token"

Instance Method Summary collapse

Constructor Details

#initialize(custom_features = {}) ⇒ Blockchain

Returns a new instance of Blockchain.



13
14
15
16
# File 'lib/ultex/eos/blockchain.rb', line 13

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

Instance Method Details

#configure(settings = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/ultex/eos/blockchain.rb', line 18

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

  supported_settings = settings.slice(*SUPPORTED_SETTINGS)
  supported_settings[:currencies]&.each do |c|
    raise MissingTokenNameError, c[:id] if c.dig(:options, :eos_token_name).blank?
  end
  @settings.merge!(supported_settings)
end

#fetch_block!(block_number) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ultex/eos/blockchain.rb', line 29

def fetch_block!(block_number)
  client.json_rpc("/v1/chain/get_block", "block_num_or_id" => block_number)
        .fetch("transactions").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 Peatio::Eos::Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#latest_block_numberObject



55
56
57
58
59
# File 'lib/ultex/eos/blockchain.rb', line 55

def latest_block_number
  client.json_rpc("/v1/chain/get_info").fetch("head_block_num")
rescue Peatio::Eos::Client::Error => e
  raise Peatio::Blockchain::ClientError, e
end

#load_balance_of_address!(address, currency_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ultex/eos/blockchain.rb', line 41

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

  balance = client.json_rpc("/v1/chain/get_currency_balance",
                            "account" => address, "code" => TOKEN_STANDARD)
                  .find {|b| b.split[1] == currency.dig(:options, :eos_token_name) }

  # EOS return array with balances for all eosio.token currencies
  balance.blank? ? 0 : normalize_balance(balance, currency)
rescue Peatio::Eos::Client::Error => e
  raise Peatio::Wallet::ClientError, e
end