| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/stablecoin-t1-arbitrage.json", |
| 5 | "getters": "{ |
| 6 | $get_curve_aa = () => params.curve_aa; |
| 7 | }", |
| 8 | "init": "{ |
| 9 | $curve_aa = $get_curve_aa(); |
| 10 | $curve_params = definition[params.curve_aa][1].params; |
| 11 | |
| 12 | |
| 13 | $reserve_asset = $curve_params.reserve_asset OTHERWISE 'base'; |
| 14 | $asset1 = var[$curve_aa]['asset1']; |
| 15 | $shares_asset = var['shares_asset']; |
| 16 | |
| 17 | $get_leverage = () => $curve_params.leverage OTHERWISE 0; |
| 18 | $dilution_factor = var[$curve_aa]['dilution_factor']; |
| 19 | |
| 20 | $get_reserve = ($s1, $s2) => { |
| 21 | $r = $dilution_factor * $s1^$curve_params.m * $s2^$curve_params.n; |
| 22 | $r |
| 23 | }; |
| 24 | |
| 25 | $get_p2 = ($s1, $s2) => { |
| 26 | $p2 = $dilution_factor * $s1^$curve_params.m * $curve_params.n * (is_integer($curve_params.n*2) ? sqrt($s2^(($curve_params.n-1)*2)) : $s2^($curve_params.n-1) ); |
| 27 | $p2 |
| 28 | }; |
| 29 | |
| 30 | $get_p1 = () => { |
| 31 | $s1 = var[$curve_aa]['supply1']/10^$curve_params.decimals1; |
| 32 | $s2 = var[$curve_aa]['supply2']/10^$curve_params.decimals2; |
| 33 | $p1_in_full_units = $dilution_factor * $curve_params.m * $s1^($curve_params.m-1) * $s2^$curve_params.n; |
| 34 | $p1_in_smallest_units = $p1_in_full_units * 10^($curve_params.reserve_asset_decimals - $curve_params.decimals1); |
| 35 | $p1_in_smallest_units |
| 36 | }; |
| 37 | |
| 38 | $get_oracles = () => { |
| 39 | $oracles = var[$curve_aa]['oracles']; |
| 40 | if ($oracles) |
| 41 | return $oracles; |
| 42 | $initial_oracles = []; |
| 43 | if ($curve_params.oracle1 AND $curve_params.feed_name1) |
| 44 | $initial_oracles[] = {oracle: $curve_params.oracle1, feed_name: $curve_params.feed_name1, op: $curve_params.op1 OTHERWISE '*'}; |
| 45 | if ($curve_params.oracle2 AND $curve_params.feed_name2) |
| 46 | $initial_oracles[] = {oracle: $curve_params.oracle2, feed_name: $curve_params.feed_name2, op: $curve_params.op2 OTHERWISE '*'}; |
| 47 | if ($curve_params.oracle3 AND $curve_params.feed_name3) |
| 48 | $initial_oracles[] = {oracle: $curve_params.oracle3, feed_name: $curve_params.feed_name3, op: $curve_params.op3 OTHERWISE '*'}; |
| 49 | $initial_oracles |
| 50 | }; |
| 51 | |
| 52 | $get_initial_interest_rate = () => exists($curve_params.interest_rate) ? $curve_params.interest_rate : 0.1; |
| 53 | |
| 54 | $get_interest_rate = () => { |
| 55 | $interest_rate_var = var[$curve_aa]['interest_rate']; |
| 56 | exists($interest_rate_var) ? $interest_rate_var : $get_initial_interest_rate() |
| 57 | }; |
| 58 | |
| 59 | $get_growth_factor = () => { |
| 60 | $interest_rate = $get_interest_rate(); |
| 61 | $term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); |
| 62 | $growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term; |
| 63 | $growth_factor |
| 64 | }; |
| 65 | |
| 66 | $get_oracle_price = () => { |
| 67 | $oracles = $get_oracles(); |
| 68 | $oracle_price = reduce($oracles, 3, ($price, $oracle_info) => { |
| 69 | $df = data_feed[[oracles=$oracle_info.oracle, feed_name=$oracle_info.feed_name]]; |
| 70 | ($oracle_info.op == '*') ? $price * $df : $price / $df |
| 71 | }, 1); |
| 72 | $oracle_price |
| 73 | }; |
| 74 | |
| 75 | $get_target_p2 = () => { |
| 76 | $target_p2 = $get_oracle_price()^($get_leverage() - 1) * $get_growth_factor(); |
| 77 | $target_p2 |
| 78 | }; |
| 79 | |
| 80 | $get_exchange_data = () => { |
| 81 | $target_p2 = $get_target_p2(); |
| 82 | $s2 = var[$curve_aa]['supply2']/10^$curve_params.decimals2; |
| 83 | $target_s1 = ($target_p2/$curve_params.n * $s2^(1-$curve_params.n))^(1/$curve_params.m); |
| 84 | $tokens1_delta = round($target_s1 * 10^$curve_params.decimals1) - var[$curve_aa]['supply1']; |
| 85 | $new_s1 = (var[$curve_aa]['supply1'] + $tokens1_delta) / 10^$curve_params.decimals1; |
| 86 | $reserve_delta = ceil($get_reserve($new_s1, $s2) * 10^$curve_params.reserve_asset_decimals) - var[$curve_aa]['reserve']; |
| 87 | |
| 88 | |
| 89 | $initial_p2 = var[$curve_aa]['p2']; |
| 90 | $distance = abs($initial_p2 - $target_p2) / $target_p2; |
| 91 | $p2 = $get_p2($new_s1, $s2); |
| 92 | $new_distance = abs($p2 - $target_p2) / $target_p2; |
| 93 | $reward = floor((1 - $new_distance/$distance) * var[$curve_aa]['fast_capacity']); |
| 94 | $reserve_needed = $reserve_delta - $reward + ($reserve_asset == 'base' ? 1000 : 0); |
| 95 | |
| 96 | { |
| 97 | tokens1_delta: $tokens1_delta, |
| 98 | reserve_delta: $reserve_delta, |
| 99 | reserve_needed: $reserve_needed, |
| 100 | reward: $reward, |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | $p1 = $get_p1(); |
| 105 | |
| 106 | $get_total_assets = () => { |
| 107 | balance[$reserve_asset] - trigger.output[[asset=$reserve_asset]] + $p1 * (balance[$asset1] - trigger.output[[asset=$asset1]]) |
| 108 | }; |
| 109 | |
| 110 | $get_reserve_share_after = ($delta_reserve, $delta_asset1) => { |
| 111 | (balance[$reserve_asset] - $delta_reserve) / (balance[$reserve_asset] - $delta_reserve + $p1 * (balance[$asset1] - $delta_asset1)) |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | $status = var['status']; |
| 116 | |
| 117 | $max_reserve_share = var['max_reserve_share'] OTHERWISE params.max_reserve_share OTHERWISE 1; |
| 118 | $min_reserve_share = exists(var['min_reserve_share']) ? var['min_reserve_share'] : (params.min_reserve_share OTHERWISE 0); |
| 119 | $triggerer_reward_share = exists(var['triggerer_reward_share']) ? var['triggerer_reward_share'] : (params.triggerer_reward_share OTHERWISE 0); |
| 120 | $min_reserve_delta = var['min_reserve_delta'] OTHERWISE params.min_reserve_delta OTHERWISE 1e5; |
| 121 | |
| 122 | $min_reserve_investment = $reserve_asset == 'base' ? 1e4 : 0; |
| 123 | |
| 124 | if (trigger.data.to AND !is_valid_address(trigger.data.to)) |
| 125 | bounce("bad to address"); |
| 126 | $to = trigger.data.to OTHERWISE trigger.address; |
| 127 | |
| 128 | $governance_base_aa = 'P5CS22H3DNZ35RTY7TFAGRUDQRHRIK7T'; |
| 129 | }", |
| 130 | "messages": { |
| 131 | "cases": [ |
| 132 | { |
| 133 | "if": "{ trigger.data.define AND !$shares_asset }", |
| 134 | "messages": [ |
| 135 | { |
| 136 | "app": "asset", |
| 137 | "payload": { |
| 138 | "is_private": false, |
| 139 | "is_transferrable": true, |
| 140 | "auto_destroy": false, |
| 141 | "fixed_denominations": false, |
| 142 | "issued_by_definer_only": true, |
| 143 | "cosigned_by_definer": false, |
| 144 | "spender_attested": false |
| 145 | } |
| 146 | }, |
| 147 | { |
| 148 | "if": "{trigger.data.factory}", |
| 149 | "app": "payment", |
| 150 | "payload": { |
| 151 | "asset": "base", |
| 152 | "outputs": [ |
| 153 | { |
| 154 | "address": "{trigger.data.factory}", |
| 155 | "amount": 1000 |
| 156 | } |
| 157 | ] |
| 158 | } |
| 159 | }, |
| 160 | { |
| 161 | "app": "definition", |
| 162 | "payload": { |
| 163 | "definition": [ |
| 164 | "autonomous agent", |
| 165 | { |
| 166 | "base_aa": "{$governance_base_aa}", |
| 167 | "params": { |
| 168 | "arb_aa": "{this_address}", |
| 169 | "challenging_period": "{params.challenging_period}", |
| 170 | "freeze_period": "{params.freeze_period}" |
| 171 | } |
| 172 | } |
| 173 | ] |
| 174 | } |
| 175 | }, |
| 176 | { |
| 177 | "app": "state", |
| 178 | "state": "{ |
| 179 | var['governance_aa'] = unit[response_unit].messages[[.app='definition']].payload.address; |
| 180 | var['shares_asset'] = response_unit; |
| 181 | response['shares_asset'] = response_unit; |
| 182 | }" |
| 183 | } |
| 184 | ] |
| 185 | }, |
| 186 | { |
| 187 | "if": "{ $shares_asset AND trigger.address == var['governance_aa'] AND trigger.data.name }", |
| 188 | "init": "{ |
| 189 | $name = trigger.data.name; |
| 190 | }", |
| 191 | "messages": [ |
| 192 | { |
| 193 | "app": "state", |
| 194 | "state": "{ |
| 195 | var[$name] = trigger.data.value; |
| 196 | }" |
| 197 | } |
| 198 | ] |
| 199 | }, |
| 200 | { |
| 201 | "if": "{ trigger.data.arb }", |
| 202 | "init": "{ |
| 203 | $data = $get_exchange_data(); |
| 204 | $tokens1 = $data.tokens1_delta; |
| 205 | if (abs($tokens1) <= 1) |
| 206 | bounce("already on-peg"); |
| 207 | if (abs($data.reserve_delta) < $min_reserve_delta) |
| 208 | bounce("reserve delta would be too small: " || $data.reserve_delta); |
| 209 | $triggerer_reward = floor($triggerer_reward_share * $data.reward); |
| 210 | }", |
| 211 | "messages": [ |
| 212 | { |
| 213 | "app": "payment", |
| 214 | "payload": { |
| 215 | "asset": "{$reserve_asset}", |
| 216 | "outputs": [ |
| 217 | { |
| 218 | "if": "{$tokens1 > 0}", |
| 219 | "address": "{$curve_aa}", |
| 220 | "amount": "{ $data.reserve_needed }" |
| 221 | }, |
| 222 | { |
| 223 | "if": "{$triggerer_reward > 100}", |
| 224 | "address": "{trigger.address}", |
| 225 | "amount": "{ $triggerer_reward }" |
| 226 | } |
| 227 | ] |
| 228 | } |
| 229 | }, |
| 230 | { |
| 231 | "if": "{$tokens1 > 0}", |
| 232 | "app": "data", |
| 233 | "payload": { |
| 234 | "tokens1": "{$tokens1}" |
| 235 | } |
| 236 | }, |
| 237 | { |
| 238 | "if": "{$tokens1 < 0}", |
| 239 | "app": "payment", |
| 240 | "payload": { |
| 241 | "asset": "{$asset1}", |
| 242 | "outputs": [ |
| 243 | { |
| 244 | "address": "{$curve_aa}", |
| 245 | "amount": "{ -$tokens1 }" |
| 246 | } |
| 247 | ] |
| 248 | } |
| 249 | }, |
| 250 | { |
| 251 | "app": "state", |
| 252 | "state": "{ |
| 253 | var['status'] = 'arbing'; |
| 254 | var['expected_asset1_amount'] = $tokens1 > 0 ? $tokens1 : 0; |
| 255 | var['expected_reserve_amount'] = $tokens1 < 0 ? -$data.reserve_needed : 0; |
| 256 | }" |
| 257 | } |
| 258 | ] |
| 259 | }, |
| 260 | { |
| 261 | "if": "{ trigger.address == $curve_aa AND (trigger.output[[asset=$asset1]] > 0 OR trigger.output[[asset=$reserve_asset]] > 0) AND $status AND $status == 'arbing' }", |
| 262 | "init": "{ |
| 263 | $received_asset1_amount = trigger.output[[asset=$asset1]]; |
| 264 | $expected_asset1_amount = var['expected_asset1_amount']; |
| 265 | if ($expected_asset1_amount != $expected_asset1_amount) |
| 266 | bounce("wrong asset1 amount received from curve AA: expected " || $expected_asset1_amount || ", got " || $received_asset1_amount); |
| 267 | $received_reserve_amount = trigger.output[[asset=$reserve_asset]]; |
| 268 | $expected_reserve_amount = var['expected_reserve_amount']; |
| 269 | if ($expected_reserve_amount != $expected_reserve_amount) |
| 270 | bounce("wrong reserve amount received from curve AA: expected " || $expected_reserve_amount || ", got " || $received_reserve_amount); |
| 271 | }", |
| 272 | "messages": [ |
| 273 | { |
| 274 | "app": "state", |
| 275 | "state": "{ |
| 276 | var['expected_asset1_amount'] = false; |
| 277 | var['expected_reserve_amount'] = false; |
| 278 | var['status'] = false; |
| 279 | }" |
| 280 | } |
| 281 | ] |
| 282 | }, |
| 283 | { |
| 284 | "if": "{ $shares_asset AND (trigger.output[[asset=$reserve_asset]] > 0 OR trigger.output[[asset=$asset1]] > 0) AND trigger.output[[asset=$shares_asset]] == 0 }", |
| 285 | "init": "{ |
| 286 | if (var['investments_paused']) |
| 287 | bounce("investments paused by governance decision"); |
| 288 | $received_reserve_amount = trigger.output[[asset=$reserve_asset]] > $min_reserve_investment ? trigger.output[[asset=$reserve_asset]] : 0; |
| 289 | $received_asset1_amount = trigger.output[[asset=$asset1]]; |
| 290 | $shares_supply = var['shares_supply'] OTHERWISE 0; |
| 291 | |
| 292 | $balance = $get_total_assets(); |
| 293 | $received_asset1_value = $p1 * $received_asset1_amount; |
| 294 | $received_assets = $received_reserve_amount + $received_asset1_value; |
| 295 | if ($received_assets == 0) |
| 296 | bounce("0 contribution"); |
| 297 | |
| 298 | if ($shares_supply > 0){ |
| 299 | if ($balance == 0) |
| 300 | bounce("shares_supply > 0 AND balance == 0"); |
| 301 | $reserve_share = $get_reserve_share_after(0, 0); |
| 302 | $received_reserve_share = $received_reserve_amount/$received_assets; |
| 303 | $type = ($received_reserve_share > $reserve_share) ? 'reserve_share_up' : 'reserve_share_down'; |
| 304 | if ($reserve_share > $max_reserve_share AND $type != 'reserve_share_down') |
| 305 | bounce("the reserve share is too large and only proportional or T1 contributions (or anything in between) are allowed"); |
| 306 | if ($reserve_share < $min_reserve_share AND $type != 'reserve_share_up') |
| 307 | bounce("the T1 share is too large and only proportional or reserve contributions (or anything in between) are allowed"); |
| 308 | } |
| 309 | $share_price = $shares_supply ? $balance / $shares_supply : 1; |
| 310 | $shares_amount = floor($received_assets / $share_price); |
| 311 | }", |
| 312 | "messages": [ |
| 313 | { |
| 314 | "app": "payment", |
| 315 | "payload": { |
| 316 | "asset": "{$shares_asset}", |
| 317 | "outputs": [ |
| 318 | { |
| 319 | "address": "{$to}", |
| 320 | "amount": "{$shares_amount}" |
| 321 | } |
| 322 | ] |
| 323 | } |
| 324 | }, |
| 325 | { |
| 326 | "app": "state", |
| 327 | "state": "{ |
| 328 | var['shares_supply'] += $shares_amount; |
| 329 | }" |
| 330 | } |
| 331 | ] |
| 332 | }, |
| 333 | { |
| 334 | "if": "{ $shares_asset AND trigger.output[[asset=$shares_asset]] > 0 }", |
| 335 | "init": "{ |
| 336 | $what = trigger.data.what OTHERWISE 'both'; |
| 337 | if ($what != 't1' AND $what != 'reserve' AND $what != 'both') |
| 338 | bounce('bad type of asset to withdraw: ' || $what); |
| 339 | $received_shares_amount = trigger.output[[asset=$shares_asset]]; |
| 340 | $shares_supply = var['shares_supply']; |
| 341 | $balance = $get_total_assets(); |
| 342 | if ($balance < 0) |
| 343 | bounce("balance < 0"); |
| 344 | if ($shares_supply > 0 AND $balance == 0) |
| 345 | bounce("shares_supply > 0 AND balance == 0"); |
| 346 | if ($what == 'both'){ |
| 347 | $reserve_amount = floor($received_shares_amount/$shares_supply * (balance[$reserve_asset] - $min_reserve_investment)); |
| 348 | $asset1_amount = floor($received_shares_amount/$shares_supply * balance[$asset1]); |
| 349 | } |
| 350 | else{ |
| 351 | $share_price = $balance / $shares_supply; |
| 352 | if ($what == 'reserve'){ |
| 353 | $reserve_amount = floor($received_shares_amount * $share_price); |
| 354 | $asset1_amount = 0; |
| 355 | } |
| 356 | else{ |
| 357 | $reserve_amount = 0; |
| 358 | $asset1_amount = floor($received_shares_amount * $share_price / $p1); |
| 359 | } |
| 360 | $reserve_share = $get_reserve_share_after($reserve_amount, $asset1_amount); |
| 361 | if ($reserve_share < $min_reserve_share AND $what == 'reserve') |
| 362 | bounce("the reserve share is too small and only proportional or T1 withdrawals are allowed"); |
| 363 | if ($reserve_share > $max_reserve_share AND $what == 't1') |
| 364 | bounce("the T1 share is too small and only proportional or reserve withdrawals are allowed"); |
| 365 | } |
| 366 | }", |
| 367 | "messages": [ |
| 368 | { |
| 369 | "app": "payment", |
| 370 | "payload": { |
| 371 | "asset": "{$reserve_asset}", |
| 372 | "outputs": [ |
| 373 | { |
| 374 | "address": "{$to}", |
| 375 | "amount": "{$reserve_amount}" |
| 376 | } |
| 377 | ] |
| 378 | } |
| 379 | }, |
| 380 | { |
| 381 | "app": "payment", |
| 382 | "payload": { |
| 383 | "asset": "{$asset1}", |
| 384 | "outputs": [ |
| 385 | { |
| 386 | "address": "{$to}", |
| 387 | "amount": "{$asset1_amount}" |
| 388 | } |
| 389 | ] |
| 390 | } |
| 391 | }, |
| 392 | { |
| 393 | "app": "state", |
| 394 | "state": "{ |
| 395 | var['shares_supply'] -= $received_shares_amount; |
| 396 | }" |
| 397 | } |
| 398 | ] |
| 399 | } |
| 400 | ] |
| 401 | } |
| 402 | } |
| 403 | ] |