1 | | - | {-# STDLIB_VERSION 6 #-} |
---|
2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let SEP = "__" |
---|
5 | | - | |
---|
6 | | - | let contractFilename = "treasury.ride" |
---|
7 | | - | |
---|
8 | | - | let ADMIN_LIST_SIZE = 5 |
---|
9 | | - | |
---|
10 | | - | let QUORUM = 3 |
---|
11 | | - | |
---|
12 | | - | let TXID_BYTES_LENGTH = 32 |
---|
13 | | - | |
---|
14 | | - | func wrapErr (s) = ((contractFilename + ": ") + s) |
---|
15 | | - | |
---|
16 | | - | |
---|
17 | | - | func throwErr (s) = throw(wrapErr(s)) |
---|
18 | | - | |
---|
19 | | - | |
---|
20 | | - | func keyAllowedTxIdVotePrefix (txId) = makeString(["%s%s%s", "allowTxId", txId], SEP) |
---|
21 | | - | |
---|
22 | | - | |
---|
23 | | - | func keyFullAdminVote (prefix,adminAddress) = makeString([prefix, adminAddress], SEP) |
---|
24 | | - | |
---|
25 | | - | |
---|
26 | | - | func keyAdminAddressList () = makeString(["%s", "adminAddressList"], SEP) |
---|
27 | | - | |
---|
28 | | - | |
---|
29 | | - | func keyAllowedTxId () = makeString(["%s", "txId"], SEP) |
---|
30 | | - | |
---|
31 | | - | |
---|
32 | | - | func keyConfigAddress () = "%s__config" |
---|
33 | | - | |
---|
34 | | - | |
---|
35 | | - | func keyVotingResultAddress () = "contract_voting_result" |
---|
36 | | - | |
---|
37 | | - | |
---|
38 | | - | func keyProposalAllowBroadcast (address,txId) = ((("proposal_allow_broadcast_" + toString(address)) + "_") + toBase58String(txId)) |
---|
39 | | - | |
---|
40 | | - | |
---|
41 | | - | let configAddressOption = match getString(this, keyConfigAddress()) { |
---|
42 | | - | case s: String => |
---|
43 | | - | addressFromString(s) |
---|
44 | | - | case _: Unit => |
---|
45 | | - | unit |
---|
46 | | - | case _ => |
---|
47 | | - | throw("Match error") |
---|
48 | | - | } |
---|
49 | | - | |
---|
50 | | - | let configAddress = valueOrErrorMessage(configAddressOption, wrapErr("invalid config address")) |
---|
51 | | - | |
---|
52 | | - | let votingResultContractOption = match configAddressOption { |
---|
53 | | - | case a: Address => |
---|
54 | | - | match getString(a, keyVotingResultAddress()) { |
---|
55 | | - | case s: String => |
---|
56 | | - | addressFromString(s) |
---|
57 | | - | case _: Unit => |
---|
58 | | - | unit |
---|
59 | | - | case _ => |
---|
60 | | - | throw("Match error") |
---|
61 | | - | } |
---|
62 | | - | case _: Unit => |
---|
63 | | - | unit |
---|
64 | | - | case _ => |
---|
65 | | - | throw("Match error") |
---|
66 | | - | } |
---|
67 | | - | |
---|
68 | | - | let votingResultContract = valueOrErrorMessage(votingResultContractOption, wrapErr("invalid voting result address")) |
---|
69 | | - | |
---|
70 | | - | func getAdminVote (prefix,admin) = { |
---|
71 | | - | let voteKey = keyFullAdminVote(prefix, admin) |
---|
72 | | - | valueOrElse(getInteger(voteKey), 0) |
---|
73 | | - | } |
---|
74 | | - | |
---|
75 | | - | |
---|
76 | | - | func getAdminsList () = match getString(this, keyAdminAddressList()) { |
---|
77 | | - | case s: String => |
---|
78 | | - | split(s, SEP) |
---|
79 | | - | case _ => |
---|
80 | | - | nil |
---|
81 | | - | } |
---|
82 | | - | |
---|
83 | | - | |
---|
84 | | - | func isInAdminList (address) = containsElement(getAdminsList(), address) |
---|
85 | | - | |
---|
86 | | - | |
---|
87 | | - | func genVotesKeysHelper (a,adminAddress) = { |
---|
88 | | - | let $t020202044 = a |
---|
89 | | - | let result = $t020202044._1 |
---|
90 | | - | let prefix = $t020202044._2 |
---|
91 | | - | $Tuple2((result :+ keyFullAdminVote(prefix, adminAddress)), prefix) |
---|
92 | | - | } |
---|
93 | | - | |
---|
94 | | - | |
---|
95 | | - | func genVotesKeys (keyPrefix) = { |
---|
96 | | - | let adminList = keyAdminAddressList() |
---|
97 | | - | let $t021912275 = { |
---|
98 | | - | let $l = getAdminsList() |
---|
99 | | - | let $s = size($l) |
---|
100 | | - | let $acc0 = $Tuple2(nil, keyPrefix) |
---|
101 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
102 | | - | then $a |
---|
103 | | - | else genVotesKeysHelper($a, $l[$i]) |
---|
104 | | - | |
---|
105 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
106 | | - | then $a |
---|
107 | | - | else throw("List size exceeds 5") |
---|
108 | | - | |
---|
109 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5) |
---|
110 | | - | } |
---|
111 | | - | let result = $t021912275._1 |
---|
112 | | - | let prefix = $t021912275._2 |
---|
113 | | - | result |
---|
114 | | - | } |
---|
115 | | - | |
---|
116 | | - | |
---|
117 | | - | func countVotesHelper (result,voteKey) = (result + valueOrElse(getInteger(voteKey), 0)) |
---|
118 | | - | |
---|
119 | | - | |
---|
120 | | - | func countVotes (prefix) = { |
---|
121 | | - | let votes = genVotesKeys(prefix) |
---|
122 | | - | let $l = votes |
---|
123 | | - | let $s = size($l) |
---|
124 | | - | let $acc0 = 0 |
---|
125 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
126 | | - | then $a |
---|
127 | | - | else countVotesHelper($a, $l[$i]) |
---|
128 | | - | |
---|
129 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
130 | | - | then $a |
---|
131 | | - | else throw("List size exceeds 5") |
---|
132 | | - | |
---|
133 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5) |
---|
134 | | - | } |
---|
135 | | - | |
---|
136 | | - | |
---|
137 | | - | func clearVotesHelper (result,key) = (result :+ DeleteEntry(key)) |
---|
138 | | - | |
---|
139 | | - | |
---|
140 | | - | func getClearVoteEntries (prefix) = { |
---|
141 | | - | let votes = genVotesKeys(prefix) |
---|
142 | | - | let $l = votes |
---|
143 | | - | let $s = size($l) |
---|
144 | | - | let $acc0 = nil |
---|
145 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
146 | | - | then $a |
---|
147 | | - | else clearVotesHelper($a, $l[$i]) |
---|
148 | | - | |
---|
149 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
150 | | - | then $a |
---|
151 | | - | else throw("List size exceeds 5") |
---|
152 | | - | |
---|
153 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5) |
---|
154 | | - | } |
---|
155 | | - | |
---|
156 | | - | |
---|
157 | | - | func voteINTERNAL (callerAddressString,keyPrefix,minVotes,voteResult) = { |
---|
158 | | - | let voteKey = keyFullAdminVote(keyPrefix, callerAddressString) |
---|
159 | | - | let adminCurrentVote = getAdminVote(keyPrefix, callerAddressString) |
---|
160 | | - | let err = if (!(isInAdminList(callerAddressString))) |
---|
161 | | - | then throwErr((("Address: " + callerAddressString) + " not in Admin list")) |
---|
162 | | - | else if ((adminCurrentVote == 1)) |
---|
163 | | - | then throwErr((voteKey + " you already voted")) |
---|
164 | | - | else unit |
---|
165 | | - | if ((err == err)) |
---|
166 | | - | then { |
---|
167 | | - | let votes = countVotes(keyPrefix) |
---|
168 | | - | if (((votes + 1) >= minVotes)) |
---|
169 | | - | then { |
---|
170 | | - | let clearVoteEntries = getClearVoteEntries(keyPrefix) |
---|
171 | | - | (clearVoteEntries ++ voteResult) |
---|
172 | | - | } |
---|
173 | | - | else [IntegerEntry(voteKey, 1)] |
---|
174 | | - | } |
---|
175 | | - | else throw("Strict value is not equal to itself.") |
---|
176 | | - | } |
---|
177 | | - | |
---|
178 | | - | |
---|
179 | | - | @Callable(i) |
---|
180 | | - | func voteForTxId (txId) = { |
---|
181 | | - | let callerAddressString = toBase58String(i.caller.bytes) |
---|
182 | | - | let keyPrefix = keyAllowedTxIdVotePrefix(txId) |
---|
183 | | - | let result = [StringEntry(keyAllowedTxId(), txId)] |
---|
184 | | - | let allowedTxIdOption = getString(this, keyAllowedTxId()) |
---|
185 | | - | let err = [if ((size(fromBase58String(txId)) == TXID_BYTES_LENGTH)) |
---|
186 | | - | then true |
---|
187 | | - | else throwErr((txId + " is not valid txId")), if (if ((allowedTxIdOption == unit)) |
---|
188 | | - | then true |
---|
189 | | - | else (value(allowedTxIdOption) != txId)) |
---|
190 | | - | then true |
---|
191 | | - | else throwErr((txId + " is already allowed"))] |
---|
192 | | - | if ((err == err)) |
---|
193 | | - | then voteINTERNAL(callerAddressString, keyPrefix, QUORUM, result) |
---|
194 | | - | else throw("Strict value is not equal to itself.") |
---|
195 | | - | } |
---|
196 | | - | |
---|
197 | | - | |
---|
198 | | - | @Verifier(tx) |
---|
199 | | - | func verify () = { |
---|
200 | | - | let allowedTxIdStringOption = getString(keyAllowedTxId()) |
---|
201 | | - | let currentAdminList = getAdminsList() |
---|
202 | | - | let byProposal = valueOrElse(getBoolean(votingResultContract, keyProposalAllowBroadcast(this, tx.id)), false) |
---|
203 | | - | let byAdmins = if ((allowedTxIdStringOption != unit)) |
---|
204 | | - | then (tx.id == fromBase58String(value(allowedTxIdStringOption))) |
---|
205 | | - | else false |
---|
206 | | - | let byOwner = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|
207 | | - | if (if ((configAddressOption != unit)) |
---|
208 | | - | then (size(currentAdminList) == ADMIN_LIST_SIZE) |
---|
209 | | - | else false) |
---|
210 | | - | then if (if ((votingResultContractOption != unit)) |
---|
211 | | - | then byProposal |
---|
212 | | - | else false) |
---|
213 | | - | then true |
---|
214 | | - | else byAdmins |
---|
215 | | - | else byOwner |
---|
216 | | - | } |
---|
217 | | - | |
---|
| 1 | + | # no script |
---|