Class: Pochette::Backends::BitcoinCore

Inherits:
Object
  • Object
show all
Defined in:
lib/pochette/backends/bitcoin_core.rb

Overview

A bitcoin backend that uses bitcoin-core to retrieve information. See Pochette::Backends::Trendy to learn more about the backend interface and contract.

Instance Method Summary collapse

Constructor Details

#initialize(rpc_url) ⇒ BitcoinCore

Returns a new instance of BitcoinCore.



5
6
7
# File 'lib/pochette/backends/bitcoin_core.rb', line 5

def initialize(rpc_url)
  @rpc_url = rpc_url
end

Instance Method Details

#balances_for(addresses, confirmations) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pochette/backends/bitcoin_core.rb', line 36

def balances_for(addresses, confirmations)
  return {} if addresses.empty?
  result = addresses.reduce({}) do |accum, address|
    accum[address] = [0,0,0,0,0,0]
    accum
  end
  
  # Populate confirmed received
  client.listreceivedbyaddress(confirmations, false, true).each do |a|
    next unless result[a[:address]]
    result[a[:address]][0] = a[:amount]
    result[a[:address]][1] = a[:amount] # Will substract UTXO from it later.
  end

  # Populate unconfirmed received
  client.listreceivedbyaddress(0, false, true).each do |a|
    next unless result[a[:address]]
    result[a[:address]][3] = a[:amount]
    result[a[:address]][4] = a[:amount] # Will substract UTXO from it later.
  end
  
  # Fix sent amounts to not include amounts which werent actually sent.
  client.listunspent(0, 99999999, addresses).each do |utxo|
    if utxo[:confirmations] >= confirmations
      result[utxo[:address]][1] -= utxo[:amount]
    end
    result[utxo[:address]][4] -= utxo[:amount]
  end
  
  # Totals are just one substraction away
  result.each do |k, v| 
    v[2] = v[0] - v[1]
    v[5] = v[3] - v[4]
  end

  result
end

#block_heightObject



102
103
104
# File 'lib/pochette/backends/bitcoin_core.rb', line 102

def block_height
  client.getinfo[:blocks]
end

#clientObject



9
10
11
# File 'lib/pochette/backends/bitcoin_core.rb', line 9

def client
  BitcoinRpc::Client.new(@rpc_url)
end

#incoming_for(addresses, min_date) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pochette/backends/bitcoin_core.rb', line 13

def incoming_for(addresses, min_date)
  return [] if addresses.empty?

  addresses = addresses.to_set
  block_height = client.getblockcount
  from_block = block_height - ((Time.now - min_date) / 60 / 60 * 6).ceil
  block_hash = client.getblockhash(from_block)
  
  result = []
  client.listsinceblock(block_hash, 1, true)[:transactions].each do |t|
    next unless t[:category] == 'receive'
    next unless addresses.include?(t[:address])
    senders = []
    client.getrawtransaction(t[:txid], 1)[:vin].each do |i|
      raw_sender = client.getrawtransaction(i[:txid], 1)
      senders += raw_sender[:vout][i[:vout]][:scriptPubKey][:addresses]
    end
    result << [(t[:amount] * 1_0000_0000).to_i, t[:address], t[:txid],
      t[:confirmations], t[:vout], senders.join(',')]
  end
  result
end

#list_transactions(txids) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pochette/backends/bitcoin_core.rb', line 81

def list_transactions(txids)
  return nil if txids.empty?
  txids.collect do |txid|
    tx = client.getrawtransaction(txid, 1)
    inputs = tx[:vin].collect do |i|
      { prev_hash: i[:txid],
        prev_index: i[:vout],
        sequence: i[:sequence],
        script_sig: i[:scriptSig][:hex] 
      }
    end

    outputs = tx[:vout].collect do |o|
      { amount: (o[:value] * 1_0000_0000).to_i, script_pubkey: o[:scriptPubKey][:hex] }
    end

    { hash: tx[:txid], version: tx[:version], lock_time: tx[:locktime],
      inputs: inputs, bin_outputs: outputs}
  end
end

#list_unspent(addresses) ⇒ Object



74
75
76
77
78
79
# File 'lib/pochette/backends/bitcoin_core.rb', line 74

def list_unspent(addresses)
  return nil if addresses.empty?
  client.listunspent(1, 99999999, addresses).collect do |u| 
    [u[:address], u[:txid], u[:vout], (u[:amount] * 1_0000_0000).to_i, u[:scriptPubKey]]
  end
end

#pushtx(hex) ⇒ Object



106
107
108
# File 'lib/pochette/backends/bitcoin_core.rb', line 106

def pushtx(hex)
  client.sendrawtransaction(hex)
end