/
backend.ride
234 lines (230 loc) · 8.89 KB
/
backend.ride
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
let NONE = "none"
func getNumberByKey(key: String) = {
let num = match getInteger(this, key) {
case a:Int => a
case _ => 0
}
num
}
func getStrByKey(key: String) = {
let str = match getString(this, key) {
case a:String => a
case _ => NONE
}
str
}
func getKeyCommentDonation(comment: String) = {
comment + "_donation"
}
func getValueCommentDonation(comment:String) = {
getNumberByKey(getKeyCommentDonation(comment))
}
func getKeyKolinerCommentCounter(koliner: String, comment: String) = {
comment + "_" + koliner + "_cnt"
}
func getValueKolinerCommentCounter(koliner: String, comment: String) = {
getNumberByKey(getKeyKolinerCommentCounter(koliner, comment))
}
func getKeyComment(author: String, title: String) = {
"comment_" + toBase58String(sha256(toBytes(author + title)))
}
func getKeyCommentData(comment: String) = {
comment + "_data"
}
func getKeyCommentAuthor(comment: String) = {
comment + "_owner"
}
func getValueCommentAuthor(comment: String) = {
getStrByKey(getKeyCommentAuthor(comment))
}
func getKeyBalanceAuthor(account: String) = {
account + "_balance"
}
func getValueBalanceAuthor(account: String) = {
getNumberByKey(getKeyBalanceAuthor(account))
}
#let VOTERS = 3
#let QUORUM = 2
let VOTING = "voting"
#let REVEAL = "reveal"
#let FEATURED = "featured"
#let DELISTED = "delisted"
#
#Comment: ThumbsUp-Reveal Voting keys:
#
func getKeyThumbsUp(comment: String, koliner: String) = {
comment + "_" + koliner + "_thumbsup"
}
func getValueThumbsUp(comment: String, koliner: String) = {
getStrByKey(getKeyThumbsUp(comment, koliner))
}
func getKeyThumbsUpsCount(comment: String) = {
comment + "_thumbsupcnt"
}
func getValueThumbsUpsCount(comment: String) = {
getNumberByKey(getKeyThumbsUpsCount(comment))
}
func getKeyThumbsDown(comment: String, koliner: String) = {
comment + "_" + koliner + "_thumbsdown"
}
func getValueThumbsDown(comment: String, koliner: String) = {
getStrByKey(getKeyThumbsDown(comment, koliner))
}
func getKeyThumbsDownsCount(comment: String) = {
comment + "_thumbsdowncnt"
}
func getValueThumbsDownCount(comment: String) = {
getNumberByKey(getKeyThumbsDownsCount(comment))
}
##func getKeyReveal(comment: String, koliner: String) = {
## comment + "_" + koliner + "_reveal"
##}
##func getValueReveal(comment: String, koliner: String) = {
## getStrByKey(getKeyReveal(comment, koliner))
##}
func getKeyCommentStatus(comment: String) = {
comment + "_status"
}
func getValueCommentStatus(comment: String) = {
getStrByKey(getKeyCommentStatus(comment))
}
func getKeyVoteCount(comment: String, vote: String) = {
comment + "_res:" + vote
}
func getValueVoteCount(comment: String, vote: String) = {
getNumberByKey(getKeyVoteCount(comment, vote))
}
##Blacklisted function Oracle
func getExtAddressOracle() = {
extract(addressFromString("3N2j5qyw6oAZs6mbey6SqefmmTuFNLAKFHx"))
}
let BLACKLISTED = "blacklist"
func getExtKeyCommentWhiteListStatus(account: String) = {
account + "_verifier_status"
}
func getExtValueCommentWhitelistStatus(oracle: Address, account: String) = {
let str = match getString(oracle, getExtKeyCommentWhiteListStatus(account)){
case a: String => a
case _ => NONE
}
str
}
##Allows the account owner to update the dApp script
@Verifier(tx)
func verify() = {
match tx {
case d: SetScriptTransaction => true
case d: DataTransaction => true
case d: TransferTransaction => true
case _ => false
}
}
#@Callable(i)
#func voteReveal(comment: String, vote: String, salt: String) = {
# let koliner = toBase58String(i.caller.bytes)
# let status = getValueCommentStatus(comment)
# let newVoteCount = getValueVoteCount(comment, vote) + 1
# if (toBase58String(sha256(toBytes(vote + salt))) != getValueThumbsUp(comment, koliner))
# then throw("reveal data not valid")
# else if (getValueThumbsUpsCount(comment) < VOTERS) then throw("max number of votes not reached")
# else if (getValueReveal(comment, koliner) != NONE) then throw("koliner already in the register")#
# else if (status != VOTING && status != REVEAL) then throw("wrong status")
# else if (vote != FEATURED && vote != DELISTED) then throw("wrong vote")
# else if (status == FEATURED || status == DELISTED) then throw("vote has finished")
# else{
# WriteSet([
# DataEntry(getKeyReveal(comment, koliner), vote),
# DataEntry(getKeyVoteCount(comment, vote), newVoteCount),
# DataEntry(getKeyCommentStatus(comment), if(newVoteCount >= QUORUM) then vote else REVEAL)
# ])
# }
#}
@Callable(i)
func voteThumbsUp(comment: String, hash:String) = {
let koliner = toBase58String(i.caller.bytes)
let thumbsups = getValueThumbsUpsCount(comment)
let status = getValueCommentStatus(comment)
# if ( thumbsups >= VOTERS) then throw("reached max num voters")
if (getValueThumbsUp(comment, koliner) != NONE) then throw("koliner has already voted")
else if (getKeyCommentAuthor(comment) == NONE) then throw("the comment that you are replying at does not exist")
else if (status !=NONE) then throw("voting is not possible")
else {
WriteSet([
DataEntry(getKeyThumbsUp(comment, koliner), hash),
DataEntry(getKeyThumbsUpsCount(comment), thumbsups + 1)
# DataEntry(getKeyCommentStatus(comment), if(thumbsups == VOTERS) then REVEAL else VOTING)
])
}
}
@Callable(i)
func voteThumbsDown(comment: String, hash:String) = {
let koliner = toBase58String(i.caller.bytes)
let thumbsdowns = getValueThumbsDownCount(comment)
let status = getValueCommentStatus(comment)
# if ( thumbsups >= VOTERS) then throw("reached max num voters")
if (getValueThumbsDown(comment, koliner) != NONE) then throw("koliner has already voted")
else if (getKeyCommentAuthor(comment) == NONE) then throw("the comment that you are replying at does not exist")
else if (status !=NONE) then throw("voting is not possible")
else {
WriteSet([
DataEntry(getKeyThumbsDown(comment, koliner), hash),
DataEntry(getKeyThumbsDownsCount(comment), thumbsdowns + 1)
# DataEntry(getKeyCommentStatus(comment), if(thumbsups == VOTERS) then REVEAL else VOTING)
])
}
}
@Callable(i)
func donate(comment:String) ={
let pmt = extract(i.payment)
if (isDefined(pmt.assetId != base58'FiKspxSpkpzT4pMUA9ccZkbJmVXTdu4JhFDXNNXr5noW')) then throw("just Kolin accepted (assetID: FiKspxSpkpzT4pMUA9ccZkbJmVXTdu4JhFDXNNXr5noW)")
else{
let kolinerAddress = toBase58String(i.caller.bytes)
let donation = getValueCommentDonation(comment)
let authorAddress = getValueCommentAuthor(comment)
if (pmt.amount < donation) then throw("you are trying to pay less than the minimum accepted, don't be greedy")
else if (pmt.amount > donation) then throw("thanks for your kind donation, but let's pay just what is needed")
else if (authorAddress == NONE) then throw("author not available in register")
else {
WriteSet([
DataEntry(getKeyKolinerCommentCounter(kolinerAddress, comment), getValueKolinerCommentCounter(kolinerAddress, comment) +1),
DataEntry(getKeyBalanceAuthor(authorAddress), getValueBalanceAuthor(authorAddress) + pmt.amount)
])
}
}
}
@Callable(i)
func withdraw() = {
let authorAddress = toBase58String(i.caller.bytes)
let balance = getValueBalanceAuthor(authorAddress)
if (balance <= 0)
then throw("you don't have enough balance to withdraw")
else ScriptResult(
WriteSet([
DataEntry(getKeyBalanceAuthor(authorAddress), 0)
]),
TransferSet([
ScriptTransfer(
addressFromStringValue(authorAddress), balance, unit
)])
)
}
@Callable(i)
func addComment(title: String, donation: Int, data: String) = {
let authorAddress = toBase58String(i.caller.bytes)
let comment = getKeyComment(authorAddress, title)
let authorwhitelist = getExtValueCommentWhitelistStatus(getExtAddressOracle(), authorAddress)
if ( donation <= 0 ) then throw("amount lower than comment donation")
else if ( getValueCommentAuthor(comment) != NONE ) then throw("this comment already exist")
else if (authorwhitelist == BLACKLISTED) then throw("author's account has been blacklisted")
else {
WriteSet([
DataEntry(getKeyCommentAuthor(comment), authorAddress),
DataEntry(getKeyCommentDonation(comment), donation),
DataEntry(getKeyCommentData(comment), data),
DataEntry(getExtKeyCommentWhiteListStatus(authorAddress), authorwhitelist)
])
}
}