| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/stable-oswap-oswap-arb.json", |
| 5 | "init": "{ |
| 6 | $min_reserve_delta = params.min_reserve_delta OTHERWISE 1e5; |
| 7 | |
| 8 | $stable_aa = params.stable_aa; |
| 9 | $stable_params = definition[$stable_aa][1].params; |
| 10 | |
| 11 | $curve_aa = $stable_params.curve_aa; |
| 12 | $curve_params = definition[$curve_aa][1].params; |
| 13 | |
| 14 | $m = $curve_params.m; |
| 15 | $n = $curve_params.n; |
| 16 | $decimals1 = $curve_params.decimals1; |
| 17 | $decimals2 = $curve_params.decimals2; |
| 18 | $reserve_asset_decimals = $curve_params.reserve_asset_decimals; |
| 19 | |
| 20 | |
| 21 | $interest_asset = var[$curve_aa]['asset2']; |
| 22 | $stable_asset = var[$stable_aa]['asset']; |
| 23 | $reserve_asset = $curve_params.reserve_asset OTHERWISE 'base'; |
| 24 | |
| 25 | $reserve_oswap_aa = params.reserve_oswap_aa; |
| 26 | $stable_oswap_aa = params.stable_oswap_aa; |
| 27 | |
| 28 | $stable_oswap_params = definition[$stable_oswap_aa][1].params; |
| 29 | $reserve_oswap_params = definition[$reserve_oswap_aa][1].params; |
| 30 | |
| 31 | $stable_oswap_fee = $stable_oswap_params.swap_fee / 1e11; |
| 32 | $reserve_oswap_fee = $reserve_oswap_params.swap_fee / 1e11; |
| 33 | $oswap_net = (1-$stable_oswap_fee)*(1-$reserve_oswap_fee); |
| 34 | |
| 35 | $stable_imported_asset = $stable_oswap_params.asset0 == $stable_asset ? $stable_oswap_params.asset1 : $stable_oswap_params.asset0; |
| 36 | $stable_imported_asset2 = $reserve_oswap_params.asset0 == $reserve_asset ? $reserve_oswap_params.asset1 : $reserve_oswap_params.asset0; |
| 37 | if ($stable_imported_asset2 != $stable_imported_asset) |
| 38 | bounce("stable imported asset mismatch " || $stable_imported_asset || ' != ' || $stable_imported_asset2); |
| 39 | |
| 40 | |
| 41 | $get_oracles = () => { |
| 42 | $oracles = var[$curve_aa]['oracles']; |
| 43 | if ($oracles) |
| 44 | return $oracles; |
| 45 | $initial_oracles = []; |
| 46 | if ($curve_params.oracle1 AND $curve_params.feed_name1) |
| 47 | $initial_oracles[] = {oracle: $curve_params.oracle1, feed_name: $curve_params.feed_name1, op: $curve_params.op1 OTHERWISE '*'}; |
| 48 | if ($curve_params.oracle2 AND $curve_params.feed_name2) |
| 49 | $initial_oracles[] = {oracle: $curve_params.oracle2, feed_name: $curve_params.feed_name2, op: $curve_params.op2 OTHERWISE '*'}; |
| 50 | if ($curve_params.oracle3 AND $curve_params.feed_name3) |
| 51 | $initial_oracles[] = {oracle: $curve_params.oracle3, feed_name: $curve_params.feed_name3, op: $curve_params.op3 OTHERWISE '*'}; |
| 52 | $initial_oracles |
| 53 | }; |
| 54 | |
| 55 | $get_oracle_price = () => { |
| 56 | $oracles = $get_oracles(); |
| 57 | $oracle_price = reduce($oracles, 3, ($price, $oracle_info) => { |
| 58 | if (!exists($price)) |
| 59 | return false; |
| 60 | $df = data_feed[[oracles=$oracle_info.oracle, feed_name=$oracle_info.feed_name, ifnone=false]]; |
| 61 | if (!exists($df)) |
| 62 | return false; |
| 63 | ($oracle_info.op == '*') ? $price * $df : $price / $df |
| 64 | }, 1); |
| 65 | $oracle_price |
| 66 | }; |
| 67 | |
| 68 | $get_leverage = () => $curve_params.leverage OTHERWISE 0; |
| 69 | |
| 70 | $get_growth_factor = () => { |
| 71 | $interest_rate = var[$curve_aa]['interest_rate']; |
| 72 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 73 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 74 | $growth_factor |
| 75 | }; |
| 76 | $g = $get_growth_factor(); |
| 77 | |
| 78 | $get_target_p2 = () => { |
| 79 | $oracle_price = $get_oracle_price(); |
| 80 | if (!exists($oracle_price)) |
| 81 | return false; |
| 82 | $target_p2 = $oracle_price^($get_leverage() - 1) * $g; |
| 83 | $target_p2 |
| 84 | }; |
| 85 | |
| 86 | $get_reserve = ($s1, $s2) => { |
| 87 | $r = $s1^$m * $s2^$n; |
| 88 | $r |
| 89 | }; |
| 90 | |
| 91 | $get_p2 = ($s1, $s2) => { |
| 92 | $p2 = $s1^$m * $n * $s2^($n-1); |
| 93 | $p2 |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | $get_slow_capacity_share = () => { |
| 98 | $slow_capacity_share_var = var[$curve_aa]['slow_capacity_share']; |
| 99 | if (exists($slow_capacity_share_var)) |
| 100 | $slow_capacity_share = $slow_capacity_share_var; |
| 101 | else if (exists($curve_params.slow_capacity_share)) |
| 102 | $slow_capacity_share = $curve_params.slow_capacity_share; |
| 103 | else |
| 104 | $slow_capacity_share = 0.5; |
| 105 | $slow_capacity_share |
| 106 | }; |
| 107 | |
| 108 | $get_distance = ($p2, $target_p2) => (exists($p2) AND exists($target_p2)) ? abs($p2 - $target_p2) / min($p2, $target_p2) : 0; |
| 109 | |
| 110 | $fee_multiplier = var[$curve_aa]['fee_multiplier'] OTHERWISE $curve_params.fee_multiplier OTHERWISE 5; |
| 111 | |
| 112 | $get_fee = ($avg_reserve, $old_distance, $new_distance) => { |
| 113 | $fee = ceil($fee_multiplier * $avg_reserve * ($new_distance - $old_distance) * ($new_distance + $old_distance)); |
| 114 | $fee |
| 115 | }; |
| 116 | |
| 117 | $get_reserve_needed = ($tokens1, $tokens2) => { |
| 118 | $slow_capacity_share = $get_slow_capacity_share(); |
| 119 | $fast_capacity_share = 1 - $slow_capacity_share; |
| 120 | |
| 121 | $initial_p2 = var[$curve_aa]['p2']; |
| 122 | $target_p2 = $get_target_p2(); |
| 123 | $distance = $get_distance($initial_p2, $target_p2); |
| 124 | |
| 125 | $reserve = var[$curve_aa]['reserve']; |
| 126 | if (!$reserve AND ($tokens1 <= 0 OR $tokens2 <= 0)) |
| 127 | bounce("initial mint must be with both tokens"); |
| 128 | $new_supply1 = var[$curve_aa]['supply1'] + $tokens1; |
| 129 | $new_supply2 = var[$curve_aa]['supply2'] + $tokens2; |
| 130 | $s1 = $new_supply1 / 10^$decimals1; |
| 131 | $s2 = $new_supply2 / 10^$decimals2; |
| 132 | $r = $get_reserve($s1, $s2); |
| 133 | $p2 = $get_p2($s1, $s2); |
| 134 | $new_reserve = ceil($r * 10^$reserve_asset_decimals); |
| 135 | $reserve_delta = $new_reserve - $reserve; |
| 136 | if ($tokens1 >= 0 AND $tokens2 >= 0 AND $reserve_delta < 0) |
| 137 | bounce("issuing tokens while the reserve decreases?"); |
| 138 | if ($tokens1 <= 0 AND $tokens2 <= 0 AND $reserve_delta > 0) |
| 139 | bounce("burning tokens while the reserve increases?"); |
| 140 | |
| 141 | $new_distance = $get_distance($p2, $target_p2); |
| 142 | $avg_reserve = ($reserve + $new_reserve) / 2; |
| 143 | $fast_capacity = var[$curve_aa]['fast_capacity']; |
| 144 | if ($distance == 0 AND $new_distance == 0){ |
| 145 | $fee = 0; |
| 146 | $reward = 0; |
| 147 | $reserve_needed = $reserve_delta; |
| 148 | } |
| 149 | else if ($new_distance >= $distance){ |
| 150 | $reward = 0; |
| 151 | $regular_fee = $get_fee($avg_reserve, $distance, $new_distance); |
| 152 | $new_fast_capacity = $fast_capacity + $regular_fee * $fast_capacity_share; |
| 153 | $distance_share = 1 - $distance/$new_distance; |
| 154 | |
| 155 | $reverse_reward = $distance_share * $new_fast_capacity; |
| 156 | if ($regular_fee >= $reverse_reward) |
| 157 | $fee = $regular_fee; |
| 158 | else |
| 159 | $fee = ceil($distance_share / (1 - $distance_share * $fast_capacity_share) * $fast_capacity); |
| 160 | $reserve_needed = $reserve_delta + $fee; |
| 161 | } |
| 162 | else { |
| 163 | $fee = 0; |
| 164 | $regular_reward = floor((1 - $new_distance/$distance) * $fast_capacity); |
| 165 | if ($curve_params.capped_reward){ |
| 166 | |
| 167 | $reverse_fee = $get_fee($avg_reserve, $new_distance, $distance); |
| 168 | $reward = min($regular_reward, $reverse_fee); |
| 169 | } |
| 170 | else |
| 171 | $reward = $regular_reward; |
| 172 | $reserve_needed = $reserve_delta - $reward; |
| 173 | } |
| 174 | |
| 175 | $reserve_needed |
| 176 | }; |
| 177 | |
| 178 | $aa2aa_bytes = 2000; |
| 179 | $network_fee = ($reserve_asset == 'base') ? 4000 : 0; |
| 180 | $full_network_fee = $network_fee + ($reserve_asset == 'base' ? $aa2aa_bytes : 0); |
| 181 | |
| 182 | $get_amount_for_buying = ($tokens2) => { |
| 183 | if ($tokens2 == 0) |
| 184 | bounce("0 T2"); |
| 185 | $reserve_needed = $get_reserve_needed(0, $tokens2); |
| 186 | if ($reserve_needed < $min_reserve_delta) |
| 187 | bounce("reserve amount too small " || $reserve_needed); |
| 188 | $amount = $reserve_needed + $full_network_fee; |
| 189 | $amount |
| 190 | }; |
| 191 | }", |
| 192 | "messages": { |
| 193 | "cases": [ |
| 194 | { |
| 195 | "if": "{ trigger.data.arb }", |
| 196 | "init": "{ |
| 197 | $s2 = var[$curve_aa]['supply2'] / 10^$decimals2; |
| 198 | $p2 = var[$curve_aa]['p2']; |
| 199 | $fc = var[$curve_aa]['fast_capacity'] / 10^$reserve_asset_decimals; |
| 200 | $target_p2 = $get_target_p2(); |
| 201 | $reward_share = ($n-1)*$fc/abs($target_p2-$p2)/$s2; |
| 202 | $fee_share = 2 * $fee_multiplier * ($n-1)/$n * abs($target_p2-$p2)/$p2; |
| 203 | |
| 204 | $s = balance[$stable_oswap_aa][$stable_asset] / 10^$decimals2; |
| 205 | $i1 = balance[$stable_oswap_aa][$stable_imported_asset]; |
| 206 | $i2 = balance[$reserve_oswap_aa][$stable_imported_asset]; |
| 207 | $ro = balance[$reserve_oswap_aa][$reserve_asset] / 10^$reserve_asset_decimals; |
| 208 | |
| 209 | $p_stable = $p2/$g; |
| 210 | $p_oswap = ($i1/$s) / ($i2/$ro); |
| 211 | |
| 212 | |
| 213 | if ($p2 < $target_p2 AND $reward_share < 1 AND $get_distance($p2, $target_p2) > 0.0001 AND $p_stable * (1-$reward_share) < $p_oswap * $oswap_net){ |
| 214 | $delta_s = (sqrt($g*$ro*$i2/$i1*$s*$oswap_net / $p2 / (1 - ($n-1)*$fc/$s2/($target_p2-$p2))) - $i2/$i1*$s) / (1 + $i2/$i1); |
| 215 | $delta_s2 = $delta_s/$g; |
| 216 | if ($delta_s2 < 0) |
| 217 | bounce("expected to buy T2, calc says should sell " || $delta_s2); |
| 218 | $tokens2 = round($delta_s2 * 10^$decimals2); |
| 219 | $amount = $get_amount_for_buying($tokens2); |
| 220 | $from = 'curve'; |
| 221 | } |
| 222 | |
| 223 | else if ($p_stable * (1-$fee_share) > $p_oswap / $oswap_net){ |
| 224 | $delta_s = (sqrt($g*$ro*$i2/$i1*$s/$oswap_net / $p2 / (1 - 2*$fee_multiplier*($n-1)/$n^2*abs($target_p2-$p2)/$p2)) - $i2/$i1*$s) / (1 + $i2/$i1); |
| 225 | $delta_s2 = $delta_s/$g; |
| 226 | if ($delta_s2 > 0) |
| 227 | bounce("expected to sell T2, calc says should buy " || $delta_s2); |
| 228 | $delta_ro = $ro/(1+$i2/$i1+$i2/$i1*$s/$delta_s) / $oswap_net; |
| 229 | if ($delta_ro > 0) |
| 230 | bounce("delta_ro > 0: " || $delta_s2); |
| 231 | $amount = round(-$delta_ro * 10^$reserve_asset_decimals); |
| 232 | if ($amount < $min_reserve_delta) |
| 233 | bounce("amount too small " || $amount); |
| 234 | $from = 'oswap'; |
| 235 | } |
| 236 | else |
| 237 | bounce("no arb opportunity exists"); |
| 238 | |
| 239 | |
| 240 | $max_amount = balance[$reserve_asset] - ($reserve_asset == 'base' ? 10000 : 0); |
| 241 | if ($amount > $max_amount){ |
| 242 | response['suboptimal'] = "optimal arb amount " || $amount || " but have only " || $max_amount; |
| 243 | if ($tokens2){ |
| 244 | $sent_tokens2 = round(0.7 * $tokens2 * $max_amount/$amount); |
| 245 | $sent_amount = $get_amount_for_buying($sent_tokens2); |
| 246 | if ($sent_amount > $max_amount) |
| 247 | bounce("balance is too small for optimal arb and scaling down didn't help"); |
| 248 | } |
| 249 | else |
| 250 | $sent_amount = $max_amount; |
| 251 | } |
| 252 | else { |
| 253 | $sent_tokens2 = $tokens2; |
| 254 | $sent_amount = $amount; |
| 255 | } |
| 256 | }", |
| 257 | "messages": [ |
| 258 | { |
| 259 | "app": "payment", |
| 260 | "payload": { |
| 261 | "asset": "{$reserve_asset}", |
| 262 | "outputs": [ |
| 263 | { |
| 264 | "address": "{$from == 'curve' ? $curve_aa : $reserve_oswap_aa}", |
| 265 | "amount": "{ $sent_amount }" |
| 266 | } |
| 267 | ] |
| 268 | } |
| 269 | }, |
| 270 | { |
| 271 | "if": "{$from == 'curve'}", |
| 272 | "app": "data", |
| 273 | "payload": { |
| 274 | "tokens2": "{$sent_tokens2}", |
| 275 | "tokens2_to": "{$stable_aa}" |
| 276 | } |
| 277 | }, |
| 278 | { |
| 279 | "if": "{$from == 'oswap'}", |
| 280 | "app": "data", |
| 281 | "payload": { |
| 282 | "to_aa": "{$stable_oswap_aa}", |
| 283 | "to": "{this_address}" |
| 284 | } |
| 285 | }, |
| 286 | { |
| 287 | "app": "state", |
| 288 | "state": "{ |
| 289 | var['sent_amount'] = $sent_amount; |
| 290 | response['sent_amount'] = $sent_amount; |
| 291 | response['message'] = $from == 'curve' ? 'will arb by buying from the curve' : 'will arb by selling to the curve'; |
| 292 | }" |
| 293 | } |
| 294 | ] |
| 295 | }, |
| 296 | { |
| 297 | "if": "{ trigger.output[[asset=$stable_asset]] > 0 AND trigger.address == $stable_aa }", |
| 298 | "init": "{ |
| 299 | if (!var['sent_amount']) |
| 300 | bounce('no sent amount when received from stable AA'); |
| 301 | }", |
| 302 | "messages": [ |
| 303 | { |
| 304 | "app": "payment", |
| 305 | "payload": { |
| 306 | "asset": "{$stable_asset}", |
| 307 | "outputs": [ |
| 308 | { |
| 309 | "address": "{$stable_oswap_aa}", |
| 310 | "amount": "{ trigger.output[[asset=$stable_asset]] }" |
| 311 | } |
| 312 | ] |
| 313 | } |
| 314 | }, |
| 315 | { |
| 316 | "app": "data", |
| 317 | "payload": { |
| 318 | "to_aa": "{$reserve_oswap_aa}", |
| 319 | "to": "{this_address}" |
| 320 | } |
| 321 | } |
| 322 | ] |
| 323 | }, |
| 324 | { |
| 325 | "if": "{ trigger.output[[asset=$stable_asset]] > 0 AND trigger.address == $stable_oswap_aa }", |
| 326 | "init": "{ |
| 327 | if (!var['sent_amount']) |
| 328 | bounce('no sent amount when received from oswap'); |
| 329 | }", |
| 330 | "messages": [ |
| 331 | { |
| 332 | "app": "payment", |
| 333 | "payload": { |
| 334 | "asset": "{$stable_asset}", |
| 335 | "outputs": [ |
| 336 | { |
| 337 | "address": "{$stable_aa}", |
| 338 | "amount": "{ trigger.output[[asset=$stable_asset]] }" |
| 339 | } |
| 340 | ] |
| 341 | } |
| 342 | }, |
| 343 | { |
| 344 | "app": "data", |
| 345 | "payload": { |
| 346 | "to": "{$curve_aa}" |
| 347 | } |
| 348 | } |
| 349 | ] |
| 350 | }, |
| 351 | { |
| 352 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 AND (trigger.address == $reserve_oswap_aa OR trigger.address == $curve_aa) }", |
| 353 | "messages": [ |
| 354 | { |
| 355 | "app": "state", |
| 356 | "state": "{ |
| 357 | $sent_amount = var['sent_amount']; |
| 358 | if (!$sent_amount) |
| 359 | bounce('no sent amount'); |
| 360 | $profit = trigger.output[[asset=$reserve_asset]] - $sent_amount; |
| 361 | response['profit'] = $profit; |
| 362 | |
| 363 | if ($profit < 0) |
| 364 | bounce('unprofitable ' || trigger.output[[asset=$reserve_asset]] || ' < ' || $sent_amount); |
| 365 | var['sent_amount'] = false; |
| 366 | }" |
| 367 | } |
| 368 | ] |
| 369 | }, |
| 370 | { |
| 371 | "if": "{ trigger.data.withdraw AND trigger.address == params.owner }", |
| 372 | "messages": [ |
| 373 | { |
| 374 | "app": "payment", |
| 375 | "payload": { |
| 376 | "asset": "{trigger.data.asset OTHERWISE $reserve_asset}", |
| 377 | "outputs": [ |
| 378 | { |
| 379 | "address": "{params.owner}", |
| 380 | "amount": "{ trigger.data.amount OTHERWISE '' }" |
| 381 | } |
| 382 | ] |
| 383 | } |
| 384 | } |
| 385 | ] |
| 386 | }, |
| 387 | { |
| 388 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 }", |
| 389 | "messages": [ |
| 390 | { |
| 391 | "app": "state", |
| 392 | "state": "{ |
| 393 | response['message'] = 'added ' || trigger.output[[asset=$reserve_asset]]; |
| 394 | }" |
| 395 | } |
| 396 | ] |
| 397 | } |
| 398 | ] |
| 399 | } |
| 400 | } |
| 401 | ] |