| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "getters": "{ |
| 8 | $get_growth_factor = ($rate_update_ts, $interest_rate, $curve_address) => { |
| 9 | $term = (timestamp - $rate_update_ts) / (360 * 24 * 3600); |
| 10 | $growth_factor = var[$curve_address]['growth_factor'] * (1 + $interest_rate)^$term; |
| 11 | $growth_factor |
| 12 | }; |
| 13 | |
| 14 | $get_oracles = ($curve_address, $curve_params) => { |
| 15 | $oracles = var[$curve_address]['oracles']; |
| 16 | if ($oracles) |
| 17 | return $oracles; |
| 18 | $initial_oracles = []; |
| 19 | if ($curve_params.oracle1 AND $curve_params.feed_name1) |
| 20 | $initial_oracles[] = {oracle: $curve_params.oracle1, feed_name: $curve_params.feed_name1, op: $curve_params.op1 OTHERWISE '*'}; |
| 21 | if ($curve_params.oracle2 AND $curve_params.feed_name2) |
| 22 | $initial_oracles[] = {oracle: $curve_params.oracle2, feed_name: $curve_params.feed_name2, op: $curve_params.op2 OTHERWISE '*'}; |
| 23 | if ($curve_params.oracle3 AND $curve_params.feed_name3) |
| 24 | $initial_oracles[] = {oracle: $curve_params.oracle3, feed_name: $curve_params.feed_name3, op: $curve_params.op3 OTHERWISE '*'}; |
| 25 | $initial_oracles |
| 26 | }; |
| 27 | |
| 28 | $get_oracle_price = ($curve_address, $curve_params) => { |
| 29 | $oracles = $get_oracles($curve_address, $curve_params); |
| 30 | $oracle_price = reduce($oracles, 3, ($price, $oracle_info) => { |
| 31 | if (!exists($price)) |
| 32 | return false; |
| 33 | $df = data_feed[[oracles=$oracle_info.oracle, feed_name=$oracle_info.feed_name, ifnone=false]]; |
| 34 | if (!exists($df)) |
| 35 | return false; |
| 36 | ($oracle_info.op == '*') ? $price * $df : $price / $df |
| 37 | }, 1); |
| 38 | $oracle_price |
| 39 | }; |
| 40 | |
| 41 | $get_target_p2 = ($curve_address, $curve_params, $leverage, $new_growth_factor) => { |
| 42 | $oracle_price = $get_oracle_price($curve_address, $curve_params); |
| 43 | if (!exists($oracle_price)) |
| 44 | return false; |
| 45 | $target_p2 = $oracle_price^($leverage - 1) * $new_growth_factor; |
| 46 | $target_p2 |
| 47 | }; |
| 48 | |
| 49 | $get_reserve = ($s1, $s2, $m, $n, $dilution_factor) => { |
| 50 | $r = $dilution_factor * $s1^$m * $s2^$n; |
| 51 | $r |
| 52 | }; |
| 53 | |
| 54 | $get_fee = ($avg_reserve, $old_distance, $new_distance, $fee_multiplier) => { |
| 55 | $fee = ceil($fee_multiplier * $avg_reserve * ($new_distance - $old_distance) * ($new_distance + $old_distance)); |
| 56 | $fee |
| 57 | }; |
| 58 | |
| 59 | $get_turnover = ($reserve_payout, $tokens1, $tokens2, $p2) => { |
| 60 | |
| 61 | $reserve_turnover = abs($reserve_payout); |
| 62 | if ($tokens1 >= 0 AND $tokens2 >= 0 OR $tokens1 <= 0 AND $tokens2 <= 0) |
| 63 | return $reserve_turnover; |
| 64 | $token2_turnover = abs($tokens2) * $p2 * 10^(params.reserve_asset_decimals - params.decimals2); |
| 65 | if ($tokens2 >= 0 AND $reserve_payout >= 0 OR $tokens2 <= 0 AND $reserve_payout <= 0) |
| 66 | return $token2_turnover + $reserve_turnover; |
| 67 | $token2_turnover |
| 68 | }; |
| 69 | }", |
| 70 | "init": "{ |
| 71 | $owner = params.owner; |
| 72 | if(!$owner) |
| 73 | bounce('no owner'); |
| 74 | }", |
| 75 | "messages": { |
| 76 | "cases": [ |
| 77 | { |
| 78 | "if": "{!exists(trigger.data.withdraw) AND !exists(trigger.data.execute) AND !exists(trigger.data.define) }", |
| 79 | "init": "{ |
| 80 | $curve_address = trigger.data.curve_address; |
| 81 | |
| 82 | if($owner != trigger.address) |
| 83 | bounce('You not owner'); |
| 84 | |
| 85 | if (!exists($curve_address) OR !is_valid_address($curve_address)) |
| 86 | bounce('bad curve_address address'); |
| 87 | |
| 88 | $asset1 = var[$curve_address]['asset1']; |
| 89 | $asset2 = var[$curve_address]['asset2']; |
| 90 | |
| 91 | if (!$asset1 AND !$asset2) |
| 92 | bounce('curve not found'); |
| 93 | |
| 94 | $id = 'exchange_' || $curve_address; |
| 95 | |
| 96 | $amount1 = trigger.output[[asset=$asset1]]; |
| 97 | $amount2 = trigger.output[[asset=$asset2]]; |
| 98 | |
| 99 | }", |
| 100 | "messages": [ |
| 101 | { |
| 102 | "app": "state", |
| 103 | "init": "{ |
| 104 | $max_fee_percent = trigger.data.max_fee_percent OTHERWISE 1; |
| 105 | |
| 106 | if($amount1 == 0 AND $amount2 == 0) |
| 107 | bounce("you don't send tokens"); |
| 108 | |
| 109 | $current_exchange = var[$id]; |
| 110 | }", |
| 111 | "state": "{ |
| 112 | if($current_exchange){ |
| 113 | $current_exchange.amount1 = $current_exchange.amount1 + $amount1; |
| 114 | $current_exchange.amount2 = $current_exchange.amount2 + $amount2; |
| 115 | var[$id] = $current_exchange; |
| 116 | response['STATUS'] = "UPDATE"; |
| 117 | } else { |
| 118 | $exchange = { |
| 119 | id: $id, |
| 120 | curve_address: $curve_address, |
| 121 | amount1: $amount1, |
| 122 | amount2: $amount2, |
| 123 | max_fee_percent: $max_fee_percent, |
| 124 | owner: $owner, |
| 125 | }; |
| 126 | |
| 127 | var[$id] = $exchange; |
| 128 | response['id'] = $id; |
| 129 | response['STATUS'] = "CREATE NEW"; |
| 130 | } |
| 131 | }" |
| 132 | } |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "if": "{ trigger.data.withdraw }", |
| 137 | "init": "{ |
| 138 | $tokens1 = trigger.data.tokens1 OTHERWISE 0; |
| 139 | $tokens2 = trigger.data.tokens2 OTHERWISE 0; |
| 140 | |
| 141 | if($owner != trigger.address) |
| 142 | bounce('You not owner'); |
| 143 | |
| 144 | $curve_address = trigger.data.curve_address; |
| 145 | |
| 146 | if (!exists($curve_address) OR !is_valid_address($curve_address)) |
| 147 | bounce('bad curve_address address'); |
| 148 | |
| 149 | $id = 'exchange_' || $curve_address; |
| 150 | $exchange = var[$id]; |
| 151 | |
| 152 | if(!$exchange) |
| 153 | bounce('exchange not found'); |
| 154 | |
| 155 | $asset1 = var[$curve_address]['asset1']; |
| 156 | $asset2 = var[$curve_address]['asset2']; |
| 157 | |
| 158 | $withdraw_all = $tokens1 == 0 AND $tokens2 == 0; |
| 159 | |
| 160 | if($tokens1 > 0 AND $exchange.amount1 < $tokens1) |
| 161 | bounce('You are trying to withdraw too many tokens1'); |
| 162 | |
| 163 | if($tokens2 > 0 AND $exchange.amount2 < $tokens2) |
| 164 | bounce('You are trying to withdraw too many tokens2'); |
| 165 | |
| 166 | }", |
| 167 | "messages": [ |
| 168 | { |
| 169 | "if": "{ $withdraw_all OR $tokens1 > 0 }", |
| 170 | "app": "payment", |
| 171 | "payload": { |
| 172 | "asset": "{$asset1}", |
| 173 | "outputs": [ |
| 174 | { |
| 175 | "address": "{$owner}", |
| 176 | "amount": "{ $tokens1 > 0 ? $tokens1 : $exchange.amount1 }" |
| 177 | } |
| 178 | ] |
| 179 | } |
| 180 | }, |
| 181 | { |
| 182 | "if": "{ $withdraw_all OR $tokens2 > 0}", |
| 183 | "app": "payment", |
| 184 | "payload": { |
| 185 | "asset": "{$asset2}", |
| 186 | "outputs": [ |
| 187 | { |
| 188 | "address": "{$owner}", |
| 189 | "amount": "{ $tokens2 > 0 ? $tokens2 : $exchange.amount2 }" |
| 190 | } |
| 191 | ] |
| 192 | } |
| 193 | }, |
| 194 | { |
| 195 | "if": "{ $withdraw_all OR $tokens1 > 0 OR $tokens2 > 0 }", |
| 196 | "app": "state", |
| 197 | "state": "{ |
| 198 | if($withdraw_all){ |
| 199 | var[$id] = false; |
| 200 | } else { |
| 201 | $exchange.amount1 = $exchange.amount1 - $tokens1; |
| 202 | $exchange.amount2 = $exchange.amount2 - $tokens2; |
| 203 | var[$id] = $exchange; |
| 204 | } |
| 205 | response['action'] = "withdraw"; |
| 206 | }" |
| 207 | } |
| 208 | ] |
| 209 | }, |
| 210 | { |
| 211 | "if": "{ trigger.data.execute }", |
| 212 | "init": "{ |
| 213 | $curve_address = trigger.data.curve_address; |
| 214 | |
| 215 | if($owner != trigger.address) |
| 216 | bounce('You not owner'); |
| 217 | |
| 218 | if (!exists($curve_address) OR !is_valid_address($curve_address)) |
| 219 | bounce('bad curve_address address'); |
| 220 | |
| 221 | $asset1 = var[$curve_address]['asset1']; |
| 222 | $asset2 = var[$curve_address]['asset2']; |
| 223 | |
| 224 | if (!$asset1 AND !$asset2) |
| 225 | bounce('curve not found'); |
| 226 | |
| 227 | if(!exists(var[$curve_address]['p2'])) |
| 228 | bounce('p2 not initialized'); |
| 229 | |
| 230 | $initial_p2 = var[$curve_address]['p2']; |
| 231 | |
| 232 | $curve_params = definition[$curve_address][1].params; |
| 233 | |
| 234 | $id = 'exchange_' || $curve_address; |
| 235 | |
| 236 | $amount1 = trigger.output[[asset=$asset1]]; |
| 237 | $amount2 = trigger.output[[asset=$asset2]]; |
| 238 | |
| 239 | $dilution_factor = var[$curve_address]['dilution_factor']; |
| 240 | $growth_factor = var[$curve_address]['growth_factor']; |
| 241 | $rate_update_ts = var[$curve_address]['rate_update_ts']; |
| 242 | |
| 243 | $leverage = $curve_params.leverage OTHERWISE 0; |
| 244 | |
| 245 | $m = $curve_params.m; |
| 246 | $n = $curve_params.n; |
| 247 | |
| 248 | $interest_rate = var[$curve_address]['interest_rate'] OTHERWISE $curve_params.interest_rate OTHERWISE 0.1; |
| 249 | |
| 250 | $exchange = var[$id]; |
| 251 | $auto_withdraw = trigger.data.auto_withdraw; |
| 252 | |
| 253 | $p2 = var[$curve_address]['p2']; |
| 254 | |
| 255 | if(!$exchange) |
| 256 | bounce('exchange not found'); |
| 257 | |
| 258 | if($exchange.amount1 == 0) |
| 259 | bounce('tokens1 not found'); |
| 260 | |
| 261 | if($exchange.amount2 == 0) |
| 262 | bounce('tokens2 not found'); |
| 263 | |
| 264 | $new_supply1 = var[$curve_address]['supply1'] - $exchange.amount1; |
| 265 | $new_supply2 = var[$curve_address]['supply2'] - $exchange.amount2; |
| 266 | |
| 267 | $s1 = $new_supply1 / 10^$curve_params.decimals1; |
| 268 | $s2 = $new_supply2 / 10^$curve_params.decimals2; |
| 269 | |
| 270 | $expectT1 = (($p2 / ($dilution_factor * $n * (is_integer($n*2) ? sqrt($s2^(($n-1)*2)) : $s2^($n-1))))^(1 / $m)) - (var[$curve_address]['supply1'] / 10^$curve_params.decimals1); |
| 271 | $expectT2 = (($p2 / ($dilution_factor * ($s1^$m) * $n)) ^ (1 / ($n - 1))) - var[$curve_address]['supply2'] / 10 ^ $curve_params.decimals2; |
| 272 | |
| 273 | $expectT1WithoutDecimals = abs(round($expectT1 * 10^$curve_params.decimals1)); |
| 274 | $expectT2WithoutDecimals = abs(round($expectT2 * 10^$curve_params.decimals2)); |
| 275 | |
| 276 | if(abs($expectT2WithoutDecimals) < $exchange.amount2) { |
| 277 | $count1 = $exchange.amount1; |
| 278 | $count2 = $expectT2WithoutDecimals; |
| 279 | $change1 = 0; |
| 280 | $change2 = $exchange.amount2 - $count2; |
| 281 | } else if(abs($expectT1WithoutDecimals) < $exchange.amount1){ |
| 282 | $count1 = $expectT1WithoutDecimals; |
| 283 | $count2 = $exchange.amount2; |
| 284 | $change1 = $exchange.amount1 - $count1; |
| 285 | $change2 = 0; |
| 286 | } else { |
| 287 | bounce("Couldn't calculate optimal quantity"); |
| 288 | } |
| 289 | |
| 290 | response['count1'] = $count1; |
| 291 | response['count2'] = $count2; |
| 292 | |
| 293 | response['change1'] = $change1; |
| 294 | response['change2'] = $change2; |
| 295 | |
| 296 | }", |
| 297 | "messages": [ |
| 298 | { |
| 299 | "app": "payment", |
| 300 | "payload": { |
| 301 | "asset": "{$asset1}", |
| 302 | "outputs": [ |
| 303 | { |
| 304 | "address": "{$curve_address}", |
| 305 | "amount": "{ $count1 }" |
| 306 | }, |
| 307 | { |
| 308 | "address": "{$owner}", |
| 309 | "amount": "{ $change1 }", |
| 310 | "if": "{ $auto_withdraw }" |
| 311 | } |
| 312 | ] |
| 313 | } |
| 314 | }, |
| 315 | { |
| 316 | "app": "payment", |
| 317 | "payload": { |
| 318 | "asset": "{$asset2}", |
| 319 | "outputs": [ |
| 320 | { |
| 321 | "address": "{$curve_address}", |
| 322 | "amount": "{ $count2 }" |
| 323 | }, |
| 324 | { |
| 325 | "address": "{$owner}", |
| 326 | "amount": "{ $change2 }", |
| 327 | "if": "{ $auto_withdraw }" |
| 328 | } |
| 329 | ] |
| 330 | } |
| 331 | }, |
| 332 | { |
| 333 | "app": "data", |
| 334 | "payload": { |
| 335 | "to": "{$owner}" |
| 336 | } |
| 337 | }, |
| 338 | { |
| 339 | "app": "state", |
| 340 | "state": "{ |
| 341 | if($auto_withdraw){ |
| 342 | var[$id] = false; |
| 343 | } else { |
| 344 | $exchange.amount1 = $exchange.amount1 - $count1; |
| 345 | $exchange.amount2 = $exchange.amount2 - $count2; |
| 346 | var[$id] = $exchange; |
| 347 | } |
| 348 | |
| 349 | $reserve = var[$curve_address]['reserve']; |
| 350 | |
| 351 | $fee_multiplier = var[$curve_address]['fee_multiplier'] OTHERWISE $curve_params.fee_multiplier OTHERWISE 5; |
| 352 | |
| 353 | $r = $get_reserve($s1, $s2, $m, $n, $dilution_factor); |
| 354 | |
| 355 | $new_reserve = ceil($r * 10^$curve_params.reserve_asset_decimals); |
| 356 | $avg_reserve = ($reserve + $new_reserve) / 2; |
| 357 | |
| 358 | $new_growth_factor = $get_growth_factor($rate_update_ts, $interest_rate, $curve_address); |
| 359 | $target_p2 = $get_target_p2($curve_address, $curve_params, $leverage, $new_growth_factor); |
| 360 | |
| 361 | $distance = abs($initial_p2 - $target_p2) / $target_p2; |
| 362 | $new_distance = abs($p2 - $target_p2) / $target_p2; |
| 363 | $fee = $get_fee($avg_reserve, $distance, $new_distance, $fee_multiplier); |
| 364 | |
| 365 | $reserve_delta = $new_reserve - $reserve; |
| 366 | $turnover = $get_turnover(-$reserve_delta, $count1, $count2, $p2); |
| 367 | |
| 368 | response['turnover'] = $turnover; |
| 369 | response['fee'] = $fee; |
| 370 | |
| 371 | $fee_percent = $fee / $turnover * 100; |
| 372 | |
| 373 | response['fee_percent'] = $fee_percent; |
| 374 | |
| 375 | if($fee_percent > $exchange.max_fee_percent){ |
| 376 | bounce('The commission percentage is greater than the maximum ' || $fee_percent || " > " || $exchange.max_fee_percent); |
| 377 | } |
| 378 | }" |
| 379 | } |
| 380 | ] |
| 381 | }, |
| 382 | { |
| 383 | "if": "{ trigger.data.define }", |
| 384 | "messages": [ |
| 385 | { |
| 386 | "app": "state", |
| 387 | "state": "{ |
| 388 | var['created_at'] = timestamp; |
| 389 | }" |
| 390 | } |
| 391 | ] |
| 392 | } |
| 393 | ] |
| 394 | } |
| 395 | } |
| 396 | ] |