2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let NONE = "none" |
---|
5 | | - | |
---|
6 | | - | let VOTERS = 3 |
---|
7 | | - | |
---|
8 | | - | let QUORUM = 2 |
---|
9 | | - | |
---|
10 | | - | let VOTING = "voting" |
---|
11 | | - | |
---|
12 | | - | let REVEAL = "reveal" |
---|
13 | | - | |
---|
14 | | - | let FEATURED = "featured" |
---|
15 | | - | |
---|
16 | | - | let DELISTED = "delisted" |
---|
17 | | - | |
---|
18 | | - | func getNumberByKey (key) = { |
---|
19 | | - | let num = match getInteger(this, key) { |
---|
20 | | - | case a: Int => |
---|
21 | | - | a |
---|
22 | | - | case _ => |
---|
23 | | - | 0 |
---|
24 | | - | } |
---|
25 | | - | num |
---|
26 | | - | } |
---|
27 | | - | |
---|
28 | | - | |
---|
29 | | - | func getStrByKey (key) = { |
---|
30 | | - | let str = match getString(this, key) { |
---|
31 | | - | case a: String => |
---|
32 | | - | a |
---|
33 | | - | case _ => |
---|
34 | | - | NONE |
---|
35 | | - | } |
---|
36 | | - | str |
---|
37 | | - | } |
---|
38 | | - | |
---|
39 | | - | |
---|
40 | | - | func getKeyItemPrice (item) = (item + "_price") |
---|
41 | | - | |
---|
42 | | - | |
---|
43 | | - | func getValueItemPrice (item) = getNumberByKey(getKeyItemPrice(item)) |
---|
44 | | - | |
---|
45 | | - | |
---|
46 | | - | func getKeyUserItemCounter (user,item) = (((item + "_") + user) + "_cnt") |
---|
47 | | - | |
---|
48 | | - | |
---|
49 | | - | func getValueUserItemCounter (user,item) = getNumberByKey(getKeyUserItemCounter(user, item)) |
---|
50 | | - | |
---|
51 | | - | |
---|
52 | | - | func getKeyItem (supplier,title) = ("item_" + toBase58String(sha256(toBytes((supplier + title))))) |
---|
53 | | - | |
---|
54 | | - | |
---|
55 | | - | func getKeyItemData (item) = (item + "_data") |
---|
56 | | - | |
---|
57 | | - | |
---|
58 | | - | func getKeyItemSupplier (item) = (item + "_owner") |
---|
59 | | - | |
---|
60 | | - | |
---|
61 | | - | func getValueItemSupplier (item) = getStrByKey(getKeyItemSupplier(item)) |
---|
62 | | - | |
---|
63 | | - | |
---|
64 | | - | func getKeyBalanceSupplier (account) = (account + "_balanace") |
---|
65 | | - | |
---|
66 | | - | |
---|
67 | | - | func getValueBalanceSupplier (account) = getNumberByKey(getKeyBalanceSupplier(account)) |
---|
68 | | - | |
---|
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 | | - | @Callable(i) |
---|
101 | | - | func addItem (title,price,data) = { |
---|
102 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
103 | | - | let item = getKeyItem(supplierAddress, title) |
---|
104 | | - | if ((0 >= price)) |
---|
105 | | - | then throw("purchase ammount cannnot be less than item price") |
---|
106 | | - | else if ((getValueItemSupplier(item) != NONE)) |
---|
107 | | - | then throw("an item is already exist") |
---|
108 | | - | else WriteSet([DataEntry(getKeyItemSupplier(item), supplierAddress), DataEntry(getKeyItemPrice(item), price), DataEntry(getKeyItemData(item), data)]) |
---|
109 | | - | } |
---|
110 | | - | |
---|
111 | | - | |
---|
112 | | - | |
---|
113 | | - | @Callable(i) |
---|
114 | | - | func purchase (item) = { |
---|
115 | | - | let pmt = extract(i.payment) |
---|
116 | | - | if (isDefined(pmt.assetId)) |
---|
117 | | - | then throw("WAVES tokens only at the moment") |
---|
118 | | - | else { |
---|
119 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
120 | | - | let price = getValueItemPrice(item) |
---|
121 | | - | let supplierAddress = getValueItemSupplier(item) |
---|
122 | | - | if ((price > pmt.amount)) |
---|
123 | | - | then throw("purchase amount cannnot be less than item price") |
---|
124 | | - | else if ((pmt.amount > price)) |
---|
125 | | - | then throw("purchase amount cannnot be higher than item price") |
---|
126 | | - | else if ((supplierAddress == NONE)) |
---|
127 | | - | then throw("supplier does not exist") |
---|
128 | | - | else WriteSet([DataEntry(getKeyUserItemCounter(userAddress, item), (getValueUserItemCounter(userAddress, item) + 1)), DataEntry(getKeyBalanceSupplier(supplierAddress), (getValueBalanceSupplier(supplierAddress) + pmt.amount))]) |
---|
129 | | - | } |
---|
130 | | - | } |
---|
131 | | - | |
---|
132 | | - | |
---|
133 | | - | |
---|
134 | | - | @Callable(i) |
---|
135 | | - | func withdraw () = { |
---|
136 | | - | let supplierAddress = toBase58String(i.caller.bytes) |
---|
137 | | - | let balance = getValueBalanceSupplier(supplierAddress) |
---|
138 | | - | if ((0 >= balance)) |
---|
139 | | - | then throw("insufficient balance") |
---|
140 | | - | else ScriptResult(WriteSet([DataEntry(getKeyBalanceSupplier(supplierAddress), 0)]), TransferSet([ScriptTransfer(addressFromStringValue(supplierAddress), balance, unit)])) |
---|
141 | | - | } |
---|
142 | | - | |
---|
143 | | - | |
---|
144 | | - | |
---|
145 | | - | @Callable(i) |
---|
146 | | - | func voteCommit (item,hash) = { |
---|
147 | | - | let user = toBase58String(i.caller.bytes) |
---|
148 | | - | let commits = getValueCommitsCount(item) |
---|
149 | | - | let status = getValueItemStatus(item) |
---|
150 | | - | if ((commits >= VOTERS)) |
---|
151 | | - | then throw("reached max num of voters") |
---|
152 | | - | else if ((getValueCommit(item, user) != NONE)) |
---|
153 | | - | then throw("user has already participated") |
---|
154 | | - | else if ((getKeyItemSupplier(item) == NONE)) |
---|
155 | | - | then throw("item does not exist") |
---|
156 | | - | else if (if ((status != NONE)) |
---|
157 | | - | then (status != VOTING) |
---|
158 | | - | else false) |
---|
159 | | - | then throw("voting is not possible") |
---|
160 | | - | else WriteSet([DataEntry(getKeyCommit(item, user), hash), DataEntry(getKeyCommitsCount(item), (commits + 1)), DataEntry(getKeyItemStatus(item), if ((commits == VOTERS)) |
---|
161 | | - | then REVEAL |
---|
162 | | - | else VOTING)]) |
---|
163 | | - | } |
---|
164 | | - | |
---|
165 | | - | |
---|
166 | | - | |
---|
167 | | - | @Callable(i) |
---|
168 | | - | func voteReveal (item,vote,salt) = { |
---|
169 | | - | let user = toBase58String(i.caller.bytes) |
---|
170 | | - | let status = getValueItemStatus(item) |
---|
171 | | - | let newVoteCount = (getValueVoteCount(item, vote) + 1) |
---|
172 | | - | if ((toBase58String(sha256(toBytes((vote + salt)))) != getValueCommit(item, user))) |
---|
173 | | - | then throw("reveal data is not valid") |
---|
174 | | - | else if ((VOTERS > getValueCommitsCount(item))) |
---|
175 | | - | then throw("max num of voters hasn't reached yet") |
---|
176 | | - | else if ((getValueReveal(item, user) != NONE)) |
---|
177 | | - | then throw("user has already participated") |
---|
178 | | - | else if (if ((status != VOTING)) |
---|
179 | | - | then (status != REVEAL) |
---|
180 | | - | else false) |
---|
181 | | - | then throw("wrong status") |
---|
182 | | - | else if (if ((vote != FEATURED)) |
---|
183 | | - | then (vote != DELISTED) |
---|
184 | | - | else false) |
---|
185 | | - | then throw("wrong vote") |
---|
186 | | - | else if (if ((status == FEATURED)) |
---|
187 | | - | then true |
---|
188 | | - | else (status == DELISTED)) |
---|
189 | | - | then throw("vote has finished") |
---|
190 | | - | else WriteSet([DataEntry(getKeyReveal(item, user), vote), DataEntry(getKeyVoteCount(item, vote), newVoteCount), DataEntry(getKeyItemStatus(item), if ((newVoteCount >= QUORUM)) |
---|
191 | | - | then vote |
---|
192 | | - | else REVEAL)]) |
---|
193 | | - | } |
---|
194 | | - | |
---|
195 | | - | |
---|
| 2 | + | {-# CONTENT_TYPE EXPRESSION #-} |
---|
| 3 | + | let expiration = 578400 |
---|
| 4 | + | match tx { |
---|
| 5 | + | case br: ReissueTransaction|BurnTransaction => |
---|
| 6 | + | let supplier = extract(assetInfo(br.assetId)) |
---|
| 7 | + | (supplier.issuerPublicKey == tx.senderPublicKey) |
---|
| 8 | + | case e: ExchangeTransaction => |
---|
| 9 | + | let withWaves = if (!(isDefined(e.sellOrder.assetPair.priceAsset))) |
---|
| 10 | + | then true |
---|
| 11 | + | else !(isDefined(e.sellOrder.assetPair.amountAsset)) |
---|
| 12 | + | if ((expiration >= height)) |
---|
| 13 | + | then withWaves |
---|
| 14 | + | else false |
---|
| 15 | + | case tr: TransferTransaction => |
---|
| 16 | + | (expiration >= height) |
---|
| 17 | + | case _ => |
---|
| 18 | + | false |
---|
| 19 | + | } |
---|