| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/bonded-stablecoin-deposits.json", |
| 5 | "getters": "{ |
| 6 | $_get_param = ($name, $state_params, $initial_params, $default_params) => { |
| 7 | $state_value = $state_params[$name]; |
| 8 | if (exists($state_value)) |
| 9 | return $state_value; |
| 10 | $initial_value = $initial_params[$name]; |
| 11 | if (exists($initial_value)) |
| 12 | return $initial_value; |
| 13 | $default_params[$name] |
| 14 | }; |
| 15 | |
| 16 | $get_curve_aa = () => params.curve_aa; |
| 17 | |
| 18 | $get_curve_aa_params = () => definition[params.curve_aa][1].params; |
| 19 | |
| 20 | $get_deposit_params = () => { |
| 21 | $default_params = { |
| 22 | min_deposit_term: 2 * 3600, |
| 23 | challenging_period: 12 * 3600, |
| 24 | challenge_immunity_period: 3600, |
| 25 | reporter_share: 0.2, |
| 26 | }; |
| 27 | $curve_aa_params = $get_curve_aa_params(); |
| 28 | $deposit_state_params = var[params.curve_aa]['deposit_params']; |
| 29 | { |
| 30 | min_deposit_term: $_get_param('min_deposit_term', $deposit_state_params, $curve_aa_params.deposits, $default_params), |
| 31 | challenging_period: $_get_param('challenging_period', $deposit_state_params, $curve_aa_params.deposits, $default_params), |
| 32 | challenge_immunity_period: $_get_param('challenge_immunity_period', $deposit_state_params, $curve_aa_params.deposits, $default_params), |
| 33 | reporter_share: $_get_param('reporter_share', $deposit_state_params, $curve_aa_params.deposits, $default_params), |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | }", |
| 38 | "init": "{ |
| 39 | $bank_aa = 'GV5YXIIRH3DH5FTEECW7IS2EQTAYJJ6S'; |
| 40 | $curve_aa = params.curve_aa; |
| 41 | |
| 42 | |
| 43 | $asset = var['asset']; |
| 44 | |
| 45 | $interest_rate = var[$curve_aa]['interest_rate']; |
| 46 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 47 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 48 | |
| 49 | $curve_aa_params = $get_curve_aa_params(); |
| 50 | $reserve_asset = $curve_aa_params.reserve_asset; |
| 51 | $interest_asset = var[$curve_aa]['asset2']; |
| 52 | |
| 53 | $deposit_params = $get_deposit_params(); |
| 54 | $min_deposit_term = $deposit_params.min_deposit_term; |
| 55 | $challenging_period = $deposit_params.challenging_period; |
| 56 | $challenge_immunity_period = $deposit_params.challenge_immunity_period; |
| 57 | $reporter_share = $deposit_params.reporter_share; |
| 58 | |
| 59 | $remove_deposit = ($id, $stable_amount) => { |
| 60 | var['deposit_' || $id] = false; |
| 61 | var['supply'] -= $stable_amount; |
| 62 | }; |
| 63 | |
| 64 | }", |
| 65 | "messages": { |
| 66 | "cases": [ |
| 67 | { |
| 68 | "if": "{ trigger.data.define AND !$asset }", |
| 69 | "messages": [ |
| 70 | { |
| 71 | "app": "asset", |
| 72 | "payload": { |
| 73 | "is_private": false, |
| 74 | "is_transferrable": true, |
| 75 | "auto_destroy": false, |
| 76 | "fixed_denominations": false, |
| 77 | "issued_by_definer_only": true, |
| 78 | "cosigned_by_definer": false, |
| 79 | "spender_attested": false |
| 80 | } |
| 81 | }, |
| 82 | { |
| 83 | "if": "{trigger.data.factory}", |
| 84 | "app": "data", |
| 85 | "payload": { |
| 86 | "write_stable_asset": 1, |
| 87 | "curve_aa": "{$curve_aa}" |
| 88 | } |
| 89 | }, |
| 90 | { |
| 91 | "if": "{trigger.data.factory}", |
| 92 | "app": "payment", |
| 93 | "payload": { |
| 94 | "asset": "base", |
| 95 | "outputs": [ |
| 96 | { |
| 97 | "address": "{trigger.data.factory}" |
| 98 | } |
| 99 | ] |
| 100 | } |
| 101 | }, |
| 102 | { |
| 103 | "app": "state", |
| 104 | "state": "{ |
| 105 | var['asset'] = response_unit; |
| 106 | response['asset'] = response_unit; |
| 107 | }" |
| 108 | } |
| 109 | ] |
| 110 | }, |
| 111 | { |
| 112 | "if": "{ $asset AND trigger.output[[asset=$interest_asset]] > 0 }", |
| 113 | "init": "{ |
| 114 | if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient)) |
| 115 | bounce("bad address of interest recipient"); |
| 116 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 117 | bounce("bad to-address"); |
| 118 | if (exists(trigger.data.owner) AND !is_valid_address(trigger.data.owner)) |
| 119 | bounce("bad owner address"); |
| 120 | $to = trigger.data.to OTHERWISE trigger.address; |
| 121 | $owner = trigger.data.owner OTHERWISE $to; |
| 122 | $deposit_amount = trigger.output[[asset=$interest_asset]]; |
| 123 | $stable_amount = floor($deposit_amount * $growth_factor); |
| 124 | $protection = max(0, trigger.output[[asset=$reserve_asset]] - ($reserve_asset == 'base' ? 10000 : 0)); |
| 125 | $id = trigger.unit; |
| 126 | }", |
| 127 | "messages": [ |
| 128 | { |
| 129 | "app": "payment", |
| 130 | "payload": { |
| 131 | "asset": "{$asset}", |
| 132 | "outputs": [ |
| 133 | { |
| 134 | "address": "{$to}", |
| 135 | "amount": "{ $stable_amount }" |
| 136 | } |
| 137 | ] |
| 138 | } |
| 139 | }, |
| 140 | { |
| 141 | "if": "{trigger.data.to AND is_aa(trigger.data.to)}", |
| 142 | "app": "data", |
| 143 | "payload": { |
| 144 | "to": "{trigger.address}" |
| 145 | } |
| 146 | }, |
| 147 | { |
| 148 | "app": "state", |
| 149 | "state": "{ |
| 150 | response['id'] = $id; |
| 151 | $deposit = { |
| 152 | amount: $deposit_amount, |
| 153 | stable_amount: $stable_amount, |
| 154 | owner: $owner, |
| 155 | ts: timestamp, |
| 156 | }; |
| 157 | if (trigger.data.interest_recipient) |
| 158 | $deposit.interest_recipient = trigger.data.interest_recipient; |
| 159 | if ($protection) |
| 160 | $deposit.protection = $protection; |
| 161 | var['deposit_' || $id] = $deposit; |
| 162 | var['supply'] += $stable_amount; |
| 163 | }" |
| 164 | } |
| 165 | ] |
| 166 | }, |
| 167 | { |
| 168 | "if": "{ $asset AND trigger.output[[asset=$asset]] > 0 AND trigger.data.id }", |
| 169 | "init": "{ |
| 170 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 171 | bounce("bad to-address"); |
| 172 | $to = trigger.data.to OTHERWISE trigger.address; |
| 173 | $id = trigger.data.id; |
| 174 | $deposit = var['deposit_' || $id]; |
| 175 | if (!$deposit) |
| 176 | bounce("no such deposit"); |
| 177 | if (timestamp < $deposit.ts + $min_deposit_term) |
| 178 | bounce("the deposit is too new to be closed"); |
| 179 | $bOwner = ($deposit.owner == trigger.address); |
| 180 | $bForceClose = !$bOwner; |
| 181 | if (var['deposit_' || $id || '_force_close']) |
| 182 | bounce("force-close already requested"); |
| 183 | if ($deposit.interest_recipient AND $deposit.interest_recipient != $deposit.owner OR $bForceClose){ |
| 184 | $new_stable_amount = floor($deposit.amount * $growth_factor); |
| 185 | $interest = $new_stable_amount - $deposit.stable_amount; |
| 186 | $expected_stable_amount = $new_stable_amount; |
| 187 | } |
| 188 | else |
| 189 | $expected_stable_amount = $deposit.stable_amount; |
| 190 | if ($expected_stable_amount > trigger.output[[asset=$asset]]) |
| 191 | bounce("expected " || $expected_stable_amount); |
| 192 | $change = trigger.output[[asset=$asset]] - $expected_stable_amount; |
| 193 | $to_aa = trigger.data.to AND is_aa(trigger.data.to); |
| 194 | }", |
| 195 | "messages": [ |
| 196 | { |
| 197 | "if": "{$bOwner}", |
| 198 | "app": "payment", |
| 199 | "payload": { |
| 200 | "asset": "{$interest_asset}", |
| 201 | "outputs": [ |
| 202 | { |
| 203 | "address": "{$to}", |
| 204 | "amount": "{ $deposit.amount }" |
| 205 | } |
| 206 | ] |
| 207 | } |
| 208 | }, |
| 209 | { |
| 210 | "if": "{$to_aa AND $bOwner}", |
| 211 | "app": "data", |
| 212 | "payload": { |
| 213 | "to": "{trigger.address}" |
| 214 | } |
| 215 | }, |
| 216 | { |
| 217 | "if": "{$to_aa AND $bOwner}", |
| 218 | "app": "payment", |
| 219 | "payload": { |
| 220 | "asset": "base", |
| 221 | "outputs": [ |
| 222 | { |
| 223 | "address": "{$to}", |
| 224 | "amount": 2000 |
| 225 | } |
| 226 | ] |
| 227 | } |
| 228 | }, |
| 229 | { |
| 230 | "app": "payment", |
| 231 | "payload": { |
| 232 | "asset": "{$asset}", |
| 233 | "outputs": [ |
| 234 | { |
| 235 | "if": "{$interest AND $bOwner}", |
| 236 | "address": "{$deposit.interest_recipient}", |
| 237 | "amount": "{ $interest }" |
| 238 | }, |
| 239 | { |
| 240 | "address": "{trigger.address}", |
| 241 | "amount": "{$change}" |
| 242 | } |
| 243 | ] |
| 244 | } |
| 245 | }, |
| 246 | { |
| 247 | "if": "{$deposit.protection AND $bOwner}", |
| 248 | "app": "payment", |
| 249 | "payload": { |
| 250 | "asset": "{$reserve_asset}", |
| 251 | "outputs": [ |
| 252 | { |
| 253 | "address": "{$deposit.owner}", |
| 254 | "amount": "{ $deposit.protection }" |
| 255 | } |
| 256 | ] |
| 257 | } |
| 258 | }, |
| 259 | { |
| 260 | "app": "state", |
| 261 | "state": "{ |
| 262 | if ($bOwner) |
| 263 | $remove_deposit($id, $deposit.stable_amount); |
| 264 | else |
| 265 | var['deposit_' || $id || '_force_close'] = { |
| 266 | ts: timestamp, |
| 267 | closer: trigger.address, |
| 268 | interest: $interest, |
| 269 | protection_ratio: $deposit.protection / $deposit.amount, |
| 270 | }; |
| 271 | }" |
| 272 | } |
| 273 | ] |
| 274 | }, |
| 275 | { |
| 276 | "if": "{ $asset AND trigger.data.commit_force_close AND trigger.data.id }", |
| 277 | "init": "{ |
| 278 | $id = trigger.data.id; |
| 279 | $deposit = var['deposit_' || $id]; |
| 280 | if (!$deposit) |
| 281 | bounce("no such deposit"); |
| 282 | $force_close = var['deposit_' || $id || '_force_close']; |
| 283 | if (!$force_close) |
| 284 | bounce("this deposit was not force closed"); |
| 285 | if (timestamp < $force_close.ts + $challenging_period) |
| 286 | bounce("challenging period has not expired yet"); |
| 287 | $interest_recipient = $deposit.interest_recipient OTHERWISE $deposit.owner; |
| 288 | |
| 289 | $payments = [ |
| 290 | { |
| 291 | address: $force_close.closer, |
| 292 | asset: $interest_asset, |
| 293 | amount: $deposit.amount |
| 294 | }, |
| 295 | { |
| 296 | |
| 297 | address: $interest_recipient, |
| 298 | asset: $asset, |
| 299 | amount: $force_close.interest |
| 300 | }, |
| 301 | { |
| 302 | address: $deposit.owner, |
| 303 | asset: $reserve_asset, |
| 304 | amount: $deposit.protection |
| 305 | }, |
| 306 | ]; |
| 307 | foreach($payments, 3, ($i, $payment) => { |
| 308 | $payments[$i].is_aa = is_aa($payment.address); |
| 309 | }); |
| 310 | $res = $bank_aa.$get_payment_messages($payments); |
| 311 | |
| 312 | $payment_messages = $res.payment_messages; |
| 313 | $buffer_recipients = $res.buffer_recipients; |
| 314 | }", |
| 315 | "messages": [ |
| 316 | "{$payment_messages[0] OTHERWISE ''}", |
| 317 | "{$payment_messages[1] OTHERWISE ''}", |
| 318 | "{$payment_messages[2] OTHERWISE ''}", |
| 319 | "{$payment_messages[3] OTHERWISE ''}", |
| 320 | { |
| 321 | "if": "{length($buffer_recipients)}", |
| 322 | "app": "data", |
| 323 | "payload": { |
| 324 | "recipients": "{$buffer_recipients}" |
| 325 | } |
| 326 | }, |
| 327 | { |
| 328 | "app": "state", |
| 329 | "state": "{ |
| 330 | $remove_deposit($id, $deposit.stable_amount); |
| 331 | var['deposit_' || $id || '_force_close'] = false; |
| 332 | var['last_force_closed_protection_ratio'] = $force_close.protection_ratio; |
| 333 | }" |
| 334 | } |
| 335 | ] |
| 336 | }, |
| 337 | { |
| 338 | "if": "{ $asset AND trigger.data.challenge_force_close AND trigger.data.id AND trigger.data.weaker_id }", |
| 339 | "init": "{ |
| 340 | $id = trigger.data.id; |
| 341 | $weaker_id = trigger.data.weaker_id; |
| 342 | |
| 343 | $deposit = var['deposit_' || $id]; |
| 344 | if (!$deposit) |
| 345 | bounce("deposit not found"); |
| 346 | |
| 347 | |
| 348 | $force_close = var['deposit_' || $id || '_force_close']; |
| 349 | if (!$force_close) |
| 350 | bounce("this deposit was not force closed"); |
| 351 | |
| 352 | $weaker_deposit = var['deposit_' || $weaker_id]; |
| 353 | if (!$weaker_deposit) |
| 354 | bounce("weaker deposit doesn't exist"); |
| 355 | |
| 356 | if (var['deposit_' || $weaker_id || '_force_close']) |
| 357 | bounce("the weaker deposit is also challenged"); |
| 358 | |
| 359 | if ($weaker_deposit.ts + $min_deposit_term + $challenge_immunity_period > $force_close.ts) |
| 360 | bounce("the weaker deposit is too new"); |
| 361 | |
| 362 | $weaker_protection_withdrawal_ts = $weaker_deposit.protection_withdrawal_ts OTHERWISE 0; |
| 363 | if ($weaker_protection_withdrawal_ts > $force_close.ts - $challenge_immunity_period) |
| 364 | bounce("weaker deposit's protection was decreased recently"); |
| 365 | |
| 366 | $weaker_protection_ratio = $weaker_deposit.protection / $weaker_deposit.amount; |
| 367 | if ($weaker_protection_ratio >= $force_close.protection_ratio) |
| 368 | bounce("the weaker deposit does not appear to be weaker: " || $weaker_protection_ratio); |
| 369 | |
| 370 | $closer_amount = $deposit.stable_amount + $force_close.interest; |
| 371 | $reporter_reward_amount = ceil($closer_amount * $reporter_share); |
| 372 | $closer_refund_amount = $closer_amount - $reporter_reward_amount; |
| 373 | |
| 374 | |
| 375 | $payments = [ |
| 376 | { |
| 377 | address: $force_close.closer, |
| 378 | asset: $asset, |
| 379 | amount: $closer_refund_amount, |
| 380 | is_aa: is_aa($force_close.closer) |
| 381 | }, |
| 382 | { |
| 383 | address: trigger.address, |
| 384 | asset: $asset, |
| 385 | amount: $reporter_reward_amount, |
| 386 | is_aa: false |
| 387 | }, |
| 388 | ]; |
| 389 | $res = $bank_aa.$get_payment_messages($payments); |
| 390 | $payment_messages = $res.payment_messages; |
| 391 | $buffer_recipients = $res.buffer_recipients; |
| 392 | }", |
| 393 | "messages": [ |
| 394 | "{$payment_messages[0] OTHERWISE ''}", |
| 395 | "{$payment_messages[1] OTHERWISE ''}", |
| 396 | { |
| 397 | "if": "{length($buffer_recipients)}", |
| 398 | "app": "data", |
| 399 | "payload": { |
| 400 | "recipients": "{$buffer_recipients}" |
| 401 | } |
| 402 | }, |
| 403 | { |
| 404 | "app": "state", |
| 405 | "state": "{ |
| 406 | |
| 407 | var['deposit_' || $id || '_force_close'] = false; |
| 408 | }" |
| 409 | } |
| 410 | ] |
| 411 | }, |
| 412 | { |
| 413 | "if": "{ $asset AND trigger.data.id AND trigger.data.add_protection AND trigger.output[[asset=$reserve_asset]] > 0 }", |
| 414 | "init": "{ |
| 415 | $id = trigger.data.id; |
| 416 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 417 | bounce("bad to-address"); |
| 418 | $to = trigger.data.to OTHERWISE trigger.address; |
| 419 | $deposit = var['deposit_' || $id]; |
| 420 | if (!$deposit) |
| 421 | bounce("deposit not found"); |
| 422 | if ($deposit.owner != $to) |
| 423 | bounce("you are not the owner"); |
| 424 | }", |
| 425 | "messages": [ |
| 426 | { |
| 427 | "app": "state", |
| 428 | "state": "{ |
| 429 | $deposit.protection = $deposit.protection + trigger.output[[asset=$reserve_asset]]; |
| 430 | var['deposit_' || $id] = $deposit; |
| 431 | }" |
| 432 | } |
| 433 | ] |
| 434 | }, |
| 435 | { |
| 436 | "if": "{ $asset AND trigger.data.id AND trigger.data.withdraw_protection AND trigger.data.amount }", |
| 437 | "init": "{ |
| 438 | $id = trigger.data.id; |
| 439 | if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to)) |
| 440 | bounce("bad to-address"); |
| 441 | $to = trigger.data.to OTHERWISE trigger.address; |
| 442 | $deposit = var['deposit_' || $id]; |
| 443 | if (!$deposit) |
| 444 | bounce("deposit not found"); |
| 445 | if ($deposit.owner != trigger.address) |
| 446 | bounce("you are not the owner"); |
| 447 | $amount = trigger.data.amount; |
| 448 | if ($amount == 'all') |
| 449 | $withdraw_amount = $deposit.protection; |
| 450 | else { |
| 451 | if (!is_integer($amount) OR $amount <= 0) |
| 452 | bounce("bad amount: " || $amount); |
| 453 | $withdraw_amount = $amount; |
| 454 | } |
| 455 | if ($withdraw_amount > $deposit.protection) |
| 456 | bounce("trying to withdraw more than you have: " || $deposit.protection); |
| 457 | }", |
| 458 | "messages": [ |
| 459 | { |
| 460 | "app": "payment", |
| 461 | "payload": { |
| 462 | "asset": "{$reserve_asset}", |
| 463 | "outputs": [ |
| 464 | { |
| 465 | "address": "{$to}", |
| 466 | "amount": "{ $withdraw_amount }" |
| 467 | } |
| 468 | ] |
| 469 | } |
| 470 | }, |
| 471 | { |
| 472 | "app": "state", |
| 473 | "state": "{ |
| 474 | $deposit.protection = $deposit.protection - $withdraw_amount; |
| 475 | $deposit.protection_withdrawal_ts = timestamp; |
| 476 | var['deposit_' || $id] = $deposit; |
| 477 | }" |
| 478 | } |
| 479 | ] |
| 480 | }, |
| 481 | { |
| 482 | "if": "{ $asset AND trigger.data.id AND trigger.data.change_interest_recipient }", |
| 483 | "init": "{ |
| 484 | |
| 485 | if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient)) |
| 486 | bounce("bad address of new interest recipient"); |
| 487 | $id = trigger.data.id; |
| 488 | $deposit = var['deposit_' || $id]; |
| 489 | if (!$deposit) |
| 490 | bounce("deposit not found"); |
| 491 | if ($deposit.owner != trigger.address) |
| 492 | bounce("you are not the owner"); |
| 493 | if (var['deposit_' || $id || '_force_close']) |
| 494 | bounce("force-close requested, can't change interest recipient"); |
| 495 | $old_recipient = $deposit.interest_recipient OTHERWISE trigger.address; |
| 496 | $new_stable_amount = floor($deposit.amount * $growth_factor); |
| 497 | $interest = $new_stable_amount - $deposit.stable_amount; |
| 498 | if ($interest < 0) |
| 499 | bounce("negative interest?"); |
| 500 | }", |
| 501 | "messages": [ |
| 502 | { |
| 503 | "if": "{$interest > 0}", |
| 504 | "app": "payment", |
| 505 | "payload": { |
| 506 | "asset": "{$asset}", |
| 507 | "outputs": [ |
| 508 | { |
| 509 | "address": "{$old_recipient}", |
| 510 | "amount": "{ $interest }" |
| 511 | } |
| 512 | ] |
| 513 | } |
| 514 | }, |
| 515 | { |
| 516 | "app": "state", |
| 517 | "state": "{ |
| 518 | $deposit.stable_amount = $new_stable_amount; |
| 519 | if (trigger.data.interest_recipient) |
| 520 | $deposit.interest_recipient = trigger.data.interest_recipient; |
| 521 | else |
| 522 | delete($deposit, 'interest_recipient'); |
| 523 | var['deposit_' || $id] = $deposit; |
| 524 | var['supply'] += $interest; |
| 525 | }" |
| 526 | } |
| 527 | ] |
| 528 | }, |
| 529 | { |
| 530 | "if": "{ $asset AND trigger.data.id }", |
| 531 | "init": "{ |
| 532 | $id = trigger.data.id; |
| 533 | $deposit = var['deposit_' || $id]; |
| 534 | if (!$deposit) |
| 535 | bounce("deposit not found"); |
| 536 | $recipient = $deposit.interest_recipient OTHERWISE trigger.address; |
| 537 | |
| 538 | if (!$deposit.interest_recipient AND $deposit.owner != trigger.address) |
| 539 | bounce("you are not the owner"); |
| 540 | if (var['deposit_' || $id || '_force_close']) |
| 541 | bounce("force-close requested, can't pay interest"); |
| 542 | $new_stable_amount = floor($deposit.amount * $growth_factor); |
| 543 | $interest = $new_stable_amount - $deposit.stable_amount; |
| 544 | if ($interest < 0) |
| 545 | bounce("negative interest?"); |
| 546 | |
| 547 | |
| 548 | }", |
| 549 | "messages": [ |
| 550 | { |
| 551 | "app": "payment", |
| 552 | "payload": { |
| 553 | "asset": "{$asset}", |
| 554 | "outputs": [ |
| 555 | { |
| 556 | "address": "{$recipient}", |
| 557 | "amount": "{ $interest }" |
| 558 | } |
| 559 | ] |
| 560 | } |
| 561 | }, |
| 562 | { |
| 563 | "app": "state", |
| 564 | "state": "{ |
| 565 | $deposit.stable_amount = $new_stable_amount; |
| 566 | var['deposit_' || $id] = $deposit; |
| 567 | var['supply'] += $interest; |
| 568 | }" |
| 569 | } |
| 570 | ] |
| 571 | } |
| 572 | ] |
| 573 | } |
| 574 | } |
| 575 | ] |