Class: Bitcoin::Transaction

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

Overview

This represents a single Bitcoin transaction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bc, transaction_id) ⇒ Transaction

bc is a Bitcoin::Client. (The String) transaction_id is our unique transaction ID. If bitcoind doesn’t know about a transaction with that ID, we raise UnknownTransaction. Note that bitcoind only knows about transactions involving private keys in our wallet even though information about other transactions is in the block chain.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/bc.rb', line 218

def initialize(bc, transaction_id)
  @bc = bc
  @transaction_id = transaction_id.freeze

  unless @bc.is_a?(Bitcoin::Client)
    raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)"
  end

  begin
    info = @bc.jr.gettransaction(transaction_id)
  rescue Jr::ServerError => ex
    if info.code == -5
      raise UnknownTransaction, transaction_id
    end
  end

  @unix_time = info.fetch('time')

  @fees = Hash.new
  @amounts = Hash.new

  info.fetch('details').each do |detail|
    address = detail.fetch('address').freeze

    @amounts[address] = detail.fetch('amount').freeze

    if detail['fee']
      @fees[address] = detail['fee'].freeze
    end
  end

  @fees.freeze
  @amounts.freeze
end

Instance Attribute Details

#amountsObject (readonly)

This is a Hash whose keys are the (String) bitcoin addresses involved in the transaction (whose private keys bitcoind has) and whose values are the (Float) amounts that the corresponding address gained (in which case the value would be positive) or lost (in which case the value would be negative).



206
207
208
# File 'lib/bc.rb', line 206

def amounts
  @amounts
end

#bcObject (readonly)

This is the Bitcoin::Client instance we’re connected to.



196
197
198
# File 'lib/bc.rb', line 196

def bc
  @bc
end

#feesObject (readonly)

This is a Hash similar to @amounts, except that the values are the amounts each address paid in transaction fees. (c.f. en.bitcoin.it/wiki/Transaction_fees)



211
212
213
# File 'lib/bc.rb', line 211

def fees
  @fees
end

#transaction_idObject (readonly)

This (String) is a unique identifier assigned to this transaction.



199
200
201
# File 'lib/bc.rb', line 199

def transaction_id
  @transaction_id
end

Instance Method Details

#include?(address) ⇒ Boolean

Does this transaction include address? (Note that this only works for addresses which we control.)

Returns:

  • (Boolean)


260
261
262
263
# File 'lib/bc.rb', line 260

def include?(address)
  address = address.to_s if address.is_a?(Address)
  @fees.keys.include?(address) or @amounts.keys.include?(address)
end

#inspectObject



265
266
267
# File 'lib/bc.rb', line 265

def inspect
  "#<Bitcoin::Transaction #{@transaction_id}>"
end

#timeObject

This is the Time the transaction was made.



254
255
256
# File 'lib/bc.rb', line 254

def time
  @time ||= Time.at(@unix_time).utc.freeze
end