Class: Peatio::Blockchain::Abstract Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/peatio/blockchain/abstract.rb

Overview

This class is abstract.

Represents basic blockchain interface.

Subclass and override abstract methods to implement a peatio plugable blockchain. Then you need to register your blockchain implementation.

Examples:


class MyBlockchain < Peatio::Abstract::Blockchain
  def fetch_block(block_number)
    # do something
  end
  ...
end

# Register MyBlockchain as peatio plugable blockchain.
Peatio::Blockchain.registry[:my_blockchain] = MyBlockchain.new

See Also:

  • Bitcoin as example of Abstract imlementation (inside peatio source https://github.com/rubykube/peatio).

Author:

Constant Summary collapse

SUPPORTED_FEATURES =
Note:

Features list:

case_sensitive - defines if transactions and addresses of current blockchain are case_sensitive.

cash_addr_format - defines if blockchain supports Cash Address format for more info see (support.exodus.io/article/664-bitcoin-cash-address-format)

List of features supported by peatio.

%i[case_sensitive cash_addr_format].freeze
SUPPORTED_SETTINGS =

List of configurable settings.

See Also:

%i[server currencies].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstract

This method is abstract.

Abstract constructor.

Examples:

class MyBlockchain < Peatio::Abstract::Blockchain

  DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze

  # You could override default features by passing them to initializer.
  def initialize(my_custom_features = {})
    @features = DEFAULT_FEATURES.merge(my_custom_features)
  end
  ...
end

# Register MyBlockchain as peatio plugable blockchain.
custom_features = {cash_addr_format: true}
Peatio::Blockchain.registry[:my_blockchain] = MyBlockchain.new(custom_features)

86
87
88
# File 'lib/peatio/blockchain/abstract.rb', line 86

def initialize(*)
  abstract_method
end

Instance Attribute Details

#featuresHash (readonly)

This method is abstract.

Hash of features supported by blockchain.

Returns:

  • (Hash)

    list of features supported by blockchain.

See Also:


37
38
39
# File 'lib/peatio/blockchain/abstract.rb', line 37

def features
  @features
end

#settingsHash (readonly)

This method is abstract.

Current blockchain settings for performing API calls and building blocks.

Returns:

  • (Hash)

    current blockchain settings.

See Also:


59
60
61
# File 'lib/peatio/blockchain/abstract.rb', line 59

def settings
  @settings
end

Instance Method Details

#configure(settings = {}) ⇒ Hash

This method is abstract.
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.


107
108
109
# File 'lib/peatio/blockchain/abstract.rb', line 107

def configure(settings = {})
  abstract_method
end

#fetch_block!(block_number) ⇒ Peatio::Block

This method is abstract.

Fetches blockchain block by calling API and builds block object from response payload.

Parameters:

  • block_number (Integer)

    the block number.

Returns:

Raises:


120
121
122
# File 'lib/peatio/blockchain/abstract.rb', line 120

def fetch_block!(block_number)
  abstract_method
end

#latest_block_numberInteger

This method is abstract.

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

Returns:

  • (Integer)

    the current blockchain height.

Raises:


131
132
133
# File 'lib/peatio/blockchain/abstract.rb', line 131

def latest_block_number
  abstract_method
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:


147
148
149
# File 'lib/peatio/blockchain/abstract.rb', line 147

def load_balance_of_address!(address, currency_id)
  raise Peatio::Blockchain::UnavailableAddressBalanceError
end