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