-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrowdfund.repl
240 lines (194 loc) · 6.12 KB
/
crowdfund.repl
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
(begin-tx "Load modules")
(env-data {
'admin-keyset: { "keys": ["admin"], "pred": "keys-all" },
'upgrade: false,
'ns: "free"
})
(env-sigs [
{ 'key: 'admin
,'caps: []
}])
(define-namespace "free" (read-keyset "admin-keyset") (read-keyset "admin-keyset"))
(load "./root/fungible-v2.pact")
(load "./root/fungible-xchain-v1.pact")
(load "./root/coin.pact")
(load "crowdfund.pact")
(commit-tx)
(begin-tx "Create a project")
(env-chain-data {"block-time": (time "2023-01-01T15:00:00Z")})
(use free.crowdfund)
(env-data {
"owner-guard": {"keys": ["owner"], "pred": "keys-all"}
,"jane-guard": {"keys": ["jane"], "pred": "keys-all"}
,"dummy-guard": {"keys": ["dummy"], "pred": "keys-all"}
})
(test-capability (coin.COINBASE))
(coin.coinbase "owner-account" (read-keyset "owner-guard") 500.0)
(coin.coinbase "jane-account" (read-keyset "jane-guard") 500.0)
(coin.coinbase "dummy-account" (read-keyset "dummy-guard") 500.0)
(expect-failure
"Start Date should be before end date"
(create-project
"project1"
"My first project"
coin
350.0
200.0
(time "2023-01-01T18:00:00Z")
(time "2021-01-03T19:00:00Z")
"owner-account"
(read-keyset "owner-guard")
))
(expect-failure
"Start Date shouldn't be in the past"
(create-project
"project1"
"My first project"
coin
350.0
200.0
(time "2023-01-01T14:00:00Z")
(time "2023-01-03T19:00:00Z")
"owner-account"
(read-keyset "owner-guard")
))
(expect-failure
"Hardcap should be higher than softcap"
(create-project
"project1"
"My first project"
coin
350.0
500.0
(time "2023-01-01T14:00:00Z")
(time "2023-01-03T19:00:00Z")
"owner-account"
(read-keyset "owner-guard")
))
(expect "Create project succeeds"
"Write succeeded"
(create-project
"project1"
"My first project"
coin
350.0
200.0
(time "2023-01-01T18:00:00Z")
(time "2023-01-03T19:00:00Z")
"owner-account"
(read-keyset "owner-guard")
))
(expect "read project should return all projects in table"
(format "{}" [[{"endDate": "2023-01-03T19:00:00Z","hardCap": 350.0,"projectId": "project1","raised": 0.0,"softCap": 200.0,"startDate": "2023-01-01T18:00:00Z","status": 0,"title": "My first project","token": coin}]])
(format "{}" [(read-projects)]))
(commit-tx)
(begin-tx "Fund a project")
(use free.crowdfund)
(expect-failure "Funding will fail since project hasn't started yet"
"PROJECT HAS NOT YET STARTED"
(fund-project "project1" "jane-account" 250.0))
;;Shift time so the project has started
(env-chain-data {"block-time": (time "2023-01-02T18:00:00Z")})
(env-sigs [{
'key: "jane",
'caps: [
(coin.TRANSFER "jane-account" "c:ogtUEBCuhLHaqKSyTm7bhmqqfy4gXjEE9PHFNxhEoNQ" 250.0)
(ACCT_GUARD "jane-account" "project1")]}])
(expect "Fund project succeeds"
"Write succeeded"
(fund-project "project1" "jane-account" 250.0))
(expect "Funded amount reflects investment"
{"fundedAmount": 250.0,"projectId": "project1"}
(fetch-funded-amount "project1" "jane-account")
)
(expect "Remaining balance is 250.0"
250.0
(coin.get-balance "jane-account"))
(expect "Raised amount is reflected in project-table"
250.0
(at "raised" (read-project "project1")))
; (expect "Number of investments is reflected in project-table"
; 1
; (at "investments" (read-project "project1")))
(commit-tx)
(begin-tx "Change project status")
(use free.crowdfund)
(env-sigs [
{
'key: "owner",
'caps: []}])
(expect-failure "Can't cancel since project has already started"
"PROJECT HAS ALREADY STARTED, CANNOT CANCEL"
(cancel-project 'project1))
(expect-failure "Can't fail project if softcap has been reached"
"PROJECT HAS NOT ENDED"
(fail-project 'project1))
(expect-failure "Can't succeed if project is still open and hardcap not met"
"PROJECT HAS NOT ENDED OR HARDCAP NOT MET"
(succeed-project 'project1))
(rollback-tx)
(begin-tx "Funding not possible if hardCap is met")
(use free.crowdfund)
(env-sigs [{
'key: "jane",
'caps: [
(coin.TRANSFER "jane-account" "c:ogtUEBCuhLHaqKSyTm7bhmqqfy4gXjEE9PHFNxhEoNQ" 100.0)
(ACCT_GUARD "jane-account" "project1")]}
])
;;
(expect "Try to fund 200 which exceeds hardCap, so only 100 is funded"
"Write succeeded"
(fund-project "project1" "jane-account" 200.0)
)
(expect "Raised amount is reflected in project-table"
350.0
(at "raised" (read-project "project1")))
; (expect "Number of investments is reflected in project-table"
; 2
; (at "investments" (read-project "project1")))
(expect-failure "Funding while hardcap is reached should fail"
"HARDCAP REACHED"
(fund-project "project1" "jane-account" 150.0))
(rollback-tx)
(begin-tx "Refund investment")
(use free.crowdfund)
(env-sigs [{
'key: "jane",
'caps: [
(ACCT_GUARD "jane-account" "project1")
(REFUND "project1" "jane-account")
]}
])
(expect "Jane has a balance of 205.0"
250.0
(coin.get-balance "jane-account"))
(expect "Jane's investment is refunded"
"Write succeeded"
(rollback-fund-project 'project1 'jane-account))
(expect "500.0" 500.0 (coin.get-balance 'jane-account))
(expect "Raised amount is reflected in project-table"
0.0
(at "raised" (read-project "project1")))
(expect-failure "Try to refund again"
"NO ACTIVE FUNDS"
(rollback-fund-project 'project1 'jane-account))
(rollback-tx)
(begin-tx "Succeed project")
(use free.crowdfund)
(env-sigs [
{
'key: "owner",
'caps: []
}])
;;Shift time so the project has ended
(env-chain-data {"block-time": (time "2023-01-04T15:00:00Z")})
(expect "Project succeeds because hardcap is met"
"Write succeeded"
(succeed-project 'project1))
(expect "The owner's account has received the invested amount 0f 250"
750.0
(coin.get-balance "owner-account"))
(expect "The project status is SUCCEEDED"
SUCCEEDED
(at "status" (read-project "project1")))
(commit-tx)