1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let separator = "__" |
---|
5 | 5 | | |
---|
6 | 6 | | func keyWxAssetId () = makeString(["%s", "wxAssetId"], separator) |
---|
7 | 7 | | |
---|
8 | 8 | | |
---|
9 | 9 | | let keyBackendPublicKey = makeString(["%s", "backendPublicKey"], separator) |
---|
10 | 10 | | |
---|
11 | 11 | | func keyExistsReferrerToReferral (programName,referrerAddress,referralAddress) = makeString(["%s%s%s%s", "existsReferrerToReferral", programName, referrerAddress, referralAddress], separator) |
---|
12 | 12 | | |
---|
13 | 13 | | |
---|
14 | 14 | | func keyClaimedReferrer (programName,claimerAddress) = makeString(["%s%s%s", "claimedReferrer", programName, claimerAddress], separator) |
---|
15 | 15 | | |
---|
16 | 16 | | |
---|
17 | 17 | | func keyClaimedReferral (programName,claimerAddress) = makeString(["%s%s%s", "claimedReferral", programName, claimerAddress], separator) |
---|
18 | 18 | | |
---|
19 | 19 | | |
---|
20 | 20 | | func keyUnclaimedReferrer (programName,claimerAddress) = makeString(["%s%s%s", "unclaimedReferrer", programName, claimerAddress], separator) |
---|
21 | 21 | | |
---|
22 | 22 | | |
---|
23 | 23 | | func keyUnclaimedReferral (programName,claimerAddress) = makeString(["%s%s%s", "unclaimedReferral", programName, claimerAddress], separator) |
---|
24 | 24 | | |
---|
25 | 25 | | |
---|
26 | 26 | | func keyReferrer (programName,referralAddress) = makeString(["%s%s%s", "referrer", programName, referralAddress], separator) |
---|
27 | 27 | | |
---|
28 | 28 | | |
---|
29 | 29 | | func keyProgramName (programName) = makeString(["%s%s", "programName", programName], separator) |
---|
30 | 30 | | |
---|
31 | 31 | | |
---|
32 | 32 | | func keyTotalReferralCount (programName,referrerAddress) = makeString(["%s%s%s", "totalReferralCount", programName, referrerAddress], separator) |
---|
33 | 33 | | |
---|
34 | 34 | | |
---|
35 | 35 | | func keyActiveReferralCount (programName,referrerAddress) = makeString(["%s%s%s", "activeReferralCount", programName, referrerAddress], separator) |
---|
36 | 36 | | |
---|
37 | 37 | | |
---|
38 | 38 | | func keyIsReferralActive (programName,referralAddress) = makeString(["%s%s%s", "activeReferral", programName, referralAddress], separator) |
---|
39 | 39 | | |
---|
40 | 40 | | |
---|
41 | 41 | | func keyClaimedTotalAddress (address) = makeString(["%s%s", "claimedTotalAddress", address], separator) |
---|
42 | 42 | | |
---|
43 | 43 | | |
---|
44 | 44 | | func keyUnclaimedTotalAddress (address) = makeString(["%s%s", "unclaimedTotalAddress", address], separator) |
---|
45 | 45 | | |
---|
46 | 46 | | |
---|
47 | 47 | | func keyClaimedTotal (programName) = makeString(["%s%s", "claimedTotal", programName], separator) |
---|
48 | 48 | | |
---|
49 | 49 | | |
---|
50 | 50 | | func keyRewardsTotal (programName) = makeString(["%s%s", "rewardsTotal", programName], separator) |
---|
51 | 51 | | |
---|
52 | 52 | | |
---|
53 | 53 | | func keyRewardAssetId (programName) = makeString(["%s%s", "rewardAssetId", programName], separator) |
---|
54 | 54 | | |
---|
55 | 55 | | |
---|
56 | 56 | | func keyTreasuryContract (programName) = makeString(["%s%s", "treasuryContract", programName], separator) |
---|
57 | 57 | | |
---|
58 | 58 | | |
---|
59 | 59 | | func keyImplementationContract (programName) = makeString(["%s%s", "implementationContract", programName], separator) |
---|
60 | 60 | | |
---|
61 | 61 | | |
---|
62 | 62 | | func keyAllReferralPrograms (address) = makeString(["%s%s", "allReferralPrograms", address], separator) |
---|
63 | 63 | | |
---|
64 | 64 | | |
---|
65 | 65 | | func keyLastIncUnclaimedWithPaymentCallBlock (address) = makeString(["%s%s", "lastIncUnclaimedWithPaymentCallBlock", address], separator) |
---|
66 | 66 | | |
---|
67 | 67 | | |
---|
68 | 68 | | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
69 | 69 | | |
---|
70 | 70 | | |
---|
71 | 71 | | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
72 | 72 | | |
---|
73 | 73 | | |
---|
74 | 74 | | func keyClaimHistory (programName,userAddress,transactionId,type) = makeString(["%s%s%s%s%s", "history", type, programName, userAddress, transactionId], separator) |
---|
75 | 75 | | |
---|
76 | 76 | | |
---|
77 | 77 | | func formatClaimHistory (amount) = makeString(["%d%d%d", toString(lastBlock.height), toString(lastBlock.timestamp), toString(amount)], separator) |
---|
78 | 78 | | |
---|
79 | 79 | | |
---|
80 | 80 | | func throwErr (msg) = throw(makeString(["referral.ride:", msg], " ")) |
---|
81 | 81 | | |
---|
82 | 82 | | |
---|
83 | 83 | | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
84 | 84 | | case s: String => |
---|
85 | 85 | | fromBase58String(s) |
---|
86 | 86 | | case _: Unit => |
---|
87 | 87 | | unit |
---|
88 | 88 | | case _ => |
---|
89 | 89 | | throw("Match error") |
---|
90 | 90 | | } |
---|
91 | 91 | | |
---|
92 | 92 | | |
---|
93 | 93 | | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
94 | 94 | | case s: String => |
---|
95 | 95 | | fromBase58String(s) |
---|
96 | 96 | | case _: Unit => |
---|
97 | 97 | | unit |
---|
98 | 98 | | case _ => |
---|
99 | 99 | | throw("Match error") |
---|
100 | 100 | | } |
---|
101 | 101 | | |
---|
102 | 102 | | |
---|
103 | 103 | | func mustManager (i) = { |
---|
104 | 104 | | let pd = throwErr("permission denied") |
---|
105 | 105 | | match managerPublicKeyOrUnit() { |
---|
106 | 106 | | case pk: ByteVector => |
---|
107 | 107 | | if ((i.callerPublicKey == pk)) |
---|
108 | 108 | | then true |
---|
109 | 109 | | else pd |
---|
110 | 110 | | case _: Unit => |
---|
111 | 111 | | if ((i.caller == this)) |
---|
112 | 112 | | then true |
---|
113 | 113 | | else pd |
---|
114 | 114 | | case _ => |
---|
115 | 115 | | throw("Match error") |
---|
116 | 116 | | } |
---|
117 | 117 | | } |
---|
118 | 118 | | |
---|
119 | 119 | | |
---|
120 | 120 | | func updateAllReferralPrograms (address,programName) = { |
---|
121 | 121 | | let allReferralPrograms = valueOrElse(getString(keyAllReferralPrograms(address)), "") |
---|
122 | 122 | | let updatedAllReferralPrograms = if (contains(allReferralPrograms, programName)) |
---|
123 | 123 | | then nil |
---|
124 | 124 | | else { |
---|
125 | 125 | | let newAllReferralPrograms = if ((allReferralPrograms == "")) |
---|
126 | 126 | | then programName |
---|
127 | 127 | | else makeString([allReferralPrograms, programName], separator) |
---|
128 | 128 | | [StringEntry(keyAllReferralPrograms(address), newAllReferralPrograms)] |
---|
129 | 129 | | } |
---|
130 | 130 | | updatedAllReferralPrograms |
---|
131 | 131 | | } |
---|
132 | 132 | | |
---|
133 | 133 | | |
---|
134 | 134 | | @Callable(i) |
---|
135 | 135 | | func claimInternal (programName,claimerAddress,isImplementationContract) = { |
---|
136 | 136 | | let checkProgramExists = if (valueOrElse(getBoolean(keyProgramName(programName)), false)) |
---|
137 | 137 | | then true |
---|
138 | 138 | | else throwErr("program does not exist") |
---|
139 | 139 | | if ((checkProgramExists == checkProgramExists)) |
---|
140 | 140 | | then { |
---|
141 | 141 | | let $t048215758 = if (isImplementationContract) |
---|
142 | 142 | | then $Tuple5(valueOrElse(getInteger(keyClaimedReferral(programName, claimerAddress)), 0), valueOrElse(getInteger(keyUnclaimedReferral(programName, claimerAddress)), 0), keyClaimedReferral(programName, claimerAddress), keyUnclaimedReferral(programName, claimerAddress), keyClaimHistory(programName, claimerAddress, toBase58String(i.transactionId), "claimReferral")) |
---|
143 | 143 | | else $Tuple5(valueOrElse(getInteger(keyClaimedReferrer(programName, claimerAddress)), 0), valueOrElse(getInteger(keyUnclaimedReferrer(programName, claimerAddress)), 0), keyClaimedReferrer(programName, claimerAddress), keyUnclaimedReferrer(programName, claimerAddress), keyClaimHistory(programName, claimerAddress, toBase58String(i.transactionId), "claimReferrer")) |
---|
144 | 144 | | let claimerClaimed = $t048215758._1 |
---|
145 | 145 | | let claimerUnclaimed = $t048215758._2 |
---|
146 | 146 | | let keyClaimed = $t048215758._3 |
---|
147 | 147 | | let keyUnclaimed = $t048215758._4 |
---|
148 | 148 | | let claimHistoryKey = $t048215758._5 |
---|
149 | 149 | | let claimedTotal = valueOrElse(getInteger(keyClaimedTotal(programName)), 0) |
---|
150 | 150 | | let treasuryContract = value(addressFromString(value(getString(keyTreasuryContract(programName))))) |
---|
151 | 151 | | let rewardAssetId = fromBase58String(value(getString(keyRewardAssetId(programName)))) |
---|
152 | 152 | | let claimedTotalAddress = valueOrElse(getInteger(keyClaimedTotalAddress(claimerAddress)), 0) |
---|
153 | 153 | | let unclaimedTotalAddress = valueOrElse(getInteger(keyUnclaimedTotalAddress(claimerAddress)), 0) |
---|
154 | 154 | | if (if (isImplementationContract) |
---|
155 | 155 | | then (claimerUnclaimed == 0) |
---|
156 | 156 | | else false) |
---|
157 | 157 | | then $Tuple2(nil, 0) |
---|
158 | 158 | | else { |
---|
159 | 159 | | let checkCanClaim = if ((claimerUnclaimed > 0)) |
---|
160 | 160 | | then true |
---|
161 | 161 | | else throwErr("nothing to claim") |
---|
162 | 162 | | if ((checkCanClaim == checkCanClaim)) |
---|
163 | 163 | | then { |
---|
164 | 164 | | let newClaimerClaimed = (claimerClaimed + claimerUnclaimed) |
---|
165 | 165 | | let newClaimedTotal = (claimedTotal + claimerUnclaimed) |
---|
166 | 166 | | let newClaimedTotalAddress = (claimedTotalAddress + claimerUnclaimed) |
---|
167 | 167 | | let newUnclaimedTotalAddress = (unclaimedTotalAddress - claimerUnclaimed) |
---|
168 | 168 | | let balanceBefore = value(assetBalance(this, rewardAssetId)) |
---|
169 | 169 | | if ((balanceBefore == balanceBefore)) |
---|
170 | 170 | | then { |
---|
171 | 171 | | let res = invoke(treasuryContract, "withdrawReferralReward", [claimerUnclaimed], nil) |
---|
172 | 172 | | if ((res == res)) |
---|
173 | 173 | | then { |
---|
174 | 174 | | let balanceAfter = value(assetBalance(this, rewardAssetId)) |
---|
175 | 175 | | if ((balanceAfter == balanceAfter)) |
---|
176 | 176 | | then { |
---|
177 | 177 | | let balanceDiff = (balanceAfter - balanceBefore) |
---|
178 | 178 | | if ((balanceDiff == balanceDiff)) |
---|
179 | 179 | | then { |
---|
180 | 180 | | let checkBalance = if ((balanceDiff == claimerUnclaimed)) |
---|
181 | 181 | | then true |
---|
182 | 182 | | else throwErr("insufficient balance on referral contract") |
---|
183 | 183 | | if ((checkBalance == checkBalance)) |
---|
184 | 184 | | then $Tuple2([IntegerEntry(keyClaimed, newClaimerClaimed), IntegerEntry(keyUnclaimed, 0), IntegerEntry(keyClaimedTotal(programName), newClaimedTotal), IntegerEntry(keyClaimedTotalAddress(claimerAddress), newClaimedTotalAddress), IntegerEntry(keyUnclaimedTotalAddress(claimerAddress), newUnclaimedTotalAddress), ScriptTransfer(addressFromStringValue(claimerAddress), claimerUnclaimed, rewardAssetId), StringEntry(claimHistoryKey, formatClaimHistory(claimerUnclaimed))], claimerUnclaimed) |
---|
185 | 185 | | else throw("Strict value is not equal to itself.") |
---|
186 | 186 | | } |
---|
187 | 187 | | else throw("Strict value is not equal to itself.") |
---|
188 | 188 | | } |
---|
189 | 189 | | else throw("Strict value is not equal to itself.") |
---|
190 | 190 | | } |
---|
191 | 191 | | else throw("Strict value is not equal to itself.") |
---|
192 | 192 | | } |
---|
193 | 193 | | else throw("Strict value is not equal to itself.") |
---|
194 | 194 | | } |
---|
195 | 195 | | else throw("Strict value is not equal to itself.") |
---|
196 | 196 | | } |
---|
197 | 197 | | } |
---|
198 | 198 | | else throw("Strict value is not equal to itself.") |
---|
199 | 199 | | } |
---|
200 | 200 | | |
---|
201 | 201 | | |
---|
202 | 202 | | |
---|
203 | 203 | | @Callable(i) |
---|
204 | 204 | | func claimBulkInternal (claimer,programNames,currentIter) = { |
---|
205 | 205 | | let checkCaller = if ((i.caller == this)) |
---|
206 | 206 | | then true |
---|
207 | 207 | | else throwErr("permission denied") |
---|
208 | 208 | | if ((checkCaller == checkCaller)) |
---|
209 | 209 | | then if ((currentIter == size(programNames))) |
---|
210 | 210 | | then nil |
---|
211 | 211 | | else { |
---|
212 | 212 | | let programName = programNames[currentIter] |
---|
213 | 213 | | let programUnclaimed = valueOrElse(getInteger(keyUnclaimedReferrer(programName, claimer)), 0) |
---|
214 | 214 | | let claimHistoryKey = keyClaimHistory(programName, claimer, toBase58String(i.transactionId), "claimBulk") |
---|
215 | 215 | | let claimBulkInternal = invoke(this, "claimBulkInternal", [claimer, programNames, (currentIter + 1)], nil) |
---|
216 | 216 | | if ((claimBulkInternal == claimBulkInternal)) |
---|
217 | 217 | | then { |
---|
218 | 218 | | let unclaimedReferrer = valueOrElse(getInteger(keyUnclaimedReferrer(programName, claimer)), 0) |
---|
219 | 219 | | let claim = if ((unclaimedReferrer > 0)) |
---|
220 | 220 | | then invoke(this, "claimInternal", [programName, claimer, false], nil) |
---|
221 | 221 | | else unit |
---|
222 | 222 | | if ((claim == claim)) |
---|
223 | 223 | | then $Tuple2(nil, [claimBulkInternal, claim]) |
---|
224 | 224 | | else throw("Strict value is not equal to itself.") |
---|
225 | 225 | | } |
---|
226 | 226 | | else throw("Strict value is not equal to itself.") |
---|
227 | 227 | | } |
---|
228 | 228 | | else throw("Strict value is not equal to itself.") |
---|
229 | 229 | | } |
---|
230 | 230 | | |
---|
231 | 231 | | |
---|
232 | 232 | | |
---|
233 | 233 | | @Callable(i) |
---|
234 | 234 | | func incUnclaimedWithPaymentInternal (paymentAmounts,programName,referrerAddresses,currentIter,resAmount) = { |
---|
235 | 235 | | let checkCaller = if ((i.caller == this)) |
---|
236 | 236 | | then true |
---|
237 | 237 | | else throwErr("permission denied") |
---|
238 | 238 | | if ((checkCaller == checkCaller)) |
---|
239 | 239 | | then { |
---|
240 | 240 | | let argsComparison = (size(paymentAmounts) == size(referrerAddresses)) |
---|
241 | 241 | | let check = if (argsComparison) |
---|
242 | 242 | | then true |
---|
243 | 243 | | else throwErr("lists in args must be same size") |
---|
244 | 244 | | if ((check == check)) |
---|
245 | 245 | | then if ((currentIter == size(paymentAmounts))) |
---|
246 | 246 | | then $Tuple2(nil, resAmount) |
---|
247 | 247 | | else { |
---|
248 | 248 | | let referrerAddress = referrerAddresses[currentIter] |
---|
249 | 249 | | let unclaimedTotalAddress = valueOrElse(getInteger(keyUnclaimedTotalAddress(referrerAddress)), 0) |
---|
250 | 250 | | let referrerUnclaimed = valueOrElse(getInteger(keyUnclaimedReferrer(programName, referrerAddress)), 0) |
---|
251 | 251 | | let rewardsTotal = valueOrElse(getInteger(keyRewardsTotal(programName)), 0) |
---|
252 | 252 | | let newUnclaimedTotalAddress = (unclaimedTotalAddress + paymentAmounts[currentIter]) |
---|
253 | 253 | | let newReferrerUnclaimed = (referrerUnclaimed + paymentAmounts[currentIter]) |
---|
254 | 254 | | let newResAmount = (resAmount + paymentAmounts[currentIter]) |
---|
255 | 255 | | let inv = invoke(this, "incUnclaimedWithPaymentInternal", [paymentAmounts, programName, referrerAddresses, (currentIter + 1), newResAmount], nil) |
---|
256 | 256 | | if ((inv == inv)) |
---|
257 | 257 | | then { |
---|
258 | 258 | | let updatedAllReferrerPrograms = updateAllReferralPrograms(referrerAddress, programName) |
---|
259 | 259 | | $Tuple2(([IntegerEntry(keyUnclaimedTotalAddress(referrerAddress), newUnclaimedTotalAddress), IntegerEntry(keyUnclaimedReferrer(programName, referrerAddress), newReferrerUnclaimed), IntegerEntry(keyRewardsTotal(programName), (rewardsTotal + paymentAmounts[currentIter]))] ++ updatedAllReferrerPrograms), inv) |
---|
260 | 260 | | } |
---|
261 | 261 | | else throw("Strict value is not equal to itself.") |
---|
262 | 262 | | } |
---|
263 | 263 | | else throw("Strict value is not equal to itself.") |
---|
264 | 264 | | } |
---|
265 | 265 | | else throw("Strict value is not equal to itself.") |
---|
266 | 266 | | } |
---|
267 | 267 | | |
---|
268 | 268 | | |
---|
269 | 269 | | |
---|
270 | 270 | | @Callable(i) |
---|
271 | 271 | | func createReferralProgram (programName,treasuryContract,implementationContract,rewardAssetId) = { |
---|
272 | 272 | | let checkCaller = mustManager(i) |
---|
273 | 273 | | if ((checkCaller == checkCaller)) |
---|
274 | 274 | | then { |
---|
275 | 275 | | let checkProgramName = if ((getBoolean(keyProgramName(programName)) == unit)) |
---|
276 | 276 | | then true |
---|
277 | 277 | | else throwErr("program name already exists") |
---|
278 | 278 | | if ((checkProgramName == checkProgramName)) |
---|
279 | 279 | | then { |
---|
280 | 280 | | let programNameIsValid = if (!(contains(programName, separator))) |
---|
281 | 281 | | then !(contains(programName, " ")) |
---|
282 | 282 | | else false |
---|
283 | 283 | | let checkProgramExists = if (programNameIsValid) |
---|
284 | 284 | | then true |
---|
285 | 285 | | else throwErr("invalid program name") |
---|
286 | 286 | | if ((checkProgramExists == checkProgramExists)) |
---|
287 | 287 | | then { |
---|
288 | 288 | | let checkRewardAsset = if ((assetInfo(fromBase58String(rewardAssetId)) != unit)) |
---|
289 | 289 | | then true |
---|
290 | 290 | | else throwErr("invalid reward asset id") |
---|
291 | 291 | | if ((checkRewardAsset == checkRewardAsset)) |
---|
292 | 292 | | then $Tuple2([BooleanEntry(keyProgramName(programName), true), StringEntry(keyTreasuryContract(programName), treasuryContract), StringEntry(keyImplementationContract(programName), implementationContract), StringEntry(keyRewardAssetId(programName), rewardAssetId), IntegerEntry(keyRewardsTotal(programName), 0)], unit) |
---|
293 | 293 | | else throw("Strict value is not equal to itself.") |
---|
294 | 294 | | } |
---|
295 | 295 | | else throw("Strict value is not equal to itself.") |
---|
296 | 296 | | } |
---|
297 | 297 | | else throw("Strict value is not equal to itself.") |
---|
298 | 298 | | } |
---|
299 | 299 | | else throw("Strict value is not equal to itself.") |
---|
300 | 300 | | } |
---|
301 | 301 | | |
---|
302 | 302 | | |
---|
303 | 303 | | |
---|
304 | 304 | | @Callable(i) |
---|
305 | 305 | | func createPair (programName,referrerAddress,referralAddress,signature) = { |
---|
306 | 306 | | let checkProgramExists = if (valueOrElse(getBoolean(keyProgramName(programName)), false)) |
---|
307 | 307 | | then true |
---|
308 | 308 | | else throwErr("program does not exist") |
---|
309 | 309 | | if ((checkProgramExists == checkProgramExists)) |
---|
310 | 310 | | then { |
---|
311 | 311 | | let pairReferrerToReferralExistCheck = if ((getBoolean(keyExistsReferrerToReferral(programName, referrerAddress, referralAddress)) == unit)) |
---|
312 | 312 | | then true |
---|
313 | 313 | | else throwErr("pair already exists") |
---|
314 | 314 | | if ((pairReferrerToReferralExistCheck == pairReferrerToReferralExistCheck)) |
---|
315 | 315 | | then { |
---|
316 | 316 | | let noRefSelf = if ((referrerAddress != referralAddress)) |
---|
317 | 317 | | then true |
---|
318 | 318 | | else throwErr("cannot refer to self") |
---|
319 | 319 | | if ((noRefSelf == noRefSelf)) |
---|
320 | 320 | | then { |
---|
321 | 321 | | let pair = toBytes(makeString([programName, referrerAddress, referralAddress], ":")) |
---|
322 | 322 | | let backendPublicKey = fromBase58String(value(getString(keyBackendPublicKey))) |
---|
323 | 323 | | let validateSignature = if (sigVerify(pair, signature, backendPublicKey)) |
---|
324 | 324 | | then true |
---|
325 | 325 | | else throwErr("bad signature") |
---|
326 | 326 | | if ((validateSignature == validateSignature)) |
---|
327 | 327 | | then { |
---|
328 | 328 | | let totalReferralCount = valueOrElse(getInteger(keyTotalReferralCount(programName, referrerAddress)), 0) |
---|
329 | 329 | | let newTotalReferralCount = (totalReferralCount + 1) |
---|
330 | 330 | | let updatedAllReferrerPrograms = updateAllReferralPrograms(referrerAddress, programName) |
---|
331 | 331 | | let updatedAllReferralPrograms = updateAllReferralPrograms(referralAddress, programName) |
---|
332 | 332 | | $Tuple2((([BooleanEntry(keyExistsReferrerToReferral(programName, referrerAddress, referralAddress), true), IntegerEntry(keyTotalReferralCount(programName, referrerAddress), newTotalReferralCount), StringEntry(keyReferrer(programName, referralAddress), referrerAddress)] ++ updatedAllReferrerPrograms) ++ updatedAllReferralPrograms), unit) |
---|
333 | 333 | | } |
---|
334 | 334 | | else throw("Strict value is not equal to itself.") |
---|
335 | 335 | | } |
---|
336 | 336 | | else throw("Strict value is not equal to itself.") |
---|
337 | 337 | | } |
---|
338 | 338 | | else throw("Strict value is not equal to itself.") |
---|
339 | 339 | | } |
---|
340 | 340 | | else throw("Strict value is not equal to itself.") |
---|
341 | 341 | | } |
---|
342 | 342 | | |
---|
343 | 343 | | |
---|
344 | 344 | | |
---|
345 | 345 | | @Callable(i) |
---|
346 | 346 | | func updateReferralActivity (programName,referralAddress,isActive) = { |
---|
347 | 347 | | let implementationContract = value(getString(keyImplementationContract(programName))) |
---|
348 | 348 | | let isReferralActive = (getBoolean(keyIsReferralActive(programName, referralAddress)) != unit) |
---|
349 | 349 | | let referrerAddress = value(getString(keyReferrer(programName, referralAddress))) |
---|
350 | 350 | | let checkCaller = if ((toString(i.caller) == implementationContract)) |
---|
351 | 351 | | then true |
---|
352 | 352 | | else throwErr("permission denied") |
---|
353 | 353 | | if ((checkCaller == checkCaller)) |
---|
354 | 354 | | then { |
---|
355 | 355 | | let update = if ((isReferralActive == isActive)) |
---|
356 | 356 | | then $Tuple2(nil, unit) |
---|
357 | 357 | | else { |
---|
358 | 358 | | let update = if ((isActive == true)) |
---|
359 | 359 | | then { |
---|
360 | 360 | | let newActiveReferralCount = (valueOrElse(getInteger(keyActiveReferralCount(programName, referrerAddress)), 0) + 1) |
---|
361 | 361 | | $Tuple2([BooleanEntry(keyIsReferralActive(programName, referralAddress), true), IntegerEntry(keyActiveReferralCount(programName, referrerAddress), newActiveReferralCount)], unit) |
---|
362 | 362 | | } |
---|
363 | 363 | | else { |
---|
364 | 364 | | let newActiveReferralCount = (valueOrElse(getInteger(keyActiveReferralCount(programName, referrerAddress)), 0) - 1) |
---|
365 | 365 | | $Tuple2([DeleteEntry(keyIsReferralActive(programName, referralAddress)), IntegerEntry(keyActiveReferralCount(programName, referrerAddress), newActiveReferralCount)], unit) |
---|
366 | 366 | | } |
---|
367 | 367 | | update |
---|
368 | 368 | | } |
---|
369 | 369 | | update |
---|
370 | 370 | | } |
---|
371 | 371 | | else throw("Strict value is not equal to itself.") |
---|
372 | 372 | | } |
---|
373 | 373 | | |
---|
374 | 374 | | |
---|
375 | 375 | | |
---|
376 | 376 | | @Callable(i) |
---|
377 | 377 | | func incUnclaimed (programName,referralAddress,referrerReward,referralReward) = { |
---|
378 | 378 | | let implementationContract = value(getString(keyImplementationContract(programName))) |
---|
379 | 379 | | let referrerAddress = value(getString(keyReferrer(programName, referralAddress))) |
---|
380 | 380 | | let checkCaller = if ((toString(i.caller) == implementationContract)) |
---|
381 | 381 | | then true |
---|
382 | 382 | | else throwErr("permission denied") |
---|
383 | 383 | | if ((checkCaller == checkCaller)) |
---|
384 | 384 | | then { |
---|
385 | 385 | | let checkProgram = if ((programName == "wxlock")) |
---|
386 | 386 | | then true |
---|
387 | 387 | | else throwErr("invalid program") |
---|
388 | 388 | | if ((checkProgram == checkProgram)) |
---|
389 | 389 | | then { |
---|
390 | 390 | | let checkReferrerInProgram = if (contains(valueOrElse(getString(keyAllReferralPrograms(referrerAddress)), ""), programName)) |
---|
391 | 391 | | then true |
---|
392 | 392 | | else throwErr(((("referrer not in program=" + programName) + " addr=") + referrerAddress)) |
---|
393 | 393 | | if ((checkReferrerInProgram == checkReferrerInProgram)) |
---|
394 | 394 | | then { |
---|
395 | 395 | | let checkReferralInProgram = if (contains(valueOrElse(getString(keyAllReferralPrograms(referralAddress)), ""), programName)) |
---|
396 | 396 | | then true |
---|
397 | 397 | | else throwErr(((("referral not in program=" + programName) + " addr=") + referralAddress)) |
---|
398 | 398 | | if ((checkReferralInProgram == checkReferralInProgram)) |
---|
399 | 399 | | then { |
---|
400 | 400 | | let unclaimedTotalReferrer = valueOrElse(getInteger(keyUnclaimedTotalAddress(referrerAddress)), 0) |
---|
401 | 401 | | let referrerUnclaimed = valueOrElse(getInteger(keyUnclaimedReferrer(programName, referrerAddress)), 0) |
---|
402 | 402 | | let unclaimedTotalReferral = valueOrElse(getInteger(keyUnclaimedTotalAddress(referralAddress)), 0) |
---|
403 | 403 | | let referralUnclaimed = valueOrElse(getInteger(keyUnclaimedReferral(programName, referralAddress)), 0) |
---|
404 | 404 | | let rewardsTotal = valueOrElse(getInteger(keyRewardsTotal(programName)), 0) |
---|
405 | 405 | | let newUnclaimedTotalReferrer = (unclaimedTotalReferrer + referrerReward) |
---|
406 | 406 | | let newReferrerUnclaimed = (referrerUnclaimed + referrerReward) |
---|
407 | 407 | | let newUnclaimedTotalReferral = (unclaimedTotalReferral + referralReward) |
---|
408 | 408 | | let newReferralUnclaimed = (referralUnclaimed + referralReward) |
---|
409 | 409 | | $Tuple2([IntegerEntry(keyUnclaimedTotalAddress(referrerAddress), newUnclaimedTotalReferrer), IntegerEntry(keyUnclaimedTotalAddress(referralAddress), newUnclaimedTotalReferral), IntegerEntry(keyUnclaimedReferrer(programName, referrerAddress), newReferrerUnclaimed), IntegerEntry(keyUnclaimedReferral(programName, referralAddress), newReferralUnclaimed), IntegerEntry(keyRewardsTotal(programName), ((rewardsTotal + referrerReward) + referralReward))], unit) |
---|
410 | 410 | | } |
---|
411 | 411 | | else throw("Strict value is not equal to itself.") |
---|
412 | 412 | | } |
---|
413 | 413 | | else throw("Strict value is not equal to itself.") |
---|
414 | 414 | | } |
---|
415 | 415 | | else throw("Strict value is not equal to itself.") |
---|
416 | 416 | | } |
---|
417 | 417 | | else throw("Strict value is not equal to itself.") |
---|
418 | 418 | | } |
---|
419 | 419 | | |
---|
420 | 420 | | |
---|
421 | 421 | | |
---|
422 | 422 | | @Callable(i) |
---|
423 | 423 | | func incUnclaimedWithPayment (programName,referrerAddresses) = { |
---|
424 | 424 | | let checkProgram = if ((programName == "wxSpotFee")) |
---|
425 | 425 | | then true |
---|
426 | 426 | | else throwErr("invalid program") |
---|
427 | 427 | | if ((checkProgram == checkProgram)) |
---|
428 | 428 | | then { |
---|
429 | 429 | | let callerAddress = toString(i.caller) |
---|
430 | 430 | | let lastCall = valueOrElse(getInteger(keyLastIncUnclaimedWithPaymentCallBlock(callerAddress)), 0) |
---|
431 | 431 | | let waitBlocks = ((lastCall - height) + 3) |
---|
432 | 432 | | let lastCallCheck = if (((height - 3) > lastCall)) |
---|
433 | 433 | | then true |
---|
434 | 434 | | else throwErr(makeString(["wait", toString(waitBlocks), "blocks"], " ")) |
---|
435 | 435 | | if ((lastCallCheck == lastCallCheck)) |
---|
436 | 436 | | then { |
---|
437 | 437 | | func map (accum,next) = { |
---|
438 | 438 | | let assetIdCheck = (toBase58String(value(next.assetId)) == value(getString(keyRewardAssetId(programName)))) |
---|
439 | 439 | | let amountCheck = if (assetIdCheck) |
---|
440 | 440 | | then true |
---|
441 | 441 | | else throwErr("invalid asset id") |
---|
442 | 442 | | if ((amountCheck == amountCheck)) |
---|
443 | 443 | | then (accum :+ next.amount) |
---|
444 | 444 | | else throw("Strict value is not equal to itself.") |
---|
445 | 445 | | } |
---|
446 | 446 | | |
---|
447 | 447 | | let payments = { |
---|
448 | 448 | | let $l = i.payments |
---|
449 | 449 | | let $s = size($l) |
---|
450 | 450 | | let $acc0 = nil |
---|
451 | 451 | | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
452 | 452 | | then $a |
---|
453 | 453 | | else map($a, $l[$i]) |
---|
454 | 454 | | |
---|
455 | 455 | | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
456 | 456 | | then $a |
---|
457 | 457 | | else throw("List size exceeds 10") |
---|
458 | 458 | | |
---|
459 | 459 | | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10) |
---|
460 | 460 | | } |
---|
461 | 461 | | let resAmount = { |
---|
462 | 462 | | let @ = invoke(this, "incUnclaimedWithPaymentInternal", [payments, programName, referrerAddresses, 0, 0], nil) |
---|
463 | 463 | | if ($isInstanceOf(@, "Int")) |
---|
464 | 464 | | then @ |
---|
465 | 465 | | else throw(($getType(@) + " couldn't be cast to Int")) |
---|
466 | 466 | | } |
---|
467 | 467 | | if ((resAmount == resAmount)) |
---|
468 | 468 | | then { |
---|
469 | 469 | | let treasuryContract = addressFromStringValue(value(getString(keyTreasuryContract(programName)))) |
---|
470 | 470 | | let rewardAssetId = fromBase58String(value(getString(keyRewardAssetId(programName)))) |
---|
471 | 471 | | [ScriptTransfer(treasuryContract, resAmount, rewardAssetId), IntegerEntry(keyLastIncUnclaimedWithPaymentCallBlock(callerAddress), height)] |
---|
472 | 472 | | } |
---|
473 | 473 | | else throw("Strict value is not equal to itself.") |
---|
474 | 474 | | } |
---|
475 | 475 | | else throw("Strict value is not equal to itself.") |
---|
476 | 476 | | } |
---|
477 | 477 | | else throw("Strict value is not equal to itself.") |
---|
478 | 478 | | } |
---|
479 | 479 | | |
---|
480 | 480 | | |
---|
481 | 481 | | |
---|
482 | 482 | | @Callable(i) |
---|
483 | 483 | | func claim (programName) = { |
---|
484 | 484 | | let checkProgramExists = if (valueOrElse(getBoolean(keyProgramName(programName)), false)) |
---|
485 | 485 | | then true |
---|
486 | 486 | | else throwErr("program does not exist") |
---|
487 | 487 | | if ((checkProgramExists == checkProgramExists)) |
---|
488 | 488 | | then { |
---|
489 | 489 | | let isImplementationContract = (toString(i.caller) == value(getString(keyImplementationContract(programName)))) |
---|
490 | 490 | | let claimerAddress = if (isImplementationContract) |
---|
491 | 491 | | then toString(i.originCaller) |
---|
492 | 492 | | else toString(i.caller) |
---|
493 | 493 | | let claimInternal = invoke(this, "claimInternal", [programName, claimerAddress, isImplementationContract], nil) |
---|
494 | 494 | | if ((claimInternal == claimInternal)) |
---|
495 | 495 | | then $Tuple2(nil, claimInternal) |
---|
496 | 496 | | else throw("Strict value is not equal to itself.") |
---|
497 | 497 | | } |
---|
498 | 498 | | else throw("Strict value is not equal to itself.") |
---|
499 | 499 | | } |
---|
500 | 500 | | |
---|
501 | 501 | | |
---|
502 | 502 | | |
---|
503 | 503 | | @Callable(i) |
---|
504 | 504 | | func claimBulk () = { |
---|
505 | 505 | | let callerAddress = toString(i.caller) |
---|
506 | 506 | | let callerProgramsStr = getString(keyAllReferralPrograms(toString(i.caller))) |
---|
507 | 507 | | let programsExists = if (isDefined(callerProgramsStr)) |
---|
508 | 508 | | then true |
---|
509 | 509 | | else throwErr("no referral programs") |
---|
510 | 510 | | if ((programsExists == programsExists)) |
---|
511 | 511 | | then { |
---|
512 | 512 | | let callerPrograms = split(value(callerProgramsStr), "__") |
---|
513 | 513 | | let bulkClaim = invoke(this, "claimBulkInternal", [callerAddress, callerPrograms, 0], nil) |
---|
514 | 514 | | if ((bulkClaim == bulkClaim)) |
---|
515 | 515 | | then $Tuple2(nil, bulkClaim) |
---|
516 | 516 | | else throw("Strict value is not equal to itself.") |
---|
517 | 517 | | } |
---|
518 | 518 | | else throw("Strict value is not equal to itself.") |
---|
519 | 519 | | } |
---|
520 | 520 | | |
---|
521 | 521 | | |
---|
522 | 522 | | |
---|
523 | 523 | | @Callable(i) |
---|
524 | 524 | | func claimREADONLY (programName,userAddress) = { |
---|
525 | 525 | | let claimerClaimed = valueOrElse(getInteger(keyClaimedReferrer(programName, userAddress)), 0) |
---|
526 | 526 | | let claimerUnclaimed = valueOrElse(getInteger(keyUnclaimedReferrer(programName, userAddress)), 0) |
---|
527 | 527 | | $Tuple2(nil, [claimerUnclaimed, claimerClaimed]) |
---|
528 | 528 | | } |
---|
529 | 529 | | |
---|
530 | 530 | | |
---|
531 | 531 | | |
---|
532 | 532 | | @Callable(i) |
---|
533 | 533 | | func claimBulkREADONLY (claimer) = { |
---|
534 | 534 | | let claimedTotalAddress = valueOrElse(getInteger(keyClaimedTotalAddress(claimer)), 0) |
---|
535 | 535 | | let unclaimedTotalAddress = valueOrElse(getInteger(keyUnclaimedTotalAddress(claimer)), 0) |
---|
536 | 536 | | $Tuple2(nil, [unclaimedTotalAddress, claimedTotalAddress]) |
---|
537 | 537 | | } |
---|
538 | 538 | | |
---|
539 | 539 | | |
---|
540 | 540 | | |
---|
541 | 541 | | @Callable(i) |
---|
542 | 542 | | func setTotalKeys (programName,address) = { |
---|
543 | 543 | | let claimedReferrer = valueOrElse(getInteger(keyClaimedReferrer(programName, address)), 0) |
---|
544 | 544 | | let claimedReferral = valueOrElse(getInteger(keyClaimedReferral(programName, address)), 0) |
---|
545 | 545 | | let unclaimedReferrer = valueOrElse(getInteger(keyUnclaimedReferrer(programName, address)), 0) |
---|
546 | 546 | | let unclaimedReferral = valueOrElse(getInteger(keyUnclaimedReferral(programName, address)), 0) |
---|
547 | 547 | | let claimedTotalAddress = (claimedReferrer + claimedReferral) |
---|
548 | 548 | | let unclaimedTotalAddress = (unclaimedReferrer + unclaimedReferral) |
---|
549 | 549 | | $Tuple2([IntegerEntry(keyClaimedTotalAddress(address), claimedTotalAddress), IntegerEntry(keyUnclaimedTotalAddress(address), unclaimedTotalAddress)], unit) |
---|
550 | 550 | | } |
---|
551 | 551 | | |
---|
552 | 552 | | |
---|
553 | 553 | | |
---|
554 | 554 | | @Callable(i) |
---|
555 | 555 | | func setManager (pendingManagerPublicKey) = { |
---|
556 | 556 | | let checkCaller = mustManager(i) |
---|
557 | 557 | | if ((checkCaller == checkCaller)) |
---|
558 | 558 | | then { |
---|
559 | 559 | | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
560 | 560 | | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
561 | 561 | | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] |
---|
562 | 562 | | else throw("Strict value is not equal to itself.") |
---|
563 | 563 | | } |
---|
564 | 564 | | else throw("Strict value is not equal to itself.") |
---|
565 | 565 | | } |
---|
566 | 566 | | |
---|
567 | 567 | | |
---|
568 | 568 | | |
---|
569 | 569 | | @Callable(i) |
---|
570 | 570 | | func confirmManager () = { |
---|
571 | 571 | | let pm = pendingManagerPublicKeyOrUnit() |
---|
572 | 572 | | let hasPM = if (isDefined(pm)) |
---|
573 | 573 | | then true |
---|
574 | 574 | | else throwErr("no pending manager") |
---|
575 | 575 | | if ((hasPM == hasPM)) |
---|
576 | 576 | | then { |
---|
577 | 577 | | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
578 | 578 | | then true |
---|
579 | 579 | | else throwErr("you are not pending manager") |
---|
580 | 580 | | if ((checkPM == checkPM)) |
---|
581 | 581 | | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] |
---|
582 | 582 | | else throw("Strict value is not equal to itself.") |
---|
583 | 583 | | } |
---|
584 | 584 | | else throw("Strict value is not equal to itself.") |
---|
585 | 585 | | } |
---|
586 | 586 | | |
---|
587 | 587 | | |
---|
588 | 588 | | @Verifier(tx) |
---|
589 | 589 | | func verify () = { |
---|
590 | 590 | | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
591 | 591 | | case pk: ByteVector => |
---|
592 | 592 | | pk |
---|
593 | 593 | | case _: Unit => |
---|
594 | 594 | | tx.senderPublicKey |
---|
595 | 595 | | case _ => |
---|
596 | 596 | | throw("Match error") |
---|
597 | 597 | | } |
---|
598 | 598 | | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
599 | 599 | | } |
---|
600 | 600 | | |
---|