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