Class: Bitbank::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bitbank/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.


3
4
5
6
7
# File 'lib/bitbank/client.rb', line 3

def initialize(config={})
  @config = config
  @endpoint = "http://#{config[:username]}:#{config[:password]}" +
              "@#{config[:host]}:#{config[:port]}"
end

Instance Method Details

#account(account_name) ⇒ Object

Retrieve a particular named account.


10
11
12
# File 'lib/bitbank/client.rb', line 10

def ()
  Bitbank::Account.new(self, , nil, true)
end

#account_by_address(address) ⇒ Object

Rerieve the account that the given Bitcoin address belongs to. Returns nil if the account


16
17
18
19
20
21
22
23
# File 'lib/bitbank/client.rb', line 16

def (address)
   = request('getaccount', address)
  if .present?
    ()
  else
    nil
  end
end

#accountsObject

Returns a list of local accounts.


26
27
28
29
30
31
# File 'lib/bitbank/client.rb', line 26

def accounts
   = request('listaccounts')
  .map do |, |
    Account.new(self, , , false)
  end
end

#balance(account_name = nil) ⇒ Object

If an account is not specified, returns the server's total available balance.

If an account is specified, returns the balance in the account.


37
38
39
# File 'lib/bitbank/client.rb', line 37

def balance(=nil)
  request('getbalance', )
end

#block_countObject

Returns the number of blocks in the longest block chain.


42
43
44
# File 'lib/bitbank/client.rb', line 42

def block_count
  request('getblockcount')
end

#block_numberObject

Returns the block number of the latest block in the longest block chain.


47
48
49
# File 'lib/bitbank/client.rb', line 47

def block_number
  request('getblocknumber')
end

#connection_countObject

Returns the number of connections to other nodes.


52
53
54
# File 'lib/bitbank/client.rb', line 52

def connection_count
  request('getconnectioncount')
end

#difficultyObject

Returns the proof-of-work difficulty as a multiple of the minimum difficulty.


58
59
60
# File 'lib/bitbank/client.rb', line 58

def difficulty
  request('getdifficulty')
end

#get_work(data = nil) ⇒ Object

If data is not specified, returns formatted hash data to work on.

If data is specified, bitcoind will try to solve the block and will return true or false indicating whether or not it was successful.


66
67
68
# File 'lib/bitbank/client.rb', line 66

def get_work(data=nil)
  request('getwork', data)
end

#infoObject

Returns a hash containing bitcoind status information.


71
72
73
# File 'lib/bitbank/client.rb', line 71

def info
  request('getinfo')
end

#new_account(name) ⇒ Object

Returns a new bitcoin account for receiving payments (along with a new address).


77
78
79
# File 'lib/bitbank/client.rb', line 77

def (name)
  (name)
end

#new_address(account_name = nil) ⇒ Object

Returns a new bitcoin address for receiving payments.

If an account is specified (recommended), the new address is added to the address book so payments received with the address are credited to the account.


86
87
88
# File 'lib/bitbank/client.rb', line 86

def new_address(=nil)
  request('getnewaddress', )
end

#request(method, *args) ⇒ Object


110
111
112
113
114
115
116
117
# File 'lib/bitbank/client.rb', line 110

def request(method, *args)
  body = { 'id' => 'jsonrpc', 'method' => method }
  body['params'] = args unless args.empty? || args.first.nil?

  response_json = RestClient.post(@endpoint, body.to_json)
  response = JSON.parse(response_json)
  response['result']
end

#transactions(account_name = nil, count = 10) ⇒ Object

Returns the most recent transactions for the specified account.


91
92
93
94
95
96
# File 'lib/bitbank/client.rb', line 91

def transactions(=nil, count=10)
  transaction_data = request('listtransactions', , count)
  transaction_data.map do |txdata|
    Transaction.new(self, txdata['txid'], txdata)
  end
end

#validate_address(address, locals_invalid = false) ⇒ Object

Determine if the given address is valid.


99
100
101
102
103
104
105
106
107
108
# File 'lib/bitbank/client.rb', line 99

def validate_address(address, locals_invalid=false)
  status = request('validateaddress', address)

  if locals_invalid && status['ismine']
    warn "WARNING: Bitcoin address '#{address}' belongs to local account '#{status['account']}'."
    return false
  end

  status['isvalid']
end