| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://oswap.io/pool-v2.json", |
| 5 | "getters": "{ |
| 6 | |
| 7 | $pool_lib_aa = 'MC5KTC25FGEMSGDBD6KBYW3DUFF35OKT'; |
| 8 | $pool_lib_by_price_aa = 'U75U5R3BYXVXBOTSRRDKS7HUIB63DJ2K'; |
| 9 | |
| 10 | $get_param = ($name, $default) => { |
| 11 | $value = var[$name]; |
| 12 | exists($value) ? $value : (exists(params[$name]) ? params[$name] : $default) |
| 13 | }; |
| 14 | |
| 15 | $get_swap_fee = () => $get_param('swap_fee', 0.003); |
| 16 | $get_exit_fee = () => $get_param('exit_fee', 0.005); |
| 17 | $get_arb_profit_tax = () => $get_param('arb_profit_tax', 0); |
| 18 | $get_leverage_profit_tax = () => $get_param('leverage_profit_tax', 0); |
| 19 | $get_leverage_token_tax = () => $get_param('leverage_token_tax', 0); |
| 20 | |
| 21 | $get_mid_price = () => $get_param('mid_price', 0); |
| 22 | $get_price_deviation = () => $get_param('price_deviation', 0); |
| 23 | |
| 24 | $get_base_interest_rate = () => $get_param('base_interest_rate', 0.2); |
| 25 | $get_pool_leverage = () => $get_param('pool_leverage', 1); |
| 26 | $get_alpha = () => $get_param('alpha', 0.5); |
| 27 | $get_period_length = () => $get_param('period_length', 3600); |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | $get_bonding_curve = () => params.shares_bonding_curve OTHERWISE 'IXBHF6T4IKMYAFGRM54F5FVMXGKCTFNT'; |
| 34 | |
| 35 | $get_linear_shares = ($issued_shares) => $get_bonding_curve()#2.$get_linear_shares($issued_shares); |
| 36 | $get_issued_shares = ($linear_shares) => $get_bonding_curve()#2.$get_issued_shares($linear_shares); |
| 37 | |
| 38 | |
| 39 | $get_shifts_and_bounds = () => { |
| 40 | $mid_price = $get_mid_price(); |
| 41 | if ($mid_price){ |
| 42 | $alpha = $get_alpha(); |
| 43 | $beta = 1 - $alpha; |
| 44 | $gamma = $get_price_deviation(); |
| 45 | $lp_shares = var['lp_shares']; |
| 46 | $s = $lp_shares.linear; |
| 47 | $s_curve = $s * $lp_shares.coef; |
| 48 | $x0 = $s_curve / $mid_price^$beta / $gamma; |
| 49 | $y0 = $x0 * $mid_price; |
| 50 | $p_max = $alpha/$beta * $gamma^(1/$beta) * $mid_price; |
| 51 | $p_min = $alpha/$beta / $gamma^(1/$alpha) * $mid_price; |
| 52 | } |
| 53 | else{ |
| 54 | $x0 = 0; |
| 55 | $y0 = 0; |
| 56 | } |
| 57 | {x0: $x0, y0: $y0, p_max: $p_max, p_min: $p_min} |
| 58 | }; |
| 59 | |
| 60 | $get_utilization_ratio = () => { |
| 61 | $alpha = $get_alpha(); |
| 62 | $shifts = $get_shifts_and_bounds(); |
| 63 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 64 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 65 | $pool_lib_aa.$get_utilization_ratio($balances, $l_balances, $shifts.x0, $shifts.y0, $alpha) |
| 66 | }; |
| 67 | |
| 68 | $get_interest_rate = () => { |
| 69 | $get_base_interest_rate() / (1 - $get_utilization_ratio()) |
| 70 | }; |
| 71 | |
| 72 | $get_balances_after_interest = () => { |
| 73 | $alpha = $get_alpha(); |
| 74 | $Lambda = $get_pool_leverage(); |
| 75 | $shifts = $get_shifts_and_bounds(); |
| 76 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 77 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 78 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 79 | $recent = var['recent'] OTHERWISE {}; |
| 80 | $i = $get_interest_rate(); |
| 81 | |
| 82 | $pool_lib = $pool_lib_aa || ''; |
| 83 | $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $Lambda); |
| 84 | { |
| 85 | balances: $balances, |
| 86 | leveraged_balances: $l_balances, |
| 87 | profits: $profits, |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | $get_price = ($asset, $base_decimals, $quote_decimals, $bAfterInterest) => { |
| 92 | if ($asset == 'x' OR $asset == params.x_asset) |
| 93 | $asset_label = 'x'; |
| 94 | else if ($asset == 'y' OR $asset == params.y_asset) |
| 95 | $asset_label = 'y'; |
| 96 | else |
| 97 | bounce("unknown asset "||$asset); |
| 98 | $mul = 10^(-$quote_decimals + $base_decimals); |
| 99 | $alpha = $get_alpha(); |
| 100 | $beta = 1 - $alpha; |
| 101 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 102 | $shifts = $get_shifts_and_bounds(); |
| 103 | |
| 104 | if ($bAfterInterest){ |
| 105 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 106 | $recent = var['recent'] OTHERWISE {}; |
| 107 | $i = $get_interest_rate(); |
| 108 | $Lambda = $get_pool_leverage(); |
| 109 | $pool_lib = $pool_lib_aa || ''; |
| 110 | $pool_lib#1.$charge_interest($balances, $l_balances, {}, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $Lambda); |
| 111 | } |
| 112 | ($asset_label == 'x') |
| 113 | ? $alpha/$beta * ($balances.y + $shifts.y0) / ($balances.x + $shifts.x0) * $mul |
| 114 | : $beta/$alpha * ($balances.x + $shifts.x0) / ($balances.y + $shifts.y0) * $mul |
| 115 | }; |
| 116 | |
| 117 | $get_swap_amounts_by_final_price = ($in_asset, $target_price_of_output_asset, $in_decimals, $out_decimals, $address) => { |
| 118 | if ($in_asset == 'x' OR $in_asset == params.x_asset) |
| 119 | $y_in = false; |
| 120 | else if ($in_asset == 'y' OR $in_asset == params.y_asset) |
| 121 | $y_in = true; |
| 122 | else |
| 123 | bounce("unknown in_asset "||$in_asset); |
| 124 | |
| 125 | |
| 126 | $in_mul = 10^(+$in_decimals); |
| 127 | $out_mul = 10^(+$out_decimals); |
| 128 | $price_mul = $in_mul/$out_mul; |
| 129 | $final_price = $target_price_of_output_asset * $price_mul; |
| 130 | |
| 131 | $alpha = $get_alpha(); |
| 132 | $beta = 1 - $alpha; |
| 133 | $pool_props = { |
| 134 | alpha: $alpha, |
| 135 | beta: $beta, |
| 136 | Lambda: $get_pool_leverage(), |
| 137 | swap_fee: $get_swap_fee(), |
| 138 | arb_profit_tax: $get_arb_profit_tax(), |
| 139 | period_length: $get_period_length(), |
| 140 | }; |
| 141 | $shifts = $get_shifts_and_bounds(); |
| 142 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 143 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 144 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 145 | $recent = var['recent'] OTHERWISE {}; |
| 146 | $i = $get_interest_rate(); |
| 147 | |
| 148 | $pool_lib = $pool_lib_aa || ''; |
| 149 | $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $pool_props.Lambda); |
| 150 | |
| 151 | $pool_lib_by_price = $pool_lib_by_price_aa || ''; |
| 152 | $res = $pool_lib_by_price#6.$swap($balances, $l_balances, $profits, $recent, $shifts.x0, $shifts.y0, $y_in, 0, $final_price, -1, 0, $address OTHERWISE 'ADDRESS', $pool_props); |
| 153 | { |
| 154 | in: $res.amount_Y/$in_mul, |
| 155 | out: $res.net_amount_X/$out_mul, |
| 156 | arb_profit_tax: $res.arb_profit_tax/$out_mul, |
| 157 | swap_fee: $res.swap_fee/$out_mul, |
| 158 | fees: { |
| 159 | in: $res.fees.Y/$in_mul, |
| 160 | out: $res.fees.X/$out_mul, |
| 161 | }, |
| 162 | balances: $balances, |
| 163 | leveraged_balances: $l_balances, |
| 164 | recent: $recent, |
| 165 | initial_price: $res.initial_price/$price_mul, |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | $get_swap_amounts_by_delta_net_balance = ($in_asset, $in_asset_delta_net_balance, $in_decimals, $out_decimals, $address) => { |
| 170 | if ($in_asset == 'x' OR $in_asset == params.x_asset) |
| 171 | $y_in = false; |
| 172 | else if ($in_asset == 'y' OR $in_asset == params.y_asset) |
| 173 | $y_in = true; |
| 174 | else |
| 175 | bounce("unknown in_asset "||$in_asset); |
| 176 | |
| 177 | |
| 178 | $in_mul = 10^(+$in_decimals); |
| 179 | $out_mul = 10^(+$out_decimals); |
| 180 | $price_mul = $in_mul/$out_mul; |
| 181 | $delta_Yn = $in_asset_delta_net_balance * $in_mul; |
| 182 | |
| 183 | $alpha = $get_alpha(); |
| 184 | $beta = 1 - $alpha; |
| 185 | $pool_props = { |
| 186 | alpha: $alpha, |
| 187 | beta: $beta, |
| 188 | Lambda: $get_pool_leverage(), |
| 189 | swap_fee: $get_swap_fee(), |
| 190 | arb_profit_tax: $get_arb_profit_tax(), |
| 191 | period_length: $get_period_length(), |
| 192 | }; |
| 193 | $shifts = $get_shifts_and_bounds(); |
| 194 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 195 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 196 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 197 | $recent = var['recent'] OTHERWISE {}; |
| 198 | $i = $get_interest_rate(); |
| 199 | |
| 200 | $pool_lib = $pool_lib_aa || ''; |
| 201 | $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $pool_props.Lambda); |
| 202 | |
| 203 | $res = $pool_lib#6.$swap($balances, $l_balances, $profits, $recent, $shifts.x0, $shifts.y0, $y_in, $delta_Yn, 0, -1, 0, $address OTHERWISE 'ADDRESS', $pool_props); |
| 204 | { |
| 205 | in: $res.amount_Y/$in_mul, |
| 206 | out: $res.net_amount_X/$out_mul, |
| 207 | arb_profit_tax: $res.arb_profit_tax/$out_mul, |
| 208 | swap_fee: $res.swap_fee/$out_mul, |
| 209 | fees: { |
| 210 | in: $res.fees.Y/$in_mul, |
| 211 | out: $res.fees.X/$out_mul, |
| 212 | }, |
| 213 | balances: $balances, |
| 214 | leveraged_balances: $l_balances, |
| 215 | recent: $recent, |
| 216 | initial_price: $res.initial_price/$price_mul, |
| 217 | final_price: $res.final_price/$price_mul, |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | $get_leveraged_price = ($asset, $L, $bAfterInterest) => { |
| 222 | if ($asset == 'x' OR $asset == params.x_asset) |
| 223 | $signedL = $L; |
| 224 | else if ($asset == 'y' OR $asset == params.y_asset) |
| 225 | $signedL = -$L; |
| 226 | else |
| 227 | bounce("unknown asset "||$asset); |
| 228 | |
| 229 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 230 | if ($bAfterInterest){ |
| 231 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 232 | $recent = var['recent'] OTHERWISE {}; |
| 233 | $i = $get_interest_rate(); |
| 234 | $alpha = $get_alpha(); |
| 235 | $Lambda = $get_pool_leverage(); |
| 236 | $shifts = $get_shifts_and_bounds(); |
| 237 | $pool_lib = $pool_lib_aa || ''; |
| 238 | $pool_lib#1.$charge_interest($balances, $l_balances, {}, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $Lambda); |
| 239 | } |
| 240 | $l_info = $l_balances[$signedL||'x']; |
| 241 | $l_info.balance / $L / $l_info.supply |
| 242 | }; |
| 243 | |
| 244 | $get_leveraged_trade_amounts = ($asset, $L, $delta_Xn, $entry_price, $address) => { |
| 245 | $asset_id = $asset == 'x' ? params.x_asset : ($asset == 'y' ? params.y_asset : $asset); |
| 246 | $alpha = $get_alpha(); |
| 247 | $beta = 1 - $alpha; |
| 248 | $pool_props = { |
| 249 | alpha: $alpha, |
| 250 | beta: $beta, |
| 251 | Lambda: $get_pool_leverage(), |
| 252 | swap_fee: $get_swap_fee(), |
| 253 | arb_profit_tax: $get_arb_profit_tax(), |
| 254 | leverage_profit_tax: $get_leverage_profit_tax(), |
| 255 | leverage_token_tax: $get_leverage_token_tax(), |
| 256 | period_length: $get_period_length(), |
| 257 | x_asset: params.x_asset, |
| 258 | y_asset: params.y_asset, |
| 259 | }; |
| 260 | $shifts = $get_shifts_and_bounds(); |
| 261 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 262 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 263 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 264 | $recent = var['recent'] OTHERWISE {}; |
| 265 | $i = $get_interest_rate(); |
| 266 | |
| 267 | $pool_lib = $pool_lib_aa || ''; |
| 268 | $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $pool_props.Lambda); |
| 269 | $res = $pool_lib#21.$trade_l_shares($balances, $l_balances, $profits, $recent, $shifts.x0, $shifts.y0, $L, $asset_id, $delta_Xn, $entry_price, $address OTHERWISE 'ADDRESS', $pool_props); |
| 270 | |
| 271 | $res.balances = $balances; |
| 272 | $res.leveraged_balances = $l_balances; |
| 273 | $res.recent = $recent; |
| 274 | $res |
| 275 | }; |
| 276 | |
| 277 | $get_total_balances = ($bAfterInterest) => { |
| 278 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 279 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 280 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 281 | $recent = var['recent'] OTHERWISE {}; |
| 282 | if ($bAfterInterest){ |
| 283 | $i = $get_interest_rate(); |
| 284 | $alpha = $get_alpha(); |
| 285 | $Lambda = $get_pool_leverage(); |
| 286 | $shifts = $get_shifts_and_bounds(); |
| 287 | $pool_lib = $pool_lib_aa || ''; |
| 288 | $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $shifts.x0, $shifts.y0, $recent.last_ts, $i, $alpha, $Lambda); |
| 289 | } |
| 290 | $leverages = $pool_lib_aa.$get_leverages(); |
| 291 | $p = this_address#30.$get_price('x', 0, 0, true); |
| 292 | $l_info = reduce($leverages, 6, ($info, $L) => { |
| 293 | $x_balance = $l_balances[$L||'x'].balance; |
| 294 | $y_balance = $l_balances[-$L||'x'].balance; |
| 295 | if ($x_balance){ |
| 296 | $info.x_assets = $info.x_assets + $x_balance; |
| 297 | $info.y_debt = $info.y_debt + $x_balance * ($L-1)/$L * $p; |
| 298 | } |
| 299 | if ($y_balance){ |
| 300 | $info.y_assets = $info.y_assets + $y_balance; |
| 301 | $info.x_debt = $info.x_debt + $y_balance * ($L-1)/$L / $p; |
| 302 | } |
| 303 | $info |
| 304 | }, {x_assets:0, y_assets:0, x_debt:0, y_debt:0}); |
| 305 | $totals = { |
| 306 | x: { |
| 307 | pool: $balances.xn, |
| 308 | profits: $profits.x, |
| 309 | leverage: { |
| 310 | assets: $l_info.x_assets, |
| 311 | debt: $l_info.x_debt, |
| 312 | net: $l_info.x_assets - $l_info.x_debt, |
| 313 | }, |
| 314 | balance: balance[params.x_asset], |
| 315 | }, |
| 316 | y: { |
| 317 | pool: $balances.yn, |
| 318 | profits: $profits.y, |
| 319 | leverage: { |
| 320 | assets: $l_info.y_assets, |
| 321 | debt: $l_info.y_debt, |
| 322 | net: $l_info.y_assets - $l_info.y_debt, |
| 323 | }, |
| 324 | balance: balance[params.y_asset], |
| 325 | }, |
| 326 | }; |
| 327 | $totals.x.total = $totals.x.pool + $totals.x.profits + $totals.x.leverage.net; |
| 328 | $totals.y.total = $totals.y.pool + $totals.y.profits + $totals.y.leverage.net; |
| 329 | $totals.x.excess = $totals.x.balance - $totals.x.total; |
| 330 | $totals.y.excess = $totals.y.balance - $totals.y.total; |
| 331 | $totals |
| 332 | }; |
| 333 | }", |
| 334 | "init": "{ |
| 335 | |
| 336 | $pool_lib = $pool_lib_aa || ''; |
| 337 | $pool_lib_by_price = $pool_lib_by_price_aa || ''; |
| 338 | |
| 339 | $x_asset = params.x_asset; |
| 340 | $y_asset = params.y_asset; |
| 341 | |
| 342 | require($x_asset != $y_asset, "identical assets"); |
| 343 | |
| 344 | $alpha = $get_alpha(); |
| 345 | require($alpha > 0 AND $alpha < 1, "invalid x_asset weight"); |
| 346 | $beta = 1 - $alpha; |
| 347 | |
| 348 | $Lambda = $get_pool_leverage(); |
| 349 | require($Lambda >= 1, "invalid pool leverage"); |
| 350 | require($alpha != 1/$Lambda, "pool leverage = 1/alpha"); |
| 351 | require($beta != 1/$Lambda, "pool leverage = 1/beta"); |
| 352 | |
| 353 | $mid_price = $get_mid_price(); |
| 354 | $gamma = $get_price_deviation(); |
| 355 | require(!$mid_price == !$gamma, "mid price and price deviation should be set or unset at the same time"); |
| 356 | |
| 357 | if ($Lambda > 1) |
| 358 | require(!$mid_price, "price range setting is incompatible with pool leverage"); |
| 359 | |
| 360 | |
| 361 | $lp_shares = var['lp_shares']; |
| 362 | $s = $lp_shares.linear; |
| 363 | $s_curve = $s * $lp_shares.coef; |
| 364 | |
| 365 | $shares_asset = $lp_shares.asset; |
| 366 | |
| 367 | $received_amount_x = trigger.output[[asset=$x_asset]]; |
| 368 | $received_amount_y = trigger.output[[asset=$y_asset]]; |
| 369 | $received_shares_amount = $shares_asset ? trigger.output[[asset=$shares_asset]] : 0; |
| 370 | |
| 371 | |
| 372 | |
| 373 | $min_x_amount = ($x_asset == 'base') ? 10000 : 0; |
| 374 | $min_y_amount = ($y_asset == 'base') ? 10000 : 0; |
| 375 | |
| 376 | $net_received_amount_x = $received_amount_x > $min_x_amount ? $received_amount_x - $min_x_amount : 0; |
| 377 | $net_received_amount_y = $received_amount_y > $min_y_amount ? $received_amount_y - $min_y_amount : 0; |
| 378 | |
| 379 | |
| 380 | if ($mid_price){ |
| 381 | |
| 382 | $mid_price_beta = $mid_price^$beta; |
| 383 | $x0 = $s_curve / $mid_price_beta / $gamma; |
| 384 | $y0 = $x0 * $mid_price; |
| 385 | } |
| 386 | else{ |
| 387 | $x0 = 0; |
| 388 | $y0 = 0; |
| 389 | } |
| 390 | |
| 391 | $pool_props = { |
| 392 | alpha: $alpha, |
| 393 | beta: $beta, |
| 394 | gamma: $gamma, |
| 395 | mid_price: $mid_price, |
| 396 | mid_price_beta: $mid_price_beta, |
| 397 | Lambda: $Lambda, |
| 398 | swap_fee: $get_swap_fee(), |
| 399 | exit_fee: $get_exit_fee(), |
| 400 | arb_profit_tax: $get_arb_profit_tax(), |
| 401 | leverage_profit_tax: $get_leverage_profit_tax(), |
| 402 | leverage_token_tax: $get_leverage_token_tax(), |
| 403 | period_length: $get_period_length(), |
| 404 | x_asset: $x_asset, |
| 405 | y_asset: $y_asset, |
| 406 | }; |
| 407 | |
| 408 | |
| 409 | $balances = var['balances'] OTHERWISE {xn:0, yn:0, x:0, y:0}; |
| 410 | $profits = var['profits'] OTHERWISE {x:0, y:0}; |
| 411 | $l_balances = var['leveraged_balances'] OTHERWISE {}; |
| 412 | $recent = var['recent'] OTHERWISE {}; |
| 413 | |
| 414 | $governance_aa = var['governance_aa']; |
| 415 | $from_governance = $governance_aa AND trigger.address == $governance_aa AND trigger.data.name; |
| 416 | |
| 417 | $get_current_interest_rate = ($base_interest_rate) => { |
| 418 | $base_interest_rate / (1 - $pool_lib#1.$get_utilization_ratio($balances, $l_balances, $x0, $y0, $alpha)) |
| 419 | }; |
| 420 | |
| 421 | if (length($l_balances) AND !$from_governance){ |
| 422 | |
| 423 | $i = $get_current_interest_rate($get_base_interest_rate()); |
| 424 | $charged_interest = $pool_lib#1.$charge_interest($balances, $l_balances, $profits, $x0, $y0, $recent.last_ts, $i, $alpha, $Lambda); |
| 425 | response['interest'] = json_stringify($charged_interest); |
| 426 | |
| 427 | |
| 428 | } |
| 429 | |
| 430 | $governance_base_aa = 'WBGLXP6WYRTHXYGBOBSBIHXM6TTGUUL7'; |
| 431 | }", |
| 432 | "messages": { |
| 433 | "cases": [ |
| 434 | { |
| 435 | "if": "{ !$shares_asset AND trigger.data.define }", |
| 436 | "messages": [ |
| 437 | { |
| 438 | "app": "definition", |
| 439 | "payload": { |
| 440 | "definition": [ |
| 441 | "autonomous agent", |
| 442 | { |
| 443 | "base_aa": "{$governance_base_aa}", |
| 444 | "params": { |
| 445 | "pool_aa": "{this_address}", |
| 446 | "challenging_period": "{params.governance_challenging_period OTHERWISE ''}", |
| 447 | "freeze_period": "{params.governance_freeze_period OTHERWISE ''}" |
| 448 | } |
| 449 | } |
| 450 | ] |
| 451 | } |
| 452 | }, |
| 453 | { |
| 454 | "app": "asset", |
| 455 | "payload": { |
| 456 | "is_private": false, |
| 457 | "is_transferrable": true, |
| 458 | "auto_destroy": false, |
| 459 | "fixed_denominations": false, |
| 460 | "issued_by_definer_only": true, |
| 461 | "cosigned_by_definer": false, |
| 462 | "spender_attested": false |
| 463 | } |
| 464 | }, |
| 465 | { |
| 466 | "if": "{trigger.data.factory}", |
| 467 | "app": "payment", |
| 468 | "payload": { |
| 469 | "asset": "base", |
| 470 | "outputs": [ |
| 471 | { |
| 472 | "address": "{trigger.data.factory}", |
| 473 | "amount": 1000 |
| 474 | } |
| 475 | ] |
| 476 | } |
| 477 | }, |
| 478 | { |
| 479 | "app": "state", |
| 480 | "state": "{ |
| 481 | var['governance_aa'] = unit[response_unit].messages[[.app='definition']].payload.address; |
| 482 | var['lp_shares'] = {issued: 0, linear: 0, coef: 1, asset: response_unit}; |
| 483 | response['shares_asset'] = response_unit; |
| 484 | }" |
| 485 | } |
| 486 | ] |
| 487 | }, |
| 488 | { |
| 489 | "if": "{ $from_governance }", |
| 490 | "init": "{ |
| 491 | $name = trigger.data.name; |
| 492 | $value = trigger.data.value; |
| 493 | $pool_lib#2.$validate_and_apply_new_governed_param_value($name, $value, $balances, $l_balances, $profits, $lp_shares, $pool_props, params.locked_governance); |
| 494 | }", |
| 495 | "messages": [ |
| 496 | { |
| 497 | "app": "state", |
| 498 | "state": "{ |
| 499 | |
| 500 | var['balances'] = $balances; |
| 501 | var['profits'] = $profits; |
| 502 | |
| 503 | if ($name == 'mid_price' OR $name == 'price_deviation') |
| 504 | var['lp_shares'] = $lp_shares; |
| 505 | var[$name] = $value; |
| 506 | }" |
| 507 | } |
| 508 | ] |
| 509 | }, |
| 510 | { |
| 511 | "if": "{ trigger.data.define_leverage AND trigger.data.leverage }", |
| 512 | "init": "{ |
| 513 | $leverage = trigger.data.leverage; |
| 514 | $allowed_leverages = {'2': true, '5': true, '10': true, '20': true, '50': true, '100': true}; |
| 515 | require($allowed_leverages[abs($leverage)], "leverage must be one of 2, 5, 10, 20, 50, or 100"); |
| 516 | require(!var['leveraged_asset' || $leverage], "asset for this leverage already defined"); |
| 517 | }", |
| 518 | "messages": [ |
| 519 | { |
| 520 | "app": "asset", |
| 521 | "payload": { |
| 522 | "is_private": false, |
| 523 | "is_transferrable": true, |
| 524 | "auto_destroy": false, |
| 525 | "fixed_denominations": false, |
| 526 | "issued_by_definer_only": true, |
| 527 | "cosigned_by_definer": false, |
| 528 | "spender_attested": false |
| 529 | } |
| 530 | }, |
| 531 | { |
| 532 | "app": "state", |
| 533 | "state": "{ |
| 534 | |
| 535 | var['leveraged_asset' || $leverage] = response_unit; |
| 536 | response['leveraged_asset' || $leverage] = response_unit; |
| 537 | }" |
| 538 | } |
| 539 | ] |
| 540 | }, |
| 541 | { |
| 542 | "if": "{ |
| 543 | $final_price = trigger.data.final_price; |
| 544 | $x_in = $received_amount_x > $min_x_amount AND (trigger.data.delta_xn OR $final_price); |
| 545 | $y_in = $received_amount_y > $min_y_amount AND (trigger.data.delta_yn OR $final_price); |
| 546 | ($x_in OR $y_in) AND $received_shares_amount == 0 AND !trigger.data.buy_shares |
| 547 | }", |
| 548 | "init": "{ |
| 549 | require(!($x_in AND $y_in), "received both assets"); |
| 550 | |
| 551 | if ($y_in){ |
| 552 | $received_amount_Y = $received_amount_y; |
| 553 | $in_asset = $y_asset; |
| 554 | $out_asset = $x_asset; |
| 555 | $delta_Yn = trigger.data.delta_yn; |
| 556 | } |
| 557 | else{ |
| 558 | $received_amount_Y = $received_amount_x; |
| 559 | $in_asset = $x_asset; |
| 560 | $out_asset = $y_asset; |
| 561 | $delta_Yn = trigger.data.delta_xn; |
| 562 | } |
| 563 | |
| 564 | $res = ($final_price ? $pool_lib_by_price : $pool_lib)#6.$swap($balances, $l_balances, $profits, $recent, $x0, $y0, $y_in, $final_price ? 0 : $delta_Yn, $final_price, $received_amount_Y, trigger.data.min_amount_out, trigger.initial_address, $pool_props); |
| 565 | |
| 566 | |
| 567 | $hops = trigger.data.hops; |
| 568 | $address = $hops[0].address OTHERWISE trigger.address; |
| 569 | $change_address = $hops[0].change_address OTHERWISE $address; |
| 570 | if ($hops){ |
| 571 | $data_for_next_hop = $hops[0].data; |
| 572 | delete($hops, 0); |
| 573 | if ($data_for_next_hop OR length($hops)){ |
| 574 | $forwarded_data = $data_for_next_hop OTHERWISE {}; |
| 575 | if (length($hops)) |
| 576 | $forwarded_data.hops = $hops; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | }", |
| 581 | "messages": [ |
| 582 | { |
| 583 | "app": "payment", |
| 584 | "payload": { |
| 585 | "asset": "{$out_asset}", |
| 586 | "outputs": [ |
| 587 | { |
| 588 | "address": "{$address}", |
| 589 | "amount": "{$res.net_amount_X}" |
| 590 | } |
| 591 | ] |
| 592 | } |
| 593 | }, |
| 594 | { |
| 595 | "app": "payment", |
| 596 | "payload": { |
| 597 | "asset": "{$in_asset}", |
| 598 | "outputs": [ |
| 599 | { |
| 600 | "address": "{$change_address}", |
| 601 | "amount": "{$res.change}" |
| 602 | } |
| 603 | ] |
| 604 | } |
| 605 | }, |
| 606 | { |
| 607 | "if": "{$forwarded_data}", |
| 608 | "app": "data", |
| 609 | "payload": "{$forwarded_data}" |
| 610 | }, |
| 611 | { |
| 612 | "app": "state", |
| 613 | "state": "{ |
| 614 | if ($Lambda == 1) |
| 615 | var['profits'] = $profits; |
| 616 | var['balances'] = $balances; |
| 617 | var['leveraged_balances'] = $l_balances; |
| 618 | var['recent'] = $recent; |
| 619 | response['event'] = $res.event; |
| 620 | }" |
| 621 | } |
| 622 | ] |
| 623 | }, |
| 624 | { |
| 625 | "if": "{ trigger.data.L AND trigger.data.asset AND (trigger.data.buy OR trigger.data.sell) AND trigger.data.delta }", |
| 626 | "init": "{ |
| 627 | require(!params.no_leverage, "leverage not allowed"); |
| 628 | |
| 629 | $res = $pool_lib#23.$handle_trade_l_shares_request(this_address, $balances, $l_balances, $profits, $recent, $x0, $y0, trigger.data, trigger.address, trigger.outputs, trigger.initial_address, $pool_props); |
| 630 | |
| 631 | $asset_out = $res.asset_out; |
| 632 | $shares = $res.shares; |
| 633 | $shares_change = $res.shares_change; |
| 634 | $asset = $res.asset; |
| 635 | $l_shares_asset = $res.l_shares_asset; |
| 636 | $position = $res.position; |
| 637 | |
| 638 | }", |
| 639 | "messages": [ |
| 640 | { |
| 641 | "if": "{trigger.data.buy AND trigger.data.tokens}", |
| 642 | "app": "payment", |
| 643 | "payload": { |
| 644 | "asset": "{$l_shares_asset}", |
| 645 | "outputs": [ |
| 646 | { |
| 647 | "address": "{trigger.address}", |
| 648 | "amount": "{$shares}" |
| 649 | } |
| 650 | ] |
| 651 | } |
| 652 | }, |
| 653 | { |
| 654 | "if": "{trigger.data.sell AND !$position}", |
| 655 | "app": "payment", |
| 656 | "payload": { |
| 657 | "asset": "{$l_shares_asset}", |
| 658 | "outputs": [ |
| 659 | { |
| 660 | "address": "{trigger.address}", |
| 661 | "amount": "{$shares_change}" |
| 662 | } |
| 663 | ] |
| 664 | } |
| 665 | }, |
| 666 | { |
| 667 | "app": "payment", |
| 668 | "payload": { |
| 669 | "asset": "{$asset}", |
| 670 | "outputs": [ |
| 671 | { |
| 672 | "address": "{trigger.address}", |
| 673 | "amount": "{$asset_out}" |
| 674 | } |
| 675 | ] |
| 676 | } |
| 677 | }, |
| 678 | { |
| 679 | "app": "state", |
| 680 | "state": "{ |
| 681 | var['recent'] = $recent; |
| 682 | var['balances'] = $balances; |
| 683 | var['leveraged_balances'] = $l_balances; |
| 684 | if (trigger.data.buy AND !trigger.data.tokens){ |
| 685 | var['positions'] += 1; |
| 686 | $new_position = 'position_' || $res.signedL || '_' || var['positions']; |
| 687 | var[$new_position] = { |
| 688 | owner: trigger.address, |
| 689 | shares: $shares, |
| 690 | price: $res.avg_share_price, |
| 691 | ts: timestamp, |
| 692 | }; |
| 693 | response['position'] = $new_position; |
| 694 | } |
| 695 | response['event'] = $res.event; |
| 696 | |
| 697 | if ($position){ |
| 698 | if ($shares_change){ |
| 699 | $position.shares = $shares_change; |
| 700 | response['remaining_shares'] = $shares_change; |
| 701 | } |
| 702 | var[trigger.data.position] = $shares_change ? $position : false; |
| 703 | } |
| 704 | if ($Lambda == 1) |
| 705 | var['profits'] = $profits; |
| 706 | }" |
| 707 | } |
| 708 | ] |
| 709 | }, |
| 710 | { |
| 711 | "if": "{ trigger.data.transfer AND trigger.data.position AND trigger.data.new_owner }", |
| 712 | "init": "{ |
| 713 | require(is_valid_address(trigger.data.new_owner), "new owner address is invalid"); |
| 714 | $position = var[trigger.data.position]; |
| 715 | require($position, "no such position"); |
| 716 | require($position.owner == trigger.address, "you are not the owner of this position"); |
| 717 | }", |
| 718 | "messages": [ |
| 719 | { |
| 720 | "app": "state", |
| 721 | "state": "{ |
| 722 | $position.owner = trigger.data.new_owner; |
| 723 | var[trigger.data.position] = $position; |
| 724 | response['message'] = "Transferred"; |
| 725 | }" |
| 726 | } |
| 727 | ] |
| 728 | }, |
| 729 | { |
| 730 | "if": "{ |
| 731 | $buy_shares = ($net_received_amount_x > 0 OR $net_received_amount_y > 0); |
| 732 | $received_shares_amount == 0 AND ($buy_shares OR trigger.data.add_profits) |
| 733 | }", |
| 734 | "init": "{ |
| 735 | $res = $pool_lib#1.$buy_shares($s, $balances, $profits, $recent, $x0, $y0, $net_received_amount_x, $net_received_amount_y, $pool_props); |
| 736 | |
| 737 | $lp_shares.coef = $lp_shares.coef * $res.coef; |
| 738 | $lp_shares.linear = $s + $res.shares_amount; |
| 739 | $new_issued = $get_issued_shares($lp_shares.linear); |
| 740 | $issue_amount = $new_issued - $lp_shares.issued; |
| 741 | $lp_shares.issued = $new_issued; |
| 742 | }", |
| 743 | "messages": [ |
| 744 | { |
| 745 | "if": "{$buy_shares}", |
| 746 | "app": "payment", |
| 747 | "payload": { |
| 748 | "asset": "{$shares_asset}", |
| 749 | "outputs": [ |
| 750 | { |
| 751 | "address": "{trigger.address}", |
| 752 | "amount": "{$issue_amount}" |
| 753 | } |
| 754 | ] |
| 755 | } |
| 756 | }, |
| 757 | { |
| 758 | "if": "{$buy_shares}", |
| 759 | "app": "payment", |
| 760 | "payload": { |
| 761 | "asset": "{$x_asset}", |
| 762 | "outputs": [ |
| 763 | { |
| 764 | "address": "{trigger.address}", |
| 765 | "amount": "{$res.change_x}" |
| 766 | } |
| 767 | ] |
| 768 | } |
| 769 | }, |
| 770 | { |
| 771 | "if": "{$buy_shares}", |
| 772 | "app": "payment", |
| 773 | "payload": { |
| 774 | "asset": "{$y_asset}", |
| 775 | "outputs": [ |
| 776 | { |
| 777 | "address": "{trigger.address}", |
| 778 | "amount": "{$res.change_y}" |
| 779 | } |
| 780 | ] |
| 781 | } |
| 782 | }, |
| 783 | { |
| 784 | "app": "state", |
| 785 | "state": "{ |
| 786 | var['lp_shares'] = $lp_shares; |
| 787 | var['balances'] = $balances; |
| 788 | var['leveraged_balances'] = $l_balances; |
| 789 | var['profits'] = $profits; |
| 790 | var['recent'] = $recent; |
| 791 | response['event'] = $res.event; |
| 792 | }" |
| 793 | } |
| 794 | ] |
| 795 | }, |
| 796 | { |
| 797 | "if": "{ $received_shares_amount > 0 AND $received_amount_x <= $min_x_amount AND $received_amount_y <= $min_y_amount }", |
| 798 | "init": "{ |
| 799 | $new_issued = $lp_shares.issued - $received_shares_amount; |
| 800 | $new_linear = $get_linear_shares($new_issued); |
| 801 | |
| 802 | $res = $pool_lib#1.$redeem_shares($s, $balances, $l_balances, $profits, $recent, $x0, $y0, $s - $new_linear, trigger.data.preferred_asset, $pool_props); |
| 803 | |
| 804 | $lp_shares.coef = $lp_shares.coef * $res.coef; |
| 805 | $lp_shares.linear = $new_linear; |
| 806 | $lp_shares.issued = $new_issued; |
| 807 | }", |
| 808 | "messages": [ |
| 809 | { |
| 810 | "app": "payment", |
| 811 | "payload": { |
| 812 | "asset": "{$x_asset}", |
| 813 | "outputs": [ |
| 814 | { |
| 815 | "address": "{trigger.address}", |
| 816 | "amount": "{$res.xn_amount}" |
| 817 | } |
| 818 | ] |
| 819 | } |
| 820 | }, |
| 821 | { |
| 822 | "app": "payment", |
| 823 | "payload": { |
| 824 | "asset": "{$y_asset}", |
| 825 | "outputs": [ |
| 826 | { |
| 827 | "address": "{trigger.address}", |
| 828 | "amount": "{$res.yn_amount}" |
| 829 | } |
| 830 | ] |
| 831 | } |
| 832 | }, |
| 833 | { |
| 834 | "app": "state", |
| 835 | "state": "{ |
| 836 | var['lp_shares'] = $lp_shares; |
| 837 | var['balances'] = $balances; |
| 838 | var['leveraged_balances'] = $l_balances; |
| 839 | var['profits'] = $profits; |
| 840 | var['recent'] = $recent; |
| 841 | response['event'] = $res.event; |
| 842 | }" |
| 843 | } |
| 844 | ] |
| 845 | } |
| 846 | ] |
| 847 | } |
| 848 | } |
| 849 | ] |