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.



234
235
236
237
238
239
240
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
# File 'lib/bc.rb', line 234

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



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

def amounts
  @amounts
end

#bcObject (readonly)

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



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

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)



227
228
229
# File 'lib/bc.rb', line 227

def fees
  @fees
end

#transaction_idObject (readonly)

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



215
216
217
# File 'lib/bc.rb', line 215

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)


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

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

#inspectObject



281
282
283
# File 'lib/bc.rb', line 281

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

#timeObject

This is the Time the transaction was made.



270
271
272
# File 'lib/bc.rb', line 270

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