| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/stable-oswap2-oswap2-arb.json", |
| 5 | "getters": "{ |
| 6 | $arblib_aa = '2R5PP7IZRWIBXAKGI6YXIYDQ4EZKAWHE'; |
| 7 | |
| 8 | $get_growth_factor = ($curve_aa) => { |
| 9 | $interest_rate = var[$curve_aa]['interest_rate']; |
| 10 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 11 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 12 | $growth_factor |
| 13 | }; |
| 14 | |
| 15 | $get_target_p2_by_params = ($curve_aa, $curve_params, $g) => { |
| 16 | $arblib = $arblib_aa||''; |
| 17 | $oracle_price = $arblib#3.$get_oracle_price(var[$curve_aa]['oracles'], $curve_params); |
| 18 | if (!exists($oracle_price)) |
| 19 | return false; |
| 20 | $target_p2 = $oracle_price^(($curve_params.leverage OTHERWISE 0) - 1) * $g; |
| 21 | $target_p2 |
| 22 | }; |
| 23 | |
| 24 | $get_distance = ($p2, $target_p2) => (exists($p2) AND exists($target_p2)) ? abs($p2 - $target_p2) / min($p2, $target_p2) : 0; |
| 25 | |
| 26 | $get_fee_multiplier = ($curve_aa, $curve_params) => var[$curve_aa]['fee_multiplier'] OTHERWISE $curve_params.fee_multiplier OTHERWISE 5; |
| 27 | |
| 28 | |
| 29 | $get_stable_prices = () => { |
| 30 | $stable_aa = params.stable_aa; |
| 31 | $stable_params = definition[$stable_aa][1].params; |
| 32 | |
| 33 | $curve_aa = $stable_params.curve_aa; |
| 34 | $curve_params = definition[$curve_aa][1].params; |
| 35 | |
| 36 | $n = $curve_params.n; |
| 37 | $decimals2 = $curve_params.decimals2; |
| 38 | $reserve_asset_decimals = $curve_params.reserve_asset_decimals; |
| 39 | |
| 40 | $mul2 = 10^$decimals2; |
| 41 | $mul_r = 10^$reserve_asset_decimals; |
| 42 | $mul_price_display_to_pennies = $mul_r/$mul2; |
| 43 | |
| 44 | $p2 = var[$curve_aa]['p2']; |
| 45 | $s2 = var[$curve_aa]['supply2'] / $mul2; |
| 46 | $fc = var[$curve_aa]['fast_capacity'] / $mul_r; |
| 47 | |
| 48 | $g = $get_growth_factor($curve_aa); |
| 49 | $target_p2 = $get_target_p2_by_params($curve_aa, $curve_params, $g); |
| 50 | |
| 51 | $fee_multiplier = $get_fee_multiplier($curve_aa, $curve_params); |
| 52 | $fee_share = 2 * $fee_multiplier * ($n-1)/$n * abs($target_p2-$p2)/$p2 * $target_p2/$p2; |
| 53 | |
| 54 | $reward_share = ($n-1)*$fc/abs($target_p2-$p2)/$s2; |
| 55 | |
| 56 | $p_stable = $p2/$g * $mul_price_display_to_pennies; |
| 57 | $p_stable_with_reward = $p_stable * (1 - $reward_share); |
| 58 | $p_stable_with_fee = $p_stable * (1 - $fee_share); |
| 59 | |
| 60 | { |
| 61 | fee_share: $fee_share, |
| 62 | reward_share: $reward_share, |
| 63 | p_stable: $p_stable, |
| 64 | p_stable_with_fee: $p_stable_with_fee, |
| 65 | p_stable_with_reward: $p_stable_with_reward, |
| 66 | } |
| 67 | }; |
| 68 | }", |
| 69 | "init": "{ |
| 70 | $arblib = $arblib_aa||''; |
| 71 | |
| 72 | $min_reserve_delta = params.min_reserve_delta OTHERWISE 1e5; |
| 73 | |
| 74 | $stable_aa = params.stable_aa; |
| 75 | $stable_params = definition[$stable_aa][1].params; |
| 76 | |
| 77 | $curve_aa = $stable_params.curve_aa; |
| 78 | $curve_params = definition[$curve_aa][1].params; |
| 79 | |
| 80 | $m = $curve_params.m; |
| 81 | $n = $curve_params.n; |
| 82 | $decimals1 = $curve_params.decimals1; |
| 83 | $decimals2 = $curve_params.decimals2; |
| 84 | $reserve_asset_decimals = $curve_params.reserve_asset_decimals; |
| 85 | $mul2 = 10^$decimals2; |
| 86 | $mul_r = 10^$reserve_asset_decimals; |
| 87 | |
| 88 | |
| 89 | $stable_asset = var[$stable_aa]['asset']; |
| 90 | $reserve_asset = $curve_params.reserve_asset OTHERWISE 'base'; |
| 91 | |
| 92 | $reserve_oswap_aa = params.reserve_oswap_aa; |
| 93 | $stable_oswap_aa = params.stable_oswap_aa; |
| 94 | |
| 95 | $stable_oswap_params = definition[$stable_oswap_aa][1].params; |
| 96 | $reserve_oswap_params = definition[$reserve_oswap_aa][1].params; |
| 97 | |
| 98 | $get_stable_oswap_param = ($name, $default) => { |
| 99 | $value = var[$stable_oswap_aa][$name]; |
| 100 | exists($value) ? $value : (exists($stable_oswap_params[$name]) ? $stable_oswap_params[$name] : $default) |
| 101 | }; |
| 102 | |
| 103 | $get_reserve_oswap_param = ($name, $default) => { |
| 104 | $value = var[$reserve_oswap_aa][$name]; |
| 105 | exists($value) ? $value : (exists($reserve_oswap_params[$name]) ? $reserve_oswap_params[$name] : $default) |
| 106 | }; |
| 107 | |
| 108 | $stable_oswap_fee = $get_stable_oswap_param('swap_fee', 0.003); |
| 109 | $reserve_oswap_fee = $get_reserve_oswap_param('swap_fee', 0.003); |
| 110 | $oswap_net = (1-$stable_oswap_fee)*(1-$reserve_oswap_fee); |
| 111 | |
| 112 | $stable_imported_asset = $stable_oswap_params.x_asset == $stable_asset ? $stable_oswap_params.y_asset : $stable_oswap_params.x_asset; |
| 113 | $stable_imported_asset2 = $reserve_oswap_params.x_asset == $reserve_asset ? $reserve_oswap_params.y_asset : $reserve_oswap_params.x_asset; |
| 114 | require($stable_imported_asset2 == $stable_imported_asset, "stable imported asset mismatch " || $stable_imported_asset || ' != ' || $stable_imported_asset2); |
| 115 | |
| 116 | $arb = var['arb']; |
| 117 | |
| 118 | |
| 119 | $g = $get_growth_factor($curve_aa); |
| 120 | |
| 121 | $get_target_p2 = () => $get_target_p2_by_params($curve_aa, $curve_params, $g); |
| 122 | |
| 123 | $get_reserve = ($s1, $s2) => { |
| 124 | $r = $s1^$m * $s2^$n; |
| 125 | $r |
| 126 | }; |
| 127 | |
| 128 | $get_p2 = ($s1, $s2) => { |
| 129 | $p2 = $s1^$m * $n * $s2^($n-1); |
| 130 | $p2 |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | /* $get_slow_capacity_share = () => { |
| 135 | $slow_capacity_share_var = var[$curve_aa]['slow_capacity_share']; |
| 136 | if (exists($slow_capacity_share_var)) |
| 137 | $slow_capacity_share = $slow_capacity_share_var; |
| 138 | else if (exists($curve_params.slow_capacity_share)) |
| 139 | $slow_capacity_share = $curve_params.slow_capacity_share; |
| 140 | else |
| 141 | $slow_capacity_share = 0.5; |
| 142 | $slow_capacity_share |
| 143 | };*/ |
| 144 | |
| 145 | $fee_multiplier = $get_fee_multiplier($curve_aa, $curve_params); |
| 146 | |
| 147 | $get_fee = ($avg_reserve, $old_distance, $new_distance) => { |
| 148 | $fee = ceil($fee_multiplier * $avg_reserve * ($new_distance - $old_distance) * ($new_distance + $old_distance)); |
| 149 | $fee |
| 150 | }; |
| 151 | |
| 152 | $get_reserve_needed = ($tokens1, $tokens2, $target_p2) => { |
| 153 | |
| 154 | |
| 155 | |
| 156 | $initial_p2 = var[$curve_aa]['p2']; |
| 157 | $distance = $get_distance($initial_p2, $target_p2); |
| 158 | |
| 159 | $reserve = var[$curve_aa]['reserve']; |
| 160 | require($reserve, "no reserve"); |
| 161 | $new_supply1 = var[$curve_aa]['supply1'] + $tokens1; |
| 162 | $new_supply2 = var[$curve_aa]['supply2'] + $tokens2; |
| 163 | $s1 = $new_supply1 / 10^$decimals1; |
| 164 | $s2 = $new_supply2 / $mul2; |
| 165 | $r = $get_reserve($s1, $s2); |
| 166 | $p2 = $get_p2($s1, $s2); |
| 167 | $new_reserve = ceil($r * $mul_r); |
| 168 | $reserve_delta = $new_reserve - $reserve; |
| 169 | if ($tokens1 >= 0 AND $tokens2 >= 0 AND $reserve_delta < 0) |
| 170 | bounce("issuing tokens while the reserve decreases?"); |
| 171 | if ($tokens1 <= 0 AND $tokens2 <= 0 AND $reserve_delta > 0) |
| 172 | bounce("burning tokens while the reserve increases?"); |
| 173 | |
| 174 | $new_distance = $get_distance($p2, $target_p2); |
| 175 | $avg_reserve = ($reserve + $new_reserve) / 2; |
| 176 | $fast_capacity = var[$curve_aa]['fast_capacity']; |
| 177 | if ($distance == 0 AND $new_distance == 0){ |
| 178 | $fee = 0; |
| 179 | $reward = 0; |
| 180 | $reserve_needed = $reserve_delta; |
| 181 | } |
| 182 | else if ($new_distance > $distance){ |
| 183 | bounce("would pay a fee"); |
| 184 | /* $reward = 0; |
| 185 | $regular_fee = $get_fee($avg_reserve, $distance, $new_distance); |
| 186 | $new_fast_capacity = $fast_capacity + $regular_fee * $fast_capacity_share; |
| 187 | $distance_share = 1 - $distance/$new_distance; |
| 188 | |
| 189 | $reverse_reward = $distance_share * $new_fast_capacity; |
| 190 | if ($regular_fee >= $reverse_reward) |
| 191 | $fee = $regular_fee; |
| 192 | else |
| 193 | $fee = ceil($distance_share / (1 - $distance_share * $fast_capacity_share) * $fast_capacity); |
| 194 | $reserve_needed = $reserve_delta + $fee; |
| 195 | */ |
| 196 | } |
| 197 | else { |
| 198 | $fee = 0; |
| 199 | $regular_reward = floor((1 - $new_distance/$distance) * $fast_capacity); |
| 200 | if ($curve_params.capped_reward){ |
| 201 | |
| 202 | $reverse_fee = $get_fee($avg_reserve, $new_distance, $distance); |
| 203 | $reward = min($regular_reward, $reverse_fee); |
| 204 | } |
| 205 | else |
| 206 | $reward = $regular_reward; |
| 207 | $reserve_needed = $reserve_delta - $reward; |
| 208 | } |
| 209 | |
| 210 | $reserve_needed |
| 211 | }; |
| 212 | |
| 213 | $aa2aa_bytes = 2000; |
| 214 | $network_fee = ($reserve_asset == 'base') ? 4000 : 0; |
| 215 | $full_network_fee = $network_fee + ($reserve_asset == 'base' ? $aa2aa_bytes : 0); |
| 216 | |
| 217 | $get_amount_for_buying = ($tokens2, $target_p2) => { |
| 218 | if ($tokens2 == 0) |
| 219 | bounce("0 T2"); |
| 220 | $reserve_needed = $get_reserve_needed(0, $tokens2, $target_p2); |
| 221 | if ($reserve_needed < $min_reserve_delta) |
| 222 | bounce("reserve amount too small " || $reserve_needed); |
| 223 | $amount = $reserve_needed + $full_network_fee; |
| 224 | $amount |
| 225 | }; |
| 226 | |
| 227 | $get_fee_share = ($p2, $target_p2) => 2 * $fee_multiplier * ($n-1)/$n * abs($target_p2-$p2)/$p2 * $target_p2/$p2; |
| 228 | |
| 229 | |
| 230 | $get_shifts = () => { |
| 231 | $mid_price = $get_stable_oswap_param('mid_price', 0); |
| 232 | if ($mid_price){ |
| 233 | $alpha = $get_stable_oswap_param('alpha', 0.5); |
| 234 | $beta = 1 - $alpha; |
| 235 | $gamma = $get_stable_oswap_param('price_deviation', 0); |
| 236 | $lp_shares = var[$stable_oswap_aa]['lp_shares']; |
| 237 | $s_curve = $lp_shares.linear * $lp_shares.coef; |
| 238 | $x0 = $s_curve / $mid_price^$beta / $gamma; |
| 239 | $y0 = $x0 * $mid_price; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | {x0: $x0, y0: $y0} |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | }", |
| 250 | "messages": { |
| 251 | "cases": [ |
| 252 | { |
| 253 | "if": "{ trigger.data.arb OR trigger.data.swap_imported OR trigger.data.swap_stable}", |
| 254 | "init": "{ |
| 255 | $do_arb = trigger.data.arb; |
| 256 | $share = trigger.data.share OTHERWISE 1; |
| 257 | $precalculated = trigger.data.amount AND trigger.data.new_arb; |
| 258 | if (!$precalculated) { |
| 259 | $max_amount = balance[$reserve_asset] - ($reserve_asset == 'base' ? 10000 : 0); |
| 260 | $stable_balances = var[$stable_oswap_aa]['balances']; |
| 261 | $stable_l_balances = var[$stable_oswap_aa]['leveraged_balances']; |
| 262 | $reserve_balances = var[$reserve_oswap_aa]['balances']; |
| 263 | $reserve_l_balances = var[$reserve_oswap_aa]['leveraged_balances']; |
| 264 | |
| 265 | $shifts = $get_shifts(); |
| 266 | $stable_alpha = $get_stable_oswap_param('alpha', 0.5); |
| 267 | $stable_beta = 1 - $stable_alpha; |
| 268 | $reserve_alpha = $get_reserve_oswap_param('alpha', 0.5); |
| 269 | $reserve_beta = 1 - $reserve_alpha; |
| 270 | |
| 271 | $pxy_stable = $stable_alpha/$stable_beta * ($stable_balances.y + $shifts.y0) / ($stable_balances.x + $shifts.x0); |
| 272 | $pxy_reserve = $reserve_alpha/$reserve_beta * $reserve_balances.y / $reserve_balances.x; |
| 273 | |
| 274 | $stable_oswap_x_is_imported = $stable_oswap_params.x_asset == $stable_imported_asset; |
| 275 | $reserve_oswap_x_is_reserve = $reserve_oswap_params.x_asset == $reserve_asset; |
| 276 | |
| 277 | $p_stable_in_imported = $stable_oswap_x_is_imported ? 1/$pxy_stable : $pxy_stable; |
| 278 | $p_reserve_in_imported = $reserve_oswap_x_is_reserve ? $pxy_reserve : 1/$pxy_reserve; |
| 279 | $p_oswap = $p_stable_in_imported / $p_reserve_in_imported; |
| 280 | $p_imported_in_stable = 1/$p_stable_in_imported; |
| 281 | $p_imported_in_reserve = 1/$p_reserve_in_imported; |
| 282 | $p_oswap_minus_fee = $p_oswap * $oswap_net; |
| 283 | $p_oswap_plus_fee = $p_oswap / $oswap_net; |
| 284 | |
| 285 | $Lambda = $get_reserve_oswap_param('pool_leverage', 1); |
| 286 | |
| 287 | |
| 288 | |
| 289 | $denoms = $arblib#1.$get_denoms( |
| 290 | $stable_balances, $reserve_balances, |
| 291 | $stable_l_balances, $reserve_l_balances, |
| 292 | $shifts, |
| 293 | $pxy_stable, $pxy_reserve, |
| 294 | $stable_oswap_x_is_imported, |
| 295 | $reserve_oswap_x_is_reserve, |
| 296 | $stable_alpha, $reserve_alpha |
| 297 | ); |
| 298 | |
| 299 | $stable_denom = $denoms.stable_denom; |
| 300 | $reserve_denom = $denoms.reserve_denom; |
| 301 | $reserve_b = $denoms.reserve_b; |
| 302 | $stable_a = $denoms.stable_a; |
| 303 | require($stable_denom > 0, "negative stable denom "||$stable_denom); |
| 304 | require($reserve_denom > 0, "negative reserve denom "||$reserve_denom); |
| 305 | |
| 306 | |
| 307 | $mul_price_display_to_pennies = $mul_r/$mul2; |
| 308 | $r = var[$curve_aa]['reserve']; |
| 309 | $s2 = var[$curve_aa]['supply2'] / $mul2; |
| 310 | $p2 = var[$curve_aa]['p2']; |
| 311 | $fc = var[$curve_aa]['fast_capacity'] / $mul_r; |
| 312 | $target_p2 = $get_target_p2(); |
| 313 | $reward_share = ($n-1)*$fc/abs($target_p2-$p2)/$s2; |
| 314 | |
| 315 | $fee_share = $get_fee_share($p2, $target_p2); |
| 316 | |
| 317 | |
| 318 | $p_stable = $p2/$g * $mul_price_display_to_pennies; |
| 319 | $p_stable_with_reward = $p_stable * (1 - $reward_share); |
| 320 | $p_stable_with_fee = $p_stable * (1 - $fee_share); |
| 321 | |
| 322 | |
| 323 | if (trigger.data.swap_imported){ |
| 324 | $imported_balance = balance[$stable_imported_asset]; |
| 325 | $amount = $imported_balance / $p_reserve_in_imported * $share; |
| 326 | $final_p_reserve_in_imported = $p_reserve_in_imported * (1 + $amount / ($reserve_denom + $reserve_b * $Lambda * $amount)); |
| 327 | } |
| 328 | else if (trigger.data.swap_stable){ |
| 329 | $stable_balance = balance[$stable_asset]; |
| 330 | $dp = $stable_balance / ($stable_denom + $stable_a * $stable_balance) * $share; |
| 331 | $final_p_imported_in_stable = $p_imported_in_stable * (1 + $dp); |
| 332 | } |
| 333 | |
| 334 | else if ($p2 < $target_p2 AND $reward_share < 1 AND $get_distance($p2, $target_p2) > 0.0001 AND $p_stable_with_reward < $p_oswap_minus_fee) { |
| 335 | $delta_r = $r * ($p_oswap_minus_fee - $p_stable_with_reward)/$p_oswap_minus_fee / ($n-1 + $r/$p_stable_with_reward/$stable_denom + $r/$reserve_denom); |
| 336 | require($delta_r > 0, "negative delta_r when buying " || $delta_r); |
| 337 | $delta_r_sent = min($delta_r, $max_amount * 0.7); |
| 338 | $delta_s = $delta_r_sent/$p_oswap_minus_fee; |
| 339 | $delta_s2 = $delta_s/$g; |
| 340 | $tokens2 = floor($delta_s2); |
| 341 | $amount = $get_amount_for_buying($tokens2, $target_p2); |
| 342 | $from = 'curve'; |
| 343 | $stable_tokens = floor($tokens2*$g); |
| 344 | $dp = $stable_tokens / ($stable_denom + $stable_a * $stable_tokens) * $share; |
| 345 | $final_p_imported_in_stable = $p_imported_in_stable * (1 + $dp); |
| 346 | $final_p_reserve_in_imported = $p_reserve_in_imported * (1 + $amount / ($reserve_denom + $reserve_b * $Lambda * $amount) * $share * $share); |
| 347 | $new_arb = { |
| 348 | final_p_imported_in_reserve: 1/$final_p_reserve_in_imported, |
| 349 | final_p_stable_in_reserve: 1/$final_p_imported_in_stable/$final_p_reserve_in_imported, |
| 350 | }; |
| 351 | /* log({ |
| 352 | p2: $p2, |
| 353 | target_p2: $target_p2, |
| 354 | reward_share: $reward_share, |
| 355 | p_stable: $p_stable, |
| 356 | p_stable_with_reward: $p_stable_with_reward, |
| 357 | p_stable_in_imported: $p_stable_in_imported, |
| 358 | p_reserve_in_imported: $p_reserve_in_imported, |
| 359 | p_oswap: $p_oswap, |
| 360 | r: $r, |
| 361 | delta_r: $delta_r, |
| 362 | tokens2: $tokens2, |
| 363 | amount: $amount, |
| 364 | final_p_imported_in_stable: $final_p_imported_in_stable, |
| 365 | final_p_reserve_in_imported: $final_p_reserve_in_imported, |
| 366 | final_p_stable_in_reserve: 1/($final_p_reserve_in_imported * $final_p_imported_in_stable), |
| 367 | });*/ |
| 368 | $oswap_data = { |
| 369 | final_price: $final_p_imported_in_stable, |
| 370 | hops: [ |
| 371 | { |
| 372 | address: $reserve_oswap_aa, |
| 373 | change_address: this_address, |
| 374 | data: {final_price: $final_p_reserve_in_imported}, |
| 375 | }, |
| 376 | {address: this_address}, |
| 377 | ] |
| 378 | }; |
| 379 | } |
| 380 | |
| 381 | else if ($p_stable_with_fee > $p_oswap_plus_fee) { |
| 382 | |
| 383 | $get_arb_params = ($upper_p, $divisor) => { |
| 384 | $dr = $r * ($upper_p - $p_oswap_plus_fee)/$upper_p / ($n-1 + $r/$p_oswap_plus_fee/$stable_denom + $r/$reserve_denom) / $divisor; |
| 385 | require($dr > 0, "negative delta_r when selling " || $dr); |
| 386 | $dr_sent = min($dr, $max_amount); |
| 387 | $ds = $dr_sent/$upper_p; |
| 388 | |
| 389 | $amount_r = floor($dr_sent); |
| 390 | |
| 391 | $final_p_imported_in_reserve = $p_imported_in_reserve * (1 + $amount_r / ($reserve_denom + $reserve_b * $Lambda * $amount_r) * $share); |
| 392 | $final_p_stable_in_imported = $p_stable_in_imported * (1 + $ds / ($stable_denom + $stable_a * $ds) * $share * $share); |
| 393 | $final_p_stable_in_reserve = $final_p_stable_in_imported*$final_p_imported_in_reserve; |
| 394 | { |
| 395 | delta_r: $dr, |
| 396 | delta_s: $ds, |
| 397 | ds2: $ds/$g/$mul2, |
| 398 | amount: $amount_r, |
| 399 | final_p_imported_in_reserve: $final_p_imported_in_reserve, |
| 400 | final_p_stable_in_imported: $final_p_stable_in_imported, |
| 401 | final_p_stable_in_reserve: $final_p_stable_in_reserve, |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | $params1 = $get_arb_params($p_stable_with_fee, 1); |
| 406 | |
| 407 | |
| 408 | $dp2 = ($n-1)*$p2*$params1.ds2/$s2; |
| 409 | $p2_avg = $p2-$dp2/2; |
| 410 | $fee_share2 = $get_fee_share($p2_avg, $target_p2); |
| 411 | $p_stable_with_fee2 = $p_stable * (1 - $fee_share2); |
| 412 | |
| 413 | |
| 414 | if ($p_stable_with_fee2 > $p_oswap_plus_fee){ |
| 415 | $params2 = $get_arb_params($p_stable_with_fee2, 1); |
| 416 | $p_stable_adjustment = ($p_stable_with_fee - $p_stable_with_fee2)/$p_stable_with_fee*100; |
| 417 | |
| 418 | response['p_stable_adjustment'] = $p_stable_adjustment || '%'; |
| 419 | } |
| 420 | else{ |
| 421 | $params2 = $get_arb_params($p_stable_with_fee, 2); |
| 422 | |
| 423 | response['half'] = $params2.delta_r; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | $delta_s = $params2.delta_s; |
| 428 | $amount = min(ceil($params1.amount * $p_stable_with_fee/$p_oswap_plus_fee * 1.3), $max_amount); |
| 429 | $final_p_imported_in_reserve = $params2.final_p_imported_in_reserve; |
| 430 | $final_p_stable_in_imported = $params2.final_p_stable_in_imported; |
| 431 | $final_p_stable_in_reserve = $params2.final_p_stable_in_reserve; |
| 432 | |
| 433 | $new_arb = { |
| 434 | final_p_imported_in_reserve: $final_p_imported_in_reserve, |
| 435 | final_p_stable_in_reserve: $final_p_stable_in_reserve, |
| 436 | }; |
| 437 | |
| 438 | $from = 'oswap'; |
| 439 | |
| 440 | /* log({ |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | p_stable_in_imported: $p_stable_in_imported, |
| 447 | p_reserve_in_imported: $p_reserve_in_imported, |
| 448 | p_oswap: $p_oswap, |
| 449 | r: $r, |
| 450 | |
| 451 | amount: $amount, |
| 452 | delta_s: $delta_s, |
| 453 | final_p_imported_in_reserve: $final_p_imported_in_reserve, |
| 454 | final_p_stable_in_imported: $final_p_stable_in_imported, |
| 455 | final_p_stable_in_reserve: $new_arb.final_p_stable_in_reserve, |
| 456 | });*/ |
| 457 | require($amount >= $min_reserve_delta, "amount too small " || $amount); |
| 458 | } |
| 459 | else |
| 460 | bounce("no arb opportunity exists"); |
| 461 | } |
| 462 | else { |
| 463 | $amount = trigger.data.amount; |
| 464 | $tokens2 = trigger.data.tokens2; |
| 465 | $final_p_imported_in_reserve = trigger.data.final_p_imported_in_reserve; |
| 466 | $final_p_stable_in_imported = trigger.data.final_p_stable_in_imported; |
| 467 | $final_p_stable_in_reserve = $final_p_stable_in_imported*$final_p_imported_in_reserve; |
| 468 | $new_arb = { |
| 469 | final_p_imported_in_reserve: $final_p_imported_in_reserve, |
| 470 | final_p_stable_in_reserve: $final_p_stable_in_reserve, |
| 471 | }; |
| 472 | $oswap_data = trigger.data.oswap_data; |
| 473 | $from = $tokens2 ? 'curve' : 'oswap'; |
| 474 | } |
| 475 | }", |
| 476 | "messages": [ |
| 477 | { |
| 478 | "if": "{$do_arb}", |
| 479 | "app": "payment", |
| 480 | "payload": { |
| 481 | "asset": "{$reserve_asset}", |
| 482 | "outputs": [ |
| 483 | { |
| 484 | "address": "{$from == 'curve' ? $curve_aa : $reserve_oswap_aa}", |
| 485 | "amount": "{ $amount }" |
| 486 | } |
| 487 | ] |
| 488 | } |
| 489 | }, |
| 490 | { |
| 491 | "if": "{$do_arb AND $from == 'curve'}", |
| 492 | "app": "data", |
| 493 | "payload": { |
| 494 | "tokens2": "{$tokens2}", |
| 495 | "tokens2_to": "{$stable_aa}" |
| 496 | } |
| 497 | }, |
| 498 | { |
| 499 | "if": "{$do_arb AND $from == 'oswap'}", |
| 500 | "app": "data", |
| 501 | "payload": { |
| 502 | "final_price": "{$final_p_imported_in_reserve}", |
| 503 | "hops": [ |
| 504 | { |
| 505 | "address": "{$stable_oswap_aa}", |
| 506 | "change_address": "{this_address}", |
| 507 | "data": { |
| 508 | "final_price": "{$final_p_stable_in_imported}" |
| 509 | } |
| 510 | }, |
| 511 | { |
| 512 | "address": "{this_address}" |
| 513 | } |
| 514 | ] |
| 515 | } |
| 516 | }, |
| 517 | { |
| 518 | "if": "{trigger.data.swap_imported}", |
| 519 | "app": "payment", |
| 520 | "payload": { |
| 521 | "asset": "{$stable_imported_asset}", |
| 522 | "outputs": [ |
| 523 | { |
| 524 | "address": "{$reserve_oswap_aa}" |
| 525 | } |
| 526 | ] |
| 527 | } |
| 528 | }, |
| 529 | { |
| 530 | "if": "{trigger.data.swap_imported}", |
| 531 | "app": "data", |
| 532 | "payload": { |
| 533 | "final_price": "{$final_p_reserve_in_imported}" |
| 534 | } |
| 535 | }, |
| 536 | { |
| 537 | "if": "{trigger.data.swap_stable}", |
| 538 | "app": "payment", |
| 539 | "payload": { |
| 540 | "asset": "{$stable_asset}", |
| 541 | "outputs": [ |
| 542 | { |
| 543 | "address": "{$stable_oswap_aa}" |
| 544 | } |
| 545 | ] |
| 546 | } |
| 547 | }, |
| 548 | { |
| 549 | "if": "{trigger.data.swap_stable}", |
| 550 | "app": "data", |
| 551 | "payload": { |
| 552 | "final_price": "{$final_p_imported_in_stable}" |
| 553 | } |
| 554 | }, |
| 555 | { |
| 556 | "app": "state", |
| 557 | "state": "{ |
| 558 | if ($do_arb){ |
| 559 | var['sent_amount'] = $amount; |
| 560 | var['arb'] = $new_arb; |
| 561 | var['from'] = $from; |
| 562 | if ($from == 'curve'){ |
| 563 | require($oswap_data, 'no oswap_data'); |
| 564 | var['oswap_data'] = $oswap_data; |
| 565 | } |
| 566 | response['sent_amount'] = $amount; |
| 567 | response['reward_share'] = $reward_share; |
| 568 | response['fee_share'] = $fee_share; |
| 569 | response['message'] = $from == 'curve' ? 'will arb by buying from the curve' : 'will arb by selling to the curve'; |
| 570 | } |
| 571 | else if (trigger.data.swap_imported) |
| 572 | response['message'] = 'will swap imported to reserve'; |
| 573 | else if (trigger.data.swap_stable) |
| 574 | response['message'] = 'will swap stable to imported'; |
| 575 | }" |
| 576 | } |
| 577 | ] |
| 578 | }, |
| 579 | { |
| 580 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 AND trigger.address == $reserve_oswap_aa AND !var['from'] }", |
| 581 | "messages": [ |
| 582 | { |
| 583 | "app": "state", |
| 584 | "state": "{ |
| 585 | |
| 586 | response['reserve_output'] = trigger.output[[asset=$reserve_asset]]; |
| 587 | }" |
| 588 | } |
| 589 | ] |
| 590 | }, |
| 591 | { |
| 592 | "if": "{ trigger.output[[asset=$stable_imported_asset]] > 0 AND trigger.address == $stable_oswap_aa AND !var['from'] }", |
| 593 | "messages": [ |
| 594 | { |
| 595 | "app": "state", |
| 596 | "state": "{ |
| 597 | |
| 598 | response['imported_output'] = trigger.output[[asset=$stable_imported_asset]]; |
| 599 | }" |
| 600 | } |
| 601 | ] |
| 602 | }, |
| 603 | { |
| 604 | "if": "{ trigger.output[[asset=$stable_asset]] > 0 AND trigger.address == $stable_aa }", |
| 605 | "init": "{ |
| 606 | require(var['sent_amount'], 'no sent amount when received from stable AA'); |
| 607 | |
| 608 | response['forwarded_stable_from_curve'] = trigger.output[[asset=$stable_asset]]; |
| 609 | }", |
| 610 | "messages": [ |
| 611 | { |
| 612 | "app": "payment", |
| 613 | "payload": { |
| 614 | "asset": "{$stable_asset}", |
| 615 | "outputs": [ |
| 616 | { |
| 617 | "address": "{$stable_oswap_aa}", |
| 618 | "amount": "{ trigger.output[[asset=$stable_asset]] }" |
| 619 | } |
| 620 | ] |
| 621 | } |
| 622 | }, |
| 623 | { |
| 624 | "app": "data", |
| 625 | "payload": "{var['oswap_data']}" |
| 626 | } |
| 627 | ] |
| 628 | }, |
| 629 | { |
| 630 | "if": "{ trigger.output[[asset=$stable_asset]] > 0 AND trigger.address == $stable_oswap_aa AND var['from'] == 'oswap' }", |
| 631 | "init": "{ |
| 632 | require(var['sent_amount'], 'no sent amount when received from oswap'); |
| 633 | |
| 634 | }", |
| 635 | "messages": [ |
| 636 | { |
| 637 | "app": "payment", |
| 638 | "payload": { |
| 639 | "asset": "{$stable_asset}", |
| 640 | "outputs": [ |
| 641 | { |
| 642 | "address": "{$stable_aa}", |
| 643 | "amount": "{ trigger.output[[asset=$stable_asset]] }" |
| 644 | } |
| 645 | ] |
| 646 | } |
| 647 | }, |
| 648 | { |
| 649 | "app": "data", |
| 650 | "payload": { |
| 651 | "to": "{$curve_aa}" |
| 652 | } |
| 653 | }, |
| 654 | { |
| 655 | "app": "state", |
| 656 | "state": "{ |
| 657 | $arb.imported = $arb.imported + trigger.output[[asset=$stable_imported_asset]]; |
| 658 | var['arb'] = $arb; |
| 659 | response['forwarded_stable_from_oswap'] = trigger.output[[asset=$stable_asset]]; |
| 660 | }" |
| 661 | } |
| 662 | ] |
| 663 | }, |
| 664 | { |
| 665 | "if": "{ trigger.output[[asset=$stable_asset]] > 0 AND trigger.address == $stable_oswap_aa AND var['from'] == 'curve' }", |
| 666 | "messages": [ |
| 667 | { |
| 668 | "app": "state", |
| 669 | "state": "{ |
| 670 | $arb.stable = $arb.stable + trigger.output[[asset=$stable_asset]]; |
| 671 | var['arb'] = $arb; |
| 672 | |
| 673 | response['stable_change'] = trigger.output[[asset=$stable_asset]]; |
| 674 | }" |
| 675 | } |
| 676 | ] |
| 677 | }, |
| 678 | { |
| 679 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 AND trigger.address == $reserve_oswap_aa AND var['from'] == 'oswap' }", |
| 680 | "messages": [ |
| 681 | { |
| 682 | "app": "state", |
| 683 | "state": "{ |
| 684 | $arb.reserve = $arb.reserve + trigger.output[[asset=$reserve_asset]]; |
| 685 | var['arb'] = $arb; |
| 686 | |
| 687 | response['reserve_change'] = trigger.output[[asset=$reserve_asset]]; |
| 688 | }" |
| 689 | } |
| 690 | ] |
| 691 | }, |
| 692 | { |
| 693 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 AND (trigger.address == $reserve_oswap_aa OR trigger.address == $curve_aa) }", |
| 694 | "messages": [ |
| 695 | { |
| 696 | "app": "state", |
| 697 | "state": "{ |
| 698 | $sent_amount = var['sent_amount']; |
| 699 | require($sent_amount, 'no sent amount'); |
| 700 | $arb.imported = $arb.imported + trigger.output[[asset=$stable_imported_asset]]; |
| 701 | $arb.reserve = $arb.reserve + trigger.output[[asset=$reserve_asset]]; |
| 702 | $received_amount = $arb.reserve + $arb.imported * $arb.final_p_imported_in_reserve + $arb.stable * $arb.final_p_stable_in_reserve; |
| 703 | $profit = $received_amount - $sent_amount; |
| 704 | |
| 705 | response['profit'] = $profit; |
| 706 | |
| 707 | $direction = trigger.address == $reserve_oswap_aa ? 'buying' : 'selling'; |
| 708 | require($profit > 0, 'unprofitable ' || $direction || ': ' || $received_amount || ' < ' || $sent_amount || ", fee% " || trigger.data.tx.res.fee_percent || ", tokens2 " || trigger.data.tx.tokens2); |
| 709 | var['sent_amount'] = false; |
| 710 | var['from'] = false; |
| 711 | var['oswap_data'] = false; |
| 712 | var['arb'] = false; |
| 713 | }" |
| 714 | } |
| 715 | ] |
| 716 | }, |
| 717 | { |
| 718 | "if": "{ trigger.data.withdraw AND trigger.address == params.owner }", |
| 719 | "messages": [ |
| 720 | { |
| 721 | "app": "payment", |
| 722 | "payload": { |
| 723 | "asset": "{trigger.data.asset OTHERWISE $reserve_asset}", |
| 724 | "outputs": [ |
| 725 | { |
| 726 | "address": "{params.owner}", |
| 727 | "amount": "{ trigger.data.amount OTHERWISE '' }" |
| 728 | } |
| 729 | ] |
| 730 | } |
| 731 | } |
| 732 | ] |
| 733 | }, |
| 734 | { |
| 735 | "if": "{ trigger.output[[asset=$reserve_asset]] > 0 }", |
| 736 | "messages": [ |
| 737 | { |
| 738 | "app": "state", |
| 739 | "state": "{ |
| 740 | response['message'] = 'added ' || trigger.output[[asset=$reserve_asset]]; |
| 741 | }" |
| 742 | } |
| 743 | ] |
| 744 | } |
| 745 | ] |
| 746 | } |
| 747 | } |
| 748 | ] |