24 | | - | func getTicketKey (addr) = makeString(["ticket", addr], separator) |
---|
25 | | - | |
---|
26 | | - | |
---|
27 | | - | func getTicketValue (addr) = getBinaryValue(this, getTicketKey(addr)) |
---|
28 | | - | |
---|
29 | | - | |
---|
30 | | - | func getItemPriceKey (itemId) = makeString(["item", itemId], separator) |
---|
31 | | - | |
---|
32 | | - | |
---|
33 | | - | func getItemPriceValue (itemId) = getIntegerValue(this, getItemPriceKey(itemId)) |
---|
34 | | - | |
---|
35 | | - | |
---|
36 | | - | func getVotingDetailsKey (votingId) = makeString(["votingDetails", votingId], separator) |
---|
37 | | - | |
---|
38 | | - | |
---|
39 | | - | func getItemSoldKey (itemId) = makeString(["sold", "item", itemId], separator) |
---|
40 | | - | |
---|
41 | | - | |
---|
42 | | - | func getVotingEndKey (votingId) = makeString(["votingEnd", votingId], separator) |
---|
43 | | - | |
---|
44 | | - | |
---|
45 | | - | func getVotingEndValue (votingId) = getIntegerValue(this, getVotingEndKey(votingId)) |
---|
46 | | - | |
---|
47 | | - | |
---|
48 | | - | func getVotingVariantKey (votingId,variant) = makeString(["votingId", votingId, "variant", toString(variant)], separator) |
---|
49 | | - | |
---|
50 | | - | |
---|
51 | | - | func getVotingVariantValue (votingId,variant) = getIntegerValue(this, getVotingVariantKey(votingId, variant)) |
---|
52 | | - | |
---|
53 | | - | |
---|
54 | | - | func getVoterKey (votingId,voterAddr) = makeString(["votingId", votingId, voterAddr], separator) |
---|
55 | | - | |
---|
56 | | - | |
---|
57 | | - | func isAdmin (sender) = { |
---|
58 | | - | let admin = getString(this, "admin") |
---|
59 | | - | if (!(isDefined(admin))) |
---|
60 | | - | then throw("init first") |
---|
61 | | - | else (addressFromPublicKey(fromBase58String(value(admin))) == sender) |
---|
62 | | - | } |
---|
63 | | - | |
---|
64 | | - | |
---|
65 | | - | func isEndOfVoting (votingId) = { |
---|
66 | | - | let now = value(blockInfoByHeight(height)).timestamp |
---|
67 | | - | let end = getVotingEndValue(votingId) |
---|
68 | | - | (now >= end) |
---|
69 | | - | } |
---|
70 | | - | |
---|
71 | | - | |
---|
72 | | - | func isCorrectVoter (votingId,caller) = !(isDefined(getBoolean(this, getVoterKey(votingId, toString(caller))))) |
---|
73 | | - | |
---|
74 | | - | |
---|
75 | | - | func isBlackListed (caller) = getBlackListValue(toString(caller)) |
---|
76 | | - | |
---|
77 | | - | |
---|
78 | | - | func calcVotesAmount (votingId,caller,attachment) = { |
---|
79 | | - | let ticket = getTicketValue(toString(caller)) |
---|
80 | | - | let attachmentAssetId = value(attachment.assetId) |
---|
81 | | - | if ((ticket == attachmentAssetId)) |
---|
82 | | - | then { |
---|
83 | | - | let callerBalance = (assetBalance(caller, tokenId) * multiplier) |
---|
84 | | - | $Tuple4(callerBalance, Burn(value(attachment.assetId), 1), DeleteEntry(getTicketKey(toString(caller))), BooleanEntry(getVoterKey(votingId, toString(caller)), true)) |
---|
85 | | - | } |
---|
86 | | - | else throw("Wrong attachment asset") |
---|
87 | | - | } |
---|
| 20 | + | func setAddrToIntMappingValue (mappingName,addr,val) = if ((150 >= size(val))) |
---|
| 21 | + | then StringEntry(getAddrToIntMappingKey("mapString", addr), val) |
---|
| 22 | + | else throw("overcrowded") |
---|
103 | | - | func startVoting (votingId,votingDesc,period) = if (isAdmin(i.caller)) |
---|
104 | | - | then { |
---|
105 | | - | let now = value(blockInfoByHeight(height)).timestamp |
---|
106 | | - | [StringEntry(getVotingDetailsKey(votingId), votingDesc), IntegerEntry(getVotingEndKey(votingId), (now + period)), IntegerEntry(getVotingVariantKey(votingId, 1), 0), IntegerEntry(getVotingVariantKey(votingId, 2), 0), IntegerEntry(getVotingVariantKey(votingId, 3), 0)] |
---|
107 | | - | } |
---|
108 | | - | else throw("Only admin") |
---|
| 31 | + | func getArrayFromStruct (index) = if ((getAddrToIntMappingIndex("mapString", toString(this)) >= index)) |
---|
| 32 | + | then [StringEntry("1", getListOfArrays("mapString", toString(this))[index])] |
---|
| 33 | + | else throw("out of range") |
---|
113 | | - | func issueNft (recipient) = if (if (isAdmin(i.caller)) |
---|
114 | | - | then !(isBlackListed(addressFromStringValue(recipient))) |
---|
115 | | - | else false) |
---|
116 | | - | then { |
---|
117 | | - | let ticketKey = getTicketKey(recipient) |
---|
118 | | - | if (!(isDefined(getBinary(this, ticketKey)))) |
---|
119 | | - | then { |
---|
120 | | - | let issue = Issue("Ticket", "Multiplier for voting", 1, 0, false) |
---|
121 | | - | let id = calculateAssetId(issue) |
---|
122 | | - | [issue, ScriptTransfer(addressFromStringValue(recipient), 1, id), BinaryEntry(ticketKey, id)] |
---|
123 | | - | } |
---|
124 | | - | else throw("Already granted") |
---|
125 | | - | } |
---|
126 | | - | else throw("Only admin") |
---|
| 38 | + | func getElementFromArray (index,indexIn) = if ((getAddrToIntMappingIndexIn(indexIn) >= indexIn)) |
---|
| 39 | + | then [StringEntry("2", split(getListOfArrays("mapString", toString(this))[index], secondSeparator)[indexIn])] |
---|
| 40 | + | else throw("out of range") |
---|
128 | | - | |
---|
129 | | - | |
---|
130 | | - | @Callable(i) |
---|
131 | | - | func getTokens () = { |
---|
132 | | - | let key = getWhiteListKey(toString(i.caller)) |
---|
133 | | - | let granted = getWhiteListValue(toString(i.caller)) |
---|
134 | | - | if (!(granted)) |
---|
135 | | - | then [BooleanEntry(key, true), ScriptTransfer(i.caller, amountGetTokens, tokenId)] |
---|
136 | | - | else throw("Already got") |
---|
137 | | - | } |
---|
138 | | - | |
---|
139 | | - | |
---|
140 | | - | |
---|
141 | | - | @Callable(i) |
---|
142 | | - | func vote (votingId,variant) = if (!(isEndOfVoting(votingId))) |
---|
143 | | - | then if (isCorrectVoter(votingId, i.caller)) |
---|
144 | | - | then { |
---|
145 | | - | let votingKey = getVotingVariantKey(votingId, variant) |
---|
146 | | - | let counter = getVotingVariantValue(votingId, variant) |
---|
147 | | - | let blackListKey = getBlackListKey(toString(i.caller)) |
---|
148 | | - | if ((size(i.payments) != 1)) |
---|
149 | | - | then { |
---|
150 | | - | let callerBalance = assetBalance(i.caller, tokenId) |
---|
151 | | - | [BooleanEntry(blackListKey, false), BooleanEntry(getVoterKey(votingId, toString(i.caller)), true), IntegerEntry(votingKey, (counter + callerBalance))] |
---|
152 | | - | } |
---|
153 | | - | else { |
---|
154 | | - | let $t051845274 = calcVotesAmount(votingId, i.caller, i.payments[0]) |
---|
155 | | - | let voteAmount = $t051845274._1 |
---|
156 | | - | let burn = $t051845274._2 |
---|
157 | | - | let clear = $t051845274._3 |
---|
158 | | - | let record = $t051845274._4 |
---|
159 | | - | [burn, clear, record, BooleanEntry(blackListKey, false), IntegerEntry(votingKey, (counter + voteAmount))] |
---|
160 | | - | } |
---|
161 | | - | } |
---|
162 | | - | else throw("Invalid voter") |
---|
163 | | - | else throw("Voting is ended") |
---|
164 | | - | |
---|
165 | | - | |
---|
166 | | - | |
---|
167 | | - | @Callable(i) |
---|
168 | | - | func joinProgram (patron) = { |
---|
169 | | - | let validPatron = getWhiteListValue(patron) |
---|
170 | | - | if (if (validPatron) |
---|
171 | | - | then !(isBlackListed(i.caller)) |
---|
172 | | - | else false) |
---|
173 | | - | then { |
---|
174 | | - | let key = getWhiteListKey(toString(i.caller)) |
---|
175 | | - | let patronKey = getWhiteListKey(patron) |
---|
176 | | - | let slot = getBoolean(this, key) |
---|
177 | | - | if (!(isDefined(slot))) |
---|
178 | | - | then [BooleanEntry(key, true), BooleanEntry(patronKey, false), ScriptTransfer(i.caller, amountGetTokens, tokenId)] |
---|
179 | | - | else throw("Already in program") |
---|
180 | | - | } |
---|
181 | | - | else throw("Wrong patron or caller from blackList") |
---|
182 | | - | } |
---|
183 | | - | |
---|
184 | | - | |
---|
185 | | - | |
---|
186 | | - | @Callable(i) |
---|
187 | | - | func buyItem (itemId) = if (!(isBlackListed(i.caller))) |
---|
188 | | - | then { |
---|
189 | | - | let pmt = i.payments[0] |
---|
190 | | - | let amount = pmt.amount |
---|
191 | | - | let price = getItemPriceValue(itemId) |
---|
192 | | - | if (if ((value(pmt.assetId) == tokenId)) |
---|
193 | | - | then (price == amount) |
---|
194 | | - | else false) |
---|
195 | | - | then [Burn(tokenId, amount), StringEntry(getItemSoldKey(itemId), toString(i.caller))] |
---|
196 | | - | else throw("Wrong payment assetId") |
---|
197 | | - | } |
---|
198 | | - | else throw("Caller from blackList") |
---|
199 | | - | |
---|
200 | | - | |
---|
201 | | - | @Verifier(tx) |
---|
202 | | - | func verify () = match tx { |
---|
203 | | - | case dtx: DataTransaction => |
---|
204 | | - | if (!(isDefined(getString(dtx.data, "admin")))) |
---|
205 | | - | then sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(getStringValue(this, "admin"))) |
---|
206 | | - | else false |
---|
207 | | - | case mtx: MassTransferTransaction => |
---|
208 | | - | sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(getStringValue(this, "admin"))) |
---|
209 | | - | case _ => |
---|
210 | | - | sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|
211 | | - | } |
---|