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