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