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