class Bitbank::Client

Public Class Methods

new(config={}) click to toggle source
# File lib/bitbank/client.rb, line 3
def initialize(config={})
  @config = config
  @endpoint = "http://#{config[:username]}:#{config[:password]}" +
              "@#{config[:host]}:#{config[:port]}"
end

Public Instance Methods

account(account_name) click to toggle source

Retrieve a particular named account.

# File lib/bitbank/client.rb, line 10
def account(account_name)
  Bitbank::Account.new(self, account_name, nil, true)
end
account_by_address(address) click to toggle source

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

# File lib/bitbank/client.rb, line 16
def account_by_address(address)
  account_name = request('getaccount', address)
  if account_name.present?
    account(account_name)
  else
    nil
  end
end
accounts() click to toggle source

Returns a list of local accounts.

# File lib/bitbank/client.rb, line 26
def accounts
  account_data = request('listaccounts')
  account_data.map do |account_name, account_value|
    Account.new(self, account_name, account_value, false)
  end
end
balance(account_name=nil) click to toggle source

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

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

# File lib/bitbank/client.rb, line 37
def balance(account_name=nil)
  request('getbalance', account_name)
end
block_count() click to toggle source

Returns the number of blocks in the longest block chain.

# File lib/bitbank/client.rb, line 42
def block_count
  request('getblockcount')
end
block_number() click to toggle source

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

# File lib/bitbank/client.rb, line 47
def block_number
  request('getblocknumber')
end
connection_count() click to toggle source

Returns the number of connections to other nodes.

# File lib/bitbank/client.rb, line 52
def connection_count
  request('getconnectioncount')
end
difficulty() click to toggle source

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

# File lib/bitbank/client.rb, line 58
def difficulty
  request('getdifficulty')
end
get_work(data=nil) click to toggle source

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.

# File lib/bitbank/client.rb, line 66
def get_work(data=nil)
  request('getwork', data)
end
info() click to toggle source

Returns a hash containing bitcoind status information.

# File lib/bitbank/client.rb, line 71
def info
  request('getinfo')
end
new_account(name) click to toggle source

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

# File lib/bitbank/client.rb, line 77
def new_account(name)
  account(name)
end
new_address(account_name=nil) click to toggle source

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.

# File lib/bitbank/client.rb, line 86
def new_address(account_name=nil)
  request('getnewaddress', account_name)
end
request(method, *args) click to toggle source
# 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) click to toggle source

Returns the most recent transactions for the specified account.

# File lib/bitbank/client.rb, line 91
def transactions(account_name=nil, count=10)
  transaction_data = request('listtransactions', account_name, count)
  transaction_data.map do |txdata|
    Transaction.new(self, txdata['txid'], txdata)
  end
end
validate_address(address, locals_invalid=false) click to toggle source

Determine if the given address is valid.

# 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