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.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/bc.rb', line 241

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).



229
230
231
# File 'lib/bc.rb', line 229

def amounts
  @amounts
end

#bcObject (readonly)

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



219
220
221
# File 'lib/bc.rb', line 219

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)



234
235
236
# File 'lib/bc.rb', line 234

def fees
  @fees
end

#transaction_idObject (readonly)

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



222
223
224
# File 'lib/bc.rb', line 222

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)


283
284
285
286
# File 'lib/bc.rb', line 283

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

#inspectObject



288
289
290
# File 'lib/bc.rb', line 288

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

#timeObject

This is the Time the transaction was made.



277
278
279
# File 'lib/bc.rb', line 277

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