| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/default-decision-engine.json", |
| 5 | "getters": "{ |
| 6 | $get_curve_aa = () => params.curve_aa; |
| 7 | }", |
| 8 | "init": "{ |
| 9 | $curve_aa = params.curve_aa; |
| 10 | $curve_params = definition[$curve_aa][1].params; |
| 11 | |
| 12 | $below_peg_threshold = params.below_peg_threshold OTHERWISE 0.001; |
| 13 | $below_peg_timeout = exists(params.below_peg_timeout) ? params.below_peg_timeout : 12 * 3600; |
| 14 | $min_reserve_delta = params.min_reserve_delta OTHERWISE 1e5; |
| 15 | $max_reserve_share = params.max_reserve_share OTHERWISE 0.1; |
| 16 | |
| 17 | $fund_aa = var[$curve_aa]['fund_aa']; |
| 18 | |
| 19 | |
| 20 | $reserve_asset = $curve_params.reserve_asset OTHERWISE 'base'; |
| 21 | $asset1 = var[$curve_aa]['asset1']; |
| 22 | $asset2 = var[$curve_aa]['asset2']; |
| 23 | $shares_asset = var[$fund_aa]['shares_asset']; |
| 24 | |
| 25 | $fee = $reserve_asset == 'base' ? 1e4 : 0; |
| 26 | $network_fee = ($reserve_asset == 'base') ? 4000 : 0; |
| 27 | |
| 28 | $get_leverage = () => $curve_params.leverage OTHERWISE 0; |
| 29 | |
| 30 | $get_reserve = ($s1, $s2) => { |
| 31 | $r = $s1^$curve_params.m * $s2^$curve_params.n; |
| 32 | $r |
| 33 | }; |
| 34 | |
| 35 | $get_p2 = ($s1, $s2) => { |
| 36 | $p2 = $s1^$curve_params.m * $curve_params.n * $s2^($curve_params.n-1); |
| 37 | $p2 |
| 38 | }; |
| 39 | |
| 40 | $get_p1 = () => { |
| 41 | $s1 = var[$curve_aa]['supply1']/10^$curve_params.decimals1; |
| 42 | $s2 = var[$curve_aa]['supply2']/10^$curve_params.decimals2; |
| 43 | $p1_in_full_units = $curve_params.m * $s1^($curve_params.m-1) * $s2^$curve_params.n; |
| 44 | $p1_in_smallest_units = $p1_in_full_units * 10^($curve_params.reserve_asset_decimals - $curve_params.decimals1); |
| 45 | $p1_in_smallest_units |
| 46 | }; |
| 47 | |
| 48 | $get_oracles = () => { |
| 49 | $oracles = var[$curve_aa]['oracles']; |
| 50 | if ($oracles) |
| 51 | return $oracles; |
| 52 | $initial_oracles = []; |
| 53 | if ($curve_params.oracle1 AND $curve_params.feed_name1) |
| 54 | $initial_oracles[] = {oracle: $curve_params.oracle1, feed_name: $curve_params.feed_name1, op: $curve_params.op1 OTHERWISE '*'}; |
| 55 | if ($curve_params.oracle2 AND $curve_params.feed_name2) |
| 56 | $initial_oracles[] = {oracle: $curve_params.oracle2, feed_name: $curve_params.feed_name2, op: $curve_params.op2 OTHERWISE '*'}; |
| 57 | if ($curve_params.oracle3 AND $curve_params.feed_name3) |
| 58 | $initial_oracles[] = {oracle: $curve_params.oracle3, feed_name: $curve_params.feed_name3, op: $curve_params.op3 OTHERWISE '*'}; |
| 59 | $initial_oracles |
| 60 | }; |
| 61 | |
| 62 | $get_initial_interest_rate = () => exists($curve_params.interest_rate) ? $curve_params.interest_rate : 0.1; |
| 63 | |
| 64 | $get_interest_rate = () => { |
| 65 | $interest_rate_var = var[$curve_aa]['interest_rate']; |
| 66 | exists($interest_rate_var) ? $interest_rate_var : $get_initial_interest_rate() |
| 67 | }; |
| 68 | |
| 69 | $get_growth_factor = () => { |
| 70 | $interest_rate = $get_interest_rate(); |
| 71 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 72 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 73 | $growth_factor |
| 74 | }; |
| 75 | |
| 76 | $get_oracle_price = () => { |
| 77 | $oracles = $get_oracles(); |
| 78 | $oracle_price = reduce($oracles, 3, ($price, $oracle_info) => { |
| 79 | if (!exists($price)) |
| 80 | return false; |
| 81 | $df = data_feed[[oracles=$oracle_info.oracle, feed_name=$oracle_info.feed_name, ifnone=false]]; |
| 82 | if (!exists($df)) |
| 83 | return false; |
| 84 | ($oracle_info.op == '*') ? $price * $df : $price / $df |
| 85 | }, 1); |
| 86 | $oracle_price |
| 87 | }; |
| 88 | |
| 89 | $get_target_p2 = () => { |
| 90 | $oracle_price = $get_oracle_price(); |
| 91 | if (!exists($oracle_price)) |
| 92 | return false; |
| 93 | $target_p2 = $oracle_price^($get_leverage() - 1) * $get_growth_factor(); |
| 94 | $target_p2 |
| 95 | }; |
| 96 | |
| 97 | $get_distance = ($p2, $target_p2) => (exists($p2) AND exists($target_p2)) ? abs($p2 - $target_p2) / min($p2, $target_p2) : 0; |
| 98 | |
| 99 | $get_fee_multiplier = () => var[$curve_aa]['fee_multiplier'] OTHERWISE $curve_params.fee_multiplier OTHERWISE 5; |
| 100 | |
| 101 | $get_fee = ($avg_reserve, $old_distance, $new_distance) => { |
| 102 | $fee_multiplier = $get_fee_multiplier(); |
| 103 | $curve_fee = ceil($fee_multiplier * $avg_reserve * ($new_distance - $old_distance) * ($new_distance + $old_distance)); |
| 104 | $curve_fee |
| 105 | }; |
| 106 | |
| 107 | $get_exchange_data = () => { |
| 108 | $supply1 = var[$curve_aa]['supply1']; |
| 109 | $supply2 = var[$curve_aa]['supply2']; |
| 110 | $reserve = var[$curve_aa]['reserve']; |
| 111 | |
| 112 | $decimals1 = $curve_params.decimals1; |
| 113 | $decimals2 = $curve_params.decimals2; |
| 114 | $reserve_asset_decimals = $curve_params.reserve_asset_decimals; |
| 115 | |
| 116 | $m = $curve_params.m; |
| 117 | $n = $curve_params.n; |
| 118 | |
| 119 | $data = {}; |
| 120 | |
| 121 | $data.target_p2 = $get_target_p2(); |
| 122 | if (!exists($data.target_p2)) |
| 123 | return $data; |
| 124 | |
| 125 | $s2 = $supply2/10^$decimals2; |
| 126 | |
| 127 | |
| 128 | $target_s1 = ($data.target_p2/$n * $s2^(1-$n))^(1/$m); |
| 129 | $data.tokens1_delta = round($target_s1 * 10^$decimals1) - $supply1; |
| 130 | $data.new_s1 = ($supply1 + $data.tokens1_delta) / 10^$decimals1; |
| 131 | $data.reserve_delta = ceil($get_reserve($data.new_s1, $s2) * 10^$reserve_asset_decimals) - $reserve; |
| 132 | |
| 133 | $available_reserve_balance = floor($max_reserve_share * balance[$fund_aa][$reserve_asset]); |
| 134 | if ($data.reserve_delta > $available_reserve_balance){ |
| 135 | $data.reserve_delta = $available_reserve_balance - $network_fee - ($reserve_asset == 'base' ? 3000 : 0); |
| 136 | if ($data.reserve_delta <= 0) { |
| 137 | $data.reserve_delta = 0; |
| 138 | $data.tokens1_delta = 0; |
| 139 | $data.new_s1 = $supply1/10^$decimals1; |
| 140 | } |
| 141 | else { |
| 142 | $new_reserve = $reserve + $data.reserve_delta; |
| 143 | $new_r = $new_reserve/10^$reserve_asset_decimals; |
| 144 | $data.new_s1 = ($new_r/$s2^$n)^(1/$m); |
| 145 | $data.tokens1_delta = floor($data.new_s1 * 10^$decimals1) - $supply1; |
| 146 | if ($data.tokens1_delta < 0) |
| 147 | bounce("partial tokens1_delta < 0: " || $data.tokens1_delta); |
| 148 | $data.partial = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | $data.current_p2 = var[$curve_aa]['p2']; |
| 154 | $distance = $get_distance($data.current_p2, $data.target_p2); |
| 155 | $data.new_p2 = $get_p2($data.new_s1, $s2); |
| 156 | $new_distance = $get_distance($data.new_p2, $data.target_p2); |
| 157 | $reward = floor((1 - $new_distance/$distance) * var[$curve_aa]['fast_capacity']); |
| 158 | $data.reserve_needed = $data.reserve_delta - $reward; |
| 159 | |
| 160 | $data |
| 161 | }; |
| 162 | |
| 163 | $p1 = $get_p1(); |
| 164 | |
| 165 | $get_total_assets = () => { |
| 166 | balance[$fund_aa][$reserve_asset] + $p1 * balance[$fund_aa][$asset1] |
| 167 | }; |
| 168 | |
| 169 | $get_net_proceeds = () => { |
| 170 | $res = trigger.data.tx.res; |
| 171 | |
| 172 | $net_proceeds = trigger.output[[asset=$reserve_asset]] - ($reserve_asset == 'base' ? 3000 : 0); |
| 173 | if ($net_proceeds != -$res.reserve_needed - $network_fee) |
| 174 | bounce("received wrong amount " || $net_proceeds || ", expected " || (-$res.reserve_needed - $network_fee)); |
| 175 | $net_proceeds |
| 176 | }; |
| 177 | |
| 178 | $redemption = var['redemption']; |
| 179 | $capacitor_sweeping = var['capacitor_sweeping']; |
| 180 | |
| 181 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) |
| 182 | bounce("bad to address"); |
| 183 | $to = trigger.data.to OTHERWISE trigger.address; |
| 184 | |
| 185 | }", |
| 186 | "messages": { |
| 187 | "cases": [ |
| 188 | { |
| 189 | "if": "{ $shares_asset AND $curve_params.allow_grants AND trigger.data.grant AND trigger.data.recipient AND trigger.data.amount AND trigger.address == var[$curve_aa]['governance_aa'] }", |
| 190 | "messages": [ |
| 191 | { |
| 192 | "app": "payment", |
| 193 | "payload": { |
| 194 | "asset": "base", |
| 195 | "outputs": [ |
| 196 | { |
| 197 | "address": "{$fund_aa}", |
| 198 | "amount": "{trigger.output[[asset=base]] - 2000}" |
| 199 | } |
| 200 | ] |
| 201 | } |
| 202 | }, |
| 203 | { |
| 204 | "app": "data", |
| 205 | "payload": { |
| 206 | "payments": [ |
| 207 | { |
| 208 | "asset": "{$shares_asset}", |
| 209 | "address": "{trigger.data.recipient}", |
| 210 | "amount": "{trigger.data.amount}" |
| 211 | } |
| 212 | ] |
| 213 | } |
| 214 | } |
| 215 | ] |
| 216 | }, |
| 217 | { |
| 218 | "if": "{ (trigger.data.tx AND trigger.address == $curve_aa AND !$capacitor_sweeping) OR trigger.data.act }", |
| 219 | "init": "{ |
| 220 | $below_peg_ts = var['below_peg_ts']; |
| 221 | |
| 222 | $request_data = {payments: []}; |
| 223 | |
| 224 | |
| 225 | if ($redemption){ |
| 226 | if (trigger.address != $curve_aa) |
| 227 | bounce("BUG: redemption is still active"); |
| 228 | if (!trigger.data.to OR trigger.data.to != $fund_aa) |
| 229 | bounce("wrong to address: " || trigger.data.to); |
| 230 | if (trigger.data.tx.tokens2 != 0) |
| 231 | bounce("t2 transaction, expected t1"); |
| 232 | $net_proceeds = $get_net_proceeds(); |
| 233 | $request_data.payments[] = { |
| 234 | asset: $reserve_asset, |
| 235 | address: $redemption.address, |
| 236 | amount: $redemption.reserve_amount + $net_proceeds |
| 237 | }; |
| 238 | response['t1_redemption_proceeds'] = $net_proceeds; |
| 239 | } |
| 240 | |
| 241 | $data = $get_exchange_data(); |
| 242 | $below_peg = (exists($data.target_p2) AND $data.current_p2 < $data.target_p2); |
| 243 | $large_below_peg = $below_peg AND ($data.target_p2 - $data.current_p2)/$data.target_p2 >= $below_peg_threshold; |
| 244 | $tokens1 = $data.tokens1_delta; |
| 245 | if ( |
| 246 | !exists($data.target_p2) |
| 247 | OR $below_peg AND $below_peg_timeout AND (!$below_peg_ts OR timestamp < $below_peg_ts + $below_peg_timeout) |
| 248 | OR abs($tokens1) <= 1 |
| 249 | OR abs($data.reserve_delta) < $min_reserve_delta |
| 250 | ){ |
| 251 | |
| 252 | } |
| 253 | else { |
| 254 | $large_below_peg_after = $data.new_p2 < $data.target_p2 AND ($data.target_p2 - $data.new_p2)/$data.target_p2 >= $below_peg_threshold; |
| 255 | |
| 256 | $bFixing = true; |
| 257 | if ($tokens1 > 0){ |
| 258 | $request_data.payments[] = {asset: $reserve_asset, address: $curve_aa, amount: $data.reserve_needed + $network_fee}; |
| 259 | $request_data.forwarded_data = {tokens1: $tokens1}; |
| 260 | } |
| 261 | else if ($tokens1 < 0) |
| 262 | $request_data.payments[] = {asset: $asset1, address: $curve_aa, amount: -$tokens1}; |
| 263 | |
| 264 | if ($reserve_asset != 'base') |
| 265 | $request_data.payments[] = {asset: 'base', address: $curve_aa, amount: 2000}; |
| 266 | } |
| 267 | |
| 268 | $bSending = length($request_data.payments) > 0; |
| 269 | }", |
| 270 | "messages": [ |
| 271 | { |
| 272 | "if": "{$bSending}", |
| 273 | "app": "data", |
| 274 | "payload": "{$request_data}" |
| 275 | }, |
| 276 | { |
| 277 | "if": "{$bSending}", |
| 278 | "app": "payment", |
| 279 | "payload": { |
| 280 | "asset": "base", |
| 281 | "outputs": [ |
| 282 | { |
| 283 | "address": "{$fund_aa}", |
| 284 | "amount": "{2000 + ($reserve_asset == 'base' ? $net_proceeds : 0)}" |
| 285 | } |
| 286 | ] |
| 287 | } |
| 288 | }, |
| 289 | { |
| 290 | "if": "{$bSending AND $redemption AND $reserve_asset != 'base'}", |
| 291 | "app": "payment", |
| 292 | "payload": { |
| 293 | "asset": "{$reserve_asset}", |
| 294 | "outputs": [ |
| 295 | { |
| 296 | "address": "{$fund_aa}", |
| 297 | "amount": "{$net_proceeds}" |
| 298 | } |
| 299 | ] |
| 300 | } |
| 301 | }, |
| 302 | { |
| 303 | "app": "state", |
| 304 | "state": "{ |
| 305 | if ($large_below_peg AND !$below_peg_ts AND $below_peg_timeout) |
| 306 | var['below_peg_ts'] = timestamp; |
| 307 | if ((!$large_below_peg OR $bFixing ) AND $below_peg_ts) |
| 308 | var['below_peg_ts'] = false; |
| 309 | if ($redemption) |
| 310 | var['redemption'] = false; |
| 311 | if ($bFixing) |
| 312 | response['message'] = $data.partial ? "DE partially fixed the peg" : "DE fixed the peg"; |
| 313 | else |
| 314 | response['message'] = "DE does not interfere yet"; |
| 315 | }" |
| 316 | } |
| 317 | ] |
| 318 | }, |
| 319 | { |
| 320 | "if": "{trigger.data.sweep_capacitor}", |
| 321 | "init": "{ |
| 322 | $target_p2 = $get_target_p2(); |
| 323 | if (!exists($target_p2)) |
| 324 | bounce("no target p2"); |
| 325 | $current_p2 = var[$curve_aa]['p2']; |
| 326 | if ($current_p2 >= $target_p2) |
| 327 | bounce("p2 must be lower than the target"); |
| 328 | $supply1 = var[$curve_aa]['supply1']; |
| 329 | $supply2 = var[$curve_aa]['supply2']; |
| 330 | $reserve = var[$curve_aa]['reserve']; |
| 331 | |
| 332 | $decimals1 = $curve_params.decimals1; |
| 333 | $decimals2 = $curve_params.decimals2; |
| 334 | $reserve_asset_decimals = $curve_params.reserve_asset_decimals; |
| 335 | |
| 336 | $m = $curve_params.m; |
| 337 | $n = $curve_params.n; |
| 338 | |
| 339 | $s1 = $supply1/10^$decimals1; |
| 340 | |
| 341 | $target_s2 = ($target_p2 / $n / $s1^$m)^(1/($n-1)); |
| 342 | $mul2 = 10^$decimals2; |
| 343 | $tokens2 = floor($target_s2 * $mul2) - $supply2; |
| 344 | if ($tokens2 <= 0) |
| 345 | bounce("would buy " || $tokens2 || " T2"); |
| 346 | response["will buy T2"] = $tokens2; |
| 347 | $rounded_target_s2 = ($supply2 + $tokens2)/$mul2; |
| 348 | |
| 349 | $new_reserve = ceil($get_reserve($s1, $rounded_target_s2) * 10^$reserve_asset_decimals); |
| 350 | $reserve_delta = $new_reserve - $reserve; |
| 351 | if ($reserve_delta <= $min_reserve_delta) |
| 352 | bounce("reserve delta too small " || $reserve_delta); |
| 353 | $avg_reserve = ($reserve + $new_reserve) / 2; |
| 354 | |
| 355 | $distance = $get_distance($current_p2, $target_p2); |
| 356 | $redemption_fee = $get_fee($avg_reserve, 0, $distance); |
| 357 | $reward = var[$curve_aa]['fast_capacity']; |
| 358 | if ($reward <= $redemption_fee) |
| 359 | bounce("reward would not be greater than the fee"); |
| 360 | }", |
| 361 | "messages": [ |
| 362 | { |
| 363 | "app": "payment", |
| 364 | "payload": { |
| 365 | "asset": "base", |
| 366 | "outputs": [ |
| 367 | { |
| 368 | "address": "{$fund_aa}", |
| 369 | "amount": 2000 |
| 370 | } |
| 371 | ] |
| 372 | } |
| 373 | }, |
| 374 | { |
| 375 | "app": "data", |
| 376 | "payload": { |
| 377 | "payments": [ |
| 378 | { |
| 379 | "asset": "{$reserve_asset}", |
| 380 | "address": "{$curve_aa}", |
| 381 | "amount": "{$reserve_delta}" |
| 382 | } |
| 383 | ], |
| 384 | "forwarded_data": { |
| 385 | "to": "{this_address}", |
| 386 | "tokens2": "{$tokens2}" |
| 387 | } |
| 388 | } |
| 389 | }, |
| 390 | { |
| 391 | "app": "state", |
| 392 | "state": "{ |
| 393 | var['capacitor_sweeping'] = {paid: $reserve_delta}; |
| 394 | response['message'] = "Expecting to make a profit of " || ($reward - $redemption_fee); |
| 395 | }" |
| 396 | } |
| 397 | ] |
| 398 | }, |
| 399 | { |
| 400 | "if": "{trigger.output[[asset=$asset2]] AND trigger.address == $curve_aa}", |
| 401 | "messages": [ |
| 402 | { |
| 403 | "app": "payment", |
| 404 | "payload": { |
| 405 | "asset": "{$asset2}", |
| 406 | "outputs": [ |
| 407 | { |
| 408 | "address": "{$curve_aa}", |
| 409 | "amount": "{trigger.output[[asset=$asset2]]}" |
| 410 | } |
| 411 | ] |
| 412 | } |
| 413 | }, |
| 414 | { |
| 415 | "app": "state", |
| 416 | "state": "{ |
| 417 | if (!$capacitor_sweeping) |
| 418 | bounce("no capacitor sweeping var"); |
| 419 | |
| 420 | $capacitor_sweeping.returned_reserve = trigger.output[[asset=$reserve_asset]]; |
| 421 | var['capacitor_sweeping'] = $capacitor_sweeping; |
| 422 | }" |
| 423 | } |
| 424 | ] |
| 425 | }, |
| 426 | { |
| 427 | "if": "{ trigger.data.tx AND trigger.address == $curve_aa AND $capacitor_sweeping }", |
| 428 | "init": "{ |
| 429 | if (trigger.data.tx.tokens2 >= 0) |
| 430 | bounce("expected t2<0, got " || trigger.data.tx.tokens2); |
| 431 | $net_proceeds = $get_net_proceeds(); |
| 432 | $returned_reserve = $capacitor_sweeping.returned_reserve; |
| 433 | if ($capacitor_sweeping.paid >= $returned_reserve + $net_proceeds) |
| 434 | bounce("capacitor sweeping was not profitable"); |
| 435 | }", |
| 436 | "messages": [ |
| 437 | { |
| 438 | "app": "payment", |
| 439 | "payload": { |
| 440 | "asset": "{$reserve_asset}", |
| 441 | "outputs": [ |
| 442 | { |
| 443 | "address": "{$fund_aa}", |
| 444 | "amount": "{$net_proceeds + $returned_reserve}" |
| 445 | } |
| 446 | ] |
| 447 | } |
| 448 | }, |
| 449 | { |
| 450 | "app": "state", |
| 451 | "state": "{ |
| 452 | var['capacitor_sweeping'] = false; |
| 453 | }" |
| 454 | } |
| 455 | ] |
| 456 | }, |
| 457 | { |
| 458 | "if": "{ $shares_asset AND trigger.output[[asset=$reserve_asset]] > 0 AND trigger.output[[asset=$shares_asset]] == 0 }", |
| 459 | "init": "{ |
| 460 | if ($redemption) |
| 461 | bounce("BUG: redeeming"); |
| 462 | $received_reserve_amount = trigger.output[[asset=$reserve_asset]] - $fee; |
| 463 | if ($received_reserve_amount <= 0) |
| 464 | bounce("0 contribution"); |
| 465 | |
| 466 | $balance = $get_total_assets(); |
| 467 | |
| 468 | $shares_supply = var[$fund_aa]['shares_supply'] OTHERWISE 0; |
| 469 | if ($shares_supply > 0 AND $balance == 0) |
| 470 | bounce("shares_supply > 0 AND balance == 0"); |
| 471 | $share_price = $shares_supply ? $balance / $shares_supply : 1; |
| 472 | $shares_amount = floor($received_reserve_amount / $share_price); |
| 473 | $request_data = {payments: [{asset: $shares_asset, address: $to, amount: $shares_amount}]}; |
| 474 | |
| 475 | |
| 476 | if (!var[$curve_aa]['supply1'] AND !var[$curve_aa]['supply2'] AND !var[$curve_aa]['reserve']){ |
| 477 | $m = $curve_params.m; |
| 478 | $n = $curve_params.n; |
| 479 | $target_p2 = $get_target_p2() OTHERWISE trigger.data.p2; |
| 480 | |
| 481 | $r = ($received_reserve_amount - $network_fee - 1000) / 10^$curve_params.reserve_asset_decimals; |
| 482 | $s2 = $n * $r / $target_p2; |
| 483 | $s1 = ($r / $s2^$n)^(1/$m); |
| 484 | $tokens2 = floor($s2 * 10^$curve_params.decimals2); |
| 485 | $tokens1 = floor($s1 * 10^$curve_params.decimals1); |
| 486 | $request_data.payments[] = {asset: $reserve_asset, address: $curve_aa, amount: $received_reserve_amount}; |
| 487 | $request_data.forwarded_data = {tokens1: $tokens1, tokens2: $tokens2}; |
| 488 | } |
| 489 | }", |
| 490 | "messages": [ |
| 491 | { |
| 492 | "app": "payment", |
| 493 | "payload": { |
| 494 | "asset": "{$reserve_asset}", |
| 495 | "outputs": [ |
| 496 | { |
| 497 | "address": "{$fund_aa}", |
| 498 | "amount": "{$received_reserve_amount + $fee/2}" |
| 499 | } |
| 500 | ] |
| 501 | } |
| 502 | }, |
| 503 | { |
| 504 | "if": "{$reserve_asset != 'base'}", |
| 505 | "app": "payment", |
| 506 | "payload": { |
| 507 | "asset": "base", |
| 508 | "outputs": [ |
| 509 | { |
| 510 | "address": "{$fund_aa}", |
| 511 | "amount": "{trigger.output[[asset=base]] - 2000}" |
| 512 | } |
| 513 | ] |
| 514 | } |
| 515 | }, |
| 516 | { |
| 517 | "app": "data", |
| 518 | "payload": "{$request_data}" |
| 519 | } |
| 520 | ] |
| 521 | }, |
| 522 | { |
| 523 | "if": "{ $shares_asset AND trigger.output[[asset=$shares_asset]] > 0 }", |
| 524 | "init": "{ |
| 525 | if ($redemption) |
| 526 | bounce("BUG: already redeeming"); |
| 527 | $received_shares_amount = trigger.output[[asset=$shares_asset]]; |
| 528 | $shares_supply = var[$fund_aa]['shares_supply']; |
| 529 | $my_share = $received_shares_amount/$shares_supply; |
| 530 | $reserve_amount = floor($my_share * balance[$fund_aa][$reserve_asset]); |
| 531 | $t1_amount = floor($my_share * balance[$fund_aa][$asset1]); |
| 532 | if ($t1_amount == 0) |
| 533 | bounce("would redeem 0 t1"); |
| 534 | |
| 535 | $payments = [{asset: $asset1, address: $curve_aa, amount: $t1_amount}]; |
| 536 | }", |
| 537 | "messages": [ |
| 538 | { |
| 539 | "app": "payment", |
| 540 | "payload": { |
| 541 | "asset": "{$shares_asset}", |
| 542 | "outputs": [ |
| 543 | { |
| 544 | "address": "{$fund_aa}", |
| 545 | "amount": "{$received_shares_amount}" |
| 546 | } |
| 547 | ] |
| 548 | } |
| 549 | }, |
| 550 | { |
| 551 | "app": "payment", |
| 552 | "payload": { |
| 553 | "asset": "base", |
| 554 | "outputs": [ |
| 555 | { |
| 556 | "address": "{$fund_aa}", |
| 557 | "amount": "{trigger.output[[asset=base]] - 2000}" |
| 558 | } |
| 559 | ] |
| 560 | } |
| 561 | }, |
| 562 | { |
| 563 | "app": "data", |
| 564 | "payload": { |
| 565 | "payments": "{$payments}", |
| 566 | "forwarded_data": { |
| 567 | "notifyDE": 1, |
| 568 | "reserve_to": "{this_address}" |
| 569 | } |
| 570 | } |
| 571 | }, |
| 572 | { |
| 573 | "app": "state", |
| 574 | "state": "{ |
| 575 | var['redemption'] = { |
| 576 | address: $to, |
| 577 | reserve_amount: $reserve_amount, |
| 578 | t1_amount: $t1_amount, |
| 579 | }; |
| 580 | response['message'] = "started redemption"; |
| 581 | response['redeemed_reserve'] = $reserve_amount; |
| 582 | response['redeemed_t1'] = $t1_amount; |
| 583 | }" |
| 584 | } |
| 585 | ] |
| 586 | } |
| 587 | ] |
| 588 | } |
| 589 | } |
| 590 | ] |