Class: Bitcoin::RPC::BitcoinCoreClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/rpc/bitcoin_core_client.rb

Overview

Client implementation for RPC to Bitcoin Core.

Usage

config = ‘http’, host: ‘localhost’, port: 18332, user: ‘xxx’, password: ‘yyy’ client = Bitcoin::RPC::BitcoinCoreClient.new(config)

You can execute the CLI command supported by Bitcoin Core as follows:

client.listunspent client.getblockchaininfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BitcoinCoreClient

Returns a new instance of BitcoinCoreClient.

Parameters:

  • config (Hash)

    a configuration required to connect to Bitcoin Core.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bitcoin/rpc/bitcoin_core_client.rb', line 22

def initialize(config)
  @config = config

  commands = request(:help).split("\n").inject([]) do |memo_ary, line|
    if !line.empty? && !line.start_with?('==')
      memo_ary << line.split(' ').first.to_sym
    end
    memo_ary
  end
  BitcoinCoreClient.class_eval do
    commands.each do |command|
      define_method(command) do |*params|
        request(command, *params)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Call CLI command on Ruby-like method names. e.g. generate_to_address, send_to_address, get_wallet_info



72
73
74
75
76
77
78
# File 'lib/bitcoin/rpc/bitcoin_core_client.rb', line 72

def method_missing(name, *args)
  if name.to_s.include?('_')
    send(name.to_s.gsub('_', '').to_sym, args)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/bitcoin/rpc/bitcoin_core_client.rb', line 19

def config
  @config
end