Class: Transaction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, value:, data:, nonce: nil) ⇒ Transaction

Returns a new instance of Transaction.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/universum/universum.rb', line 291

def initialize( from:, to:, value:, data:, nonce: nil )
  ## note: from only allows accounts
  if from.is_a?( Account )
     = from
  else
     = Account.at( from )   ## lookup account by address
  end

  @from = .address

  if to.is_a?( Contract )
    @to = "#{to.address} (#{to.class.name})"
  elsif to.is_a?( Account )     ## note: to allows Contracts AND Accounts
    @to = to.address
  else
    @to = to    # might be a contract or account (pass through for now)
  end

  @value = value
  @data  = data

  if nonce
    @nonce = nonce
  else
    ## auto-add nonce (that is, tx counter - auto-increment)
    @nonce = .tx  ## get transaction (tx) counter (starts with 0)
    ._auto_inc_tx
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



289
290
291
# File 'lib/universum/universum.rb', line 289

def data
  @data
end

#fromObject (readonly)

Returns the value of attribute from.



289
290
291
# File 'lib/universum/universum.rb', line 289

def from
  @from
end

#nonceObject (readonly)

Returns the value of attribute nonce.



289
290
291
# File 'lib/universum/universum.rb', line 289

def nonce
  @nonce
end

#toObject (readonly)

Returns the value of attribute to.



289
290
291
# File 'lib/universum/universum.rb', line 289

def to
  @to
end

#valueObject (readonly)

Returns the value of attribute value.



289
290
291
# File 'lib/universum/universum.rb', line 289

def value
  @value
end

Class Method Details

.send(**kwargs) ⇒ Object

convenience helper for Uni.send_transaction



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

def self.send( **kwargs )  ## convenience helper for Uni.send_transaction
  Universum.send_transaction( **kwargs )
end

Instance Method Details

#contractObject

convenience helper (quick contract lookup)



357
358
359
360
361
362
363
364
# File 'lib/universum/universum.rb', line 357

def contract   # convenience helper (quick contract lookup)
  rec = receipt
  if rec
    rec.contract
  else
    nil
  end
end

#log_strObject



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/universum/universum.rb', line 321

def log_str
  ## for debug add transaction (tx) args (e.g. from, value, etc.)
  tx_args_str = ""
  tx_args_str << "from: #{@from} ##{@nonce}"
  tx_args_str << ", value: #{@value}"  if @value > 0

  if @to == '0x0000'  ## special case - contract creation transaction
    klass      = @data[0]       ## contract class - todo/fix: check if data[] is a contract class!!!
    call_args  = @data[1..-1]   ## arguments

    ## convert all args to string (with inspect) for debugging
    ##   check if pretty_inspect adds trailing newline? why? why not? possible?
    call_args_str = call_args.reduce( [] ) { |ary,arg| ary; ary << arg.inspect }.join( ', ' )

    "#{tx_args_str} => to: #{@to} create contract #{klass.name}.new( #{call_args_str} )"
  else
    if @data.empty?  ## assume receive (default method) for now if data empty (no method specified)
      "#{tx_args_str} => to: #{@to} call default fallback"
    else
      m         = @data[0]       ## method name / signature
      call_args = @data[1..-1]   ## arguments

      ## convert all args to string (with inspect) for debugging
      ##   check if pretty_inspect adds trailing newline? why? why not? possible?
      call_args_str = call_args.reduce( [] ) { |ary,arg| ary; ary << arg.inspect }.join( ', ' )

      "#{tx_args_str} => to: #{@to} call #{m}( #{call_args_str} )"
    end
  end
end

#receiptObject



353
354
355
# File 'lib/universum/universum.rb', line 353

def receipt
  Receipt.find( self )
end