| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $HOUSE_CUT = var["FEE"]; |
| 9 | $HALF_HOUR = 1800; |
| 10 | $RESERVE_AMOUNT = 200000; |
| 11 | $MIN_BID_INCREMENT = 0.0001; |
| 12 | $ONE_MONTH = 2592000; |
| 13 | $USER_REGISTRY_ADDRESS = "2NEH2TEG725U3JGJOIE3FCAEGY5HGVFU"; |
| 14 | $CURRENCY_REGISTRY = "R4MRGA3MRU33EAWQFX4TBKT6INKWCTHH"; |
| 15 | $MAX_ROYALTY = var[$USER_REGISTRY_ADDRESS]["maxRoyalty"]; |
| 16 | |
| 17 | if (NOT trigger.data["method"]) |
| 18 | bounce("method field is mandatory"); |
| 19 | |
| 20 | $method = trigger.data["method"]; |
| 21 | |
| 22 | $spendableFunds = balance["base"] - var["locked"] - storage_size - $RESERVE_AMOUNT; |
| 23 | $owner = var[$USER_REGISTRY_ADDRESS]["owner"]; |
| 24 | if (NOT $owner) |
| 25 | bounce("The smart contract has not been initialized yet"); |
| 26 | }", |
| 27 | "getters": "{ |
| 28 | $saleInfo = ($NFT, $seller)=>{ |
| 29 | $unit = unit[$NFT]; |
| 30 | $info = var["FREE_" || $NFT || "_" || $seller] OTHERWISE var["NFT_" || $NFT || "_" || $seller]; |
| 31 | |
| 32 | if (NOT $info) |
| 33 | bounce("That NFT is not for sale or does not exist"); |
| 34 | |
| 35 | return $info; |
| 36 | }; |
| 37 | $fetchNFT = $asset => { |
| 38 | $unitInfo = unit[$asset]; |
| 39 | $parentUnit = unit[$unitInfo.parent_units[0]]; |
| 40 | if (NOT $unitInfo.messages) |
| 41 | bounce("Bad unit"); |
| 42 | $schema = $unitInfo.authors[0].authentifiers |
| 43 | ? false |
| 44 | : $unitInfo.authors[0].address; |
| 45 | if (NOT $schema) |
| 46 | bounce("We can only accept NFTs defined with a schema"); |
| 47 | $definedBy = $unitInfo.authors[0].authentifiers |
| 48 | ? $unitInfo.authors[0].address |
| 49 | : $parentUnit.authors[0].address; |
| 50 | $metadata = $parentUnit.messages[[.app='data']].payload; |
| 51 | $isAccepted = var["SCHEMA_" || $schema]; |
| 52 | if (NOT $isAccepted) |
| 53 | bounce("We do not accept NFTs with that schema"); |
| 54 | $assetMetadata = $unitInfo.messages[[.app='asset']].payload; |
| 55 | return {asset: $assetMetadata, meta: $metadata, author: $definedBy}; |
| 56 | }; |
| 57 | }", |
| 58 | "messages": { |
| 59 | "cases": [ |
| 60 | { |
| 61 | "if": "{NOT $HOUSE_CUT}", |
| 62 | "messages": [ |
| 63 | { |
| 64 | "app": "state", |
| 65 | "state": "{ |
| 66 | var["locked"] = 0; |
| 67 | var["FEE"] = 0.1; |
| 68 | }" |
| 69 | } |
| 70 | ] |
| 71 | }, |
| 72 | { |
| 73 | "if": "{ |
| 74 | trigger.address == $owner |
| 75 | AND ($method == "payout" |
| 76 | OR $method == "transferOwnership" |
| 77 | OR $method == "reduceFee" |
| 78 | OR $method == "setSchema") |
| 79 | }", |
| 80 | "messages": { |
| 81 | "cases": [ |
| 82 | { |
| 83 | "if": "{$method == "payout"}", |
| 84 | "messages": [ |
| 85 | { |
| 86 | "app": "payment", |
| 87 | "payload": { |
| 88 | "asset": "base", |
| 89 | "outputs": [ |
| 90 | { |
| 91 | "address": "{trigger.address}", |
| 92 | "amount": "{$spendableFunds}" |
| 93 | } |
| 94 | ] |
| 95 | } |
| 96 | } |
| 97 | ] |
| 98 | }, |
| 99 | { |
| 100 | "if": "{$method == "reduceFee"}", |
| 101 | "init": "{ |
| 102 | if (NOT exists(trigger.data["fee"])) |
| 103 | bounce("fee field is mandatory"); |
| 104 | if (trigger.data["fee"] < 0) |
| 105 | bounce("Are you drunk?"); |
| 106 | if (trigger.data["fee"] >= $HOUSE_CUT) |
| 107 | bounce("fee must be lower than the current one"); |
| 108 | }", |
| 109 | "messages": [ |
| 110 | { |
| 111 | "app": "state", |
| 112 | "state": "{ |
| 113 | var["FEE"] = trigger.data["fee"]; |
| 114 | }" |
| 115 | } |
| 116 | ] |
| 117 | }, |
| 118 | { |
| 119 | "if": "{$method == "setSchema"}", |
| 120 | "init": "{ |
| 121 | if (NOT trigger.data["schema"]) |
| 122 | bounce("schema field is mandatory"); |
| 123 | if (NOT is_aa(trigger.data["schema"])) |
| 124 | bounce("schema is not the address of an autonomous agent"); |
| 125 | }", |
| 126 | "messages": [ |
| 127 | { |
| 128 | "app": "state", |
| 129 | "state": "{ |
| 130 | var["SCHEMA_" || trigger.data["schema"]] = true; |
| 131 | }" |
| 132 | } |
| 133 | ] |
| 134 | } |
| 135 | ] |
| 136 | } |
| 137 | }, |
| 138 | { |
| 139 | "if": "{$method == "BUY"}", |
| 140 | "init": "{ |
| 141 | if (NOT trigger.data["address"]) |
| 142 | bounce("address field is mandatory"); |
| 143 | if (NOT is_valid_address(trigger.data["address"])) |
| 144 | bounce("address field is an invalid address"); |
| 145 | if (NOT trigger.data["NFT"]) |
| 146 | bounce("NFT field is mandatory"); |
| 147 | if (trigger.address == trigger.data["address"]) |
| 148 | bounce("You are buying your own NFT. Claim it instead in order to avoid fees"); |
| 149 | |
| 150 | $sale = $saleInfo(trigger.data["NFT"], trigger.data["address"]); |
| 151 | $nft = $fetchNFT(trigger.data["NFT"]); |
| 152 | $artist = $USER_REGISTRY_ADDRESS.$artistInfoOf($nft.author); |
| 153 | |
| 154 | $amount = $sale.isAuction |
| 155 | ? 1 |
| 156 | : trigger.data["amount"] OTHERWISE 1; |
| 157 | $pricePerUnit = $sale.currency |
| 158 | ? $CURRENCY_REGISTRY.$convert($sale.price, $sale.currency) |
| 159 | : $sale.price; |
| 160 | $fullPrice = $amount * $pricePerUnit; |
| 161 | $cut = round($fullPrice * $HOUSE_CUT, 0); |
| 162 | $toAuthor = round($fullPrice * (min($nft.meta["royalty"] OTHERWISE 0, $MAX_ROYALTY))/100, 0); |
| 163 | response["meta"] = $nft.meta; |
| 164 | response["toAuthor"] = $toAuthor; |
| 165 | $toSeller = $fullPrice - $cut - $toAuthor - 10000; |
| 166 | }", |
| 167 | "messages": [ |
| 168 | { |
| 169 | "if": "{NOT $sale.auction}", |
| 170 | "init": "{ |
| 171 | if (exists(trigger.data["amount"])){ |
| 172 | if (NOT is_integer(trigger.data["amount"])) |
| 173 | bounce("amount field must be an integer"); |
| 174 | if (trigger.data["amount"] < 1) |
| 175 | bounce("amount field must be at least 1"); |
| 176 | if (trigger.data["amount"] > $sale.amount) |
| 177 | bounce("That user is selling only " || $sale.amount || " units"); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | if ($sale.currency) |
| 182 | response["rate"] = $CURRENCY_REGISTRY.$getExchangeRate($sale.currency); |
| 183 | |
| 184 | response["realPrice"] = $pricePerUnit; |
| 185 | response["total"] = $fullPrice; |
| 186 | |
| 187 | if (trigger.output[[asset="base"]].amount < $fullPrice) |
| 188 | bounce("Your payment is lower than the NFT price. You have to send " || $fullPrice || " bytes"); |
| 189 | }", |
| 190 | "app": "payment", |
| 191 | "payload": { |
| 192 | "asset": "base", |
| 193 | "outputs": [ |
| 194 | { |
| 195 | "address": "{$sale.soldBy}", |
| 196 | "amount": "{$toSeller}" |
| 197 | }, |
| 198 | { |
| 199 | "if": "{$toAuthor > 5000}", |
| 200 | "address": "{$nft.author}", |
| 201 | "amount": "{$toAuthor}" |
| 202 | } |
| 203 | ] |
| 204 | } |
| 205 | }, |
| 206 | { |
| 207 | "if": "{NOT $sale.auction}", |
| 208 | "app": "payment", |
| 209 | "payload": { |
| 210 | "asset": "{trigger.data['NFT']}", |
| 211 | "outputs": [ |
| 212 | { |
| 213 | "address": "{trigger.address}", |
| 214 | "amount": "{$amount}" |
| 215 | } |
| 216 | ] |
| 217 | } |
| 218 | }, |
| 219 | { |
| 220 | "app": "state", |
| 221 | "state": "{ |
| 222 | if ($sale.auction) { |
| 223 | if (trigger.output[[asset="base"]].amount < 20000) |
| 224 | bounce("The minimum bid is 20000 bytes"); |
| 225 | if (trigger.output[[asset="base"]].amount <= $sale.price * (1 + $MIN_BID_INCREMENT)) |
| 226 | bounce("Your bid must be at least " || ceil($sale.price * (1 + $MIN_BID_INCREMENT), 0) || " (0.01% higher than the current)"); |
| 227 | |
| 228 | if ($sale.soldBy == trigger.address) |
| 229 | bounce("You cannot bid on your own auction"); |
| 230 | if (timestamp > $sale.endTime AND $sale.bids) |
| 231 | bounce("The auction is over"); |
| 232 | |
| 233 | var["locked"] += trigger.output[[asset="base"]].amount - ($sale.by ? $sale.price : 0); |
| 234 | $saleState = $sale || { |
| 235 | price: trigger.output[[asset="base"]].amount, |
| 236 | at: timestamp, |
| 237 | bids: $sale.bids + 1, |
| 238 | by: trigger.address, |
| 239 | endTime: $sale.endTime + $HALF_HOUR |
| 240 | }; |
| 241 | } |
| 242 | else { |
| 243 | $newAmount = $sale.amount - $amount; |
| 244 | $saleState = $newAmount == 0 ? false : $sale || {amount: $newAmount, at: timestamp}; |
| 245 | } |
| 246 | |
| 247 | $key = var["FREE_" || trigger.data["NFT"] || "_" || trigger.data["address"]] |
| 248 | ? "FREE_" |
| 249 | : "NFT_"; |
| 250 | $fullKey = $key || trigger.data["NFT"] || "_" || trigger.data["address"]; |
| 251 | |
| 252 | var[$fullKey] = $saleState; |
| 253 | }" |
| 254 | } |
| 255 | ] |
| 256 | }, |
| 257 | { |
| 258 | "if": "{$method == "CLAIM"}", |
| 259 | "init": "{ |
| 260 | $address = trigger.data["address"] OTHERWISE trigger.address; |
| 261 | $sale = $saleInfo(trigger.data["NFT"], $address); |
| 262 | if (NOT trigger.data["NFT"]) |
| 263 | bounce("NFT field is mandatory"); |
| 264 | $nft = $fetchNFT(trigger.data["NFT"]); |
| 265 | $artist = $USER_REGISTRY_ADDRESS.$artistInfoOf($nft.author); |
| 266 | if (NOT $sale.auction) |
| 267 | bounce("That sale is not an auction"); |
| 268 | if ($sale.auction AND timestamp <= $sale.endTime) |
| 269 | bounce("The auction is not over yet"); |
| 270 | $fullKey = var["FREE_" || trigger.data["NFT"] || "_" || $sale.soldBy] |
| 271 | ? "FREE_" || trigger.data["NFT"] || "_" || $sale.soldBy |
| 272 | : "NFT_" || trigger.data["NFT"] || "_" || $sale.soldBy; |
| 273 | }", |
| 274 | "messages": { |
| 275 | "cases": [ |
| 276 | { |
| 277 | "if": "{$sale.auction}", |
| 278 | "messages": [ |
| 279 | { |
| 280 | "if": "{$sale.bids > 0}", |
| 281 | "app": "payment", |
| 282 | "init": "{ |
| 283 | $share = $sale.price * (1 - $HOUSE_CUT); |
| 284 | if ($nft.meta["royalty"]){ |
| 285 | $royalty = $sale.price * min($nft.meta["royalty"], $MAX_ROYALTY) / 100; |
| 286 | $outputs = [ |
| 287 | { |
| 288 | address: $nft.author, |
| 289 | amount: floor($royalty, 0) |
| 290 | }, |
| 291 | { |
| 292 | address: $sale.soldBy, |
| 293 | amount: floor($share - $royalty, 0) |
| 294 | } |
| 295 | ]; |
| 296 | } |
| 297 | else{ |
| 298 | $outputs = [ |
| 299 | { |
| 300 | address: $sale.soldBy, |
| 301 | amount: floor($share, 0) |
| 302 | } |
| 303 | ]; |
| 304 | } |
| 305 | $payload = {outputs: $outputs}; |
| 306 | }", |
| 307 | "payload": "{$payload}" |
| 308 | }, |
| 309 | { |
| 310 | "app": "payment", |
| 311 | "payload": { |
| 312 | "asset": "{trigger.data['NFT']}", |
| 313 | "outputs": [ |
| 314 | { |
| 315 | "address": "{$sale.by OTHERWISE $sale.soldBy}", |
| 316 | "amount": "{$sale.amount}" |
| 317 | } |
| 318 | ] |
| 319 | } |
| 320 | }, |
| 321 | { |
| 322 | "app": "state", |
| 323 | "state": "{ |
| 324 | if ($sale.bids > 0) |
| 325 | var["locked"] -= $sale.price; |
| 326 | |
| 327 | var[$fullKey] = false; |
| 328 | }" |
| 329 | } |
| 330 | ] |
| 331 | }, |
| 332 | { |
| 333 | "if": "{NOT $sale.auction}", |
| 334 | "init": "{ |
| 335 | if (trigger.address != $sale.soldBy) |
| 336 | bounce("You cannot claim other people's sales"); |
| 337 | }", |
| 338 | "messages": [ |
| 339 | { |
| 340 | "app": "payment", |
| 341 | "payload": { |
| 342 | "asset": "{trigger.data['NFT']}", |
| 343 | "outputs": [ |
| 344 | { |
| 345 | "address": "{$sale.soldBy}", |
| 346 | "amount": "{$sale.amount}" |
| 347 | } |
| 348 | ] |
| 349 | } |
| 350 | }, |
| 351 | { |
| 352 | "app": "state", |
| 353 | "state": "{ |
| 354 | var[$fullKey] = false; |
| 355 | }" |
| 356 | } |
| 357 | ] |
| 358 | } |
| 359 | ] |
| 360 | } |
| 361 | }, |
| 362 | { |
| 363 | "if": "{$method == "CHANGE_PRICE"}", |
| 364 | "init": "{ |
| 365 | if (NOT trigger.data["NFT"]) |
| 366 | bounce("NFT field is mandatory"); |
| 367 | |
| 368 | $sale = $saleInfo(trigger.data["NFT"], trigger.address); |
| 369 | |
| 370 | if (NOT $sale) |
| 371 | bounce("Sale not found"); |
| 372 | |
| 373 | if (NOT exists(trigger.data["price"])) |
| 374 | bounce("price field is mandatory"); |
| 375 | |
| 376 | if (NOT exists(trigger.data["currency"])){ |
| 377 | if (NOT is_integer(trigger.data["price"])) |
| 378 | bounce("price must be an integer"); |
| 379 | if (NOT is_valid_amount(trigger.data["price"])) |
| 380 | bounce("price is invalid"); |
| 381 | if (trigger.data["price"] < 20000) |
| 382 | bounce("The minimum price is 20000 bytes"); |
| 383 | } |
| 384 | else{ |
| 385 | if (NOT $CURRENCY_REGISTRY.$getCurrency(trigger.data["currency"])) |
| 386 | bounce("You cannot set the price in that currency"); |
| 387 | } |
| 388 | |
| 389 | if ($sale.auction) |
| 390 | bounce("You cannot change an auction price!"); |
| 391 | }", |
| 392 | "messages": [ |
| 393 | { |
| 394 | "app": "state", |
| 395 | "state": "{ |
| 396 | if (var["NFT_" || trigger.data["NFT"] || "_" || trigger.address]) |
| 397 | var["NFT_" || trigger.data["NFT"] || "_" || trigger.address] ||= {price: trigger.data["price"], soldBy: trigger.address, currency: trigger.data["currency"] OTHERWISE false}; |
| 398 | else |
| 399 | var["FREE_" || trigger.data["NFT"] || "_" || trigger.address] ||= {price: trigger.data["price"], soldBy: trigger.address, currency: trigger.data["currency"] OTHERWISE false}; |
| 400 | }" |
| 401 | } |
| 402 | ] |
| 403 | }, |
| 404 | { |
| 405 | "if": "{$method == "SELL"}", |
| 406 | "init": "{ |
| 407 | if (trigger.output[[asset!="base"]].asset == "ambigous") |
| 408 | bounce("You can only send a single NFT at a time"); |
| 409 | |
| 410 | $NFT = $fetchNFT(trigger.output[[asset!="base"]].asset); |
| 411 | |
| 412 | $prof = $USER_REGISTRY_ADDRESS.$profileOf($NFT.author); |
| 413 | |
| 414 | if (2NEH2TEG725U3JGJOIE3FCAEGY5HGVFU.$isRevoked(trigger.output[[asset!="base"]].asset)) |
| 415 | bounce("We revoked the trading of that token probably due to copyright reasons"); |
| 416 | |
| 417 | if (NOT trigger.data["price"]) |
| 418 | bounce("price field is mandatory"); |
| 419 | if (trigger.data["currency"]){ |
| 420 | if (NOT $CURRENCY_REGISTRY.$getCurrency(trigger.data["currency"])) |
| 421 | bounce("We do not support that currency"); |
| 422 | } |
| 423 | else { |
| 424 | if (NOT is_integer(trigger.data["price"])) |
| 425 | bounce("Price must be an integer"); |
| 426 | if (NOT is_valid_amount(trigger.data["price"])) |
| 427 | bounce("Price is not a valid amount"); |
| 428 | if (NOT trigger.data["auction"]){ |
| 429 | if (trigger.data["price"] * trigger.output[[asset!="base"]].amount < 20000) |
| 430 | bounce("The minimum sale price is 20.000 bytes"); |
| 431 | } |
| 432 | } |
| 433 | if (trigger.data["auction"]){ |
| 434 | if (NOT exists(trigger.data["endTime"])) |
| 435 | bounce("If you want to hold an auction you have to specify the endTime"); |
| 436 | if (trigger.data["price"] < 20000) |
| 437 | bounce("Minimum auction price is 20.000 bytes"); |
| 438 | if (trigger.data["currency"]) |
| 439 | bounce("You cannot set the auction price in other currency than bytes"); |
| 440 | if (trigger.data["endTime"] <= timestamp) |
| 441 | bounce("You cannot set the end time in the past"); |
| 442 | if (trigger.data["endTime"] > timestamp + 2628000) |
| 443 | bounce("You cannot set the end time in more than 30 days"); |
| 444 | } |
| 445 | if (var["FREE_" || trigger.output[[asset!="base"]].asset || "_" || trigger.address] OR var["NFT_" || trigger.output[[asset!="base"]].asset || "_" || trigger.address]) |
| 446 | bounce("You already have an open sale or auction for that NFT. Please claim it before starting another one"); |
| 447 | }", |
| 448 | "messages": [ |
| 449 | { |
| 450 | "app": "state", |
| 451 | "state": "{ |
| 452 | $key = ($prof.verified ? "NFT_" : "FREE_") || trigger.output[[asset!="base"]].asset || "_" || trigger.address; |
| 453 | var[$key] = { |
| 454 | price: trigger.data["price"], |
| 455 | amount: trigger.output[[asset!="base"]].amount, |
| 456 | endTime: trigger.data["auction"] |
| 457 | ? trigger.data["endTime"] |
| 458 | : false, |
| 459 | soldBy: trigger.address, |
| 460 | bids: trigger.data["auction"] |
| 461 | ? 0 |
| 462 | : false, |
| 463 | auction: trigger.data["auction"] |
| 464 | ? true |
| 465 | : false, |
| 466 | currency: trigger.data["auction"] |
| 467 | ? '' |
| 468 | : trigger.data["currency"] OTHERWISE false |
| 469 | }; |
| 470 | }" |
| 471 | } |
| 472 | ] |
| 473 | } |
| 474 | ] |
| 475 | } |
| 476 | } |
| 477 | ] |