| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $BYTES_IN_GBYTE = 1000000000; |
| 9 | $USER_REGISTRY_ADDRESS = "7CBKTGRK5MTTA22XXC4G472ERRFVOOML"; |
| 10 | $CURRENCY_REGISTRY = "AO46ELAIDT2LX2RXVVABIABXNBKHRSCE"; |
| 11 | |
| 12 | if (NOT exists(trigger.data["method"])) |
| 13 | bounce("method field is mandatory"); |
| 14 | |
| 15 | $method = trigger.data["method"]; |
| 16 | |
| 17 | $spendableFunds = balance["base"] - storage_size; |
| 18 | $owner = var["owner"]; |
| 19 | $isHelper = var["helper_" || trigger.address]; |
| 20 | |
| 21 | /* |
| 22 | ** Provides information about the given license |
| 23 | */ |
| 24 | $getNFTInfo = $NFT=>{ |
| 25 | $unit = unit[$NFT]; |
| 26 | $info = var["FREE_" || $NFT] OTHERWISE var["NFT_" || $NFT]; |
| 27 | $transferrable = $unit.messages[[.app="asset"]]["payload"]["transferrable"]; |
| 28 | |
| 29 | if (NOT $transferrable) |
| 30 | return false; |
| 31 | |
| 32 | return $info || { |
| 33 | cap: $unit.messages[[.app="asset"]]["payload"]["cap"], |
| 34 | mintedAt: $unit.timestamp |
| 35 | }; |
| 36 | }; |
| 37 | $FEE = var["FEE"]; |
| 38 | }", |
| 39 | "messages": { |
| 40 | "cases": [ |
| 41 | { |
| 42 | "if": "{ |
| 43 | NOT $owner |
| 44 | }", |
| 45 | "messages": [ |
| 46 | { |
| 47 | "app": "state", |
| 48 | "state": "{ |
| 49 | var["owner"] = "IUU43O7TS2TBYKAPGKUARDZHOTAE275A"; |
| 50 | var["FEE"] = 0.04; |
| 51 | }" |
| 52 | } |
| 53 | ] |
| 54 | }, |
| 55 | { |
| 56 | "if": "{ |
| 57 | trigger.address == $owner |
| 58 | AND ($method == "payout" |
| 59 | OR $method == "stopSale" |
| 60 | OR $method == "transferOwnership" |
| 61 | OR $method == "setHelper" |
| 62 | OR $method == "reduceFee" |
| 63 | OR $method == "untrust" |
| 64 | OR $method == "delHelper") |
| 65 | }", |
| 66 | "messages": { |
| 67 | "cases": [ |
| 68 | { |
| 69 | "if": "{ |
| 70 | $method == "payout" |
| 71 | }", |
| 72 | "messages": [ |
| 73 | { |
| 74 | "app": "payment", |
| 75 | "payload": { |
| 76 | "asset": "base", |
| 77 | "outputs": [ |
| 78 | { |
| 79 | "address": "{trigger.address}", |
| 80 | "amount": "{$spendableFunds}" |
| 81 | } |
| 82 | ] |
| 83 | } |
| 84 | } |
| 85 | ] |
| 86 | }, |
| 87 | { |
| 88 | "if": "{ |
| 89 | $method == "untrust" |
| 90 | }", |
| 91 | "init": "{ |
| 92 | if (NOT trigger.data["user"]) |
| 93 | bounce("user field is mandatory"); |
| 94 | if (NOT is_valid_address(trigger.data["user"])) |
| 95 | bounce("user field is an invalid address"); |
| 96 | }", |
| 97 | "messages": [ |
| 98 | { |
| 99 | "app": "state", |
| 100 | "state": "{ |
| 101 | var["trusted_" || trigger.data["user"]] = false; |
| 102 | }" |
| 103 | } |
| 104 | ] |
| 105 | }, |
| 106 | { |
| 107 | "if": "{ |
| 108 | $method == "transferOwnership" |
| 109 | }", |
| 110 | "init": "{ |
| 111 | if (NOT exists(trigger.data["newOwner"])) |
| 112 | bounce("newOwner field is mandatory"); |
| 113 | if (NOT is_valid_address(trigger.data["newOwner"])) |
| 114 | bounce("newOwner field is not a valid address"); |
| 115 | }", |
| 116 | "messages": [ |
| 117 | { |
| 118 | "app": "payment", |
| 119 | "payload": { |
| 120 | "asset": "base", |
| 121 | "outputs": [ |
| 122 | { |
| 123 | "address": "{trigger.address}", |
| 124 | "amount": "{$spendableFunds}" |
| 125 | } |
| 126 | ] |
| 127 | } |
| 128 | }, |
| 129 | { |
| 130 | "app": "state", |
| 131 | "state": "{ |
| 132 | var["owner"] = trigger.data["newOwner"]; |
| 133 | }" |
| 134 | } |
| 135 | ] |
| 136 | }, |
| 137 | { |
| 138 | "if": "{ |
| 139 | $method == "reduceFee" |
| 140 | }", |
| 141 | "init": "{ |
| 142 | if (NOT exists(trigger.data["fee"])) |
| 143 | bounce("fee field is mandatory"); |
| 144 | if (trigger.data["fee"] <= 0) |
| 145 | bounce("Fee must be an strictly positive value"); |
| 146 | if (trigger.data["fee"] > $FEE) |
| 147 | bounce("You can only reduce the current fee"); |
| 148 | }", |
| 149 | "messages": [ |
| 150 | { |
| 151 | "app": "state", |
| 152 | "state": "{ |
| 153 | var["FEE"] = trigger.data["fee"]; |
| 154 | }" |
| 155 | } |
| 156 | ] |
| 157 | }, |
| 158 | { |
| 159 | "if": "{ |
| 160 | $method == "setHelper" |
| 161 | }", |
| 162 | "init": "{ |
| 163 | if (NOT exists(trigger.data["helper"])) |
| 164 | bounce("helper field is mandatory"); |
| 165 | if (NOT is_valid_address(trigger.data["helper"])) |
| 166 | bounce("helper address is invalid"); |
| 167 | }", |
| 168 | "messages": [ |
| 169 | { |
| 170 | "app": "state", |
| 171 | "state": "{ |
| 172 | var["helper_" || trigger.data["helper"]] = true; |
| 173 | }" |
| 174 | } |
| 175 | ] |
| 176 | }, |
| 177 | { |
| 178 | "if": "{ |
| 179 | $method == "delHelper" |
| 180 | }", |
| 181 | "init": "{ |
| 182 | if (NOT exists(trigger.data["helper"])) |
| 183 | bounce("helper field is mandatory"); |
| 184 | if (NOT is_valid_address(trigger.data["helper"])) |
| 185 | bounce("helper address is invalid"); |
| 186 | }", |
| 187 | "messages": [ |
| 188 | { |
| 189 | "app": "state", |
| 190 | "state": "{ |
| 191 | var["helper_" || trigger.data["helper"]] = false; |
| 192 | }" |
| 193 | } |
| 194 | ] |
| 195 | } |
| 196 | ] |
| 197 | } |
| 198 | }, |
| 199 | { |
| 200 | "if": "{ |
| 201 | (trigger.address == $owner OR $isHelper) |
| 202 | AND $method == "trust" |
| 203 | }", |
| 204 | "init": "{ |
| 205 | if (NOT trigger.data["user"]) |
| 206 | bounce("user field is mandatory"); |
| 207 | if (NOT is_valid_address(trigger.data["user"])) |
| 208 | bounce("user field is an invalid address"); |
| 209 | }", |
| 210 | "messages": [ |
| 211 | { |
| 212 | "app": "state", |
| 213 | "state": "{ |
| 214 | var["trusted_" || trigger.data["user"]] = true; |
| 215 | }" |
| 216 | } |
| 217 | ] |
| 218 | }, |
| 219 | { |
| 220 | "if": "{ |
| 221 | (trigger.address == $owner OR $isHelper) |
| 222 | AND $method == "untrust" |
| 223 | }", |
| 224 | "init": "{ |
| 225 | if (NOT trigger.data["NFT"]) |
| 226 | bounce("NFT field is mandatory"); |
| 227 | }", |
| 228 | "messages": [ |
| 229 | { |
| 230 | "app": "state", |
| 231 | "state": "{ |
| 232 | var["FREE_" || trigger.data["NFT"]] = var["NFT_" || trigger.data["NFT"]]; |
| 233 | var["NFT_" || trigger.data["NFT"]] = false; |
| 234 | }" |
| 235 | } |
| 236 | ] |
| 237 | }, |
| 238 | { |
| 239 | "if": "{ |
| 240 | $method == "MINT" |
| 241 | }", |
| 242 | "init": "{ |
| 243 | if (NOT exists(trigger.data["amount"])) |
| 244 | bounce("amount field is mandatory. Set it to 0 for unlimited"); |
| 245 | |
| 246 | if ((NOT is_valid_amount(trigger.data["amount"])) AND trigger.data["amount"] != 0) |
| 247 | bounce("The amount of license copies to mint is not valid"); |
| 248 | |
| 249 | if (NOT exists(trigger.data["price"])) |
| 250 | bounce("price field is mandatory"); |
| 251 | |
| 252 | $currency = $CURRENCY_REGISTRY.$getCurrency(trigger.data["currency"]); |
| 253 | $profile = $USER_REGISTRY_ADDRESS.$profileOf(trigger.address); |
| 254 | |
| 255 | if (trigger.data["currency"]){ |
| 256 | if (NOT $currency) |
| 257 | bounce("The provided currency is not supported"); |
| 258 | } |
| 259 | |
| 260 | $price = trigger.data["currency"] |
| 261 | ? $CURRENCY_REGISTRY.$convert(trigger.data["price"], trigger.data["currency"]) |
| 262 | : trigger.data["price"]; |
| 263 | |
| 264 | if ($price <= 0) |
| 265 | bounce("price field must be higher than 0"); |
| 266 | |
| 267 | if (NOT exists(trigger.data["currency"])){ |
| 268 | if ($price < 20000) |
| 269 | bounce("The minium price is 20.000 bytes"); |
| 270 | if (NOT is_valid_amount($price)) |
| 271 | bounce("price is not valid"); |
| 272 | } |
| 273 | |
| 274 | if (exists(trigger.data["maxPeriods"])){ |
| 275 | if (NOT is_integer(trigger.data["maxPeriods"])) |
| 276 | bounce("maxPeriods must be an integer"); |
| 277 | |
| 278 | if (trigger.data["maxPeriods"] <= 0) |
| 279 | bounce("maxPeriods must be positive"); |
| 280 | |
| 281 | if (NOT exists(trigger.data["validity"])) |
| 282 | bounce("If you set maxPeriods you have to set validity too"); |
| 283 | } |
| 284 | |
| 285 | if (exists(trigger.data["validity"])){ |
| 286 | if (NOT is_integer(trigger.data["validity"])) |
| 287 | bounce("validity must be an integer"); |
| 288 | |
| 289 | if (trigger.data["validity"] <= 0) |
| 290 | bounce("You cannot make a license valid for 0s or less"); |
| 291 | } |
| 292 | |
| 293 | if (trigger.address == $owner OR $profile.isLicensor){ |
| 294 | if (NOT exists(trigger.data["ipfs"])) |
| 295 | bounce("ipfs field is mandatory"); |
| 296 | |
| 297 | if (NOT exists(trigger.data["title"])) |
| 298 | bounce("title field is mandatory"); |
| 299 | if (length(trigger.data["title"]) > 512) |
| 300 | bounce("The license title must be at most 512 characters long"); |
| 301 | |
| 302 | if (exists(trigger.data["ticker"])){ |
| 303 | if (length(trigger.data["ticker"] < 128)) |
| 304 | bounce("The length of the license ticker must be at most 128 characters long"); |
| 305 | } |
| 306 | |
| 307 | if (trigger.address == $owner){ |
| 308 | if (NOT exists(trigger.data["seller"])) |
| 309 | bounce("seller field is mandatory"); |
| 310 | |
| 311 | if (NOT is_valid_address(trigger.data["seller"])) |
| 312 | bounce("The seller address is not valid"); |
| 313 | } |
| 314 | |
| 315 | $pendingNaming = var["NFT_" || var["pendingNaming"]]; |
| 316 | } |
| 317 | |
| 318 | if (exists(trigger.data["soldAt"])){ |
| 319 | if (NOT is_integer(trigger.data["soldAt"])) |
| 320 | bounce("soldAt must be an integer"); |
| 321 | if (trigger.data["soldAt"] <= timestamp) |
| 322 | bounce("soldAt cannot be set in the past"); |
| 323 | } |
| 324 | if (exists(trigger.data["tranferrable"])){ |
| 325 | $isTransferrable = exists(trigger.data["validity"]) |
| 326 | ? false |
| 327 | : trigger.data["tranferrable"] == "true" |
| 328 | ? true |
| 329 | : false; |
| 330 | } |
| 331 | else{ |
| 332 | $isTransferrable = false; |
| 333 | } |
| 334 | }", |
| 335 | "messages": { |
| 336 | "cases": [ |
| 337 | { |
| 338 | "if": "{ |
| 339 | trigger.data["amount"] |
| 340 | }", |
| 341 | "messages": [ |
| 342 | { |
| 343 | "app": "asset", |
| 344 | "payload": { |
| 345 | "cap": "{trigger.data["amount"]}", |
| 346 | "is_private": false, |
| 347 | "is_transferrable": "{$isTransferrable}", |
| 348 | "auto_destroy": false, |
| 349 | "fixed_denominations": false, |
| 350 | "issued_by_definer_only": true, |
| 351 | "cosigned_by_definer": false, |
| 352 | "spender_attested": false |
| 353 | } |
| 354 | }, |
| 355 | { |
| 356 | "app": "data", |
| 357 | "if": "{ |
| 358 | $pendingNaming |
| 359 | }", |
| 360 | "payload": { |
| 361 | "asset": "{var["recordToData"]}", |
| 362 | "name": "{($pendingNaming.ticker OTHERWISE "Untitled")|| " license"}", |
| 363 | "author": "{$pendingNaming.author}", |
| 364 | "ipfs": "{$pendingNaming.ipfs}", |
| 365 | "type": "LIC", |
| 366 | "decimals": 0 |
| 367 | } |
| 368 | }, |
| 369 | { |
| 370 | "app": "state", |
| 371 | "state": "{ |
| 372 | if (trigger.address == $owner OR $profile.isLicensor){ |
| 373 | var["NFT_" || response_unit] = { |
| 374 | price: trigger.data["price"] OTHERWISE false, |
| 375 | currency: trigger.data["currency"] OTHERWISE false, |
| 376 | title: trigger.data["title"], |
| 377 | ipfs: trigger.data["ipfs"], |
| 378 | author: trigger.data["seller"], |
| 379 | validity: trigger.data["validity"] OTHERWISE false, |
| 380 | maxPeriods: trigger.data["maxPeriods"] OTHERWISE false, |
| 381 | soldAt: trigger.data["soldAt"] OTHERWISE false, |
| 382 | onSale: true, |
| 383 | type: 'LIC' |
| 384 | }; |
| 385 | |
| 386 | var["pendingNaming"] = response_unit; |
| 387 | } |
| 388 | else{ |
| 389 | var["FREE_" || response_unit] = { |
| 390 | price: trigger.data["price"] OTHERWISE false, |
| 391 | currency: trigger.data["currency"] OTHERWISE false, |
| 392 | author: trigger.address, |
| 393 | validity: trigger.data["validity"], |
| 394 | maxPeriods: trigger.data["maxPeriods"], |
| 395 | soldAt: trigger.data["soldAt"] OTHERWISE false, |
| 396 | onSale: true, |
| 397 | type: 'LIC' |
| 398 | }; |
| 399 | } |
| 400 | }" |
| 401 | } |
| 402 | ] |
| 403 | }, |
| 404 | { |
| 405 | "messages": [ |
| 406 | { |
| 407 | "app": "asset", |
| 408 | "payload": { |
| 409 | "is_private": false, |
| 410 | "is_transferrable": "{$isTransferrable}", |
| 411 | "auto_destroy": false, |
| 412 | "fixed_denominations": false, |
| 413 | "issued_by_definer_only": true, |
| 414 | "cosigned_by_definer": false, |
| 415 | "spender_attested": false |
| 416 | } |
| 417 | }, |
| 418 | { |
| 419 | "app": "data", |
| 420 | "if": "{ |
| 421 | $pendingNaming |
| 422 | }", |
| 423 | "payload": { |
| 424 | "asset": "{var["recordToData"]}", |
| 425 | "name": "{($pendingNaming.ticker OTHERWISE "Untitled") || " license"}", |
| 426 | "author": "{$pendingNaming.author}", |
| 427 | "ipfs": "{$pendingNaming.ipfs}", |
| 428 | "type": "LIC", |
| 429 | "decimals": 0 |
| 430 | } |
| 431 | }, |
| 432 | { |
| 433 | "app": "state", |
| 434 | "state": "{ |
| 435 | if (trigger.address == $owner OR $profile.isLicensor){ |
| 436 | var["NFT_" || response_unit] = { |
| 437 | price: trigger.data["price"] OTHERWISE false, |
| 438 | currency: trigger.data["currency"] OTHERWISE false, |
| 439 | title: trigger.data["title"], |
| 440 | ipfs: trigger.data["ipfs"], |
| 441 | author: trigger.data["seller"], |
| 442 | validity: trigger.data["validity"] OTHERWISE false, |
| 443 | maxPeriods: trigger.data["maxPeriods"] OTHERWISE false, |
| 444 | onSale: true, |
| 445 | type: 'LIC' |
| 446 | }; |
| 447 | |
| 448 | var["pendingNaming"] = response_unit; |
| 449 | } |
| 450 | else{ |
| 451 | var["FREE_" || response_unit] = { |
| 452 | price: trigger.data["price"] OTHERWISE false, |
| 453 | priceCurrency: trigger.data["priceCurrency"] OTHERWISE false, |
| 454 | author: trigger.address, |
| 455 | validity: trigger.data["validity"], |
| 456 | maxPeriods: trigger.data["maxPeriods"], |
| 457 | onSale: true, |
| 458 | type: 'LIC' |
| 459 | }; |
| 460 | } |
| 461 | }" |
| 462 | } |
| 463 | ] |
| 464 | } |
| 465 | ] |
| 466 | } |
| 467 | }, |
| 468 | { |
| 469 | "if": "{ |
| 470 | $method == "END_SALE" |
| 471 | }", |
| 472 | "init": "{ |
| 473 | if (NOT exists(trigger.data["NFT"])) |
| 474 | bounce("NFT field is mandatory"); |
| 475 | |
| 476 | $NFT = var["NFT_" || trigger.data["NFT"]] OTHERWISE var["FREE_" || trigger.data["NFT"]]; |
| 477 | |
| 478 | if (NOT $NFT) |
| 479 | bounce("That NFT is not known by this AA"); |
| 480 | |
| 481 | if (trigger.address != $NFT.author) |
| 482 | bounce("You cannot stop a sale not created by yourself"); |
| 483 | }", |
| 484 | "messages": [ |
| 485 | { |
| 486 | "app": "state", |
| 487 | "state": "{ |
| 488 | if (var["NFT_" || trigger.data["NFT"]]) |
| 489 | var["NFT_" || trigger.data["NFT"]] ||= {onSale: false}; |
| 490 | else |
| 491 | var["FREE_" || trigger.data["NFT"]] ||= {onSale: false}; |
| 492 | }" |
| 493 | } |
| 494 | ] |
| 495 | }, |
| 496 | { |
| 497 | "if": "{ |
| 498 | trigger.data["method"] == "BUY" |
| 499 | }", |
| 500 | "init": "{ |
| 501 | $NFT = var["NFT_" || trigger.data["NFT"]] OTHERWISE var["FREE_" || trigger.data["NFT"]]; |
| 502 | |
| 503 | $previousExpiry = $USER_REGISTRY_ADDRESS.$licenseExpirationOf(trigger.address, trigger.data["NFT"]); |
| 504 | |
| 505 | if (NOT $NFT) |
| 506 | bounce("NFT field is mandatory"); |
| 507 | |
| 508 | if ($NFT.soldAt){ |
| 509 | if ($NFT.soldAt < timestamp) |
| 510 | bounce("That license sale period ended on " || timestamp_to_string($NFT.soldAt) || " UTC"); |
| 511 | } |
| 512 | |
| 513 | if (NOT $NFT.onSale) |
| 514 | bounce("The owner of that license cancelled its sale. No more copies will be ever sold."); |
| 515 | |
| 516 | if (exists(trigger.data["periods"])){ |
| 517 | if (NOT is_integer(trigger.data["periods"])) |
| 518 | bounce("periods must be an integer"); |
| 519 | } |
| 520 | |
| 521 | $periods = trigger.data["periods"] OTHERWISE 1; |
| 522 | |
| 523 | if ($NFT.currency) |
| 524 | $expectedAmount = $CURRENCY_REGISTRY.$convert($NFT.price, $NFT.currency) * $periods; |
| 525 | else |
| 526 | $expectedAmount = $NFT.price * $periods; |
| 527 | |
| 528 | if ($NFT.maxPeriods){ |
| 529 | $remainingPeriods = ceil((($USER_REGISTRY_ADDRESS.$licenseExpirationOf(trigger.address, trigger.data["NFT"]) OTHERWISE timestamp) - timestamp) / $NFT.validity, 0); |
| 530 | if ($remainingPeriods + $periods > $NFT.maxPeriods) |
| 531 | bounce("You cannot buy that many periods in advance. Please try again when your license is closer to expiry"); |
| 532 | } |
| 533 | |
| 534 | response["rate"] = $CURRENCY_REGISTRY.$convert(1, $NFT.currency); |
| 535 | response["price"] = $expectedAmount; |
| 536 | response["currency"] = $NFT.currency; |
| 537 | |
| 538 | if (trigger.output[[asset="base"]].amount < $expectedAmount) |
| 539 | bounce("Your payment is lower than the NFT price. You have to send " || $expectedAmount || " bytes"); |
| 540 | |
| 541 | $pendingNaming = var["NFT_" || var["recordToData"]]; |
| 542 | }", |
| 543 | "messages": [ |
| 544 | { |
| 545 | "app": "payment", |
| 546 | "if": "{ |
| 547 | floor($expectedAmount * (1 - $FEE), 0) > 0 |
| 548 | }", |
| 549 | "payload": { |
| 550 | "asset": "base", |
| 551 | "outputs": [ |
| 552 | { |
| 553 | "address": "{$NFT.author}", |
| 554 | "amount": "{floor($expectedAmount * (1 - $FEE), 0)}" |
| 555 | }, |
| 556 | { |
| 557 | "if": "{$NFT.validity}", |
| 558 | "address": "{$USER_REGISTRY_ADDRESS}", |
| 559 | "amount": 1 |
| 560 | }, |
| 561 | { |
| 562 | "if": "{ |
| 563 | trigger.data["r"] |
| 564 | AND is_valid_address(trigger.data["r"]) |
| 565 | AND trigger.data["r"] != $NFT.author |
| 566 | AND floor($expectedAmount * ($FEE / 2), 0) > 10000 |
| 567 | }", |
| 568 | "init": "{ |
| 569 | $profile = $USER_REGISTRY_ADDRESS.$profileOf(trigger.data["r"]); |
| 570 | if (NOT $profile.isVerified OR NOT $profile.isReseller) |
| 571 | bounce("The user you are buying the license from is not an approved reseller"); |
| 572 | }", |
| 573 | "address": "{trigger.data["r"]}", |
| 574 | "amount": "{floor($expectedAmount * ($FEE / 2), 0)}" |
| 575 | } |
| 576 | ] |
| 577 | } |
| 578 | }, |
| 579 | { |
| 580 | "app": "payment", |
| 581 | "payload": { |
| 582 | "asset": "{trigger.data['NFT']}", |
| 583 | "outputs": [ |
| 584 | { |
| 585 | "address": "{trigger.address}", |
| 586 | "amount": "{$periods}" |
| 587 | } |
| 588 | ] |
| 589 | } |
| 590 | }, |
| 591 | { |
| 592 | "app": "data", |
| 593 | "if": "{ |
| 594 | $pendingNaming OR $NFT.validity |
| 595 | }", |
| 596 | "init": "{ |
| 597 | $payload = ($pendingNaming?{ |
| 598 | asset: var["recordToData"], |
| 599 | name: ($pendingNaming.ticker OTHERWISE "Untitled") || " license", |
| 600 | author: $pendingNaming.author, |
| 601 | ipfs: $pendingNaming.ipfs, |
| 602 | type: "LIC", |
| 603 | decimals: 0 |
| 604 | } : {}) || ($NFT.validity? {NFT: trigger.data["NFT"], time: $NFT.validity * $periods, method: "addTime"} : {}); |
| 605 | }", |
| 606 | "payload": "{$payload}" |
| 607 | }, |
| 608 | { |
| 609 | "app": "state", |
| 610 | "state": "{ |
| 611 | if ($pendingNaming) |
| 612 | var["recordToData"] = false; |
| 613 | }" |
| 614 | } |
| 615 | ] |
| 616 | }, |
| 617 | { |
| 618 | "if": "{ |
| 619 | $method == "CHANGE_PRICE" |
| 620 | }", |
| 621 | "init": "{ |
| 622 | if (NOT exists(trigger.data["NFT"])) |
| 623 | bounce("NFT field is mandatory"); |
| 624 | |
| 625 | $currency = $CURRENCY_REGISTRY.$getCurrency(trigger.data["currency"]); |
| 626 | |
| 627 | if (NOT exists(trigger.data["price"])) |
| 628 | bounce("price field is mandatory"); |
| 629 | if (NOT exists(trigger.data["currency"])){ |
| 630 | if (trigger.data["price"] < 20000) |
| 631 | bounce("price must be greater than 20.000 bytes"); |
| 632 | if (NOT is_valid_amount(trigger.data["price"])) |
| 633 | bounce("price is invalid"); |
| 634 | } |
| 635 | else{ |
| 636 | if (NOT $currency) |
| 637 | bounce("That currency is unsupported"); |
| 638 | } |
| 639 | |
| 640 | $price = trigger.data["currency"] ? $CURRENCY_REGISTRY.$convert(trigger.data["price"], trigger.data["currency"]) : trigger.data["price"]; |
| 641 | |
| 642 | if ($price <= 0) |
| 643 | bounce("price field must be higher than 0"); |
| 644 | |
| 645 | $NFT = var["NFT_" || trigger.data["NFT"]] OTHERWISE var["FREE_" || trigger.data["NFT"]]; |
| 646 | |
| 647 | $isRegisteredNFT = var["NFT_" || trigger.data["NFT"]]; |
| 648 | |
| 649 | if (NOT $NFT) |
| 650 | bounce("NFT not found"); |
| 651 | |
| 652 | if ($NFT.author != trigger.address) |
| 653 | bounce("You can only change the price of your own licenses!"); |
| 654 | }", |
| 655 | "messages": [ |
| 656 | { |
| 657 | "app": "state", |
| 658 | "state": "{ |
| 659 | var[($isRegisteredNFT ? "NFT_" : "FREE_") || trigger.data["NFT"]] ||= { |
| 660 | price: $price, |
| 661 | currency: trigger.data["currency"] OTHERWISE false |
| 662 | }; |
| 663 | }" |
| 664 | } |
| 665 | ] |
| 666 | }, |
| 667 | { |
| 668 | "if": "{ |
| 669 | $method == "REDEEM" |
| 670 | }", |
| 671 | "init": "{ |
| 672 | if (NOT exists(trigger.data["signed_message"])) |
| 673 | bounce("signed_message field is mandatory"); |
| 674 | if (NOT exists(trigger.data["authors"])) |
| 675 | bounce('authors field is mandatory'); |
| 676 | if (NOT exists(trigger.data["meta"])) |
| 677 | bounce("meta field is mandatory"); |
| 678 | |
| 679 | $spack = { |
| 680 | signed_message: trigger.data["signed_message"], |
| 681 | authors: trigger.data["authors"] |
| 682 | } || trigger.data["meta"]; |
| 683 | |
| 684 | if ($spack.signed_message["amount"]) |
| 685 | if (NOT is_integer($spack.signed_message["amount"])) |
| 686 | bounce("You cannot redeem fractional license periods"); |
| 687 | |
| 688 | if (NOT is_valid_signed_package($spack, $spack.authors[0]["address"])) |
| 689 | bounce("The signed message signature is invalid"); |
| 690 | if (NOT $spack.signed_message["serial"]) |
| 691 | bounce("The signed message does not contains a serial field"); |
| 692 | if (NOT $spack.signed_message["NFT"]) |
| 693 | bounce("The signer of this message did not include a NFT field"); |
| 694 | |
| 695 | |
| 696 | if (var["CODE_" || $spack.signed_message["NFT"] || "_" || sha256($spack.signed_message["serial"])]) |
| 697 | bounce("That code was already redeemed"); |
| 698 | |
| 699 | |
| 700 | if ($spack.signed_message["claimer"]) |
| 701 | if ($spack.signed_message["claimer"] != trigger.address) |
| 702 | bounce("You cannot claim that NFT from that address"); |
| 703 | |
| 704 | |
| 705 | $NFT = var["NFT_" || $spack.signed_message["NFT"]] OTHERWISE var["FREE_" || $spack.signed_message["NFT"]]; |
| 706 | |
| 707 | if (NOT $NFT) |
| 708 | bounce("That license does not exist"); |
| 709 | |
| 710 | if ($spack.signed_message["expireDate"]){ |
| 711 | if ($NFT.soldAt){ |
| 712 | if ($NFT.soldAt < timestamp) |
| 713 | bounce("The sale of that license has already ended. You cannot claim a copy of it"); |
| 714 | } |
| 715 | if ($spack.signed_message["expireDate"] < timestamp){ |
| 716 | bounce("That NFT is no longer claimable"); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | $signedAmount = $spack.signed_message["amount"] OTHERWISE 1; |
| 721 | |
| 722 | if ($NFT.author != $spack.authors[0].address) |
| 723 | bounce("The signer of that message is not the issuer of that license"); |
| 724 | |
| 725 | |
| 726 | if (NOT $NFT.cap){ |
| 727 | $periods = $signedAmount; |
| 728 | } |
| 729 | |
| 730 | else { |
| 731 | if ($NFT.unitsSold + $signedAmount > $NFT.cap) |
| 732 | $periods = $NFT.cap - $NFT.unitsSold; |
| 733 | else |
| 734 | $periods = $signedAmount; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | if ($periods == 0) |
| 739 | bounce("All copies of that NFT have been already minted"); |
| 740 | }", |
| 741 | "messages": [ |
| 742 | { |
| 743 | "app": "payment", |
| 744 | "payload": { |
| 745 | "asset": "{$spack.signed_message["NFT"]}", |
| 746 | "outputs": [ |
| 747 | { |
| 748 | "address": "{trigger.address}", |
| 749 | "amount": "{$periods}" |
| 750 | }, |
| 751 | { |
| 752 | "if": "{$NFT.validity}", |
| 753 | "address": "{$USER_REGISTRY_ADDRESS}", |
| 754 | "amount": 1 |
| 755 | } |
| 756 | ] |
| 757 | } |
| 758 | }, |
| 759 | { |
| 760 | "if": "{ |
| 761 | $NFT.validity |
| 762 | }", |
| 763 | "app": "data", |
| 764 | "payload": { |
| 765 | "NFT": "{$spack.signed_message["NFT"]}", |
| 766 | "time": "{$signedAmount * $NFT.validity}" |
| 767 | } |
| 768 | }, |
| 769 | { |
| 770 | "app": "state", |
| 771 | "state": "{ |
| 772 | var["CODE_" || $spack.signed_message["NFT"] || "_" || sha256($spack.signed_message["serial"])] = true; |
| 773 | }" |
| 774 | } |
| 775 | ] |
| 776 | } |
| 777 | ] |
| 778 | } |
| 779 | } |
| 780 | ] |