-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorp.se
More file actions
67 lines (59 loc) · 1.87 KB
/
corp.se
File metadata and controls
67 lines (59 loc) · 1.87 KB
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
# Stored variables
TRANSFER_MSG = 1
BALANCE_MSG = 2
DIVIDEND_MSG = 3
SHARES = 100000
ADDRESSES_IDX = 2^160
NUM_SHAREHOLDERS_IDX = 1
# 0 : number of shareholders
# 1 : stock issuing address
# 2 : addresses and shares
# 2^160 + 3x : hash
# 2^160 + 3x + 1: shares
# 2^160 + 3x + 2: address
init:
contract.storage[NUM_SHAREHOLDERS_IDX] = 0
contract.storage[ADDRESSES_IDX] = msg.sender
contract.storage[msg.sender] = SHARES
# Message Types
# 1: Transfer Stock
# 2: Receive payment
code:
msg_type = msg.data[0]
# Transferring stock
# [1, receiver_address, quantity]
if msg_type == TRANSFER_MSG:
msg.sender
sender_holdings = contract.storage[msg.sender]
to = msg.data[1]
transfer_quantity = msg.data[2]
if sender_holdings >= transfer_quantity:
# If sender doesn't exist yet, we add their address
if contract.storage[to] == 0:
contract.storage[NUM_SHAREHOLDERS_IDX] = contract.storage[NUM_SHAREHOLDERS_IDX] + 1
contract.storage[ADDRESSES_IDX + contract.storage[NUM_SHAREHOLDERS_IDX]] = to
contract.storage[msg.sender] = sender_holdings - transfer_quantity
contract.storage[to] = contract.storage[to] + transfer_quantity
return(contract.storage[msg.sender])
else
return(0)
# Checking stock balance
# [2]
elif msg_type == BALANCE_MSG:
return(contract.storage[msg.sender])
# Paying dividends
# [3]
elif msg_type == DIVIDEND_MSG:
if msg.sender == contract.storage[ADDRESSES_IDX]:
total_dispursement = contract.balance
num_addresses = contract.storage[NUM_SHAREHOLDERS_IDX]
i = 0
while i <= num_addresses:
add = contract.storage[ADDRESSES_IDX + i]
add_shares = contract.storage[add]
dispursement = (total_dispursement*add_shares/SHARES)
send(add, dispursement)
i += 1
return(total_dispursement)
else:
return(msg.value)