-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rsh
247 lines (235 loc) · 6.29 KB
/
index.rsh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'reach 0.1'
const [isOutcome, NOT_PASSED, PASSED] = makeEnum(2)
const state = Bytes(20)
const checkStatus = (upVotes, downVotes) => {
if (downVotes > upVotes) {
return NOT_PASSED
} else if (upVotes == downVotes) {
return NOT_PASSED
} else {
return PASSED
}
}
assert(checkStatus(100, 100) == NOT_PASSED)
assert(checkStatus(50, 100) == NOT_PASSED)
assert(checkStatus(100, 50) == PASSED)
forall(UInt, (upVotes) =>
forall(UInt, (downVotes) =>
assert(isOutcome(checkStatus(upVotes, downVotes)))
)
)
export const main = Reach.App(() => {
setOptions({ untrustworthyMaps: true })
const Deployer = Participant('Deployer', {
getProposal: Object({
id: UInt,
title: Bytes(25),
link: Bytes(150),
description: Bytes(180),
owner: Address,
deadline: UInt,
isProposal: Bool,
}),
})
const objectRep = Struct([
['id', UInt],
['title', Bytes(25)],
['link', Bytes(150)],
['description', Bytes(180)],
['owner', Address],
['contractInfo', Contract],
['blockCreated', UInt],
])
const claimResponse = Struct([
['didRefund', Bool],
['balance', UInt],
])
const Voters = API('Voters', {
upvote: Fun([], UInt),
downvote: Fun([], UInt),
contribute: Fun([UInt], UInt),
claimRefund: Fun([], claimResponse),
created: Fun([objectRep], Null),
upvoted: Fun([UInt, UInt], Null),
downvoted: Fun([UInt, UInt], Null),
contributed: Fun([UInt, UInt], Null),
timedOut: Fun([UInt, UInt], Null),
projectDown: Fun([UInt], Null),
})
const Proposals = Events({
create: [UInt, Bytes(25), Bytes(150), Bytes(180), Address, Contract, UInt],
that: [state, UInt, UInt],
log: [state, UInt],
created: [UInt, Bytes(25), Bytes(150), Bytes(180), Address, Contract, UInt],
})
init()
Deployer.only(() => {
const { title, link, description, owner, id, isProposal, deadline } =
declassify(interact.getProposal)
})
Deployer.publish(description, isProposal)
if (isProposal) {
commit()
Deployer.publish(title, link, owner, id, deadline)
Proposals.created(
id,
title,
link,
description,
owner,
getContract(),
thisConsensusTime()
)
const [timeRemaining, keepGoing] = makeDeadline(deadline)
const contributors = new Map(Address, Address)
const amtContributed = new Map(Address, UInt)
const contributorsSet = new Set()
const [upvote, downvote, amtTotal] = parallelReduce([0, 0, balance()])
.invariant(balance() == amtTotal)
.while(keepGoing())
.api(Voters.upvote, (notify) => {
notify(upvote + 1)
return [upvote + 1, downvote, amtTotal]
})
.api(Voters.downvote, (notify) => {
notify(downvote + 1)
return [upvote, downvote + 1, amtTotal]
})
.api_(Voters.contribute, (amt) => {
check(amt > 0, 'Contribution too small')
const payment = amt
return [
payment,
(notify) => {
notify(balance())
if (contributorsSet.member(this)) {
const fromMapAmt = (m) =>
fromMaybe(
m,
() => 0,
(x) => x
)
amtContributed[this] = fromMapAmt(amtContributed[this]) + amt
} else {
contributors[this] = this
amtContributed[this] = amt
contributorsSet.insert(this)
}
return [upvote, downvote, amtTotal + amt]
},
]
})
.timeout(timeRemaining(), () => {
Deployer.publish()
if (checkStatus(upvote, downvote) == PASSED) {
Proposals.log(state.pad('passed'), id)
transfer(balance()).to(owner)
} else {
if (balance() > 0) {
const fromMapAdd = (m) =>
fromMaybe(
m,
() => Deployer,
(x) => x
)
const fromMapAmt = (m) =>
fromMaybe(
m,
() => 0,
(x) => x
)
Proposals.log(state.pad('failed'), id)
const currentBalance = parallelReduce(balance())
.invariant(balance() == currentBalance)
.while(currentBalance > 0)
.api(Voters.claimRefund, (notify) => {
const amountTransferable = fromMapAmt(amtContributed[this])
if (
balance() >= amountTransferable &&
contributorsSet.member(this)
) {
transfer(amountTransferable).to(
fromMapAdd(contributors[this])
)
contributorsSet.remove(this)
Proposals.log(state.pad('refundPassed'), id)
const currentBal = currentBalance - amountTransferable
const response = claimResponse.fromObject({
didRefund: true,
balance: currentBal,
})
notify(response)
return currentBal
} else {
Proposals.log(state.pad('refundFailed'), id)
const response = claimResponse.fromObject({
didRefund: false,
balance: currentBalance,
})
notify(response)
return currentBalance
}
})
}
Proposals.log(state.pad('down'), id)
}
return [upvote, downvote, balance()]
})
transfer(balance()).to(Deployer)
} else {
const keepGoing = parallelReduce(true)
.invariant(balance() == 0)
.while(keepGoing)
.api(Voters.created, (obj, notify) => {
notify(null)
const proposalStruct = objectRep.fromObject(obj)
const proposalObject = objectRep.toObject(proposalStruct)
Proposals.create(
proposalObject.id,
proposalObject.title,
proposalObject.link,
proposalObject.description,
proposalObject.owner,
proposalObject.contractInfo,
proposalObject.blockCreated
)
return keepGoing
})
.api(Voters.upvoted, (fNum, sNum, notify) => {
notify(null)
const num1 = fNum
const num2 = sNum
Proposals.that(state.pad('upvoted'), num1, num2)
return keepGoing
})
.api(Voters.downvoted, (fNum, sNum, notify) => {
notify(null)
const num1 = fNum
const num2 = sNum
Proposals.that(state.pad('downvoted'), num1, num2)
return keepGoing
})
.api(Voters.contributed, (fNum, sNum, notify) => {
notify(null)
const num1 = fNum
const num2 = sNum
Proposals.that(state.pad('contributed'), num1, num2)
return keepGoing
})
.api(Voters.timedOut, (fNum, sNum, notify) => {
notify(null)
const num1 = fNum
const num2 = sNum
Proposals.that(state.pad('timedOut'), num1, num2)
return keepGoing
})
.api(Voters.projectDown, (fNum, notify) => {
notify(null)
const num1 = fNum
Proposals.that(state.pad('projectDown'), num1, 0)
return keepGoing
})
}
commit()
exit()
})