| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://obyte.org/prediction.json", |
| 5 | "bounce_fees": { |
| 6 | "base": 10000 |
| 7 | }, |
| 8 | "getters": "{ |
| 9 | $get_reserve = ($yes_amount, $no_amount, $draw_amount) => { |
| 10 | |
| 11 | $coef = var['coef'] OTHERWISE 1; |
| 12 | |
| 13 | $r = ceil($coef * sqrt($yes_amount * $yes_amount + $no_amount * $no_amount + $draw_amount * $draw_amount)); |
| 14 | $r |
| 15 | }; |
| 16 | }", |
| 17 | "init": "{ |
| 18 | $lib_aa = "ODKLBHNUOXRQGVG4WIV5GOCUONOAPTB7" || ''; |
| 19 | |
| 20 | |
| 21 | $event = params.event OTHERWISE ''; |
| 22 | require(length($event) >= 3 AND length($event) <= 128, "additional description cannot be over 128 chars"); |
| 23 | |
| 24 | $oracle = params.oracle; |
| 25 | require($oracle AND is_valid_address($oracle), "oracle isn't valid"); |
| 26 | |
| 27 | $comparison = params.comparison OTHERWISE "=="; |
| 28 | require($comparison == "==" OR $comparison == ">" OR $comparison == ">=" OR $comparison == "<" OR $comparison == "<=" OR $comparison == "!=", "operation is unknown"); |
| 29 | |
| 30 | $feed_name = params.feed_name OTHERWISE ''; |
| 31 | require(length($feed_name) > 0 AND $feed_name != "none", "feed_name isn't valid"); |
| 32 | |
| 33 | $reserve_asset = params.reserve_asset OTHERWISE 'base'; |
| 34 | require(asset[$reserve_asset].exists, "no such asset: " || $reserve_asset); |
| 35 | |
| 36 | $allow_draw = params.allow_draw OTHERWISE false; |
| 37 | |
| 38 | $datafeed_draw_value = params.datafeed_draw_value OTHERWISE 'none'; |
| 39 | $datafeed_value = params.datafeed_value; |
| 40 | |
| 41 | require(exists($datafeed_value), "datafeed_value does not exist"); |
| 42 | |
| 43 | $end_of_trading_period = params.end_of_trading_period OTHERWISE 0; |
| 44 | require(is_integer($end_of_trading_period) AND $end_of_trading_period > 0, "end_of_trading_period must be integer"); |
| 45 | |
| 46 | $waiting_period_length = params.waiting_period_length OTHERWISE 5 * 24 * 3600; |
| 47 | require(is_integer($waiting_period_length) AND $waiting_period_length >= 0, "trading period must be longer than a day"); |
| 48 | |
| 49 | $issue_fee = exists(params.issue_fee) ? params.issue_fee : 0.01; |
| 50 | require($issue_fee >= 0 AND $issue_fee < 1, "issue_fee isn't valid"); |
| 51 | |
| 52 | $redeem_fee = exists(params.redeem_fee) ? params.redeem_fee : 0.02; |
| 53 | require($redeem_fee >= 0 AND $redeem_fee < 1, "redeem_fee isn't valid"); |
| 54 | |
| 55 | |
| 56 | $ready = var['yes_asset'] AND var['no_asset'] AND (var['draw_asset'] OR !$allow_draw); |
| 57 | |
| 58 | $result = var['result']; |
| 59 | |
| 60 | $yes_asset = var['yes_asset']; |
| 61 | $no_asset = var['no_asset']; |
| 62 | $draw_asset = var['draw_asset']; |
| 63 | |
| 64 | $end_of_waiting_period = $end_of_trading_period + $waiting_period_length; |
| 65 | |
| 66 | $network_fee = ($reserve_asset == 'base') ? 10000 : 0; |
| 67 | |
| 68 | $reserve = var['reserve'] OTHERWISE 0; |
| 69 | $reserve_amount = trigger.output[[asset=$reserve_asset]]; |
| 70 | |
| 71 | $type = trigger.data.type OTHERWISE false; |
| 72 | |
| 73 | $supply_yes = var['supply_yes'] OTHERWISE 0; |
| 74 | $supply_no = var['supply_no'] OTHERWISE 0; |
| 75 | $supply_draw = var['supply_draw'] OTHERWISE 0; |
| 76 | |
| 77 | $to = trigger.data.to OTHERWISE trigger.address; |
| 78 | |
| 79 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) |
| 80 | bounce("bad to address"); |
| 81 | |
| 82 | if ($type) { |
| 83 | require($type == 'yes' OR $type == 'no' OR $type == 'draw', "unknown type"); |
| 84 | } |
| 85 | }", |
| 86 | "messages": { |
| 87 | "cases": [ |
| 88 | { |
| 89 | "if": "{!$ready AND trigger.data.define}", |
| 90 | "init": "{ |
| 91 | $define_type = $yes_asset ? ($no_asset ? "draw_asset" : "no_asset") : "yes_asset"; |
| 92 | $define_asset_forwarder = 'E4BAASPOCW6WHXSUOY2XEKXKA42RRD5I'; |
| 93 | require(($define_type == 'draw_asset' AND $allow_draw) OR $define_type == 'no_asset' OR $define_type == 'yes_asset', "unknown define type"); |
| 94 | require(!var[$define_type], "asset already defined"); |
| 95 | }", |
| 96 | "messages": [ |
| 97 | { |
| 98 | "app": "asset", |
| 99 | "payload": { |
| 100 | "is_private": false, |
| 101 | "is_transferrable": true, |
| 102 | "auto_destroy": false, |
| 103 | "fixed_denominations": false, |
| 104 | "issued_by_definer_only": true, |
| 105 | "cosigned_by_definer": false, |
| 106 | "spender_attested": false |
| 107 | } |
| 108 | }, |
| 109 | { |
| 110 | "if": "{trigger.data.factory AND !$yes_asset}", |
| 111 | "app": "data", |
| 112 | "payload": { |
| 113 | "factory": "{trigger.data.factory}" |
| 114 | } |
| 115 | }, |
| 116 | { |
| 117 | "if": "{trigger.data.factory AND !$yes_asset}", |
| 118 | "app": "payment", |
| 119 | "payload": { |
| 120 | "asset": "base", |
| 121 | "outputs": [ |
| 122 | { |
| 123 | "address": "{$define_asset_forwarder}", |
| 124 | "amount": 4000 |
| 125 | } |
| 126 | ] |
| 127 | } |
| 128 | }, |
| 129 | { |
| 130 | "if": "{trigger.data.factory AND $yes_asset AND !$no_asset}", |
| 131 | "app": "data", |
| 132 | "payload": { |
| 133 | "yes_asset": "{$yes_asset}", |
| 134 | "allow_draw": "{params.allow_draw}" |
| 135 | } |
| 136 | }, |
| 137 | { |
| 138 | "if": "{trigger.data.factory AND $yes_asset AND !$no_asset}", |
| 139 | "app": "payment", |
| 140 | "payload": { |
| 141 | "asset": "base", |
| 142 | "outputs": [ |
| 143 | { |
| 144 | "address": "{trigger.data.factory}", |
| 145 | "amount": 4000 |
| 146 | } |
| 147 | ] |
| 148 | } |
| 149 | }, |
| 150 | { |
| 151 | "if": "{trigger.data.factory AND $yes_asset AND $no_asset AND !$draw_asset AND $allow_draw}", |
| 152 | "app": "data", |
| 153 | "payload": { |
| 154 | "no_asset": "{$no_asset}" |
| 155 | } |
| 156 | }, |
| 157 | { |
| 158 | "if": "{trigger.data.factory AND $yes_asset AND $no_asset AND !$draw_asset AND $allow_draw}", |
| 159 | "app": "payment", |
| 160 | "payload": { |
| 161 | "asset": "base", |
| 162 | "outputs": [ |
| 163 | { |
| 164 | "address": "{trigger.data.factory}", |
| 165 | "amount": 4000 |
| 166 | } |
| 167 | ] |
| 168 | } |
| 169 | }, |
| 170 | { |
| 171 | "app": "state", |
| 172 | "state": "{ |
| 173 | var[$define_type] = response_unit; |
| 174 | response[$define_type] = response_unit; |
| 175 | }" |
| 176 | } |
| 177 | ] |
| 178 | }, |
| 179 | { |
| 180 | "if": "{$ready AND !exists(trigger.data.claim_profit) AND !exists(trigger.data.add_liquidity) AND (((trigger.output[[asset=$reserve_asset]] > $network_fee) AND (trigger.data.yes_amount OR trigger.data.no_amount OR trigger.data.draw_amount OR trigger.data.type)) OR trigger.output[[asset=$yes_asset]] > 0 OR trigger.output[[asset=$no_asset]] > 0 OR ($allow_draw AND trigger.output[[asset=$draw_asset]] > 0))}", |
| 181 | "init": "{ |
| 182 | require($allow_draw OR !exists(trigger.data.draw_amount), "draw asset does not exist"); |
| 183 | |
| 184 | if ((trigger.data.yes_amount OR trigger.data.no_amount OR trigger.data.draw_amount OR trigger.output[[asset=$yes_asset]] > 0 OR trigger.output[[asset=$no_asset]] > 0 OR ($allow_draw AND trigger.output[[asset=$draw_asset]] > 0)) AND trigger.data.type) { |
| 185 | bounce("both type and amount"); |
| 186 | } |
| 187 | |
| 188 | $min_expected_amount = trigger.data.min_expected_amount OTHERWISE 0; |
| 189 | |
| 190 | if ($type AND (!is_integer($min_expected_amount) OR $min_expected_amount < 0)) { |
| 191 | bounce("invalid min_expected_amount"); |
| 192 | } |
| 193 | |
| 194 | if (!(timestamp <= $end_of_trading_period OR timestamp >= $end_of_waiting_period)){ |
| 195 | $error = "the trading period is closed"; |
| 196 | response['error'] = $error; |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | if ($result) { |
| 201 | $error = "result already exists"; |
| 202 | response['error'] = $error; |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if ($type) { |
| 207 | $amount_by_type = $lib_aa#11.$get_token_amount(this_address, $type, $reserve_amount); |
| 208 | |
| 209 | if ($type == 'yes') { |
| 210 | $yes_amount = $amount_by_type; |
| 211 | $no_amount = 0; |
| 212 | $draw_amount = 0; |
| 213 | } else if ($type == 'no') { |
| 214 | $yes_amount = 0; |
| 215 | $no_amount = $amount_by_type; |
| 216 | $draw_amount = 0; |
| 217 | } else if ($type == 'no') { |
| 218 | $yes_amount = 0; |
| 219 | $no_amount = 0; |
| 220 | $draw_amount = $amount_by_type; |
| 221 | } |
| 222 | |
| 223 | } else { |
| 224 | $yes_amount = trigger.data.yes_amount OTHERWISE -trigger.output[[asset=$yes_asset]]; |
| 225 | $no_amount = trigger.data.no_amount OTHERWISE -trigger.output[[asset=$no_asset]]; |
| 226 | $draw_amount = $allow_draw ? trigger.data.draw_amount OTHERWISE -trigger.output[[asset=$draw_asset]] : 0; |
| 227 | } |
| 228 | |
| 229 | if (trigger.data.yes_amount AND (!is_integer(trigger.data.yes_amount) OR trigger.data.yes_amount <= 0)) |
| 230 | bounce("invalid number of yes_amount"); |
| 231 | if (trigger.data.no_amount AND (!is_integer(trigger.data.no_amount) OR trigger.data.no_amount <= 0)) |
| 232 | bounce("invalid number of no_amount"); |
| 233 | if ($allow_draw AND trigger.data.draw_amount AND (!is_integer(trigger.data.draw_amount) OR trigger.data.draw_amount <= 0)) |
| 234 | bounce("invalid number of draw_amount"); |
| 235 | |
| 236 | if (trigger.data.yes_amount AND trigger.output[[asset=$yes_asset]] > 0) |
| 237 | bounce("both yes_amount param and amount"); |
| 238 | if (trigger.data.no_amount AND trigger.output[[asset=$no_asset]] > 0) |
| 239 | bounce("both no_amount param and amount"); |
| 240 | if ($allow_draw AND trigger.data.draw_amount AND trigger.output[[asset=$draw_asset]] > 0) |
| 241 | bounce("both draw_amount param and amount"); |
| 242 | |
| 243 | $res = $lib_aa#12.$get_exchange_result(this_address, $yes_amount, $no_amount, $draw_amount); |
| 244 | |
| 245 | $payout = floor($res.payout + $reserve_amount - $res.reserve_needed - $res.fee - $network_fee); |
| 246 | |
| 247 | if ($type) { |
| 248 | $amount = $type == 'yes' ? $yes_amount : ($type == 'no' ? $no_amount : $draw_amount); |
| 249 | |
| 250 | if ($amount < $min_expected_amount) { |
| 251 | $error = "amount less than minimum"; |
| 252 | response['error'] = $error; |
| 253 | return; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if ($res.reserve_needed > 0 AND ($res.reserve_needed + $res.fee + $network_fee > $reserve_amount)) { |
| 258 | $error = "expected reserve amount: " || ($res.reserve_needed + $res.fee + $network_fee); |
| 259 | response['error'] = $error; |
| 260 | return; |
| 261 | } |
| 262 | }", |
| 263 | "messages": [ |
| 264 | { |
| 265 | "if": "{$yes_amount AND $yes_amount > 0 AND !$error}", |
| 266 | "app": "payment", |
| 267 | "payload": { |
| 268 | "asset": "{$yes_asset}", |
| 269 | "outputs": [ |
| 270 | { |
| 271 | "address": "{$to}", |
| 272 | "amount": "{$yes_amount}" |
| 273 | } |
| 274 | ] |
| 275 | } |
| 276 | }, |
| 277 | { |
| 278 | "if": "{$no_amount AND $no_amount > 0 AND !$error}", |
| 279 | "app": "payment", |
| 280 | "payload": { |
| 281 | "asset": "{$no_asset}", |
| 282 | "outputs": [ |
| 283 | { |
| 284 | "address": "{$to}", |
| 285 | "amount": "{$no_amount}" |
| 286 | } |
| 287 | ] |
| 288 | } |
| 289 | }, |
| 290 | { |
| 291 | "if": "{$allow_draw AND $draw_amount AND $draw_amount > 0 AND !$error}", |
| 292 | "app": "payment", |
| 293 | "payload": { |
| 294 | "asset": "{$draw_asset}", |
| 295 | "outputs": [ |
| 296 | { |
| 297 | "address": "{$to}", |
| 298 | "amount": "{$draw_amount}" |
| 299 | } |
| 300 | ] |
| 301 | } |
| 302 | }, |
| 303 | { |
| 304 | "if": "{($payout AND $payout > 0) OR $error}", |
| 305 | "app": "payment", |
| 306 | "payload": { |
| 307 | "asset": "{$reserve_asset}", |
| 308 | "outputs": [ |
| 309 | { |
| 310 | "address": "{$to}", |
| 311 | "amount": "{$error ? $reserve_amount - $network_fee : $payout}" |
| 312 | } |
| 313 | ] |
| 314 | } |
| 315 | }, |
| 316 | { |
| 317 | "if": "{!$error}", |
| 318 | "app": "state", |
| 319 | "state": "{ |
| 320 | response['next_reserve'] = $res.new_reserve OTHERWISE 0; |
| 321 | response['next_coef'] = $res.next_coef OTHERWISE 1; |
| 322 | |
| 323 | response['yes_amount'] = $yes_amount OTHERWISE 0; |
| 324 | response['no_amount'] = $no_amount OTHERWISE 0; |
| 325 | response['draw_amount'] = $draw_amount OTHERWISE 0; |
| 326 | |
| 327 | response['yes_price'] = $res.yes_price; |
| 328 | response['no_price'] = $res.no_price; |
| 329 | response['draw_price'] = $res.draw_price; |
| 330 | |
| 331 | var['reserve'] = $res.new_reserve; |
| 332 | var['coef'] = $res.next_coef; |
| 333 | |
| 334 | if ($res.fee > 0){ |
| 335 | response['fee'] = $res.fee; |
| 336 | } |
| 337 | |
| 338 | response['arb_profit_tax'] = $res.arb_profit_tax; |
| 339 | |
| 340 | var['supply_yes'] += $yes_amount; |
| 341 | response['supply_yes'] = var['supply_yes'] OTHERWISE 0; |
| 342 | |
| 343 | var['supply_no'] += $no_amount; |
| 344 | response['supply_no'] = var['supply_no'] OTHERWISE 0; |
| 345 | |
| 346 | if ($allow_draw) { |
| 347 | var['supply_draw'] += $draw_amount; |
| 348 | response['supply_draw'] = var['supply_draw'] OTHERWISE 0; |
| 349 | } |
| 350 | }" |
| 351 | } |
| 352 | ] |
| 353 | }, |
| 354 | { |
| 355 | "if": "{$ready AND trigger.data.add_liquidity AND $reserve_amount > $network_fee}", |
| 356 | "init": "{ |
| 357 | $fee = $reserve_amount * $issue_fee; |
| 358 | $reserve_amount_without_fee = $reserve_amount - $fee - $network_fee; |
| 359 | |
| 360 | if ((timestamp <= $end_of_trading_period OR timestamp >= $end_of_waiting_period) AND !$result) { |
| 361 | |
| 362 | if ($supply_yes + $supply_no + $supply_draw == 0) { |
| 363 | $yes_amount_ratio = trigger.data.yes_amount_ratio OTHERWISE 0; |
| 364 | require($yes_amount_ratio >= 0, 'yes ratio isnt valid'); |
| 365 | |
| 366 | if ($allow_draw) { |
| 367 | $no_amount_ratio = trigger.data.no_amount_ratio OTHERWISE 0; |
| 368 | require($no_amount_ratio >= 0, 'no ratio isnt valid'); |
| 369 | |
| 370 | $draw_amount_ratio = 1 - $yes_amount_ratio - $no_amount_ratio; |
| 371 | |
| 372 | require($draw_amount_ratio >= 0, 'ratio isnt valid'); |
| 373 | } else { |
| 374 | $no_amount_ratio = 1 - $yes_amount_ratio; |
| 375 | |
| 376 | require($no_amount_ratio >= 0, 'yes ratio isnt valid'); |
| 377 | } |
| 378 | |
| 379 | require($yes_amount_ratio >= 0 AND $no_amount_ratio >= 0 AND $yes_amount_ratio + $no_amount_ratio + $draw_amount_ratio == 1, 'yes_amount_ratio + no_amount_ratio + draw_amount_ratio must be equal 1'); |
| 380 | |
| 381 | $yes_amount = floor($reserve_amount_without_fee * sqrt($yes_amount_ratio)); |
| 382 | $no_amount = floor($reserve_amount_without_fee * sqrt($no_amount_ratio)); |
| 383 | $draw_amount = $allow_draw ? floor($reserve_amount_without_fee * sqrt($draw_amount_ratio)) : 0; |
| 384 | |
| 385 | } else { |
| 386 | $ratio = ($reserve_amount_without_fee + $reserve) / $reserve; |
| 387 | |
| 388 | $yes_amount = floor($ratio * $supply_yes - $supply_yes); |
| 389 | $no_amount = floor($ratio * $supply_no - $supply_no); |
| 390 | $draw_amount = floor($ratio * $supply_draw - $supply_draw); |
| 391 | } |
| 392 | } else { |
| 393 | $hasError = true; |
| 394 | } |
| 395 | }", |
| 396 | "messages": [ |
| 397 | { |
| 398 | "if": "{!$hasError AND $yes_amount > 0}", |
| 399 | "app": "payment", |
| 400 | "payload": { |
| 401 | "asset": "{$yes_asset}", |
| 402 | "outputs": [ |
| 403 | { |
| 404 | "address": "{$to}", |
| 405 | "amount": "{$yes_amount}" |
| 406 | } |
| 407 | ] |
| 408 | } |
| 409 | }, |
| 410 | { |
| 411 | "if": "{!$hasError AND $no_amount > 0}", |
| 412 | "app": "payment", |
| 413 | "payload": { |
| 414 | "asset": "{$no_asset}", |
| 415 | "outputs": [ |
| 416 | { |
| 417 | "address": "{$to}", |
| 418 | "amount": "{$no_amount}" |
| 419 | } |
| 420 | ] |
| 421 | } |
| 422 | }, |
| 423 | { |
| 424 | "if": "{!$hasError AND $allow_draw AND $draw_amount > 0}", |
| 425 | "app": "payment", |
| 426 | "payload": { |
| 427 | "asset": "{$draw_asset}", |
| 428 | "outputs": [ |
| 429 | { |
| 430 | "address": "{$to}", |
| 431 | "amount": "{$draw_amount}" |
| 432 | } |
| 433 | ] |
| 434 | } |
| 435 | }, |
| 436 | { |
| 437 | "if": "{$hasError}", |
| 438 | "app": "payment", |
| 439 | "payload": { |
| 440 | "asset": "{$reserve_asset}", |
| 441 | "outputs": [ |
| 442 | { |
| 443 | "address": "{$to}", |
| 444 | "amount": "{$reserve_amount - $network_fee}" |
| 445 | } |
| 446 | ] |
| 447 | } |
| 448 | }, |
| 449 | { |
| 450 | "if": "{!$hasError}", |
| 451 | "app": "state", |
| 452 | "state": "{ |
| 453 | var['reserve'] += $reserve_amount; |
| 454 | |
| 455 | var['supply_yes'] += $yes_amount; |
| 456 | response['supply_yes'] = var['supply_yes'] OTHERWISE 0; |
| 457 | |
| 458 | var['supply_no'] += $no_amount; |
| 459 | response['supply_no'] = var['supply_no'] OTHERWISE 0; |
| 460 | |
| 461 | if ($allow_draw) { |
| 462 | var['supply_draw'] += $draw_amount; |
| 463 | response['supply_draw'] = var['supply_draw'] OTHERWISE 0; |
| 464 | } |
| 465 | |
| 466 | $new_supply_yes = var['supply_yes'] OTHERWISE 0; |
| 467 | $new_supply_no = var['supply_no'] OTHERWISE 0; |
| 468 | $new_supply_draw = var['supply_draw'] OTHERWISE 0; |
| 469 | |
| 470 | $new_reserve = $get_reserve($new_supply_yes, $new_supply_no, $new_supply_draw); |
| 471 | |
| 472 | $next_coef = (var['coef'] OTHERWISE 1) * (($new_reserve + $fee) / $new_reserve); |
| 473 | |
| 474 | var['coef'] = $next_coef; |
| 475 | |
| 476 | |
| 477 | response['yes_amount'] = $yes_amount OTHERWISE 0; |
| 478 | response['no_amount'] = $no_amount OTHERWISE 0; |
| 479 | response['draw_amount'] = $draw_amount OTHERWISE 0; |
| 480 | |
| 481 | response['next_reserve'] = var['reserve']; |
| 482 | response['next_coef'] = $next_coef; |
| 483 | |
| 484 | $new_den = sqrt($new_supply_yes * $new_supply_yes + $new_supply_no * $new_supply_no + $new_supply_draw * $new_supply_draw); |
| 485 | |
| 486 | $new_yes_price = $next_coef * ($new_supply_yes / $new_den); |
| 487 | $new_no_price = $next_coef * ($new_supply_no / $new_den); |
| 488 | $new_draw_price = $next_coef * ($new_supply_draw / $new_den); |
| 489 | |
| 490 | response['yes_price'] = $new_yes_price; |
| 491 | response['no_price'] = $new_no_price; |
| 492 | response['draw_price'] = $new_draw_price; |
| 493 | }" |
| 494 | } |
| 495 | ] |
| 496 | }, |
| 497 | { |
| 498 | "if": "{$ready AND trigger.data.commit AND !$result}", |
| 499 | "init": "{ |
| 500 | require(timestamp > $end_of_trading_period, "trading period has not yet ended"); |
| 501 | |
| 502 | $current_datafeed_value = data_feed[[oracles=$oracle, feed_name=$feed_name, ifnone='none']]; |
| 503 | |
| 504 | require($current_datafeed_value != 'none', "data_feed is empty"); |
| 505 | require(timestamp > $end_of_trading_period, "trading period has not ended yet"); |
| 506 | require(timestamp < $end_of_waiting_period, "it's late, the waiting period has passed"); |
| 507 | |
| 508 | if ($comparison == '>') |
| 509 | $datafeed_comparison = $current_datafeed_value > $datafeed_value; |
| 510 | else if ($comparison == '<') |
| 511 | $datafeed_comparison = $current_datafeed_value < $datafeed_value; |
| 512 | else if ($comparison == '!=') |
| 513 | $datafeed_comparison = $current_datafeed_value != $datafeed_value; |
| 514 | else if ($comparison == '==') |
| 515 | $datafeed_comparison = $current_datafeed_value == $datafeed_value; |
| 516 | else if ($comparison == '>=') |
| 517 | $datafeed_comparison = $current_datafeed_value >= $datafeed_value; |
| 518 | else if ($comparison == '<=') |
| 519 | $datafeed_comparison = $current_datafeed_value <= $datafeed_value; |
| 520 | else |
| 521 | bounce('Comparison operator not found'); |
| 522 | |
| 523 | if ($datafeed_comparison) { |
| 524 | $res = 'yes'; |
| 525 | } else if ($allow_draw AND $current_datafeed_value == $datafeed_draw_value) { |
| 526 | $res = 'draw'; |
| 527 | } else { |
| 528 | $res = 'no'; |
| 529 | } |
| 530 | |
| 531 | }", |
| 532 | "messages": [ |
| 533 | { |
| 534 | "app": "state", |
| 535 | "state": "{ |
| 536 | var['result'] = $res; |
| 537 | response['messages'] = "The result is committed"; |
| 538 | response['result'] = $res; |
| 539 | }" |
| 540 | } |
| 541 | ] |
| 542 | }, |
| 543 | { |
| 544 | "if": "{$ready AND trigger.data.claim_profit}", |
| 545 | "init": "{ |
| 546 | $yes_amount = trigger.output[[asset=$yes_asset]]; |
| 547 | $no_amount = trigger.output[[asset=$no_asset]]; |
| 548 | $draw_amount = $allow_draw ? trigger.output[[asset=$draw_asset]] : 0; |
| 549 | |
| 550 | require(($reserve_asset == 'base' AND $reserve_amount == $network_fee) OR ($reserve_asset != 'base' AND $reserve_amount == 0), "should not send a reserve"); |
| 551 | |
| 552 | require($result, "no results yet"); |
| 553 | |
| 554 | $winner_amount = $result == 'yes' ? $yes_amount : $result == 'no' ? $no_amount : $draw_amount; |
| 555 | |
| 556 | $winner_supply = $result == 'yes' ? var['supply_yes'] : $result == 'no' ? var['supply_no'] : var['supply_draw']; |
| 557 | |
| 558 | require($winner_supply > 0 AND $winner_amount > 0, "winner supply < 0 or sending not a winner token"); |
| 559 | |
| 560 | $price_winner_by_reserve = $reserve / $winner_supply; |
| 561 | |
| 562 | $payout = floor($winner_amount * $price_winner_by_reserve); |
| 563 | }", |
| 564 | "messages": [ |
| 565 | { |
| 566 | "if": "{ $payout > 0 }", |
| 567 | "app": "payment", |
| 568 | "payload": { |
| 569 | "asset": "{$reserve_asset}", |
| 570 | "outputs": [ |
| 571 | { |
| 572 | "address": "{$to}", |
| 573 | "amount": "{$payout}" |
| 574 | } |
| 575 | ] |
| 576 | } |
| 577 | }, |
| 578 | { |
| 579 | "app": "state", |
| 580 | "state": "{ |
| 581 | response["profit"] = $payout; |
| 582 | |
| 583 | if ($yes_amount > 0) { |
| 584 | var['supply_yes'] -= $yes_amount; |
| 585 | } |
| 586 | |
| 587 | if ($no_amount > 0) { |
| 588 | var['supply_no'] -= $no_amount; |
| 589 | } |
| 590 | |
| 591 | if ($allow_draw AND $draw_amount > 0) { |
| 592 | var['supply_draw'] -= $draw_amount; |
| 593 | } |
| 594 | |
| 595 | var['reserve'] -= $payout; |
| 596 | }" |
| 597 | } |
| 598 | ] |
| 599 | } |
| 600 | ] |
| 601 | } |
| 602 | } |
| 603 | ] |