Class: BTC::TransactionOutput
- Inherits:
-
Object
- Object
- BTC::TransactionOutput
- Defined in:
- lib/btcruby/transaction_output.rb
Instance Attribute Summary collapse
-
#block_hash ⇒ Object
Binary hash of the block at which transaction was included.
-
#block_height ⇒ Object
Height of the block at which transaction was included.
-
#block_id ⇒ Object
Returns the value of attribute block_id.
-
#block_time ⇒ Object
Time of the block at which tx was included (::Time instance or nil).
-
#confirmations ⇒ Object
Number of confirmations.
-
#data ⇒ Object
readonly
Serialized binary form of the output (payload).
-
#dictionary ⇒ Object
readonly
Dictionary representation of transaction ready to be encoded in JSON, PropertyList etc.
-
#index ⇒ Object
Index of this output in its transaction.
-
#script ⇒ Object
BTC::Script defining redemption rules for this output (aka scriptPubKey or pk_script).
-
#spent ⇒ Object
If available, returns whether this output is spent (true or false).
-
#spent_confirmations ⇒ Object
If this transaction is spent, contains number of confirmations of the spending transaction.
-
#transaction ⇒ Object
Reference to the owning transaction.
-
#transaction_hash ⇒ Object
Identifier of the transaction.
-
#transaction_id ⇒ Object
Transaction ID.
-
#value ⇒ Object
Value of output in satoshis.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#dup ⇒ Object
Makes a deep copy of a transaction output.
- #dust?(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) ⇒ Boolean
- #dust_limit(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) ⇒ Object
- #init_with_dictionary(dict) ⇒ Object
- #init_with_stream(stream) ⇒ Object
-
#initialize(data: nil, stream: nil, dictionary: nil, value: -1,, script: BTC::Script.new, transaction: nil, transaction_hash: nil, transaction_id: nil, index: nil, block_hash: nil, block_id: nil, block_height: nil, block_time: nil, confirmations: nil, spent: nil, spent_confirmations: nil) ⇒ TransactionOutput
constructor
A new instance of TransactionOutput.
- #inspect(style = :full) ⇒ Object
-
#open_assets_marker? ⇒ Boolean
Returns ‘true` if this transaction output contains an Open Assets marker.
- #outpoint ⇒ Object
- #outpoint_id ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data: nil, stream: nil, dictionary: nil, value: -1,, script: BTC::Script.new, transaction: nil, transaction_hash: nil, transaction_id: nil, index: nil, block_hash: nil, block_id: nil, block_height: nil, block_time: nil, confirmations: nil, spent: nil, spent_confirmations: nil) ⇒ TransactionOutput
Returns a new instance of TransactionOutput.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/btcruby/transaction_output.rb', line 74 def initialize(data: nil, stream: nil, dictionary: nil, value: -1, script: BTC::Script.new, # optional attributes transaction: nil, transaction_hash: nil, transaction_id: nil, index: nil, block_hash: nil, block_id: nil, block_height: nil, block_time: nil, confirmations: nil, spent: nil, spent_confirmations: nil) if stream || data init_with_stream(stream || StringIO.new(data)) elsif dictionary init_with_dictionary(dictionary) else @value = value || 0 @script = script || BTC::Script.new end @transaction = transaction @transaction_hash = transaction_hash @transaction_hash = BTC.hash_from_id(transaction_id) if transaction_id @index = index @block_hash = block_hash @block_hash = BTC.hash_from_id(block_id) if block_id @block_height = block_height @block_time = block_time @confirmations = confirmations @spent = spent @spent_confirmations = spent_confirmations end |
Instance Attribute Details
#block_hash ⇒ Object
Binary hash of the block at which transaction was included. If not confirmed or not available, equals nil.
47 48 49 |
# File 'lib/btcruby/transaction_output.rb', line 47 def block_hash @block_hash end |
#block_height ⇒ Object
Height of the block at which transaction was included. If not confirmed equals -1. Note: ‘block_height` might not be provided by some APIs while `confirmations` may be. Default value is derived from `transaction` if possible or equals nil.
54 55 56 |
# File 'lib/btcruby/transaction_output.rb', line 54 def block_height @block_height end |
#block_id ⇒ Object
Returns the value of attribute block_id.
48 49 50 |
# File 'lib/btcruby/transaction_output.rb', line 48 def block_id @block_id end |
#block_time ⇒ Object
Time of the block at which tx was included (::Time instance or nil). Default value is derived from ‘transaction` if possible or equals nil.
58 59 60 |
# File 'lib/btcruby/transaction_output.rb', line 58 def block_time @block_time end |
#confirmations ⇒ Object
Number of confirmations. Default value is derived from ‘transaction` if possible or equals nil.
62 63 64 |
# File 'lib/btcruby/transaction_output.rb', line 62 def confirmations @confirmations end |
#data ⇒ Object (readonly)
Serialized binary form of the output (payload)
19 20 21 |
# File 'lib/btcruby/transaction_output.rb', line 19 def data @data end |
#dictionary ⇒ Object (readonly)
Dictionary representation of transaction ready to be encoded in JSON, PropertyList etc.
22 23 24 |
# File 'lib/btcruby/transaction_output.rb', line 22 def dictionary @dictionary end |
#index ⇒ Object
Index of this output in its transaction. Default is nil (unknown).
43 44 45 |
# File 'lib/btcruby/transaction_output.rb', line 43 def index @index end |
#script ⇒ Object
BTC::Script defining redemption rules for this output (aka scriptPubKey or pk_script)
13 14 15 |
# File 'lib/btcruby/transaction_output.rb', line 13 def script @script end |
#spent ⇒ Object
If available, returns whether this output is spent (true or false). Default is nil. See also ‘spent_confirmations`.
67 68 69 |
# File 'lib/btcruby/transaction_output.rb', line 67 def spent @spent end |
#spent_confirmations ⇒ Object
If this transaction is spent, contains number of confirmations of the spending transaction. Returns nil if not available or output is not spent. Returns 0 if spending transaction is unconfirmed.
72 73 74 |
# File 'lib/btcruby/transaction_output.rb', line 72 def spent_confirmations @spent_confirmations end |
#transaction ⇒ Object
Reference to the owning transaction. It is set on tx.add_output() and reset to nil on tx.remove_all_outputs. Default is nil.
34 35 36 |
# File 'lib/btcruby/transaction_output.rb', line 34 def transaction @transaction end |
#transaction_hash ⇒ Object
Identifier of the transaction. Default is nil.
37 38 39 |
# File 'lib/btcruby/transaction_output.rb', line 37 def transaction_hash @transaction_hash end |
#transaction_id ⇒ Object
Transaction ID. Always in sync with transaction_hash. Default is nil.
40 41 42 |
# File 'lib/btcruby/transaction_output.rb', line 40 def transaction_id @transaction_id end |
#value ⇒ Object
Value of output in satoshis.
10 11 12 |
# File 'lib/btcruby/transaction_output.rb', line 10 def value @value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
276 277 278 279 280 |
# File 'lib/btcruby/transaction_output.rb', line 276 def ==(other) return true if super(other) return true if data == other.data return false end |
#dup ⇒ Object
Makes a deep copy of a transaction output
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/btcruby/transaction_output.rb', line 284 def dup TransactionOutput.new(value: @value, script: @script.dup, transaction: @transaction, transaction_hash: @transaction_hash, index: @index, block_hash: @block_hash, block_height: @block_height, block_time: @block_time, confirmations: @confirmations, spent: @spent, spent_confirmations: @spent_confirmations) end |
#dust?(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) ⇒ Boolean
251 252 253 |
# File 'lib/btcruby/transaction_output.rb', line 251 def dust?(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) return self.value < self.dust_limit(relay_fee_rate) end |
#dust_limit(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/btcruby/transaction_output.rb', line 255 def dust_limit(relay_fee_rate = Transaction::DEFAULT_RELAY_FEE_RATE) # "Dust" is defined in terms of Transaction::DEFAULT_RELAY_FEE_RATE, # which has units satoshis-per-kilobyte. # If you'd pay more than 1/3 in fees # to spend something, then we consider it dust. # A typical txout is 34 bytes big, and will # need a TransactionInput of at least 148 bytes to spend: # so dust is a txout less than 546 satoshis (3*(34+148)) # with default relay_fee_rate. size = self.data.bytesize + 148 return 3*Transaction.compute_fee(size, fee_rate: relay_fee_rate) end |
#init_with_dictionary(dict) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/btcruby/transaction_output.rb', line 133 def init_with_dictionary(dict) @value = 0 if amount_string = dict["value"] @value = CurrencyFormatter.btc_long_formatter.number_from_string(amount_string) if !@value raise ArgumentError, "Failed to parse bitcoin amount from dictionary 'value': #{amount_string.inspect}" end end @script = nil if dict["scriptPubKey"] && dict["scriptPubKey"].is_a?(Hash) if hex = dict["scriptPubKey"]["hex"] @script = Script.new(data: BTC.from_hex(hex)) if !@script raise ArgumentError, "Failed to parse script from scriptPubKey.hex" end end end end |
#init_with_stream(stream) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/btcruby/transaction_output.rb', line 115 def init_with_stream(stream) if stream.eof? raise ArgumentError, "Can't parse transaction output from stream because it is already closed." end # Read value if !(@value = BTC::WireFormat.read_int64le(stream: stream).first) raise ArgumentError, "Failed to read output value from stream." end # Read script if !(scriptdata = BTC::WireFormat.read_string(stream: stream).first) raise ArgumentError, "Failed to read output script data from stream." end @script = BTC::Script.new(data: scriptdata) end |
#inspect(style = :full) ⇒ Object
298 299 300 301 |
# File 'lib/btcruby/transaction_output.rb', line 298 def inspect(style = :full) %{#<#{self.class.name} value:#{CurrencyFormatter.btc_long_formatter.string_from_number(self.value)}} + %{ script:#{self.script.to_s.inspect}>} end |
#open_assets_marker? ⇒ Boolean
Returns ‘true` if this transaction output contains an Open Assets marker. Does not perform expensive validation. Use this method to quickly filter out non-asset transactions.
247 248 249 |
# File 'lib/btcruby/transaction_output.rb', line 247 def open_assets_marker? self.script.open_assets_marker? end |
#outpoint ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/btcruby/transaction_output.rb', line 200 def outpoint return @outpoint if @outpoint if transaction_hash && index @outpoint = Outpoint.new(transaction_hash: transaction_hash, index: index) end @outpoint end |
#outpoint_id ⇒ Object
208 209 210 |
# File 'lib/btcruby/transaction_output.rb', line 208 def outpoint_id outpoint.outpoint_id end |
#to_h ⇒ Object
268 269 270 |
# File 'lib/btcruby/transaction_output.rb', line 268 def to_h self.dictionary end |