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