4 | | - | let SEP = "__" |
---|
5 | | - | |
---|
6 | | - | func blockHeightError () = throw("The block's height is too big for this proposal") |
---|
7 | | - | |
---|
8 | | - | |
---|
9 | | - | func alreadyVoteError () = throw("You have already voted") |
---|
10 | | - | |
---|
11 | | - | |
---|
12 | | - | func noVoteError () = throw("You have not already voted") |
---|
13 | | - | |
---|
14 | | - | |
---|
15 | | - | func gwxContractAddress () = makeString(["%s", "gwxContractAddress"], SEP) |
---|
16 | | - | |
---|
17 | | - | |
---|
18 | | - | func keyCurrentIndex () = makeString(["%s", "currentIndex"], SEP) |
---|
19 | | - | |
---|
20 | | - | |
---|
21 | | - | func keyProposalInfo (number) = makeString(["%s%d", "proposalInfo", toString(number)], SEP) |
---|
22 | | - | |
---|
23 | | - | |
---|
24 | | - | func keyProposalDescription (number) = makeString(["%s%d", "proposalDescription", toString(number)], SEP) |
---|
25 | | - | |
---|
26 | | - | |
---|
27 | | - | func keyUserVoteOnProposal (number,user) = makeString(["%s%d%s", "userVoteOnProposal", toString(number), user], SEP) |
---|
28 | | - | |
---|
29 | | - | |
---|
30 | | - | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
31 | | - | |
---|
32 | | - | |
---|
33 | | - | func keyManagerVaultAddress () = "%s__managerVaultAddress" |
---|
34 | | - | |
---|
35 | | - | |
---|
36 | | - | func getCurrentIndex () = getIntegerValue(this, keyCurrentIndex()) |
---|
37 | | - | |
---|
38 | | - | |
---|
39 | | - | func getManagerVaultAddressOrThis () = match getString(keyManagerVaultAddress()) { |
---|
40 | | - | case s: String => |
---|
41 | | - | addressFromStringValue(s) |
---|
42 | | - | case _ => |
---|
43 | | - | this |
---|
44 | | - | } |
---|
45 | | - | |
---|
46 | | - | |
---|
47 | | - | func formatProposalInfo (name,end,quorumNumber,positiveVotes,negativeVotes) = makeString(["%d%d%d%d%d", name, end, quorumNumber, positiveVotes, negativeVotes], SEP) |
---|
48 | | - | |
---|
49 | | - | |
---|
50 | | - | func formatUserVoteOnProposal (vote,gwxNumber) = makeString(["%d%d", vote, gwxNumber], SEP) |
---|
51 | | - | |
---|
52 | | - | |
---|
53 | | - | func asInt (val) = match val { |
---|
54 | | - | case valInt: Int => |
---|
55 | | - | valInt |
---|
56 | | - | case _ => |
---|
57 | | - | throw("fail to cast into Int") |
---|
58 | | - | } |
---|
59 | | - | |
---|
60 | | - | |
---|
61 | | - | func managerPublicKeyOrUnit () = { |
---|
62 | | - | let managerVaultAddress = getManagerVaultAddressOrThis() |
---|
63 | | - | match getString(managerVaultAddress, keyManagerPublicKey()) { |
---|
64 | | - | case s: String => |
---|
65 | | - | fromBase58String(s) |
---|
66 | | - | case _: Unit => |
---|
67 | | - | unit |
---|
68 | | - | case _ => |
---|
69 | | - | throw("Match error") |
---|
70 | | - | } |
---|
71 | | - | } |
---|
72 | | - | |
---|
73 | | - | |
---|
74 | | - | func isManager (i) = match managerPublicKeyOrUnit() { |
---|
75 | | - | case pk: ByteVector => |
---|
76 | | - | (i.callerPublicKey == pk) |
---|
77 | | - | case _: Unit => |
---|
78 | | - | (i.caller == this) |
---|
79 | | - | case _ => |
---|
80 | | - | throw("Match error") |
---|
81 | | - | } |
---|
82 | | - | |
---|
83 | | - | |
---|
84 | | - | func mustManager (i) = if (isManager(i)) |
---|
85 | | - | then true |
---|
86 | | - | else throw("permission denied") |
---|
87 | | - | |
---|
| 4 | + | let kassetId = "AssetID" |
---|
98 | | - | func voteFor (proposalIndex,choice) = { |
---|
99 | | - | let EMPTY = "EMPTY" |
---|
100 | | - | let voteInfo = valueOrElse(getString(this, keyProposalInfo(proposalIndex)), EMPTY) |
---|
101 | | - | let voteInfoArray = split(voteInfo, SEP) |
---|
102 | | - | let userVoteInfo = valueOrElse(getString(this, keyUserVoteOnProposal(proposalIndex, toString(i.caller))), EMPTY) |
---|
103 | | - | let checks = [if ((voteInfo != EMPTY)) |
---|
104 | | - | then true |
---|
105 | | - | else throw("No such vote"), if ((value(parseInt(voteInfoArray[2])) > lastBlock.height)) |
---|
106 | | - | then true |
---|
107 | | - | else blockHeightError(), if ((userVoteInfo == EMPTY)) |
---|
108 | | - | then true |
---|
109 | | - | else alreadyVoteError()] |
---|
110 | | - | if ((checks == checks)) |
---|
111 | | - | then { |
---|
112 | | - | let gwxNumber = asInt(invoke(addressFromStringValue(getStringValue(gwxContractAddress())), "getUserGwxAmountAtHeightREADONLY", [toString(i.caller), value(parseInt(voteInfoArray[2]))], nil)) |
---|
113 | | - | let $t035164552 = if (choice) |
---|
114 | | - | then { |
---|
115 | | - | let action1 = StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], toString((value(parseInt(voteInfoArray[4])) + gwxNumber)), voteInfoArray[5])) |
---|
116 | | - | let action2 = StringEntry(keyUserVoteOnProposal(proposalIndex, toString(i.caller)), formatUserVoteOnProposal("1", toString(gwxNumber))) |
---|
117 | | - | $Tuple2(action1, action2) |
---|
118 | | - | } |
---|
119 | | - | else { |
---|
120 | | - | let action1 = StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], voteInfoArray[4], toString((value(parseInt(voteInfoArray[5])) + gwxNumber)))) |
---|
121 | | - | let action2 = StringEntry(keyUserVoteOnProposal(proposalIndex, toString(i.caller)), formatUserVoteOnProposal("0", toString(gwxNumber))) |
---|
122 | | - | $Tuple2(action1, action2) |
---|
123 | | - | } |
---|
124 | | - | let action1 = $t035164552._1 |
---|
125 | | - | let action2 = $t035164552._2 |
---|
126 | | - | [action1, action2] |
---|
127 | | - | } |
---|
128 | | - | else throw("Strict value is not equal to itself.") |
---|
129 | | - | } |
---|
130 | | - | |
---|
131 | | - | |
---|
132 | | - | |
---|
133 | | - | @Callable(i) |
---|
134 | | - | func deleteVote (proposalIndex) = { |
---|
135 | | - | let EMPTY = "EMPTY" |
---|
136 | | - | let voteInfo = valueOrElse(getString(this, keyProposalInfo(proposalIndex)), EMPTY) |
---|
137 | | - | let voteInfoArray = split(voteInfo, SEP) |
---|
138 | | - | let userVoteInfo = valueOrElse(getString(this, keyUserVoteOnProposal(proposalIndex, toString(i.caller))), EMPTY) |
---|
139 | | - | let userVoteInfoArray = split(userVoteInfo, SEP) |
---|
140 | | - | let checks = [if ((voteInfo != EMPTY)) |
---|
141 | | - | then true |
---|
142 | | - | else throw("No such vote"), if ((value(parseInt(voteInfoArray[2])) > lastBlock.height)) |
---|
143 | | - | then true |
---|
144 | | - | else blockHeightError(), if ((userVoteInfo != EMPTY)) |
---|
145 | | - | then true |
---|
146 | | - | else noVoteError()] |
---|
147 | | - | if ((checks == checks)) |
---|
148 | | - | then { |
---|
149 | | - | let action = if ((userVoteInfoArray[1] == "1")) |
---|
150 | | - | then StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], toString((value(parseInt(voteInfoArray[4])) - value(parseInt(userVoteInfoArray[2])))), voteInfoArray[5])) |
---|
151 | | - | else StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], voteInfoArray[4], toString((value(parseInt(voteInfoArray[5])) - value(parseInt(userVoteInfoArray[2])))))) |
---|
152 | | - | [action, DeleteEntry(keyUserVoteOnProposal(proposalIndex, toString(i.caller)))] |
---|
153 | | - | } |
---|
154 | | - | else throw("Strict value is not equal to itself.") |
---|
155 | | - | } |
---|
156 | | - | |
---|
157 | | - | |
---|
158 | | - | |
---|
159 | | - | @Callable(i) |
---|
160 | | - | func changeVote (proposalIndex,choice) = { |
---|
161 | | - | let EMPTY = "EMPTY" |
---|
162 | | - | let voteInfo = valueOrElse(getString(this, keyProposalInfo(proposalIndex)), EMPTY) |
---|
163 | | - | let voteInfoArray = split(voteInfo, SEP) |
---|
164 | | - | let userVoteInfo = valueOrElse(getString(this, keyUserVoteOnProposal(proposalIndex, toString(i.caller))), EMPTY) |
---|
165 | | - | let userVoteInfoArray = split(userVoteInfo, SEP) |
---|
166 | | - | let checks = [if ((voteInfo != EMPTY)) |
---|
167 | | - | then true |
---|
168 | | - | else throw("No such vote"), if ((value(parseInt(voteInfoArray[2])) > lastBlock.height)) |
---|
169 | | - | then true |
---|
170 | | - | else blockHeightError(), if ((userVoteInfo != EMPTY)) |
---|
171 | | - | then true |
---|
172 | | - | else noVoteError()] |
---|
173 | | - | if ((checks == checks)) |
---|
174 | | - | then { |
---|
175 | | - | let gwxNumber = asInt(invoke(addressFromStringValue(getStringValue(gwxContractAddress())), "getUserGwxAmountAtHeightREADONLY", [toString(i.caller), value(parseInt(voteInfoArray[3]))], nil)) |
---|
176 | | - | let $t067207059 = if (choice) |
---|
177 | | - | then { |
---|
178 | | - | let forNumber = gwxNumber |
---|
179 | | - | let againstNumber = 0 |
---|
180 | | - | let choiceInteger = 1 |
---|
181 | | - | $Tuple3(forNumber, againstNumber, choiceInteger) |
---|
182 | | - | } |
---|
183 | | - | else { |
---|
184 | | - | let forNumber = 0 |
---|
185 | | - | let againstNumber = gwxNumber |
---|
186 | | - | let choiceInteger = 0 |
---|
187 | | - | $Tuple3(forNumber, againstNumber, choiceInteger) |
---|
188 | | - | } |
---|
189 | | - | let userPowerFor = $t067207059._1 |
---|
190 | | - | let userPowerAgainst = $t067207059._2 |
---|
191 | | - | let choiceInt = $t067207059._3 |
---|
192 | | - | let action = if ((userVoteInfoArray[1] == "1")) |
---|
193 | | - | then StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], toString(((value(parseInt(voteInfoArray[4])) - value(parseInt(userVoteInfoArray[2]))) + userPowerFor)), toString((value(parseInt(voteInfoArray[5])) + userPowerAgainst)))) |
---|
194 | | - | else StringEntry(keyProposalInfo(proposalIndex), formatProposalInfo(voteInfoArray[1], voteInfoArray[2], voteInfoArray[3], toString((value(parseInt(voteInfoArray[4])) + userPowerFor)), toString(((value(parseInt(voteInfoArray[5])) - value(parseInt(userVoteInfoArray[2]))) + userPowerAgainst)))) |
---|
195 | | - | [action, StringEntry(keyUserVoteOnProposal(proposalIndex, toString(i.caller)), formatUserVoteOnProposal(toString(choiceInt), toString(gwxNumber)))] |
---|
196 | | - | } |
---|
197 | | - | else throw("Strict value is not equal to itself.") |
---|
198 | | - | } |
---|
199 | | - | |
---|
200 | | - | |
---|
201 | | - | |
---|
202 | | - | @Callable(i) |
---|
203 | | - | func getResultREADONLY (proposalIndex) = { |
---|
204 | | - | let EMPTY = "EMPTY" |
---|
205 | | - | let voteInfo = valueOrElse(getString(this, keyProposalInfo(proposalIndex)), EMPTY) |
---|
206 | | - | let voteInfoArray = split(voteInfo, SEP) |
---|
207 | | - | let checks = [if ((voteInfo != EMPTY)) |
---|
208 | | - | then true |
---|
209 | | - | else throw("No such vote")] |
---|
210 | | - | if ((checks == checks)) |
---|
211 | | - | then { |
---|
212 | | - | let quorumNumber = voteInfoArray[3] |
---|
213 | | - | let positiveVotes = voteInfoArray[4] |
---|
214 | | - | let negativeVotes = voteInfoArray[5] |
---|
215 | | - | $Tuple2(nil, [positiveVotes, negativeVotes, quorumNumber]) |
---|
216 | | - | } |
---|
217 | | - | else throw("Strict value is not equal to itself.") |
---|
| 22 | + | func senderToken () = { |
---|
| 23 | + | let assetID = valueOrErrorMessage(getBinaryValue(this, kassetId), "error") |
---|
| 24 | + | [ScriptTransfer(this, 2, unit), ScriptTransfer(i.caller, 20, assetID)] |
---|