| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "getters": "{ |
| 5 | $check_aa = ($aa) => { |
| 6 | require(is_aa($aa), "not an AA"); |
| 7 | }; |
| 8 | |
| 9 | $get_reserve = ($aa, $yes_amount, $no_amount, $draw_amount) => { |
| 10 | |
| 11 | |
| 12 | $coef = var[$aa]['coef'] OTHERWISE 1; |
| 13 | |
| 14 | $r = ceil($coef * sqrt($yes_amount * $yes_amount + $no_amount * $no_amount + $draw_amount * $draw_amount)); |
| 15 | $r |
| 16 | }; |
| 17 | |
| 18 | $get_exchange_result = ($aa, $yes_amount, $no_amount, $draw_amount) => { |
| 19 | |
| 20 | |
| 21 | $params = definition[$aa][1].params; |
| 22 | $supply_yes = var[$aa]['supply_yes'] OTHERWISE 0; |
| 23 | $supply_no = var[$aa]['supply_no'] OTHERWISE 0; |
| 24 | $supply_draw = var[$aa]['supply_draw'] OTHERWISE 0; |
| 25 | $allow_draw = $params.allow_draw OTHERWISE false; |
| 26 | $current_reserve = var[$aa]['reserve'] OTHERWISE 0; |
| 27 | $coef = var[$aa]['coef'] OTHERWISE 1; |
| 28 | |
| 29 | $issue_fee = exists($params.issue_fee) ? $params.issue_fee : 0.01; |
| 30 | $redeem_fee = exists($params.redeem_fee) ? $params.redeem_fee : 0.02; |
| 31 | $arb_profit_tax = exists($params.arb_profit_tax) ? $params.arb_profit_tax : 0.9; |
| 32 | |
| 33 | $new_supply_yes = $supply_yes + ($yes_amount ? $yes_amount : 0); |
| 34 | $new_supply_no = $supply_no + ($no_amount ? $no_amount : 0); |
| 35 | $new_supply_draw = $supply_draw + ($draw_amount ? $draw_amount : 0); |
| 36 | |
| 37 | $new_reserve = $get_reserve($aa, $new_supply_yes, $new_supply_no, $new_supply_draw); |
| 38 | |
| 39 | $reserve_delta = $new_reserve - $current_reserve; |
| 40 | |
| 41 | require($new_supply_yes >= 0 AND $new_supply_no >= 0 AND $new_supply_draw >= 0, "too many tokens to redeem?"); |
| 42 | require($allow_draw OR $draw_amount == 0, "draw_asset doesn't exist"); |
| 43 | |
| 44 | $reserve_needed = $reserve_delta > 0 ? $reserve_delta : 0; |
| 45 | $gross_payout = $reserve_delta < 0 ? abs($reserve_delta) : 0; |
| 46 | |
| 47 | require($gross_payout <= $current_reserve, "too much reserve to redeem?"); |
| 48 | |
| 49 | $new_den = sqrt($new_supply_yes * $new_supply_yes + $new_supply_no * $new_supply_no + $new_supply_draw * $new_supply_draw); |
| 50 | |
| 51 | |
| 52 | if (($supply_yes + $supply_no + $supply_draw) != 0) { |
| 53 | $old_den = sqrt($supply_yes*$supply_yes + $supply_no*$supply_no + $supply_draw*$supply_draw); |
| 54 | |
| 55 | $old_yes_price = $coef * ($supply_yes / $old_den); |
| 56 | $old_no_price = $coef * ($supply_no / $old_den); |
| 57 | $old_draw_price = $coef * ($supply_draw / $old_den); |
| 58 | |
| 59 | $new_yes_price = $coef * ($new_supply_yes / $new_den); |
| 60 | $new_no_price = $coef * ($new_supply_no / $new_den); |
| 61 | $new_draw_price = $coef * ($new_supply_draw / $new_den); |
| 62 | |
| 63 | $yes_arb_profit_tax = (abs(($old_yes_price - $new_yes_price) * $yes_amount) / 2) * $arb_profit_tax; |
| 64 | $no_arb_profit_tax = (abs(($old_no_price - $new_no_price) * $no_amount) / 2) * $arb_profit_tax; |
| 65 | $draw_arb_profit_tax = $allow_draw ? (abs(($old_draw_price - $new_draw_price) * $draw_amount) / 2) * $arb_profit_tax : 0; |
| 66 | } else { |
| 67 | $yes_arb_profit_tax = 0; |
| 68 | $no_arb_profit_tax = 0; |
| 69 | $draw_arb_profit_tax = 0; |
| 70 | } |
| 71 | |
| 72 | $total_arb_profit_tax = $yes_arb_profit_tax + $no_arb_profit_tax + $draw_arb_profit_tax; |
| 73 | require($yes_arb_profit_tax >= 0 AND $no_arb_profit_tax >= 0 AND $draw_arb_profit_tax >= 0, "negative arb profit tax"); |
| 74 | |
| 75 | $fee = ceil($reserve_needed * $issue_fee + $gross_payout * $redeem_fee + $total_arb_profit_tax); |
| 76 | |
| 77 | $next_coef = $coef * (($new_reserve + $fee) / $new_reserve); |
| 78 | |
| 79 | { |
| 80 | reserve_needed: $reserve_needed, |
| 81 | new_reserve: $new_reserve + $fee, |
| 82 | gross_payout: $gross_payout, |
| 83 | fee: $fee, |
| 84 | arb_profit_tax: $total_arb_profit_tax, |
| 85 | next_coef: $next_coef, |
| 86 | old_coef: $coef, |
| 87 | yes_price: $next_coef * ($new_supply_yes / $new_den), |
| 88 | no_price: $next_coef * ($new_supply_no / $new_den), |
| 89 | draw_price: $next_coef * ($new_supply_draw / $new_den), |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | |
| 95 | $get_result_for_buying_by_type = ($aa, $type, $reserve_amount) => { |
| 96 | |
| 97 | $params = definition[$aa][1].params; |
| 98 | |
| 99 | $reserve_asset = $params.reserve_asset OTHERWISE 'base'; |
| 100 | $issue_fee = exists($params.issue_fee) ? $params.issue_fee : 0.01; |
| 101 | $arb_profit_tax = exists($params.arb_profit_tax) ? $params.arb_profit_tax : 0.9; |
| 102 | |
| 103 | require($type == 'yes' OR $type == 'no' OR $type == 'draw', "unknown type"); |
| 104 | require($params.allow_draw OR $type != 'draw', "draw token does not exist"); |
| 105 | require($reserve_amount - ($reserve_asset == 'base' ? 10000 : 0) > 0, "negative amount"); |
| 106 | |
| 107 | $coef = var[$aa]['coef'] OTHERWISE 1; |
| 108 | |
| 109 | $supply_yes = var[$aa]['supply_yes'] OTHERWISE 0; |
| 110 | $supply_no = var[$aa]['supply_no'] OTHERWISE 0; |
| 111 | $supply_draw = var[$aa]['supply_draw'] OTHERWISE 0; |
| 112 | |
| 113 | $network_fee = ($reserve_asset == 'base' ? 10000 : 0); |
| 114 | $gross_reserve_delta = $reserve_amount - $network_fee; |
| 115 | |
| 116 | $fee = ceil($gross_reserve_delta * $issue_fee); |
| 117 | |
| 118 | $old_reserve = var[$aa]['reserve']; |
| 119 | $new_reserve = $old_reserve + $gross_reserve_delta; |
| 120 | $reserve_without_fee = $new_reserve - $fee; |
| 121 | |
| 122 | $ratio = ($reserve_without_fee * $reserve_without_fee) / ($coef * $coef); |
| 123 | |
| 124 | $supply_yes_squared = $supply_yes * $supply_yes; |
| 125 | $supply_no_squared = $supply_no * $supply_no; |
| 126 | $supply_draw_squared = $supply_draw * $supply_draw; |
| 127 | |
| 128 | if ($type == 'yes') { |
| 129 | $prepare_calc = $ratio - $supply_no_squared - $supply_draw_squared; |
| 130 | } else if ($type == 'no') { |
| 131 | $prepare_calc = $ratio - $supply_yes_squared - $supply_draw_squared; |
| 132 | } else { |
| 133 | $prepare_calc = $ratio - $supply_yes_squared - $supply_no_squared; |
| 134 | } |
| 135 | |
| 136 | $supply = $type == 'yes' ? $supply_yes : $type == 'no' ? $supply_no : $supply_draw; |
| 137 | $amount = floor(sqrt($prepare_calc) - $supply); |
| 138 | $new_supply = $supply + $amount; |
| 139 | |
| 140 | $new_supply_squared = $new_supply * $new_supply; |
| 141 | $new_den = sqrt(($type == 'yes' ? $new_supply_squared : $supply_yes_squared) + ($type == 'no' ? $new_supply_squared : $supply_no_squared) + ($type == 'draw' ? $new_supply_squared : $supply_draw_squared)); |
| 142 | |
| 143 | if (($supply_yes + $supply_no + $supply_draw) != 0) { |
| 144 | $old_den = sqrt($supply_yes_squared + $supply_no_squared + $supply_draw_squared); |
| 145 | $old_price = $coef * ($supply / $old_den); |
| 146 | |
| 147 | $new_price = $coef * ($new_supply / $new_den); |
| 148 | $arb_profit_tax_amount = ((abs($old_price - $new_price) * $amount) / 2) * $arb_profit_tax; |
| 149 | |
| 150 | $fee_with_arb_profit_tax = $fee + $arb_profit_tax_amount; |
| 151 | |
| 152 | $reserve_without_tax_and_fee = $new_reserve - $fee_with_arb_profit_tax; |
| 153 | $new_ratio = ($reserve_without_tax_and_fee * $reserve_without_tax_and_fee) / ($coef * $coef); |
| 154 | |
| 155 | if ($type == 'yes') { |
| 156 | $prepare_calc_2 = $new_ratio - $supply_no_squared - $supply_draw_squared; |
| 157 | } else if ($type == 'no') { |
| 158 | $prepare_calc_2 = $new_ratio - $supply_yes_squared - $supply_draw_squared; |
| 159 | } else { |
| 160 | $prepare_calc_2 = $new_ratio - $supply_yes_squared - $supply_no_squared; |
| 161 | } |
| 162 | |
| 163 | $token_amount = floor(sqrt($prepare_calc_2) - $supply); |
| 164 | |
| 165 | } else { |
| 166 | $token_amount = $amount; |
| 167 | $fee_with_arb_profit_tax = $fee; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | $next_coef = $coef * $new_reserve / ($new_reserve - $fee_with_arb_profit_tax); |
| 172 | |
| 173 | $supply_by_type = $supply + $token_amount; |
| 174 | $supply_by_type_squared = $supply_by_type * $supply_by_type; |
| 175 | $den = sqrt(($type == 'yes' ? $supply_by_type_squared : $supply_yes_squared) + ($type == 'no' ? $supply_by_type_squared : $supply_no_squared) + ($type == 'draw' ? $supply_by_type_squared : $supply_draw_squared)); |
| 176 | |
| 177 | $yes_price = $next_coef * (($type == 'yes' ? $supply_by_type : $supply_yes) / $den); |
| 178 | $no_price = $next_coef * (($type == 'no' ? $supply_by_type : $supply_no) / $den); |
| 179 | $draw_price = $params.allow_draw ? $next_coef * (($type == 'draw' ? $supply_by_type : $supply_draw) / $den) : 0; |
| 180 | |
| 181 | |
| 182 | { |
| 183 | fee: $fee_with_arb_profit_tax, |
| 184 | gross_payout: 0, |
| 185 | old_coef: $coef, |
| 186 | reserve_needed: $gross_reserve_delta - $fee_with_arb_profit_tax, |
| 187 | new_reserve: $new_reserve, |
| 188 | arb_profit_tax: $arb_profit_tax_amount, |
| 189 | next_coef: $next_coef, |
| 190 | yes_price: $yes_price, |
| 191 | no_price: $no_price, |
| 192 | draw_price: $draw_price, |
| 193 | amount: $token_amount |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | }", |
| 198 | "messages": [ |
| 199 | { |
| 200 | "app": "state", |
| 201 | "state": "{ |
| 202 | bounce("library only"); |
| 203 | }" |
| 204 | } |
| 205 | ] |
| 206 | } |
| 207 | ] |