| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/stablecoin-interest-arbitrage.json", |
| 5 | "getters": "{ |
| 6 | $get_deposit_aa = () => params.deposit_aa; |
| 7 | $get_curve_aa = () => definition[params.deposit_aa][1].params.curve_aa; |
| 8 | $get_oswap_aa = () => params.oswap_aa; |
| 9 | $get_manager = () => params.manager; |
| 10 | $get_management_fee = () => params.management_fee; |
| 11 | $get_success_fee = () => params.success_fee; |
| 12 | $get_oswap_fee = () => definition[params.oswap_aa][1].params.swap_fee / 1e11; |
| 13 | |
| 14 | $get_oswap_output = ($in_amount, $in_asset, $out_asset) => { |
| 15 | $fee = $get_oswap_fee(); |
| 16 | $net_in_amount = $in_amount * (1 - $fee); |
| 17 | $in_balance = balance[params.oswap_aa][$in_asset]; |
| 18 | $out_balance = balance[params.oswap_aa][$out_asset]; |
| 19 | $out_amount = $out_balance * $net_in_amount / ($in_balance + $net_in_amount); |
| 20 | floor($out_amount) |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | $get_oswap_input = ($out_amount, $in_asset, $out_asset) => { |
| 25 | $fee = $get_oswap_fee(); |
| 26 | $in_balance = balance[params.oswap_aa][$in_asset]; |
| 27 | $out_balance = balance[params.oswap_aa][$out_asset]; |
| 28 | if ($out_amount >= $out_balance) |
| 29 | bounce("not enough out asset in the pool"); |
| 30 | $net_in_amount = $in_balance * $out_amount / ($out_balance - $out_amount); |
| 31 | $in_amount = $net_in_amount / (1 - $fee); |
| 32 | ceil($in_amount) |
| 33 | }; |
| 34 | |
| 35 | }", |
| 36 | "init": "{ |
| 37 | $trigger_reward = 20000; |
| 38 | $bank_aa = 'GV5YXIIRH3DH5FTEECW7IS2EQTAYJJ6S'; |
| 39 | $deposit_aa = params.deposit_aa; |
| 40 | $curve_aa = $get_curve_aa(); |
| 41 | |
| 42 | |
| 43 | $stable_asset = var[$deposit_aa]['asset']; |
| 44 | $interest_asset = var[$curve_aa]['asset2']; |
| 45 | $shares_asset = var['shares_asset']; |
| 46 | |
| 47 | $interest_rate = var[$curve_aa]['interest_rate']; |
| 48 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 49 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 50 | |
| 51 | $status = var['status']; |
| 52 | |
| 53 | |
| 54 | $get_interest_balance = () => balance[$interest_asset] + var[$bank_aa]['balance_' || this_address || '_' || $interest_asset] + var['balance_in_challenging_period']; |
| 55 | |
| 56 | |
| 57 | $get_optimal_deposit_amount = () => { |
| 58 | $target_price = $growth_factor; |
| 59 | $fee = $get_oswap_fee(); |
| 60 | $interest_balance = balance[params.oswap_aa][$interest_asset]; |
| 61 | $stable_balance = balance[params.oswap_aa][$stable_asset]; |
| 62 | $net_share = 1 - $fee; |
| 63 | $deposit_amount = (sqrt($interest_balance * $stable_balance * $net_share * $target_price) - $stable_balance) / ($net_share * $target_price); |
| 64 | floor($deposit_amount) |
| 65 | }; |
| 66 | |
| 67 | }", |
| 68 | "messages": { |
| 69 | "cases": [ |
| 70 | { |
| 71 | "if": "{ trigger.data.define AND !$shares_asset }", |
| 72 | "messages": [ |
| 73 | { |
| 74 | "app": "asset", |
| 75 | "payload": { |
| 76 | "is_private": false, |
| 77 | "is_transferrable": true, |
| 78 | "auto_destroy": false, |
| 79 | "fixed_denominations": false, |
| 80 | "issued_by_definer_only": true, |
| 81 | "cosigned_by_definer": false, |
| 82 | "spender_attested": false |
| 83 | } |
| 84 | }, |
| 85 | { |
| 86 | "if": "{trigger.data.factory}", |
| 87 | "app": "payment", |
| 88 | "payload": { |
| 89 | "asset": "base", |
| 90 | "outputs": [ |
| 91 | { |
| 92 | "address": "{trigger.data.factory}", |
| 93 | "amount": 1000 |
| 94 | } |
| 95 | ] |
| 96 | } |
| 97 | }, |
| 98 | { |
| 99 | "app": "state", |
| 100 | "state": "{ |
| 101 | var['last_mf_withdrawal_ts'] = timestamp; |
| 102 | var['last_sf_withdrawal_share_price'] = 1; |
| 103 | var['shares_asset'] = response_unit; |
| 104 | response['shares_asset'] = response_unit; |
| 105 | }" |
| 106 | } |
| 107 | ] |
| 108 | }, |
| 109 | { |
| 110 | "if": "{ trigger.data.open_deposit }", |
| 111 | "init": "{ |
| 112 | $deposit_amount = min($get_optimal_deposit_amount(), balance[$interest_asset]); |
| 113 | if ($deposit_amount <= 0) |
| 114 | bounce("would lose money"); |
| 115 | |
| 116 | |
| 117 | $stable_amount = floor($deposit_amount * $growth_factor); |
| 118 | $out_amount = $get_oswap_output($stable_amount, $stable_asset, $interest_asset); |
| 119 | $profit = $out_amount - $deposit_amount; |
| 120 | if ($profit <= 0) |
| 121 | bounce("unexpected: would lose money"); |
| 122 | |
| 123 | |
| 124 | if (trigger.address != params.manager AND balance[base] - storage_size > 2 * $trigger_reward) |
| 125 | $reward = $trigger_reward; |
| 126 | }", |
| 127 | "messages": [ |
| 128 | { |
| 129 | "app": "payment", |
| 130 | "payload": { |
| 131 | "asset": "{$interest_asset}", |
| 132 | "outputs": [ |
| 133 | { |
| 134 | "address": "{$deposit_aa}", |
| 135 | "amount": "{ $deposit_amount }" |
| 136 | } |
| 137 | ] |
| 138 | } |
| 139 | }, |
| 140 | { |
| 141 | "if": "{$reward}", |
| 142 | "app": "payment", |
| 143 | "payload": { |
| 144 | "asset": "base", |
| 145 | "outputs": [ |
| 146 | { |
| 147 | "address": "{trigger.address}", |
| 148 | "amount": "{ $reward }" |
| 149 | } |
| 150 | ] |
| 151 | } |
| 152 | }, |
| 153 | { |
| 154 | "app": "state", |
| 155 | "state": "{ |
| 156 | var['status'] = 'opening_deposit'; |
| 157 | var['expected_stable_amount'] = $stable_amount; |
| 158 | var['expected_interest_amount'] = $out_amount; |
| 159 | response['expected_profit'] = $profit; |
| 160 | }" |
| 161 | } |
| 162 | ] |
| 163 | }, |
| 164 | { |
| 165 | "if": "{ trigger.address == $deposit_aa AND trigger.output[[asset=$stable_asset]] > 0 AND $status AND $status == 'opening_deposit' }", |
| 166 | "init": "{ |
| 167 | $received_stable_amount = trigger.output[[asset=$stable_asset]]; |
| 168 | $expected_stable_amount = var['expected_stable_amount']; |
| 169 | if ($received_stable_amount != $expected_stable_amount) |
| 170 | bounce("wrong stable amount received from deposit AA: expected " || $expected_stable_amount || ", got " || $received_stable_amount); |
| 171 | }", |
| 172 | "messages": [ |
| 173 | { |
| 174 | "app": "payment", |
| 175 | "payload": { |
| 176 | "asset": "{$stable_asset}", |
| 177 | "outputs": [ |
| 178 | { |
| 179 | "address": "{params.oswap_aa}", |
| 180 | "amount": "{ $received_stable_amount }" |
| 181 | } |
| 182 | ] |
| 183 | } |
| 184 | }, |
| 185 | { |
| 186 | "app": "payment", |
| 187 | "payload": { |
| 188 | "asset": "base", |
| 189 | "outputs": [ |
| 190 | { |
| 191 | "address": "{params.oswap_aa}", |
| 192 | "amount": 1000 |
| 193 | } |
| 194 | ] |
| 195 | } |
| 196 | }, |
| 197 | { |
| 198 | "app": "data", |
| 199 | "payload": { |
| 200 | "to": "{this_address}" |
| 201 | } |
| 202 | }, |
| 203 | { |
| 204 | "app": "state", |
| 205 | "state": "{ |
| 206 | var['status'] = 'swapping_s2i'; |
| 207 | }" |
| 208 | } |
| 209 | ] |
| 210 | }, |
| 211 | { |
| 212 | "if": "{ trigger.address == params.oswap_aa AND trigger.output[[asset=$interest_asset]] > 0 AND $status AND $status == 'swapping_s2i' }", |
| 213 | "init": "{ |
| 214 | $received_interest_amount = trigger.output[[asset=$interest_asset]]; |
| 215 | $expected_interest_amount = var['expected_interest_amount']; |
| 216 | if ($received_interest_amount != $expected_interest_amount) |
| 217 | bounce("wrong interest amount received from oswap AA: expected " || $expected_interest_amount || ", got " || $received_interest_amount); |
| 218 | }", |
| 219 | "messages": [ |
| 220 | { |
| 221 | "app": "state", |
| 222 | "state": "{ |
| 223 | var['expected_stable_amount'] = false; |
| 224 | var['expected_interest_amount'] = false; |
| 225 | var['status'] = false; |
| 226 | }" |
| 227 | } |
| 228 | ] |
| 229 | }, |
| 230 | { |
| 231 | "if": "{ trigger.data.close_deposit AND trigger.data.id }", |
| 232 | "init": "{ |
| 233 | if (trigger.address != params.manager) |
| 234 | bounce("you are not the manager"); |
| 235 | $deposit = var[$deposit_aa]['deposit_' || trigger.data.id]; |
| 236 | if (!$deposit) |
| 237 | bounce("no such deposit"); |
| 238 | $stable_amount = ($deposit.owner == this_address) ? $deposit.stable_amount : floor($deposit.amount * $growth_factor); |
| 239 | $in_amount = $get_oswap_input($stable_amount, $interest_asset, $stable_asset); |
| 240 | $profit = $deposit.amount - $in_amount; |
| 241 | if ($profit <= 0) |
| 242 | bounce("would lose money"); |
| 243 | }", |
| 244 | "messages": [ |
| 245 | { |
| 246 | "app": "payment", |
| 247 | "payload": { |
| 248 | "asset": "{$interest_asset}", |
| 249 | "outputs": [ |
| 250 | { |
| 251 | "address": "{params.oswap_aa}", |
| 252 | "amount": "{ $in_amount }" |
| 253 | } |
| 254 | ] |
| 255 | } |
| 256 | }, |
| 257 | { |
| 258 | "app": "data", |
| 259 | "payload": { |
| 260 | "to": "{this_address}" |
| 261 | } |
| 262 | }, |
| 263 | { |
| 264 | "app": "state", |
| 265 | "state": "{ |
| 266 | var['status'] = 'swapping_i2s'; |
| 267 | var['expected_stable_amount'] = $stable_amount; |
| 268 | var['id'] = trigger.data.id; |
| 269 | if ($deposit.owner != this_address){ |
| 270 | var['amount_' || trigger.data.id] = $deposit.amount; |
| 271 | var['balance_in_challenging_period'] += $deposit.amount; |
| 272 | response['expected_profit'] = $profit; |
| 273 | } |
| 274 | }" |
| 275 | } |
| 276 | ] |
| 277 | }, |
| 278 | { |
| 279 | "if": "{ trigger.address == params.oswap_aa AND trigger.output[[asset=$stable_asset]] > 0 AND $status AND $status == 'swapping_i2s' }", |
| 280 | "init": "{ |
| 281 | $received_stable_amount = trigger.output[[asset=$stable_asset]]; |
| 282 | $expected_stable_amount = var['expected_stable_amount']; |
| 283 | if ($received_stable_amount < $expected_stable_amount) |
| 284 | bounce("wrong stable amount received from oswap AA: expected " || $expected_stable_amount || ", got " || $received_stable_amount); |
| 285 | }", |
| 286 | "messages": [ |
| 287 | { |
| 288 | "app": "payment", |
| 289 | "payload": { |
| 290 | "asset": "{$stable_asset}", |
| 291 | "outputs": [ |
| 292 | { |
| 293 | "address": "{$deposit_aa}", |
| 294 | "amount": "{ $expected_stable_amount }" |
| 295 | } |
| 296 | ] |
| 297 | } |
| 298 | }, |
| 299 | { |
| 300 | "app": "data", |
| 301 | "payload": { |
| 302 | "id": "{var['id']}" |
| 303 | } |
| 304 | }, |
| 305 | { |
| 306 | "app": "state", |
| 307 | "state": "{ |
| 308 | var['expected_stable_amount'] = false; |
| 309 | var['id'] = false; |
| 310 | var['status'] = false; |
| 311 | }" |
| 312 | } |
| 313 | ] |
| 314 | }, |
| 315 | { |
| 316 | "if": "{ (trigger.address == $deposit_aa OR trigger.address == params.oswap_aa OR trigger.address == $bank_aa) AND trigger.output[[asset=$interest_asset]] > 0 AND !$status }", |
| 317 | "messages": [ |
| 318 | { |
| 319 | "app": "state", |
| 320 | "state": "{ |
| 321 | |
| 322 | }" |
| 323 | } |
| 324 | ] |
| 325 | }, |
| 326 | { |
| 327 | "if": "{ (trigger.data.sell_stable OR (trigger.address == $deposit_aa OR trigger.address == $bank_aa) AND trigger.output[[asset=$stable_asset]] > 0) AND !$status }", |
| 328 | "messages": [ |
| 329 | { |
| 330 | "app": "payment", |
| 331 | "payload": { |
| 332 | "asset": "{$stable_asset}", |
| 333 | "outputs": [ |
| 334 | { |
| 335 | "address": "{params.oswap_aa}" |
| 336 | } |
| 337 | ] |
| 338 | } |
| 339 | }, |
| 340 | { |
| 341 | "app": "data", |
| 342 | "payload": { |
| 343 | "to": "{this_address}" |
| 344 | } |
| 345 | } |
| 346 | ] |
| 347 | }, |
| 348 | { |
| 349 | "if": "{ trigger.data.unlock AND trigger.data.id }", |
| 350 | "init": "{ |
| 351 | $id = trigger.data.id; |
| 352 | $amount = var['amount_' || $id]; |
| 353 | if (!$amount) |
| 354 | bounce("we have no funds locked in closure of this deposit"); |
| 355 | if (var[$deposit_aa]['deposit_' || $id || '_force_close']) |
| 356 | bounce("this deposit is still in challenging period"); |
| 357 | }", |
| 358 | "messages": [ |
| 359 | { |
| 360 | "app": "state", |
| 361 | "state": "{ |
| 362 | var['amount_' || $id] = false; |
| 363 | var['balance_in_challenging_period'] -= $amount; |
| 364 | }" |
| 365 | } |
| 366 | ] |
| 367 | }, |
| 368 | { |
| 369 | "if": "{ trigger.data.challenge_force_close AND trigger.data.id AND trigger.data.weaker_id AND trigger.output[[asset=base]] >= 3000 }", |
| 370 | "messages": [ |
| 371 | { |
| 372 | "app": "payment", |
| 373 | "payload": { |
| 374 | "asset": "base", |
| 375 | "outputs": [ |
| 376 | { |
| 377 | "address": "{$deposit_aa}", |
| 378 | "amount": 2000 |
| 379 | } |
| 380 | ] |
| 381 | } |
| 382 | }, |
| 383 | { |
| 384 | "app": "data", |
| 385 | "payload": "{trigger.data}" |
| 386 | } |
| 387 | ] |
| 388 | }, |
| 389 | { |
| 390 | "if": "{ trigger.data.withdraw_from_bank AND trigger.data.asset AND (trigger.data.asset == $interest_asset OR trigger.data.asset == $stable_asset) }", |
| 391 | "messages": [ |
| 392 | { |
| 393 | "app": "payment", |
| 394 | "payload": { |
| 395 | "asset": "base", |
| 396 | "outputs": [ |
| 397 | { |
| 398 | "address": "{$bank_aa}", |
| 399 | "amount": 2000 |
| 400 | } |
| 401 | ] |
| 402 | } |
| 403 | }, |
| 404 | { |
| 405 | "app": "data", |
| 406 | "payload": { |
| 407 | "withdraw": 1, |
| 408 | "asset": "{trigger.data.asset}", |
| 409 | "amount": "all" |
| 410 | } |
| 411 | } |
| 412 | ] |
| 413 | }, |
| 414 | { |
| 415 | "if": "{ $shares_asset AND trigger.output[[asset=$interest_asset]] > 0 }", |
| 416 | "init": "{ |
| 417 | $received_interest_amount = trigger.output[[asset=$interest_asset]]; |
| 418 | $shares_supply = var['shares_supply'] OTHERWISE 0; |
| 419 | $interest_balance = $get_interest_balance() - $received_interest_amount; |
| 420 | if ($interest_balance < 0) |
| 421 | bounce("interest_balance < 0"); |
| 422 | if ($shares_supply > 0 AND $interest_balance == 0) |
| 423 | bounce("shares_supply > 0 AND interest_balance == 0"); |
| 424 | $share_price = $shares_supply ? $interest_balance / $shares_supply : 1; |
| 425 | $shares_amount = floor($received_interest_amount / $share_price); |
| 426 | }", |
| 427 | "messages": [ |
| 428 | { |
| 429 | "app": "payment", |
| 430 | "payload": { |
| 431 | "asset": "{$shares_asset}", |
| 432 | "outputs": [ |
| 433 | { |
| 434 | "address": "{trigger.address}", |
| 435 | "amount": "{$shares_amount}" |
| 436 | } |
| 437 | ] |
| 438 | } |
| 439 | }, |
| 440 | { |
| 441 | "app": "state", |
| 442 | "state": "{ |
| 443 | var['shares_supply'] += $shares_amount; |
| 444 | }" |
| 445 | } |
| 446 | ] |
| 447 | }, |
| 448 | { |
| 449 | "if": "{ $shares_asset AND trigger.output[[asset=$shares_asset]] > 0 }", |
| 450 | "init": "{ |
| 451 | $received_shares_amount = trigger.output[[asset=$shares_asset]]; |
| 452 | $shares_supply = var['shares_supply']; |
| 453 | $interest_balance = $get_interest_balance(); |
| 454 | if ($interest_balance < 0) |
| 455 | bounce("interest_balance < 0"); |
| 456 | if ($shares_supply > 0 AND $interest_balance == 0) |
| 457 | bounce("shares_supply > 0 AND interest_balance == 0"); |
| 458 | $share_price = $interest_balance / $shares_supply; |
| 459 | $interest_amount = floor($received_shares_amount * $share_price); |
| 460 | }", |
| 461 | "messages": [ |
| 462 | { |
| 463 | "app": "payment", |
| 464 | "payload": { |
| 465 | "asset": "{$interest_asset}", |
| 466 | "outputs": [ |
| 467 | { |
| 468 | "address": "{trigger.address}", |
| 469 | "amount": "{$interest_amount}" |
| 470 | } |
| 471 | ] |
| 472 | } |
| 473 | }, |
| 474 | { |
| 475 | "app": "state", |
| 476 | "state": "{ |
| 477 | var['shares_supply'] -= $received_shares_amount; |
| 478 | }" |
| 479 | } |
| 480 | ] |
| 481 | }, |
| 482 | { |
| 483 | "if": "{ $shares_asset AND trigger.data.withdraw_management_fee AND trigger.address == params.manager }", |
| 484 | "init": "{ |
| 485 | $shares_supply = var['shares_supply'] OTHERWISE 0; |
| 486 | $mf_term = (timestamp - var['last_mf_withdrawal_ts']) / (360 * 24 * 3600); |
| 487 | $mf_growth_factor = (1 + params.management_fee)^$term; |
| 488 | $mf = floor($shares_supply * ($mf_growth_factor - 1)); |
| 489 | }", |
| 490 | "messages": [ |
| 491 | { |
| 492 | "app": "payment", |
| 493 | "payload": { |
| 494 | "asset": "{$shares_asset}", |
| 495 | "outputs": [ |
| 496 | { |
| 497 | "address": "{trigger.address}", |
| 498 | "amount": "{$mf}" |
| 499 | } |
| 500 | ] |
| 501 | } |
| 502 | }, |
| 503 | { |
| 504 | "app": "state", |
| 505 | "state": "{ |
| 506 | var['shares_supply'] += $mf; |
| 507 | var['last_mf_withdrawal_ts'] = timestamp; |
| 508 | }" |
| 509 | } |
| 510 | ] |
| 511 | }, |
| 512 | { |
| 513 | "if": "{ $shares_asset AND trigger.data.withdraw_success_fee AND trigger.address == params.manager }", |
| 514 | "init": "{ |
| 515 | $shares_supply = var['shares_supply']; |
| 516 | if (!$shares_supply) |
| 517 | bounce("no shares yet"); |
| 518 | $interest_balance = $get_interest_balance(); |
| 519 | $share_price = $interest_balance / $shares_supply; |
| 520 | $profit = ($share_price - var['last_sf_withdrawal_share_price']) * $shares_supply; |
| 521 | $sf = floor($profit * params.success_fee); |
| 522 | if ($sf <= 0) |
| 523 | bounce("there is no profit since the last withdrawal"); |
| 524 | }", |
| 525 | "messages": [ |
| 526 | { |
| 527 | "app": "payment", |
| 528 | "payload": { |
| 529 | "asset": "{$interest_asset}", |
| 530 | "outputs": [ |
| 531 | { |
| 532 | "address": "{trigger.address}", |
| 533 | "amount": "{$sf}" |
| 534 | } |
| 535 | ] |
| 536 | } |
| 537 | }, |
| 538 | { |
| 539 | "app": "state", |
| 540 | "state": "{ |
| 541 | var['last_sf_withdrawal_share_price'] = $share_price; |
| 542 | }" |
| 543 | } |
| 544 | ] |
| 545 | }, |
| 546 | { |
| 547 | "if": "{ $shares_asset AND trigger.data.withdraw_bytes AND trigger.data.amount AND trigger.address == params.manager }", |
| 548 | "messages": [ |
| 549 | { |
| 550 | "app": "payment", |
| 551 | "payload": { |
| 552 | "asset": "base", |
| 553 | "outputs": [ |
| 554 | { |
| 555 | "address": "{trigger.address}", |
| 556 | "amount": "{trigger.data.amount}" |
| 557 | } |
| 558 | ] |
| 559 | } |
| 560 | } |
| 561 | ] |
| 562 | } |
| 563 | ] |
| 564 | } |
| 565 | } |
| 566 | ] |