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