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