universum 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +2 -0
- data/README.md +7 -4
- data/Rakefile +1 -1
- data/lib/universum.rb +2 -0
- data/lib/universum/contract.rb +1 -0
- data/lib/universum/enum.rb +9 -0
- data/lib/universum/units_money.rb +2 -3
- data/lib/universum/version.rb +1 -1
- data/test/contracts/ballot.rb +66 -0
- data/test/test_ballot.rb +46 -0
- data/test/test_units_money.rb +0 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a83f038e0a43351f492fc32ea68042559a82782
|
4
|
+
data.tar.gz: 604e2ed189008bc319d88b2cbb0d1cafbe8bffc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e27c2c0b46fd2e82bbfa1994656cdc174499efcb23b4f0cbb8304ef497c421ca4c32ab1b778003f0e24470de430ab9126e01dbf0d95c8b9d265b9208a2c4ad1
|
7
|
+
data.tar.gz: c0c4e8c82b5c4cf32a9d6414b7246a66f0bd675820cdb9c27b6376bfcaba37c028086d43955e61b468f865db0c563eee93cac86f87e7b70f691936be983cc9f1
|
data/Manifest.txt
CHANGED
@@ -18,9 +18,11 @@ lib/universum/units_money.rb
|
|
18
18
|
lib/universum/units_time.rb
|
19
19
|
lib/universum/universum.rb
|
20
20
|
lib/universum/version.rb
|
21
|
+
test/contracts/ballot.rb
|
21
22
|
test/contracts/greeter.rb
|
22
23
|
test/contracts/mytoken.rb
|
23
24
|
test/helper.rb
|
25
|
+
test/test_ballot.rb
|
24
26
|
test/test_enum.rb
|
25
27
|
test/test_event.rb
|
26
28
|
test/test_greeter.rb
|
data/README.md
CHANGED
@@ -82,8 +82,6 @@ And let's run the greeter with Universum (Uni):
|
|
82
82
|
``` ruby
|
83
83
|
require 'universum'
|
84
84
|
|
85
|
-
Account['0x1111'] ## setup a test account
|
86
|
-
|
87
85
|
## create contract (english version)
|
88
86
|
tx = Uni.send_transaction( from: '0x1111', data: ['./greeter', 'Hello World!'] )
|
89
87
|
greeter = tx.receipt.contract
|
@@ -149,7 +147,7 @@ def initialize( initial_supply )
|
|
149
147
|
end
|
150
148
|
|
151
149
|
def transfer( to, value )
|
152
|
-
assert @balance_of[ msg.sender ] >= value
|
150
|
+
assert @balance_of[ msg.sender ] >= value
|
153
151
|
assert @balance_of[ to ] + value >= @balance_of[ to ]
|
154
152
|
|
155
153
|
@balance_of[ msg.sender ] -= value
|
@@ -171,7 +169,10 @@ See the [`/universum-contracts`](https://github.com/s6ruby/universum-contracts)
|
|
171
169
|
|
172
170
|
## More Documentation / Articles / Contracts
|
173
171
|
|
174
|
-
[Programming Crypto Blockchain Contracts Step-by-Step Book / Guide](https://github.com/s6ruby/programming-cryptocontracts) - Let's
|
172
|
+
[Programming Crypto Blockchain Contracts Step-by-Step Book / Guide](https://github.com/s6ruby/programming-cryptocontracts) - Let's start with ponzi & pyramid schemes. Run your own lotteries, gambling casinos and more on the blockchain world computer...
|
173
|
+
|
174
|
+
[Safe Data Structures (Array, Hash, Struct)](https://github.com/s6ruby/safestruct) - Say goodbye to null / nil (and maybe) and the Billion-Dollar mistake. Say hello to zero and the Billon-Dollar fix.
|
175
|
+
|
175
176
|
|
176
177
|
|
177
178
|
|
@@ -186,6 +187,8 @@ $ gem install universum
|
|
186
187
|
|
187
188
|
## License
|
188
189
|
|
190
|
+
![](https://publicdomainworks.github.io/buttons/zero88x31.png)
|
191
|
+
|
189
192
|
The `universum` scripts are dedicated to the public domain.
|
190
193
|
Use it as you please with no restrictions whatsoever.
|
191
194
|
|
data/Rakefile
CHANGED
data/lib/universum.rb
CHANGED
data/lib/universum/contract.rb
CHANGED
data/lib/universum/enum.rb
CHANGED
@@ -52,6 +52,15 @@ class Enum
|
|
52
52
|
###################
|
53
53
|
## meta-programming "macro" - build class (on the fly)
|
54
54
|
def self.build_class( *keys )
|
55
|
+
|
56
|
+
## check if all keys are symbols and follow the ruby id(entifier) naming rules
|
57
|
+
keys.each do |key|
|
58
|
+
if key.is_a?( Symbol ) && key =~ /\A[a-z][a-zA-Z0-9_]*\z/
|
59
|
+
else
|
60
|
+
raise ArgumentError.new( "[Enum] arguments to Enum.new must be all symbols following the ruby id naming rules; >#{key}< failed" )
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
55
64
|
klass = Class.new( Enum )
|
56
65
|
|
57
66
|
## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
|
@@ -24,7 +24,7 @@ class Integer
|
|
24
24
|
#
|
25
25
|
# wei 1 wei | 1
|
26
26
|
# kwei (babbage) 1e3 wei | 1_000
|
27
|
-
# mwei (lovelace
|
27
|
+
# mwei (lovelace) 1e6 wei | 1_000_000
|
28
28
|
# gwei (shannon) 1e9 wei | 1_000_000_000
|
29
29
|
# microether (szabo) 1e12 wei | 1_000_000_000_000
|
30
30
|
# milliether (finney) 1e15 wei | 1_000_000_000_000_000
|
@@ -32,7 +32,7 @@ class Integer
|
|
32
32
|
#
|
33
33
|
# Names in Honor:
|
34
34
|
# wei => Wei Dai
|
35
|
-
# lovelace
|
35
|
+
# lovelace => Ada Lovelace (1815-1852)
|
36
36
|
# babbage => Charles Babbage (1791-1871)
|
37
37
|
# shannon => Claude Shannon (1916-2001)
|
38
38
|
# szabo => Nick Szabo
|
@@ -50,7 +50,6 @@ class Integer
|
|
50
50
|
## alias - use alias or alias_method - why? why not?
|
51
51
|
def babbage() kwei; end
|
52
52
|
def lovelace() mwei; end
|
53
|
-
def ada() mwei; end ## todo/check: in use, really? keep just lovelave- why? why not?
|
54
53
|
def shannon() gwei; end
|
55
54
|
def szabo() microether; end
|
56
55
|
def finney() milliether; end
|
data/lib/universum/version.rb
CHANGED
@@ -0,0 +1,66 @@
|
|
1
|
+
#########################
|
2
|
+
# Ballot Contract
|
3
|
+
|
4
|
+
Voter = Struct.new( weight: 0,
|
5
|
+
voted: false,
|
6
|
+
vote: 0,
|
7
|
+
delegate: Address(0))
|
8
|
+
|
9
|
+
Proposal = Struct.new( vote_count: 0 )
|
10
|
+
|
11
|
+
## Create a new ballot with $(num_proposals) different proposals.
|
12
|
+
def initialize( num_proposals )
|
13
|
+
@chairperson = msg.sender
|
14
|
+
@voters = Mapping.of( Address => Voter )
|
15
|
+
@proposals = Array.of( Proposal, num_proposals )
|
16
|
+
|
17
|
+
@voters[@chairperson].weight = 1
|
18
|
+
end
|
19
|
+
|
20
|
+
## Give $(to_voter) the right to vote on this ballot.
|
21
|
+
## May only be called by $(chairperson).
|
22
|
+
def give_right_to_vote( to_voter )
|
23
|
+
assert msg.sender == @chairperson && @voters[to_voter].voted? == false
|
24
|
+
@voters[to_voter].weight = 1
|
25
|
+
end
|
26
|
+
|
27
|
+
## Delegate your vote to the voter $(to).
|
28
|
+
def delegate( to )
|
29
|
+
sender = @voters[msg.sender] # assigns reference
|
30
|
+
assert sender.voted? == false
|
31
|
+
|
32
|
+
while @voters[to].delegate != Address(0) && @voters[to].delegate != msg.sender do
|
33
|
+
to = @voters[to].delegate
|
34
|
+
end
|
35
|
+
assert to != msg.sender
|
36
|
+
|
37
|
+
sender.voted = true
|
38
|
+
sender.delegate = to
|
39
|
+
delegate_to = @voters[to]
|
40
|
+
if delegate_to.voted
|
41
|
+
@proposals[delegate_to.vote].vote_count += sender.weight
|
42
|
+
else
|
43
|
+
delegate_to.weight += sender.weight
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
## Give a single vote to proposal $(to_proposal).
|
48
|
+
def vote( to_proposal )
|
49
|
+
sender = @voters[msg.sender]
|
50
|
+
assert sender.voted? == false && to_proposal < @proposals.length
|
51
|
+
sender.voted = true
|
52
|
+
sender.vote = to_proposal
|
53
|
+
@proposals[to_proposal].vote_count += sender.weight
|
54
|
+
end
|
55
|
+
|
56
|
+
def winning_proposal
|
57
|
+
winning_vote_count = 0
|
58
|
+
winning_proposal = 0
|
59
|
+
@proposals.each_with_index do |proposal,i|
|
60
|
+
if proposal.vote_count > winning_vote_count
|
61
|
+
winning_vote_count = proposal.vote_count
|
62
|
+
winning_proposal = i
|
63
|
+
end
|
64
|
+
end
|
65
|
+
winning_proposal
|
66
|
+
end
|
data/test/test_ballot.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_ballot.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
class TestBallot < MiniTest::Test
|
13
|
+
|
14
|
+
Ballot = Contract.load( "#{Universum.root}/test/contracts/ballot" )
|
15
|
+
|
16
|
+
def test_uni
|
17
|
+
Account['0x1111'] ### todo/fix: auto-add account in from: if not present or missing!!!!
|
18
|
+
Account['0xaaaa']
|
19
|
+
Account['0xbbbb']
|
20
|
+
Account['0xcccc']
|
21
|
+
Account['0xdddd']
|
22
|
+
|
23
|
+
tx = Uni.send_transaction( from: '0x1111', data: [Ballot, 2] )
|
24
|
+
pp tx
|
25
|
+
pp tx.receipt
|
26
|
+
|
27
|
+
ballot = tx.receipt.contract
|
28
|
+
pp ballot
|
29
|
+
|
30
|
+
Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xaaaa'] )
|
31
|
+
Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xbbbb'] )
|
32
|
+
Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xcccc'] )
|
33
|
+
Uni.send_transaction( from: '0x1111', to: ballot, data: [:give_right_to_vote, '0xdddd'] )
|
34
|
+
pp ballot
|
35
|
+
|
36
|
+
Uni.send_transaction( from: '0xaaaa', to: ballot, data: [:vote, 1] )
|
37
|
+
Uni.send_transaction( from: '0xbbbb', to: ballot, data: [:delegate, '0xaaaa'] )
|
38
|
+
Uni.send_transaction( from: '0xcccc', to: ballot, data: [:vote, 0] )
|
39
|
+
Uni.send_transaction( from: '0xdddd', to: ballot, data: [:delegate, '0xbbbb'] )
|
40
|
+
pp ballot
|
41
|
+
|
42
|
+
## todo/fix: get return value from sent_transaction
|
43
|
+
assert_equal 1, ballot.winning_proposal
|
44
|
+
end
|
45
|
+
|
46
|
+
end # class TestBallot
|
data/test/test_units_money.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: universum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: safestruct
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rdoc
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -85,9 +85,11 @@ files:
|
|
85
85
|
- lib/universum/units_time.rb
|
86
86
|
- lib/universum/universum.rb
|
87
87
|
- lib/universum/version.rb
|
88
|
+
- test/contracts/ballot.rb
|
88
89
|
- test/contracts/greeter.rb
|
89
90
|
- test/contracts/mytoken.rb
|
90
91
|
- test/helper.rb
|
92
|
+
- test/test_ballot.rb
|
91
93
|
- test/test_enum.rb
|
92
94
|
- test/test_event.rb
|
93
95
|
- test/test_greeter.rb
|