17 | | - | func getStrByKey (key) = { |
---|
18 | | - | let str = match getString(this, key) { |
---|
19 | | - | case a: String => |
---|
20 | | - | a |
---|
21 | | - | case _ => |
---|
22 | | - | NONE |
---|
23 | | - | } |
---|
24 | | - | str |
---|
25 | | - | } |
---|
26 | | - | |
---|
27 | | - | |
---|
28 | | - | func getKeyItemPrice (item) = (item + "_price") |
---|
29 | | - | |
---|
30 | | - | |
---|
31 | | - | func getValueItemPrice (item) = getNumberByKey(getKeyItemPrice(item)) |
---|
32 | | - | |
---|
33 | | - | |
---|
34 | | - | func getKeyUserItemCounter (user,item) = (((item + "_") + user) + "_cnt") |
---|
35 | | - | |
---|
36 | | - | |
---|
37 | | - | func getValueUserItemCounter (user,item) = getNumberByKey(getKeyUserItemCounter(user, item)) |
---|
38 | | - | |
---|
39 | | - | |
---|
40 | | - | func getKeyItem (supplier,title) = ("item_" + toBase58String(sha256(toBytes((supplier + title))))) |
---|
41 | | - | |
---|
42 | | - | |
---|
43 | | - | func getKeyItemData (item) = (item + "_data") |
---|
44 | | - | |
---|
45 | | - | |
---|
46 | | - | func getKeyItemSupplier (item) = (item + "_owner") |
---|
47 | | - | |
---|
48 | | - | |
---|
49 | | - | func getValueItemSupplier (item) = getStrByKey(getKeyItemSupplier(item)) |
---|
50 | | - | |
---|
51 | | - | |
---|
52 | | - | func getKeyBalanceSupplier (account) = (account + "_balance") |
---|
53 | | - | |
---|
54 | | - | |
---|
55 | | - | func getValueBalanceSupplier (account) = getNumberByKey(getKeyBalanceSupplier(account)) |
---|
56 | | - | |
---|
57 | | - | |
---|
58 | | - | let VOTERS = 3 |
---|
59 | | - | |
---|
60 | | - | let QUORUM = 2 |
---|
61 | | - | |
---|
62 | | - | let VOTING = "voting" |
---|
63 | | - | |
---|
64 | | - | let REVEAL = "reveal" |
---|
65 | | - | |
---|
66 | | - | let FEATURED = "featured" |
---|
67 | | - | |
---|
68 | | - | let DELISTED = "delisted" |
---|
69 | | - | |
---|
70 | | - | func getKeyCommit (item,user) = (((item + "_") + user) + "_commit") |
---|
71 | | - | |
---|
72 | | - | |
---|
73 | | - | func getValueCommit (item,user) = getStrByKey(getKeyCommit(item, user)) |
---|
74 | | - | |
---|
75 | | - | |
---|
76 | | - | func getKeyCommitsCount (item) = (item + "_comcnt") |
---|
77 | | - | |
---|
78 | | - | |
---|
79 | | - | func getValueCommitsCount (item) = getNumberByKey(getKeyCommitsCount(item)) |
---|
80 | | - | |
---|
81 | | - | |
---|
82 | | - | func getKeyReveal (item,user) = (((item + "_") + user) + "_reveal") |
---|
83 | | - | |
---|
84 | | - | |
---|
85 | | - | func getValueReveal (item,user) = getStrByKey(getKeyReveal(item, user)) |
---|
86 | | - | |
---|
87 | | - | |
---|
88 | | - | func getKeyItemStatus (item) = (item + "_status") |
---|
89 | | - | |
---|
90 | | - | |
---|
91 | | - | func getValueItemStatus (item) = getStrByKey(getKeyItemStatus(item)) |
---|
92 | | - | |
---|
93 | | - | |
---|
94 | | - | func getKeyVoteCount (item,vote) = ((item + "_res:") + vote) |
---|
95 | | - | |
---|
96 | | - | |
---|
97 | | - | func getValueVoteCount (item,vote) = getNumberByKey(getKeyVoteCount(item, vote)) |
---|
98 | | - | |
---|
99 | | - | |
---|
100 | | - | func getExtAddressOracle () = value(addressFromString("3MsbiyFyy8B3aN3kF8ntda474SPn4YMVkiq")) |
---|
101 | | - | |
---|
102 | | - | |
---|
103 | | - | let BLACKLISTED = "blacklist" |
---|
104 | | - | |
---|
105 | | - | func getExtKeyItemWhiteListStatus (account) = (account + "_verifier_status") |
---|
106 | | - | |
---|
107 | | - | |
---|
108 | | - | func getExtValueItemWhiteListStatus (oracle,account) = { |
---|
109 | | - | let str = match getString(oracle, getExtKeyItemWhiteListStatus(account)) { |
---|
110 | | - | case a: String => |
---|
111 | | - | a |
---|
112 | | - | case _ => |
---|
113 | | - | NONE |
---|
114 | | - | } |
---|
115 | | - | str |
---|
116 | | - | } |
---|
117 | | - | |
---|
118 | | - | |
---|
119 | | - | @Callable(i) |
---|
120 | | - | func addItem (title,price,data) = { |
---|
121 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
122 | | - | let item = getKeyItem(supplierAddress, title) |
---|
123 | | - | let supplierWhiteList = getExtValueItemWhiteListStatus(getExtAddressOracle(), supplierAddress) |
---|
124 | | - | if ((0 >= price)) |
---|
125 | | - | then throw("Purchase amount cannot be less than item price") |
---|
126 | | - | else if ((getValueItemSupplier(item) != NONE)) |
---|
127 | | - | then throw("an item already exists") |
---|
128 | | - | else if ((supplierWhiteList == BLACKLISTED)) |
---|
129 | | - | then throw("supplier's account has been blacklisted") |
---|
130 | | - | else [StringEntry(getKeyItemSupplier(item), supplierAddress), IntegerEntry(getKeyItemPrice(item), price), StringEntry(getKeyItemData(item), data), StringEntry(getExtKeyItemWhiteListStatus(supplierAddress), supplierWhiteList)] |
---|
131 | | - | } |
---|
132 | | - | |
---|
133 | | - | |
---|
134 | | - | |
---|
135 | | - | @Callable(i) |
---|
136 | | - | func purchase (item) = { |
---|
137 | | - | let pmt = i.payments[0] |
---|
138 | | - | if (isDefined(pmt.assetId)) |
---|
139 | | - | then throw("WAVES ONLY HHEUAWHEUAS") |
---|
140 | | - | else { |
---|
141 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
142 | | - | let price = getValueItemPrice(item) |
---|
143 | | - | let supplierAddress = getValueItemSupplier(item) |
---|
144 | | - | if ((price > pmt.amount)) |
---|
145 | | - | then throw("Purchase amount cannot be less than item price") |
---|
146 | | - | else if ((supplierAddress == NONE)) |
---|
147 | | - | then throw("Supplier does not exist") |
---|
148 | | - | else [IntegerEntry(getKeyUserItemCounter(userAddress, item), (getValueUserItemCounter(userAddress, item) + 1)), IntegerEntry(getKeyBalanceSupplier(supplierAddress), (getValueBalanceSupplier(supplierAddress) + pmt.amount))] |
---|
149 | | - | } |
---|
150 | | - | } |
---|
151 | | - | |
---|
152 | | - | |
---|
153 | | - | |
---|
154 | | - | @Callable(i) |
---|
155 | | - | func withdraw () = { |
---|
156 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
157 | | - | let balance = getValueBalanceSupplier(supplierAddress) |
---|
158 | | - | if ((0 >= balance)) |
---|
159 | | - | then throw("insuficient balance") |
---|
160 | | - | else [IntegerEntry(getKeyBalanceSupplier(supplierAddress), 0), ScriptTransfer(addressFromStringValue(supplierAddress), balance, unit)] |
---|
161 | | - | } |
---|
162 | | - | |
---|
163 | | - | |
---|
164 | | - | |
---|
165 | | - | @Callable(i) |
---|
166 | | - | func voteCommit (item,hash) = { |
---|
167 | | - | let user = toBase58String(i.caller.bytes) |
---|
168 | | - | let commits = getValueCommitsCount(item) |
---|
169 | | - | let status = getValueItemStatus(item) |
---|
170 | | - | if ((commits >= VOTERS)) |
---|
171 | | - | then throw("Reached max num of voters") |
---|
172 | | - | else if ((getValueCommit(item, user) != NONE)) |
---|
173 | | - | then throw("b1") |
---|
174 | | - | else if ((getKeyItemSupplier(item) == NONE)) |
---|
175 | | - | then throw("b2") |
---|
176 | | - | else if (if ((status != NONE)) |
---|
177 | | - | then (status != VOTING) |
---|
178 | | - | else false) |
---|
179 | | - | then throw("b3") |
---|
180 | | - | else [StringEntry(getKeyCommit(item, user), hash), IntegerEntry(getKeyCommitsCount(item), (commits + 1)), StringEntry(getKeyItemStatus(item), if ((commits == VOTERS)) |
---|
181 | | - | then REVEAL |
---|
182 | | - | else VOTING)] |
---|
183 | | - | } |
---|
184 | | - | |
---|
185 | | - | |
---|
186 | | - | |
---|
187 | | - | @Callable(i) |
---|
188 | | - | func voteReveal (item,vote,salt) = { |
---|
189 | | - | let user = toBase58String(i.caller.bytes) |
---|
190 | | - | let status = getValueItemStatus(item) |
---|
191 | | - | let newVoteCount = (getValueVoteCount(item, vote) + 1) |
---|
192 | | - | if ((toBase58String(sha256(toBytes((vote + salt)))) != getValueCommit(item, user))) |
---|
193 | | - | then throw("Reveal data is not valid") |
---|
194 | | - | else if ((VOTERS > getValueCommitsCount(item))) |
---|
195 | | - | then throw("max num of voters hasnt reached yet") |
---|
196 | | - | else if ((getValueReveal(item, user) != NONE)) |
---|
197 | | - | then throw("user has already participated") |
---|
198 | | - | else if (if ((status != VOTING)) |
---|
199 | | - | then (status != REVEAL) |
---|
200 | | - | else false) |
---|
201 | | - | then throw("wrong status") |
---|
202 | | - | else if (if ((vote != FEATURED)) |
---|
203 | | - | then (vote != DELISTED) |
---|
204 | | - | else false) |
---|
205 | | - | then throw("wrong vote") |
---|
206 | | - | else if (if ((status == FEATURED)) |
---|
207 | | - | then true |
---|
208 | | - | else (status == DELISTED)) |
---|
209 | | - | then throw("vote has finished") |
---|
210 | | - | else [StringEntry(getKeyReveal(item, user), vote), IntegerEntry(getKeyVoteCount(item, vote), newVoteCount), StringEntry(getKeyItemStatus(item), if ((newVoteCount >= QUORUM)) |
---|
211 | | - | then vote |
---|
212 | | - | else REVEAL)] |
---|
213 | | - | } |
---|
214 | | - | |
---|
| 16 | + | @Verifier(tx) |
---|
| 17 | + | func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|