class UN

note: Universum is a class (and NOT a module for now)

Constants

MAJOR
MINOR
PATCH
VERSION

Public Class Methods

accounts() click to toggle source
# File lib/universum/universum.rb, line 104
def self.accounts()  Account.all; end
banner() click to toggle source
block() click to toggle source
# File lib/universum/universum.rb, line 127
def self.block
  @@block ||= Block.new
end
block=( value ) click to toggle source
# File lib/universum/universum.rb, line 131
def self.block=( value )
  if value.is_a? Hash
    kwargs = value
    @@block = Block.new( kwargs )
  else   ## assume Block class/type
    @@block = value
  end
end
blockhash( number ) click to toggle source
# File lib/universum/universum.rb, line 122
def self.blockhash( number )
  ## for now return "dummy" blockhash
  sha256( "blockhash #{number}" )
end
contracts() click to toggle source
# File lib/universum/universum.rb, line 105
def self.contracts() Contract.all; end
handlers() click to toggle source
# File lib/universum/universum.rb, line 150
def self.handlers    ## use listeners/observers/subscribers/... - why? why not?
  @@handlers ||= []
end
log( event ) click to toggle source
# File lib/universum/universum.rb, line 154
def self.log( event )
  handlers.each { |h| h.call( event ) }
end
msg() click to toggle source
# File lib/universum/universum.rb, line 108
def self.msg
  @@msg ||= Msg.new
end
msg=( value ) click to toggle source
# File lib/universum/universum.rb, line 112
def self.msg=( value )
  if value.is_a? Hash
    kwargs = value
    @@msg = Msg.new( kwargs )
  else   ## assume Msg class/type
    @@msg = value
  end
end
root() click to toggle source
# File lib/universum/version.rb, line 22
def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end
send_transaction( from:, to: '0x0000', value: 0, data: [] ) click to toggle source

convenience helpers

# File lib/universum/universum.rb, line 30
def self.send_transaction( from:, to: '0x0000', value: 0, data: [] )
  counter = @@counter ||= 0    ## total tx counter for debugging (start with 0)
  @@counter += 1

  ## todo/fix:
  ##   allow/add auto-create account for convenience (and easy testing)!!!

  ## note: always lookup (use) account for now
  if from.is_a?( Account )
    account = from
  else
    account = Account.at( from )
  end

  ## setup contract msg context
  self.msg = { sender: account.address.hex, value: value }


  ## allow shortcut for Class (without ctor arguments) - no need to wrap in array
  data = [data]   if data.is_a? Class

  tx = Transaction.new( from: account, to: to, value: value, data: data )


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

    puts "** tx ##{counter} (block ##{block.number}): #{tx.log_str}"
    contract = klass.create( *args )   ## note: balance and this (and msg.send/transfer) NOT available/possible !!!!

    if value > 0
      ## move value to msg (todo/fix: restore if exception)
      account._sub( value )  # (sub)tract / debit from the sender (account)
      contract._add( value )        # add / credit to the recipient
    end

    puts " new #{contract.class.name} contract adddress: #{contract.address.hex.inspect}"
    ## add new contract to (lookup) directory
    Contract.store( contract.address.hex, contract )

    ## issue (mined) transaction receipt
    receipt = Receipt.new( tx: tx,
                           block: block,
                           contract: contract )
  else
     if value > 0
       ## move value to msg (todo/fix: restore if exception)
       account._sub( value )  # (sub)tract / debit from the sender (account)
       to._add( value )       # add / credit to the recipient
     end

     self.this = to    ## assumes for now that to is always a contract (and NOT an account)!!!

     data = [:receive]   if data.empty?  ## assume receive (default method) for now if data empty (no method specified)

     m    = data[0]       ## method name / signature
     args = data[1..-1]   ## arguments

     puts "** tx ##{counter} (block ##{block.number}): #{tx.log_str}"

     to.__send__( m, *args )    ## note: use __send__ to avoid clash with send( value ) for sending payments!!!

     ## issue (mined) transaction receipt
     receipt = Receipt.new( tx: tx,
                            block: block )
  end

  Receipt.store( receipt )
  tx   # return transaction
end
this() click to toggle source
# File lib/universum/universum.rb, line 140
def self.this    ## returns current contract
  @@this
end
this=(value) click to toggle source
# File lib/universum/universum.rb, line 144
def self.this=(value)
  ## todo/fix: check that value is a contract
  @@this = value
end
version() click to toggle source
# File lib/universum/version.rb, line 14
def self.version
  VERSION
end