| 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 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 | $oracle = var["oracle"]; |
| 19 | }", |
| 20 | "messages": { |
| 21 | "cases": [ |
| 22 | { |
| 23 | "if": "{ |
| 24 | NOT $owner |
| 25 | }", |
| 26 | "messages": [ |
| 27 | { |
| 28 | "app": "state", |
| 29 | "state": "{ |
| 30 | var["owner"] = "IUU43O7TS2TBYKAPGKUARDZHOTAE275A"; |
| 31 | var["oracle"] = "F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N"; |
| 32 | }" |
| 33 | } |
| 34 | ] |
| 35 | }, |
| 36 | { |
| 37 | "if": "{ |
| 38 | trigger.address == $owner |
| 39 | AND ($method == "payout" |
| 40 | OR $method == "mint" |
| 41 | OR $method == "stopSale" |
| 42 | OR $method == "transferOwnership" |
| 43 | OR $method == "setOracle") |
| 44 | }", |
| 45 | "messages": { |
| 46 | "cases": [ |
| 47 | { |
| 48 | "if": "{ |
| 49 | $method == "payout" |
| 50 | }", |
| 51 | "messages": [ |
| 52 | { |
| 53 | "app": "payment", |
| 54 | "payload": { |
| 55 | "asset": "base", |
| 56 | "outputs": [ |
| 57 | { |
| 58 | "address": "{trigger.address}", |
| 59 | "amount": "{$spendableFunds}" |
| 60 | } |
| 61 | ] |
| 62 | } |
| 63 | } |
| 64 | ] |
| 65 | }, |
| 66 | { |
| 67 | "if": "{ |
| 68 | $method == "transferOwnership" |
| 69 | }", |
| 70 | "init": "{ |
| 71 | if (NOT trigger.data["newOwner"]) |
| 72 | bounce("newOwner field is mandatory"); |
| 73 | if (NOT is_valid_address(trigger.data["newOwner"])) |
| 74 | bounce("newOwner field is not a valid address"); |
| 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 | "app": "state", |
| 91 | "state": "{ |
| 92 | var["owner"] = trigger.data["newOwner"]; |
| 93 | }" |
| 94 | } |
| 95 | ] |
| 96 | }, |
| 97 | { |
| 98 | "if": "{ |
| 99 | $method == "mint" |
| 100 | }", |
| 101 | "init": "{ |
| 102 | if (NOT exists(trigger.data["amount"])) |
| 103 | bounce("amount field is mandatory. Set it to 0 for unlimited"); |
| 104 | |
| 105 | if ((NOT is_valid_amount(trigger.data["amount"])) AND trigger.data["amount"] != 0) |
| 106 | bounce("The amount of NFT copies to mint is not valid"); |
| 107 | |
| 108 | $price = trigger.data["priceUSD"] OTHERWISE trigger.data["price"]; |
| 109 | |
| 110 | if (NOT $price) |
| 111 | bounce("price or priceUSD field must be filled"); |
| 112 | |
| 113 | if (NOT trigger.data["title"]) |
| 114 | bounce("title field is mandatory"); |
| 115 | |
| 116 | if (NOT trigger.data["priceUSD"]){ |
| 117 | if ($price < 20000) |
| 118 | bounce("The minium price is 20000 bytes"); |
| 119 | if (NOT is_valid_amount($price)) |
| 120 | bounce("price is not valid"); |
| 121 | } |
| 122 | |
| 123 | if (NOT trigger.data["ipfs"]) |
| 124 | bounce("ipfs field is mandatory"); |
| 125 | |
| 126 | if (exists(trigger.data["validity"])){ |
| 127 | if (NOT is_integer(trigger.data["validity"])) |
| 128 | bounce("validity must be an integer"); |
| 129 | if (trigger.data["validity"] <= 0) |
| 130 | bounce("You cannot make a license valid for 0s"); |
| 131 | } |
| 132 | |
| 133 | if (NOT is_valid_address(trigger.data["seller"])) |
| 134 | bounce("The seller address is not valid"); |
| 135 | |
| 136 | $pendingNaming = var["NFT_" || var["pendingNaming"]]; |
| 137 | }", |
| 138 | "messages": { |
| 139 | "cases": [ |
| 140 | { |
| 141 | "if": "{ |
| 142 | trigger.data["amount"] |
| 143 | }", |
| 144 | "messages": [ |
| 145 | { |
| 146 | "app": "asset", |
| 147 | "payload": { |
| 148 | "cap": "{trigger.data["amount"]}", |
| 149 | "is_private": false, |
| 150 | "is_transferrable": "{trigger.data["tranferrable"]}", |
| 151 | "auto_destroy": false, |
| 152 | "fixed_denominations": false, |
| 153 | "issued_by_definer_only": true, |
| 154 | "cosigned_by_definer": false, |
| 155 | "spender_attested": false |
| 156 | } |
| 157 | }, |
| 158 | { |
| 159 | "app": "data", |
| 160 | "if": "{ |
| 161 | $pendingNaming |
| 162 | }", |
| 163 | "payload": { |
| 164 | "asset": "{var["recordToData"]}", |
| 165 | "name": "{$pendingNaming.title || "license"}", |
| 166 | "author": "{$pendingNaming.seller}", |
| 167 | "ipfs": "{$pendingNaming.ipfs}", |
| 168 | "type": "LIC", |
| 169 | "decimals": 0 |
| 170 | } |
| 171 | }, |
| 172 | { |
| 173 | "app": "state", |
| 174 | "state": "{ |
| 175 | var["NFT_" || response_unit] = { |
| 176 | price: trigger.data["price"] OTHERWISE false, |
| 177 | priceUSD: trigger.data["priceUSD"] OTHERWISE false, |
| 178 | title: trigger.data["title"], |
| 179 | ipfs: trigger.data["ipfs"], |
| 180 | author: trigger.data["seller"], |
| 181 | validity: trigger.data["validity"], |
| 182 | onSale: true |
| 183 | }; |
| 184 | |
| 185 | var["pendingNaming"] = response_unit; |
| 186 | }" |
| 187 | } |
| 188 | ] |
| 189 | }, |
| 190 | { |
| 191 | "messages": [ |
| 192 | { |
| 193 | "app": "asset", |
| 194 | "payload": { |
| 195 | "is_private": false, |
| 196 | "is_transferrable": "{trigger.data["tranferrable"] OTHERWISE false}", |
| 197 | "auto_destroy": false, |
| 198 | "fixed_denominations": false, |
| 199 | "issued_by_definer_only": true, |
| 200 | "cosigned_by_definer": false, |
| 201 | "spender_attested": false |
| 202 | } |
| 203 | }, |
| 204 | { |
| 205 | "app": "data", |
| 206 | "if": "{ |
| 207 | $pendingNaming |
| 208 | }", |
| 209 | "payload": { |
| 210 | "asset": "{var["recordToData"]}", |
| 211 | "name": "{$pendingNaming.title OTHERWISE "Untitled"}", |
| 212 | "author": "{$pendingNaming.seller}", |
| 213 | "ipfs": "{$pendingNaming.ipfs}", |
| 214 | "type": "LIC", |
| 215 | "decimals": 0 |
| 216 | } |
| 217 | }, |
| 218 | { |
| 219 | "app": "state", |
| 220 | "state": "{ |
| 221 | var["NFT_" || response_unit] = { |
| 222 | price: trigger.data["price"] OTHERWISE false, |
| 223 | priceUSD: trigger.data["priceUSD"] OTHERWISE false, |
| 224 | title: trigger.data["title"], |
| 225 | ipfs: trigger.data["ipfs"], |
| 226 | author: trigger.data["seller"], |
| 227 | onSale: true |
| 228 | }; |
| 229 | |
| 230 | var["recordToData"] = response_unit; |
| 231 | }" |
| 232 | } |
| 233 | ] |
| 234 | } |
| 235 | ] |
| 236 | } |
| 237 | }, |
| 238 | { |
| 239 | "if": "{ |
| 240 | $method == "setOracle" |
| 241 | }", |
| 242 | "init": "{ |
| 243 | if (NOT is_valid_address(trigger.data["oracle"])) |
| 244 | bounce("Invalid oracle address"); |
| 245 | if (NOT data_feed[[oracles=trigger.data["oracle"], feed_name="GBYTE_USD", ifseveral="last"]]) |
| 246 | bounce("That oracle is not posting a GBYTE_USD datafeed"); |
| 247 | }", |
| 248 | "messages": [ |
| 249 | { |
| 250 | "app": "state", |
| 251 | "state": "{ |
| 252 | var["oracle"] = trigger.data["oracle"]; |
| 253 | }" |
| 254 | } |
| 255 | ] |
| 256 | } |
| 257 | ] |
| 258 | } |
| 259 | }, |
| 260 | { |
| 261 | "if": "{ |
| 262 | $method == "END_SALE" |
| 263 | }", |
| 264 | "init": "{ |
| 265 | if (NOT trigger.data["NFT"]) |
| 266 | bounce("NFT field is mandatory"); |
| 267 | |
| 268 | $NFT = var["NFT_" || trigger.data["NFT"]]; |
| 269 | |
| 270 | if (NOT $NFT) |
| 271 | bounce("That NFT is not known by this AA"); |
| 272 | |
| 273 | if (trigger.address != $NFT.author AND trigger.address != $owner) |
| 274 | bounce("You cannot stop a sale not created by yourself"); |
| 275 | }", |
| 276 | "messages": [ |
| 277 | { |
| 278 | "app": "state", |
| 279 | "state": "{ |
| 280 | var["NFT_" || trigger.data["NFT"]] ||= {onSale: false}; |
| 281 | }" |
| 282 | } |
| 283 | ] |
| 284 | }, |
| 285 | { |
| 286 | "if": "{ |
| 287 | trigger.data["method"] == "BUY" |
| 288 | }", |
| 289 | "init": "{ |
| 290 | $NFT = var["NFT_" || trigger.data["NFT"]]; |
| 291 | |
| 292 | if (NOT $NFT.onSale) |
| 293 | bounce("That NFT is not on sale"); |
| 294 | |
| 295 | $expectedAmount = $NFT.price |
| 296 | ? $NFT.price |
| 297 | : round($NFT.priceUSD * 1 / data_feed[[oracles=$oracle, feed_name="GBYTE_USD", ifseveral="last"]] * $BYTES_IN_GBYTE, 0); |
| 298 | |
| 299 | if (trigger.output[[asset="base"]].amount < $expectedAmount) |
| 300 | bounce("Your payment is lower than the NFT price. You have to send " || $expectedAmount || " bytes"); |
| 301 | |
| 302 | $pendingNaming = var["NFT_" || var["recordToData"]]; |
| 303 | }", |
| 304 | "messages": [ |
| 305 | { |
| 306 | "app": "payment", |
| 307 | "if": "{ |
| 308 | floor($expectedAmount * (1 - $HOUSE_CUT), 0) > 0 |
| 309 | }", |
| 310 | "payload": { |
| 311 | "asset": "base", |
| 312 | "outputs": [ |
| 313 | { |
| 314 | "address": "{$NFT.author}", |
| 315 | "amount": "{floor($expectedAmount * (1 - $HOUSE_CUT), 0)}" |
| 316 | } |
| 317 | ] |
| 318 | } |
| 319 | }, |
| 320 | { |
| 321 | "app": "payment", |
| 322 | "payload": { |
| 323 | "asset": "{trigger.data['NFT']}", |
| 324 | "outputs": [ |
| 325 | { |
| 326 | "address": "{trigger.address}", |
| 327 | "amount": 1 |
| 328 | } |
| 329 | ] |
| 330 | } |
| 331 | }, |
| 332 | { |
| 333 | "app": "data", |
| 334 | "if": "{ |
| 335 | $pendingNaming |
| 336 | }", |
| 337 | "payload": { |
| 338 | "asset": "{var["recordToData"]}", |
| 339 | "name": "{$pendingNaming.title OTHERWISE "Untitled"}", |
| 340 | "author": "{$pendingNaming.seller}", |
| 341 | "ipfs": "{$pendingNaming.ipfs}", |
| 342 | "type": "LIC", |
| 343 | "decimals": 0 |
| 344 | } |
| 345 | }, |
| 346 | { |
| 347 | "app": "state", |
| 348 | "state": "{ |
| 349 | if ($pendingNaming){ |
| 350 | var["recordToData"] = false; |
| 351 | } |
| 352 | if ($NFT.validity){ |
| 353 | var[trigger.data["NFT"] || trigger.data["address"]] = timestamp + $NFT.validity; |
| 354 | } |
| 355 | }" |
| 356 | } |
| 357 | ] |
| 358 | }, |
| 359 | { |
| 360 | "if": "{ |
| 361 | $method == "CHANGE_PRICE" |
| 362 | }", |
| 363 | "init": "{ |
| 364 | if (NOT trigger.data["NFT"]) |
| 365 | bounce("NFT field is mandatory"); |
| 366 | |
| 367 | $NFT = var["NFT_" || trigger.data["NFT"]]; |
| 368 | |
| 369 | if (NOT $NFT) |
| 370 | bounce("NFT not found"); |
| 371 | |
| 372 | if ($NFT.author != trigger.address) |
| 373 | bounce("You can only change the price of your own licenses!"); |
| 374 | |
| 375 | if (NOT exists(trigger.data["price"])) |
| 376 | bounce("price field is mandatory"); |
| 377 | |
| 378 | if (NOT is_integer(trigger.data["price"])) |
| 379 | bounce("price must be an integer"); |
| 380 | |
| 381 | if (NOT is_valid_amount(trigger.data["price"])) |
| 382 | bounce("price is invalid"); |
| 383 | }", |
| 384 | "messages": [ |
| 385 | { |
| 386 | "app": "state", |
| 387 | "state": "{ |
| 388 | var["NFT_" || trigger.data["NFT"]] ||= { |
| 389 | price: $NFT.price ? trigger.data["price"] : false, |
| 390 | priceUSD: $NFT.priceUSD ? trigger.data["price"] : false |
| 391 | }; |
| 392 | }" |
| 393 | } |
| 394 | ] |
| 395 | }, |
| 396 | { |
| 397 | "if": "{ |
| 398 | $method == "REDEEM" |
| 399 | }", |
| 400 | "init": "{ |
| 401 | if (NOT exists(trigger.data["spack"])) |
| 402 | bounce("You need to provide a signed package"); |
| 403 | |
| 404 | $spack = json_parse(trigger.data["spack"]); |
| 405 | |
| 406 | if (NOT is_valid_signed_package($spack, $spack.authors[0].address)) |
| 407 | bounce("The signed message signature is invalid"); |
| 408 | if (NOT $spack.signed_message.NFT) |
| 409 | bounce("The signer of this message did not include a NFT field"); |
| 410 | |
| 411 | $NFT = var["NFT_" || $spack.signed_message.NFT]; |
| 412 | |
| 413 | |
| 414 | if ($spack.signed_message.expireDate){ |
| 415 | if ($spack.signed_message.expireDate < timestamp) |
| 416 | bounce("That license is no longer claimable"); |
| 417 | } |
| 418 | $signedAmount = $spack.signed_message.amount OTHERWISE 1; |
| 419 | |
| 420 | if (NOT $NFT.cap) |
| 421 | $amount = $signedAmount; |
| 422 | |
| 423 | else{ |
| 424 | if ($NFT.unitsSold + $signedAmount > $NFT.cap) |
| 425 | $amount = $NFT.cap - $NFT.unitsSold; |
| 426 | else |
| 427 | $amount = $signedAmount; |
| 428 | } |
| 429 | |
| 430 | if ($amount == 0) |
| 431 | bounce("All units of that NFT have been already minted"); |
| 432 | |
| 433 | if ($NFT.author != $spack.authors[0].address) |
| 434 | bounce("The signer of that message is not the author of that NFT"); |
| 435 | if (NOT $NFT) |
| 436 | bounce("That NFT does not exist"); |
| 437 | }", |
| 438 | "messages": [ |
| 439 | { |
| 440 | "app": "payment", |
| 441 | "payload": { |
| 442 | "asset": "{$spack.signed_message.NFT}", |
| 443 | "outputs": [ |
| 444 | { |
| 445 | "address": "{trigger.address}", |
| 446 | "amount": "{$amount}" |
| 447 | } |
| 448 | ] |
| 449 | } |
| 450 | }, |
| 451 | { |
| 452 | "app": "state", |
| 453 | "state": "{ |
| 454 | if (var["FREE_" || $spack.signed_message.NFT]) |
| 455 | var["FREE_" || $spack.signed_message.NFT] ||= {unitsSold: $NFT.unitsSold + $amount}; |
| 456 | else |
| 457 | var["NFT_" || $spack.signed_message.NFT] ||= {unitsSold: $NFT.unitsSold + $amount}; |
| 458 | }" |
| 459 | } |
| 460 | ] |
| 461 | } |
| 462 | ] |
| 463 | } |
| 464 | } |
| 465 | ] |