| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "getters": "{ |
| 8 | $isLicenseRevoked = $user=>{ |
| 9 | return NOT NOT var["revoked_" || $user]; |
| 10 | }; |
| 11 | $isLicenseActive = $user =>{ |
| 12 | return var["user_" || $user] >= timestamp AND NOT $isLicenseRevoked($user); |
| 13 | }; |
| 14 | $timeRemainingOf = $user=>{ |
| 15 | $stored = var["user_" || $user]; |
| 16 | if (NOT $stored) |
| 17 | return 0; |
| 18 | return max($stored - timestamp, 0); |
| 19 | }; |
| 20 | $licensePeriod = ()=>var["period"]; |
| 21 | $periodPrice = ()=>var["price"]; |
| 22 | $isTradeable = ()=>var["tradeable"]; |
| 23 | $maxPeriods = ()=>var["maxPeriods"] OTHERWISE 31556952 * 100; |
| 24 | $maxTopUpOf = ($user)=>{ |
| 25 | $expiry = var["user_" || $user]; |
| 26 | $currentPeriods = ($expiry - timestamp) / $licensePeriod(); |
| 27 | $maxPeriodsInAdvance = $maxPeriods(); |
| 28 | return $maxPeriodsInAdvance - $currentPeriods; |
| 29 | }; |
| 30 | $topUpPrice = ($periods)=>{ |
| 31 | $price = $periodPrice(); |
| 32 | return $price * $periods; |
| 33 | }; |
| 34 | $issuer = ()=>params.issuer; |
| 35 | $owner = ()=>var["owner"] OTHERWISE params.issuer; |
| 36 | $name = ()=>params.name; |
| 37 | $isHelper = ($user)=>var["helper_" || $user]; |
| 38 | }", |
| 39 | "init": "{ |
| 40 | $currentOwner = $owner(); |
| 41 | $trig = unit[trigger.initial_unit]; |
| 42 | $data = $trig.messages[[.app="data"]]["payload"]; |
| 43 | $method = $data.method; |
| 44 | if (NOT $method) |
| 45 | bounce("method field is mandatory " || json_stringify($trig)); |
| 46 | $KRANGA_ADDRESS = "ZIT3YPDTLGAO7MVO2ONNDMSEYS2VELX6"; |
| 47 | $SERVICE_FEE = 0.03; |
| 48 | $REFERRAL_FEE = 0.015; |
| 49 | |
| 50 | $currency = var["currency"]; |
| 51 | |
| 52 | $PERIOD_PRICE = var["price"]; |
| 53 | $IS_TRADEABLE = var["tradeable"]; |
| 54 | $ASSET = var["asset"]; |
| 55 | $PERIOD_DURATION = var["period"]; |
| 56 | $CALLER_TIME_REMAINING = $timeRemainingOf(trigger.initial_address); |
| 57 | $REFERRAL_ADDRESS = $data["r"] AND is_valid_address($data["r"]) ? $data["r"] : false; |
| 58 | }", |
| 59 | "messages": { |
| 60 | "cases": [ |
| 61 | { |
| 62 | "if": "{ |
| 63 | trigger.address == $currentOwner AND |
| 64 | ($method == "init" OR |
| 65 | $method == "payout" OR |
| 66 | $method == "changePrice" OR |
| 67 | $method == "setTradeable" OR |
| 68 | $method == "setMaxPeriods" OR |
| 69 | $method == "setPeriod" OR |
| 70 | $method == "toggleHelper" OR |
| 71 | $method == "transferOwnership") |
| 72 | }", |
| 73 | "messages": { |
| 74 | "cases": [ |
| 75 | { |
| 76 | "if": "{$method == "toggleHelper"}", |
| 77 | "init": "{ |
| 78 | if (NOT trigger.data["helper"]) |
| 79 | bounce("helper field is mandatory"); |
| 80 | if (NOT is_valid_address(trigger.data["helper"])) |
| 81 | bounce("Helper address is invalid"); |
| 82 | }", |
| 83 | "messages": [ |
| 84 | { |
| 85 | "app": "state", |
| 86 | "state": "{ |
| 87 | var["helper_" || trigger.data["helper"]] = NOT var["helper_" || trigger.data["helper"]]; |
| 88 | }" |
| 89 | } |
| 90 | ] |
| 91 | }, |
| 92 | { |
| 93 | "if": "{$method == "init"}", |
| 94 | "init": "{ |
| 95 | if (var["price"]) |
| 96 | bounce("The AA has already been initialized"); |
| 97 | }", |
| 98 | "messages": [ |
| 99 | { |
| 100 | "app": "asset", |
| 101 | "payload": { |
| 102 | "is_private": false, |
| 103 | "is_transferrable": true, |
| 104 | "auto_destroy": false, |
| 105 | "fixed_denominations": false, |
| 106 | "issued_by_definer_only": true, |
| 107 | "cosigned_by_definer": false, |
| 108 | "spender_attested": false |
| 109 | } |
| 110 | }, |
| 111 | { |
| 112 | "app": "state", |
| 113 | "init": "{ |
| 114 | if (NOT trigger.data["initialPrice"]) |
| 115 | bounce("initialPrice field is mandatory"); |
| 116 | if (NOT trigger.data["currency"] AND NOT is_valid_amount(trigger.data["initialPrice"])) |
| 117 | bounce("The price is not a valid amount"); |
| 118 | if (trigger.data["initialPrice"] < 0) |
| 119 | bounce("The initial price must be >= 0"); |
| 120 | if (trigger.data["maxPeriods"]){ |
| 121 | if (NOT is_integer(trigger.data["maxPeriods"])) |
| 122 | bounce("maxPeriods must be an integer"); |
| 123 | if (trigger.data["maxPeriods"] < 0) |
| 124 | bounce("maxPeriods must be a positive integer"); |
| 125 | } |
| 126 | if (NOT is_integer(trigger.data["period"])) |
| 127 | bounce ("period must be an integer"); |
| 128 | if (trigger.data["period"] < 0) |
| 129 | bounce("period must be positive"); |
| 130 | if (trigger.data["currency"] AND NOT asset[trigger.data["currency"]].exists) |
| 131 | bounce("That currency does not exist"); |
| 132 | }", |
| 133 | "state": "{ |
| 134 | var["price"] = trigger.data["initialPrice"]; |
| 135 | var["tradeable"] = NOT NOT trigger.data["tradeable"]; |
| 136 | var["period"] = trigger.data["period"]; |
| 137 | var["currency"] = trigger.data["currency"] OTHERWISE "base"; |
| 138 | var["asset"] = response_unit; |
| 139 | }" |
| 140 | } |
| 141 | ] |
| 142 | }, |
| 143 | { |
| 144 | "if": "{$method == "payout"}", |
| 145 | "messages": [ |
| 146 | { |
| 147 | "app": "payment", |
| 148 | "payload": { |
| 149 | "outputs": [ |
| 150 | { |
| 151 | "address": "{$currentOwner}" |
| 152 | } |
| 153 | ] |
| 154 | } |
| 155 | }, |
| 156 | { |
| 157 | "if": "{balance[$currency] AND $currency != "base"}", |
| 158 | "app": "payment", |
| 159 | "payload": { |
| 160 | "asset": "{$currency}", |
| 161 | "outputs": [ |
| 162 | { |
| 163 | "address": "{$currentOwner}" |
| 164 | } |
| 165 | ] |
| 166 | } |
| 167 | }, |
| 168 | { |
| 169 | "if": "{trigger.data["asset"] AND balance[trigger.data["asset"]]}", |
| 170 | "app": "payment", |
| 171 | "payload": { |
| 172 | "asset": "{trigger.data["asset"]}", |
| 173 | "outputs": [ |
| 174 | { |
| 175 | "address": "{$currentOwner}" |
| 176 | } |
| 177 | ] |
| 178 | } |
| 179 | } |
| 180 | ] |
| 181 | }, |
| 182 | { |
| 183 | "if": "{$method == "changePrice"}", |
| 184 | "init": "{ |
| 185 | if (NOT trigger.data["price"]) |
| 186 | bounce("price field is mandatory"); |
| 187 | if (trigger.data["price"] < 0) |
| 188 | bounce("price must be positive"); |
| 189 | else if (NOT is_valid_amount(trigger.data["price"])) |
| 190 | bounce("Price is not a valid amount"); |
| 191 | |
| 192 | }", |
| 193 | "messages": [ |
| 194 | { |
| 195 | "app": "state", |
| 196 | "state": "{ |
| 197 | var["price"] = trigger.data["price"]; |
| 198 | if (trigger.data["currency"]){ |
| 199 | var["currency"] = trigger.data["currency"]; |
| 200 | } |
| 201 | }" |
| 202 | } |
| 203 | ] |
| 204 | }, |
| 205 | { |
| 206 | "if": "{$method == "setMaxPeriods"}", |
| 207 | "init": "{ |
| 208 | if (NOT trigger.data["maxPeriods"]) |
| 209 | bounce("maxPeriods field is mandatory"); |
| 210 | if (trigger.data["maxPeriods"] < 0) |
| 211 | bounce("maxPeriods must be positive"); |
| 212 | if (NOT is_integer(trigger.data["maxPeriods"])) |
| 213 | bounce("maxPeriods must be an integer"); |
| 214 | }", |
| 215 | "messages": [ |
| 216 | { |
| 217 | "app": "state", |
| 218 | "state": "{ |
| 219 | var["maxPeriods"] = trigger.data["maxPeriods"]; |
| 220 | }" |
| 221 | } |
| 222 | ] |
| 223 | }, |
| 224 | { |
| 225 | "if": "{$method == "setTradeable"}", |
| 226 | "messages": [ |
| 227 | { |
| 228 | "app": "state", |
| 229 | "state": "{ |
| 230 | var["tradeable"] = NOT $IS_TRADEABLE; |
| 231 | }" |
| 232 | } |
| 233 | ] |
| 234 | }, |
| 235 | { |
| 236 | "if": "{$method == "transferOwnership"}", |
| 237 | "init": "{ |
| 238 | if (NOT trigger.data["address"]) |
| 239 | bounce("address field is mandatory"); |
| 240 | if (NOT is_valid_address(trigger.data["address"])) |
| 241 | bounce("The provided address is not a valid address"); |
| 242 | }", |
| 243 | "messages": [ |
| 244 | { |
| 245 | "app": "state", |
| 246 | "state": "{ |
| 247 | var["owner"] = trigger.data["address"]; |
| 248 | }" |
| 249 | } |
| 250 | ] |
| 251 | } |
| 252 | ] |
| 253 | } |
| 254 | }, |
| 255 | { |
| 256 | "if": "{ |
| 257 | ($isHelper(trigger.address) OR trigger.address == $currentOwner) AND |
| 258 | (trigger.data["method"] == "toggleRevoked") |
| 259 | }", |
| 260 | "messages": { |
| 261 | "cases": [ |
| 262 | { |
| 263 | "if": "{trigger.data["method"] == "toggleRevoked"}", |
| 264 | "init": "{ |
| 265 | if (NOT trigger.data["user"]) |
| 266 | bounce("user field is mandatory"); |
| 267 | if (NOT is_valid_address(trigger.data["user"])) |
| 268 | bounce("user field is not a valid address"); |
| 269 | }", |
| 270 | "messages": [ |
| 271 | { |
| 272 | "app": "state", |
| 273 | "state": "{ |
| 274 | var["revoked_" || trigger.data["user"]] = NOT var["revoked_" || trigger.data["user"]]; |
| 275 | }" |
| 276 | } |
| 277 | ] |
| 278 | } |
| 279 | ] |
| 280 | } |
| 281 | }, |
| 282 | { |
| 283 | "if": "{$method == "topup"}", |
| 284 | "init": "{ |
| 285 | $_periodsToBuy = trigger.output[[asset=$currency]].amount / $PERIOD_PRICE; |
| 286 | $periodsToBuy = floor($_periodsToBuy, 0); |
| 287 | $maxTopup = $maxTopUpOf(trigger.initial_address); |
| 288 | $cut = round(trigger.output[[asset=$currency]].amount * ($REFERRAL_ADDRESS ? $REFERRAL_FEE : $SERVICE_FEE), 0); |
| 289 | if ($periodsToBuy == 0) |
| 290 | bounce("That's not enough to buy even a single license period"); |
| 291 | if ($periodsToBuy > $maxTopup) |
| 292 | bounce("You can buy at most " || $maxTopup || " periods"); |
| 293 | }", |
| 294 | "messages": [ |
| 295 | { |
| 296 | "app": "payment", |
| 297 | "payload": { |
| 298 | "asset": "{$currency}", |
| 299 | "outputs": [ |
| 300 | { |
| 301 | "address": "{$KRANGA_ADDRESS}", |
| 302 | "amount": "{$cut}" |
| 303 | }, |
| 304 | { |
| 305 | "if": "{$REFERRAL_ADDRESS}", |
| 306 | "address": "{$data["r"]}", |
| 307 | "amount": "{$cut}" |
| 308 | } |
| 309 | ] |
| 310 | } |
| 311 | }, |
| 312 | { |
| 313 | "app": "state", |
| 314 | "state": "{ |
| 315 | var["user_" || trigger.initial_address] = timestamp + ($CALLER_TIME_REMAINING OTHERWISE 0) + $periodsToBuy * $PERIOD_DURATION; |
| 316 | }" |
| 317 | } |
| 318 | ] |
| 319 | }, |
| 320 | { |
| 321 | "if": "{$method == "buy"}", |
| 322 | "init": "{ |
| 323 | if (NOT $IS_TRADEABLE) |
| 324 | bounce("That license cannot be traded"); |
| 325 | $amount = floor(trigger.output[[asset=$currency]].amount / $PERIOD_PRICE); |
| 326 | if ($amount < 1) |
| 327 | bounce("You cannot buy less than 1 license period. You are buying " || trigger.output[[asset=$currency]].amount / $PERIOD_PRICE || " " || trigger.output[[asset=$currency]].amount || $PERIOD_PRICE); |
| 328 | $cut = round(trigger.output[[asset=$currency]].amount * ($REFERRAL_ADDRESS ? $REFERRAL_FEE : $SERVICE_FEE), 0); |
| 329 | }", |
| 330 | "messages": [ |
| 331 | { |
| 332 | "app": "payment", |
| 333 | "payload": { |
| 334 | "asset": "{$ASSET}", |
| 335 | "outputs": [ |
| 336 | { |
| 337 | "address": "{trigger.initial_address}", |
| 338 | "amount": "{$amount}" |
| 339 | } |
| 340 | ] |
| 341 | } |
| 342 | }, |
| 343 | { |
| 344 | "app": "payment", |
| 345 | "payload": { |
| 346 | "asset": "{$currency}", |
| 347 | "outputs": [ |
| 348 | { |
| 349 | "address": "{$KRANGA_ADDRESS}", |
| 350 | "amount": "{$cut}" |
| 351 | }, |
| 352 | { |
| 353 | "if": "{$REFERRAL_ADDRESS}", |
| 354 | "address": "{$REFERRAL_ADDRESS}", |
| 355 | "amount": "{$cut}" |
| 356 | } |
| 357 | ] |
| 358 | } |
| 359 | } |
| 360 | ] |
| 361 | }, |
| 362 | { |
| 363 | "if": "{$method == "deposit"}", |
| 364 | "init": "{ |
| 365 | $periods = trigger.output[[asset=$ASSET]].amount; |
| 366 | }", |
| 367 | "messages": [ |
| 368 | { |
| 369 | "app": "state", |
| 370 | "state": "{ |
| 371 | var["user_" || trigger.initial_address] = timestamp + ($CALLER_TIME_REMAINING OTHERWISE 0) + $periods * $PERIOD_DURATION; |
| 372 | }" |
| 373 | } |
| 374 | ] |
| 375 | }, |
| 376 | { |
| 377 | "if": "{$method == "tip"}", |
| 378 | "init": "{ |
| 379 | if (trigger.output[[asset!="base"]].asset == "ambigous") |
| 380 | bounce("You cannot tip several assets at the same time"); |
| 381 | if (trigger.output[[asset!="base"]].asset != 'none'){ |
| 382 | $assetCut = round(trigger.output[[asset!="base"]].amount * $SERVICE_FEE); |
| 383 | $asset = trigger.output[[asset!="base"]].asset; |
| 384 | $cut = round(trigger.output[[asset!="base"]].amount * $SERVICE_FEE); |
| 385 | } |
| 386 | else |
| 387 | $cut = round(trigger.output[[asset="base"]].amount * $SERVICE_FEE); |
| 388 | }", |
| 389 | "messages": [ |
| 390 | { |
| 391 | "app": "payment", |
| 392 | "payload": { |
| 393 | "outputs": [ |
| 394 | { |
| 395 | "amount": "{$cut}", |
| 396 | "address": "{$KRANGA_ADDRESS}" |
| 397 | } |
| 398 | ] |
| 399 | } |
| 400 | }, |
| 401 | { |
| 402 | "if": "{$asset}", |
| 403 | "app": "payment", |
| 404 | "payload": { |
| 405 | "asset": "{$asset}", |
| 406 | "outputs": [ |
| 407 | { |
| 408 | "amount": "{$assetCut}", |
| 409 | "address": "{$KRANGA_ADDRESS}" |
| 410 | } |
| 411 | ] |
| 412 | } |
| 413 | }, |
| 414 | { |
| 415 | "app": "state", |
| 416 | "state": "{ |
| 417 | if ($assetCut) |
| 418 | var["tip_" || $asset || "_" || trigger.initial_address] += trigger.output[[asset!="base"]].amount; |
| 419 | var["tip_base_" || trigger.initial_address] += trigger.output[[asset="base"]].amount; |
| 420 | }" |
| 421 | } |
| 422 | ] |
| 423 | } |
| 424 | ] |
| 425 | } |
| 426 | } |
| 427 | ] |