-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyramid.cll
112 lines (86 loc) · 3.6 KB
/
pyramid.cll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
#define ORIGINATOR_ADDRESS 1000
#define TOTAL_MEMBERS 1001
#define MEMBER_DATABASE_START 1100
#define MEMBER_COLUMNS 3
#define INDEX_REFERENCE 0
#define INDEX_PARENTS 1
#define INDEX_BALANCE 2
arraybase = MEMBER_DATABASE_START
arraylen = MEMBER_COLUMNS
if tx.datan:
// Initialize the pyramid with the founder.
if tx.data[0] == 0:
// Only initialize once.
if contract.storage[ORIGINATOR_ADDRESS] > 0:
stop
// The sender is now the top of the pyramid
contract.storage[ORIGINATOR_ADDRESS] = tx.sender
contract.storage[TOTAL_MEMBERS] = 1
// Set them as the first member number, as their own reference.
contract.storage[arraybase+INDEX_REFERENCE] = tx.sender
contract.storage[arraybase+INDEX_PARENTS] = 1
contract.storage[arraybase+INDEX_BALANCE] = tx.value
contract.storage[tx.sender] = 1
// Join the pyramid
if tx.data[0] == 1:
// Check if pyramid is initialized
if contract.storage[ORIGINATOR_ADDRESS] < 1:
stop
// Check if the reference exists
if contract.storage[tx.data[1]] < 1:
stop
// Make a new member number.
membernumber = contract.storage[TOTAL_MEMBERS] + 1
contract.storage[TOTAL_MEMBERS] = membernumber
// Remember me as my new number
contract.storage[tx.sender] = membernumber
// Get my contracts reference number
refnum = contract.storage[tx.data[1]]
// Look up references point in the array.
refpoint = (refnum - 1) * arraylen + arraybase
parents = contract.storage[refpoint+INDEX_PARENTS]
// Setup my information in the db.
mystart = (membernumber - 1) * arraylen + arraybase
contract.storage[mystart] = tx.data[1]
contract.storage[mystart+INDEX_PARENTS] = parents + 1
// Figure out the redistribution
redist = tx.value / parents
numerator = tx.value
divisor = parents
remainder = numerator % divisor
redist = numerator / divisor
index = 0
// Loop through parental tree.
while index < parents:
index = index + 1
refbalance = contract.storage[refpoint+INDEX_BALANCE]
contract.storage[refpoint+INDEX_BALANCE] = refbalance + redist
// Then the remainder goes to the head of the pyramid, obviously unfair (as if the rest isn't)
masterbalance = contract.storage[arraybase+INDEX_BALANCE]
contract.storage[arraybase+INDEX_BALANCE] = masterbalance + remainder
// Debug values.
// contract.storage[2000] = membernumber
// contract.storage[2001] = refpoint
// contract.storage[2002] = mystart
// contract.storage[2003] = redist
// contract.storage[2004] = parents
// Withdraw mode.
if tx.data[0] == 2:
// Setup what we know about this person.
// See if this person exists.
membernumber = contract.storage[tx.sender]
if membernumber < 1:
stop
// Find them in the database.
mystart = (membernumber - 1) * arraylen + arraybase
// See if this person's balance is enough.
balance = contract.storage[mystart+INDEX_BALANCE]
// The originator can always withdraw.
if membernumber > 1:
// Stop if their balance is too low.
if tx.data[1] > balance:
stop
// Calculate their new balance.
contract.storage[mystart+INDEX_BALANCE] = balance - tx.data[1]
// Now we can transact with them.
mktx(tx.sender,tx.data[1],0,0)