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