forked from Reach-Insurance/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rsh
250 lines (214 loc) · 10.2 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
248
'reach 0.1';
export const main = Reach.App(() => {
//NOTE: The reach program starts in a "step" mode
const Insurer = Participant('Insurer', {
mandatoryEntryFee: UInt,
communityGroupName: Bytes(60),
contractIsRunning: Bool,
createInvoices: Fun([], Null),
moveMaturedPayments: Fun([], Null),
saveNewMemberDetails: Fun([Struct([
["fullName", Bytes(60)], ["phone", Bytes(20)],
["email", Bytes(60)], ["chosenInsurancePackage", UInt]
])], Null),
saveNewClaim: Fun([Struct([
["amountRequested", UInt], ["description", Bytes(200)]
])], Null),
notifyMembersAboutNewClaim: Fun([Struct([
["ownerAddr", Address],
["amountRequested", UInt],
["description", Bytes(600)],
["supportDocuments", Bytes(100)]
])], Null),
seeFeedback: Fun([], Null),
signout: Fun([], Null),
notifyFundedMember: Fun([Address], Null),
stopContract: Fun([], Null),
log: Fun(true, Null)
});
const CommunityMember = API('CommunityMember', {
registerMembership: Fun([Struct([
["fullName", Bytes(60)], ["phone", Bytes(20)],
["email", Bytes(60)],
["chosenInsurancePackage", UInt]
])], Bool),
payMonthlyFee: Fun([Struct([["who", Address], ["mfee", UInt]])], Bool),
createClaim: Fun([Struct([
["amountRequested", UInt], ["amountSet", UInt], ["accepted", Bool],
["approvalsCount", UInt], ["sumOfSetAmounts", UInt],
["insrPackageId", UInt], ["amountDue", UInt], ["matureBalance", UInt],
["fundLimit", UInt], ["description", Bytes(200)]
])], Bool),
respondToClaim: Fun([Struct([
["claimant", Address], ["accepted", Bool], ["setAmount", UInt]
])], Bool),
withDrawClaim: Fun([], Bool),
stopContract: Fun([], Bool)
});
//NOTE: we are still in "step" mode
setOptions({ untrustworthyMaps: true });
init();
//NOTE: we switched to "consensus step" by calling init() function above
Insurer.only(() => {
//NOTE: we switched to "local step" by calling .only() function. This is the body of .only()
const mandatoryEntryFee = declassify(interact.mandatoryEntryFee);
const contractIsRunning = declassify(interact.contractIsRunning);
interact.seeFeedback();
});
//NOTE: we switched to "concensus step" because this is the "continuation" of .only() function.
Insurer.publish(mandatoryEntryFee, contractIsRunning);
const invariantCondition = true;
commit();
Insurer.publish();
//keep a list of all community members' Addresses,
//more info about the community members is kept away (in the db).
const registeredMembers = new Set();
//claim shape
const insuranceClaims = new Map(Struct([
["amountRequested", UInt], ["amountSet", UInt], ["accepted", Bool],
["approvalsCount", UInt], ["sumOfSetAmounts", UInt]
]));
//details of members with open claims are kept close,
//other members are kept away from here (off-chain)
const claimOwners = new Map(Struct([
["insrPackageId", UInt],
["amountDue", UInt],
["matureBalance", UInt]
]));
const [
membersCount,
claimsCount
] = parallelReduce([1, 1])
.define(() => {
const readFromMap = (key) => {
return (objectInMapInstance) => {
return objectInMapInstance[key];
};
};
})
.invariant(invariantCondition)
.while(contractIsRunning)
.api(CommunityMember.registerMembership,
(_) => { const _ = true; },
(_) => mandatoryEntryFee,
((newMemberDetails, sendResponse) => {
const who = this;
sendResponse(true);
Insurer.interact.saveNewMemberDetails(newMemberDetails);
Insurer.interact.log("backend: done.");
transfer(mandatoryEntryFee).to(Insurer);
//add member's address to the list of addresses
registeredMembers.insert(who);
return [membersCount + 1, claimsCount];
})
).api(CommunityMember.payMonthlyFee,
(ob) => { const _ = true; },
(ob) => ob.mfee,
((ob, sendResponse) => {
Insurer.interact.log("backend: API.CommunityMember.payMonthlyFee invoked.");
sendResponse(true);
//deposit into the treasury account
transfer(ob.mfee).to(Insurer);
return [membersCount, claimsCount];
})
).api(CommunityMember.createClaim,
(_) => { const _ = true; },
(_) => 0,
((claimInfo, sendResponse) => {
const who = this;
Insurer.interact.saveNewClaim(Struct([["amountRequested", UInt], ["description", Bytes(200)]
]).fromObject({amountRequested: claimInfo.amountRequested, description: claimInfo.description }));
Insurer.interact.log("backend: API.CommunityMember.createClaim ...");
//add the details to the map of current claim owners
claimOwners[who] = Struct([["insrPackageId", UInt], ["amountDue", UInt], ["matureBalance", UInt]]).fromObject({
insrPackageId: claimInfo.insrPackageId,
amountDue: claimInfo.amountDue,
matureBalance: claimInfo.matureBalance
});
const fundLimitt = claimInfo.fundLimit;
const amt = claimInfo.amountRequested;
const claimAmount = amt >= fundLimitt ? amt : fundLimitt;
insuranceClaims[who] = Struct([["amountRequested", UInt], ["amountSet", UInt], ["accepted", Bool], ["approvalsCount", UInt], ["sumOfSetAmounts", UInt]]).fromObject({
amountRequested: amt,
amountSet: claimAmount,
accepted: false, approvalsCount: 0,
sumOfSetAmounts: amt
})
sendResponse(true);
//change mode from "concensus step" to "step"
commit();
//now change mode back to "concensus step" and pay for the claimant.
Insurer.pay(claimAmount);
return [membersCount, claimsCount + 1];
})
).api(CommunityMember.respondToClaim,
(_) => { const _ = true; },
(_) => 0,
((opinion, sendResponse) => {
const who = this;
const forWho = opinion.claimant;
sendResponse(true);
Insurer.interact.log("backend: API.CommunityMember.respondToClaim ...");
if (opinion.accepted) {
const approvalsCnt = maybe(insuranceClaims[forWho], 1, readFromMap("approvalsCount"));
const sumOfSetAmts = maybe(insuranceClaims[forWho], 0, readFromMap("sumOfSetAmounts"));
const amtRqsted = maybe(insuranceClaims[forWho], 0, readFromMap("amountRequested"));
const amtSet = maybe(insuranceClaims[forWho], amtRqsted, readFromMap("amountSet"));
const agreedClaimAmount = (approvalsCnt < 5) ? amtSet : sumOfSetAmts / approvalsCnt;
insuranceClaims[forWho] = Struct([["amountRequested", UInt], ["amountSet", UInt], ["accepted", Bool], ["approvalsCount", UInt], ["sumOfSetAmounts", UInt]]).fromObject({
approvalsCount: approvalsCnt + 1,
amountSet: agreedClaimAmount,
accepted: true,
amountRequested: amtRqsted,
sumOfSetAmounts: sumOfSetAmts
});
if (approvalsCnt >= 5) {
//transfer(agreedClaimAmount).to(forWho);
//eliminate the member from the list of claim owners (membersWithClaims)
delete claimOwners[forWho];
//eliminate the claim from the list of open claims (openClaims)
delete insuranceClaims[forWho];
//Notify the funded member
Insurer.interact.notifyFundedMember(forWho);
}
}
return [membersCount, claimsCount];
})
).api(CommunityMember.withDrawClaim,
() => { const _ = true; },
() => 0,
(sendResponse) => {
const who = this;
sendResponse(true);
//take the funds that the insurer had put on the table (paid), //back into the treasury
const memberRequestedAmount = maybe(insuranceClaims[who], 0, readFromMap("amountRequested"));
//transfer(memberRequestedAmount).to(Insurer);
//delete the claim from the list of open claims
delete insuranceClaims[who];
//eliminate this member from the list of claim owners (membersWithClaims)
delete claimOwners[who];
return [membersCount, claimsCount];
}
).api(CommunityMember.stopContract,
() => { const _ = true; },
() => 0,
((sendResponse) => {
//this must be done by the deployer of the contract only.
const who = this;
//TODO: require(addressOf(who) == addressOf(Insurer), "You are not allowed to take this action.");
Insurer.interact.stopContract();
sendResponse(true);
return [membersCount, claimsCount];
})
);
//cleanup: send all pending funds to the insurer (if any)
transfer(balance()).to(Insurer);
//terminate the concensus step
commit();
exit();
});
//REF:
//follow example: https://github.com/reach-sh/reach-lang/blob/master/examples/rsvp/index.rsh
//invariants info: https://en.wikipedia.org/wiki/Loop_invariant
//TODO: use IPFS: https://www.freecodecamp.org/news/technical-guide-to-ipfs-decentralized-storage-of-web3/
//to use supabase: https://supabase.com/docs/reference/javascript/delete