Class: Contract

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

Constant Summary collapse

@@directory =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Address

#_add, #_sub, #address, #balance, #send, #transfer, zero

Class Method Details

.[](key) ⇒ Object



33
# File 'lib/universum/contract.rb', line 33

def self.[]( key ) find_by_address( key ); end

.allObject



36
# File 'lib/universum/contract.rb', line 36

def self.all() @@directory.values; end

.at(key) ⇒ Object

another “classic” alias for find_by_address



32
# File 'lib/universum/contract.rb', line 32

def self.at( key) find_by_address( key ); end

.find(key) ⇒ Object

make find_by_address the default finder



31
# File 'lib/universum/contract.rb', line 31

def self.find( key ) find_by_address( key ); end

.find_by_address(key) ⇒ Object



26
27
28
29
30
# File 'lib/universum/contract.rb', line 26

def self.find_by_address( key )
   ## clean key (allow "embedded" class name e.g 0x4de2ee8 (SatoshiDice))
   key = key.gsub(/\(.+\)/, '' ).strip
   @@directory[ key ];
end

.load(path) ⇒ Object

load “class-less” contract

e.g.   SathoshiDice = Contract.load( './sathoshi_dice' )


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/universum/contract.rb', line 9

def self.load( path )
  extname = File.extname( path )   #=> ".rb"
  if extname == '.rb'
    ## do nothing
  else
    ## "convenience" helper - (auto-)add .rb extension
    path = "#{path}.rb"
  end

  code = File.open( path, 'r:bom|utf-8' ) { |f| f.read }
  klass = Class.new( Contract )
  klass.class_eval( code )   ## note: use class_eval (NOT instance_eval)
  klass
end

.payable(*args) ⇒ Object

function sig(nature) macro for types (dummy for now) e.g. use like

payable :process
payable :initialize
payable :bet, Integer
payable :lend_money, Address => Bool   ## returns Bool


50
# File 'lib/universum/contract.rb', line 50

def self.payable( *args ); end

.store(key, o) ⇒ Object

store (add) new contract object (o) to hash / directory



35
# File 'lib/universum/contract.rb', line 35

def self.store( key, o ) @@directory.store( key, o ); end

Instance Method Details

#assert(condition) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/universum/contract.rb', line 60

def assert( condition )
  if condition == true
    ## do nothing
  else
    raise 'Contract Assertion Failed; Contract Halted (Stopped)'
  end
end

#blockObject



72
# File 'lib/universum/contract.rb', line 72

def block()      Universum.block;        end

#blockhash(number) ⇒ Object



73
74
75
76
# File 'lib/universum/contract.rb', line 73

def blockhash( number )
  ## todo/fix: only allow going back 255 blocks; check if number is in range!!!
  Universum.blockhash( number )
end

#log(event) ⇒ Object



70
# File 'lib/universum/contract.rb', line 70

def log( event ) Universum.log( event ); end

#msgObject



71
# File 'lib/universum/contract.rb', line 71

def msg()        Universum.msg;          end

#receiveObject

todo/double check: auto-add payable default fallback - why? why not?



56
57
# File 'lib/universum/contract.rb', line 56

def receive    ## @payable default fallback - use different name - why? why not? (e.g. handle/process/etc.)
end

#thisObject

returns current contract



69
# File 'lib/universum/contract.rb', line 69

def this()       Universum.this;         end