| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "getters": "{ |
| 5 | $get_whitelisted_base_aas = () => { |
| 6 | 'DYZOJKX4MJOQRAUPX7K6WCEV5STMKOHI': true, |
| 7 | }; |
| 8 | |
| 9 | $get_available_balance = ($arb_aa, $a) => balance[$arb_aa][$a] - ($a == 'base' ? 10000 : 0); |
| 10 | |
| 11 | $get_denom = ($balances, $l_balances, $shifts, $pAB, $alpha, $in_label) => { |
| 12 | $leverages = [2, 5, 10, 20, 50, 100]; |
| 13 | |
| 14 | $beta = 1 - $alpha; |
| 15 | |
| 16 | $reversed = $in_label == 'y'; |
| 17 | $b = $reversed ? $alpha : $beta; |
| 18 | |
| 19 | |
| 20 | $L_sums = {x: 0}; |
| 21 | foreach($leverages, 6, $L => { |
| 22 | $signedL = $reversed ? -$L : $L; |
| 23 | $L_sums.x = $L_sums.x + ($L - 1) * ($l_balances[$signedL||'x'].balance + $l_balances[-$signedL||'x'].balance / $pAB); |
| 24 | }); |
| 25 | |
| 26 | $denom = $b * ($balances[$in_label] + $shifts[$in_label||'0']) - $L_sums.x; |
| 27 | |
| 28 | require($denom > 0, "negative denom " || $denom); |
| 29 | $denom |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | $calc_arb_params = ($arb_aa, $asset, $oswap_aas, $share, $secondary_share) => { |
| 34 | $whitelisted_base_aas = $get_whitelisted_base_aas(); |
| 35 | $balance = $get_available_balance($arb_aa, $asset); |
| 36 | $oswap_params = map($oswap_aas, 3, ($oswap_aa) => { |
| 37 | $definition = definition[$oswap_aa][1]; |
| 38 | require($whitelisted_base_aas[$definition.base_aa], "disallowed base AA in oswap AA "||$oswap_aa); |
| 39 | $definition.params |
| 40 | }); |
| 41 | $assetA = $asset; |
| 42 | $in_labels = []; |
| 43 | |
| 44 | |
| 45 | if ($oswap_params[0].x_asset == $asset){ |
| 46 | $in_labels[0] = 'x'; |
| 47 | $assetB = $oswap_params[0].y_asset; |
| 48 | } |
| 49 | else if ($oswap_params[0].y_asset == $asset){ |
| 50 | $in_labels[0] = 'y'; |
| 51 | $assetB = $oswap_params[0].x_asset; |
| 52 | } |
| 53 | else |
| 54 | bounce("first pool doesn't trade the reserve asset"); |
| 55 | |
| 56 | |
| 57 | $in_labels[1] = 'undef'; |
| 58 | if ($oswap_params[2].x_asset == $asset){ |
| 59 | $in_labels[2] = 'y'; |
| 60 | $assetC = $oswap_params[2].y_asset; |
| 61 | } |
| 62 | else if ($oswap_params[2].y_asset == $asset){ |
| 63 | $in_labels[2] = 'x'; |
| 64 | $assetC = $oswap_params[2].x_asset; |
| 65 | } |
| 66 | else |
| 67 | bounce("third pool doesn't trade the reserve asset"); |
| 68 | |
| 69 | |
| 70 | require($assetB != $assetC, "B and C assets are the same"); |
| 71 | |
| 72 | if ($oswap_params[1].x_asset == $assetB){ |
| 73 | $in_labels[1] = 'x'; |
| 74 | require($oswap_params[1].y_asset == $assetC, "second pool's y is not C"); |
| 75 | } |
| 76 | else if ($oswap_params[1].y_asset == $assetB){ |
| 77 | $in_labels[1] = 'y'; |
| 78 | require($oswap_params[1].x_asset == $assetC, "second pool's x is not C"); |
| 79 | } |
| 80 | else |
| 81 | bounce("second pool doesn't trade asset B"); |
| 82 | |
| 83 | $get_param = ($i, $name, $default) => { |
| 84 | $value = var[$oswap_aas[$i]][$name]; |
| 85 | exists($value) ? $value : (exists($oswap_params[$i][$name]) ? $oswap_params[$i][$name] : $default) |
| 86 | }; |
| 87 | |
| 88 | $fees = map([0,1,2], 3, $i => $get_param($i, 'swap_fee', 0.003)); |
| 89 | $alphas = map([0,1,2], 3, $i => $get_param($i, 'alpha', 0.5)); |
| 90 | $Lambdas = map([0,1,2], 3, $i => $get_param($i, 'pool_leverage', 1)); |
| 91 | $arb_profit_taxes = map([0,1], 2, $i => $get_param($i, 'arb_profit_tax', 0)); |
| 92 | |
| 93 | |
| 94 | $get_shifts = ($i) => { |
| 95 | $mid_price = $get_param($i, 'mid_price', 0); |
| 96 | if ($mid_price){ |
| 97 | $alpha = $alphas[$i]; |
| 98 | $beta = 1 - $alpha; |
| 99 | $gamma = $get_param($i, 'price_deviation', 0); |
| 100 | $lp_shares = var[$oswap_aas[$i]]['lp_shares']; |
| 101 | $s_curve = $lp_shares.linear * $lp_shares.coef; |
| 102 | $x0 = $s_curve / $mid_price^$beta / $gamma; |
| 103 | $y0 = $x0 * $mid_price; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | {x0: $x0, y0: $y0} |
| 110 | }; |
| 111 | |
| 112 | $shifts = map([0,1,2], 3, $i => $get_shifts($i)); |
| 113 | $oswap_balances = map([0,1,2], 3, $i => var[$oswap_aas[$i]]['balances']); |
| 114 | |
| 115 | $get_price = ($i) => { |
| 116 | $alpha = $alphas[$i]; |
| 117 | $beta = 1 - $alpha; |
| 118 | $balances = $oswap_balances[$i]; |
| 119 | $sh = $shifts[$i]; |
| 120 | |
| 121 | ($in_labels[$i] == 'x') |
| 122 | ? $alpha/$beta * ($balances.y + $sh.y0) / ($balances.x + $sh.x0) |
| 123 | : $beta/$alpha * ($balances.x + $sh.x0) / ($balances.y + $sh.y0) |
| 124 | }; |
| 125 | |
| 126 | $pAB = $get_price(0); |
| 127 | $pBC = $get_price(1); |
| 128 | $pCA = $get_price(2); |
| 129 | $pAB2 = 1/$pBC/$pCA; |
| 130 | log('pAB', $pAB, 'pAB2', $pAB2); |
| 131 | |
| 132 | $pAC = 1/$pCA; |
| 133 | $pCB = 1/$pBC; |
| 134 | $pBA = 1/$pAB; |
| 135 | |
| 136 | $denomAB = $get_denom($oswap_balances[0], var[$oswap_aas[0]]['leveraged_balances'], $shifts[0], $pAB, $alphas[0], $in_labels[0]); |
| 137 | $denomBC = $get_denom($oswap_balances[1], var[$oswap_aas[1]]['leveraged_balances'], $shifts[1], $pBC, $alphas[1], $in_labels[1]); |
| 138 | $denomCA = $get_denom($oswap_balances[2], var[$oswap_aas[2]]['leveraged_balances'], $shifts[2], $pCA, $alphas[2], $in_labels[2]); |
| 139 | |
| 140 | |
| 141 | $amounts = {A: 0, B: 0, C: 0}; |
| 142 | $b = []; |
| 143 | $b[0] = $in_labels[0] == 'x' ? 1 - $alphas[0] : $alphas[0]; |
| 144 | $b[1] = $in_labels[1] == 'x' ? 1 - $alphas[1] : $alphas[1]; |
| 145 | $b[2] = $in_labels[2] == 'x' ? 1 - $alphas[2] : $alphas[2]; |
| 146 | |
| 147 | |
| 148 | if ($pAB * (1 - $fees[0]) > $pAB2 * (1 + $fees[1]) * (1 + $fees[2])){ |
| 149 | $pAB_minus_fee = $pAB * (1 - $fees[0]); |
| 150 | $pAB2_plus_fee = $pAB2 * (1 + $fees[1]) * (1 + $fees[2]); |
| 151 | |
| 152 | $amounts.A = floor(($pAB_minus_fee - $pAB2_plus_fee)/$pAB_minus_fee / (1/$denomAB + $pAB/$denomBC + $pAB*$pBC/$denomCA)); |
| 153 | if ($amounts.A > $balance) |
| 154 | $amounts.A = $balance; |
| 155 | |
| 156 | $amounts.netA = $amounts.A * $share; |
| 157 | $final_pBA = $pBA * (1 + $amounts.netA/($denomAB + $b[0] * $Lambdas[0] * $amounts.netA)); |
| 158 | $amounts.B = $amounts.netA / $final_pBA * (1 - $fees[0]) - ($pAB - 1/$final_pBA) * $amounts.netA / 2 * $arb_profit_taxes[0]; |
| 159 | $amounts.netB = $amounts.B * $secondary_share; |
| 160 | $final_pCB = $pCB * (1 + $amounts.netB/($denomBC + $b[1] * $Lambdas[1] * $amounts.netB) ); |
| 161 | $amounts.C = $amounts.netB / $final_pCB * (1 - $fees[1]) - ($pBC - 1/$final_pCB) * $amounts.netB / 2 * $arb_profit_taxes[1]; |
| 162 | $amounts.netC = $amounts.C * $secondary_share; |
| 163 | $final_pAC = $pAC * (1 + $amounts.netC/($denomCA + $b[2] * $Lambdas[2] * $amounts.netC) ); |
| 164 | |
| 165 | |
| 166 | |
| 167 | $from = 'AB'; |
| 168 | $amount = $amounts.A; |
| 169 | $address = $oswap_aas[0]; |
| 170 | $data = { |
| 171 | final_price: $final_pBA, |
| 172 | hops: [ |
| 173 | { |
| 174 | address: $oswap_aas[1], |
| 175 | change_address: $arb_aa, |
| 176 | data: {final_price: $final_pCB}, |
| 177 | }, |
| 178 | { |
| 179 | address: $oswap_aas[2], |
| 180 | change_address: $arb_aa, |
| 181 | data: {final_price: $final_pAC}, |
| 182 | }, |
| 183 | { |
| 184 | address: $arb_aa, |
| 185 | }, |
| 186 | ] |
| 187 | }; |
| 188 | } |
| 189 | else if ($pAB * (1 + $fees[0]) < $pAB2 * (1 - $fees[1]) * (1 - $fees[2])){ |
| 190 | $denomBA = $denomAB * $pAB; |
| 191 | $denomCB = $denomBC * $pBC; |
| 192 | $denomAC = $denomCA * $pCA; |
| 193 | $pAC2 = $pAB * $pBC; |
| 194 | $pAC_minus_fee = $pAC * (1 - $fees[2]); |
| 195 | $pAC2_plus_fee = $pAC2 * (1 + $fees[0]) * (1 + $fees[1]); |
| 196 | |
| 197 | $amounts.A = floor(($pAC_minus_fee - $pAC2_plus_fee)/$pAC_minus_fee / (1/$denomAC + $pAC/$denomCB + $pAC*$pCB/$denomBA)); |
| 198 | if ($amounts.A > $balance) |
| 199 | $amounts.A = $balance; |
| 200 | |
| 201 | $b[0] = 1 - $b[0]; |
| 202 | $b[1] = 1 - $b[1]; |
| 203 | $b[2] = 1 - $b[2]; |
| 204 | $amounts.netA = $amounts.A * $share; |
| 205 | $final_pCA = $pCA * (1 + $amounts.netA/($denomAC + $b[2] * $Lambdas[2] * $amounts.netA)); |
| 206 | $amounts.C = $amounts.netA / $final_pCA * (1 - $fees[2]) - ($pAC - 1/$final_pCA) * $amounts.netA / 2 * $arb_profit_taxes[2]; |
| 207 | $amounts.netC = $amounts.C * $secondary_share; |
| 208 | $final_pBC = $pBC * (1 + $amounts.netC/($denomCB + $b[1] * $Lambdas[1] * $amounts.netC)); |
| 209 | $amounts.B = $amounts.netC / $final_pBC * (1 - $fees[1]) - ($pCB - 1/$final_pBC) * $amounts.netC / 2 * $arb_profit_taxes[1]; |
| 210 | $amounts.netB = $amounts.B * $secondary_share; |
| 211 | $final_pAB = $pAB * (1 + $amounts.netB/($denomBA + $b[0] * $Lambdas[0] * $amounts.netB)); |
| 212 | |
| 213 | |
| 214 | |
| 215 | $from = 'AC'; |
| 216 | $amount = $amounts.A; |
| 217 | $address = $oswap_aas[2]; |
| 218 | $data = { |
| 219 | final_price: $final_pCA, |
| 220 | hops: [ |
| 221 | { |
| 222 | address: $oswap_aas[1], |
| 223 | change_address: $arb_aa, |
| 224 | data: {final_price: $final_pBC}, |
| 225 | }, |
| 226 | { |
| 227 | address: $oswap_aas[0], |
| 228 | change_address: $arb_aa, |
| 229 | data: {final_price: $final_pAB}, |
| 230 | }, |
| 231 | { |
| 232 | address: $arb_aa, |
| 233 | }, |
| 234 | ] |
| 235 | }; |
| 236 | } |
| 237 | else |
| 238 | bounce("no arb opportunity exists"); |
| 239 | { |
| 240 | from: $from, |
| 241 | amount: $amount, |
| 242 | address: $address, |
| 243 | data: $data, |
| 244 | pAB: $pAB, |
| 245 | pBC: $pBC, |
| 246 | pCA: $pCA, |
| 247 | assetA: $assetA, |
| 248 | assetB: $assetB, |
| 249 | assetC: $assetC, |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | $calc_exchange_params = ($arb_aa, $oswap_aa, $asset, $share) => { |
| 255 | $whitelisted_base_aas = $get_whitelisted_base_aas(); |
| 256 | $definition = definition[$oswap_aa][1]; |
| 257 | require($whitelisted_base_aas[$definition.base_aa], "disallowed base AA in "||$oswap_aa); |
| 258 | $oswap_params = $definition.params; |
| 259 | if ($oswap_params.x_asset == $asset){ |
| 260 | $in_label = 'y'; |
| 261 | $assetB = $oswap_params.y_asset; |
| 262 | } |
| 263 | else if ($oswap_params.y_asset == $asset){ |
| 264 | $in_label = 'x'; |
| 265 | $assetB = $oswap_params.x_asset; |
| 266 | } |
| 267 | else |
| 268 | bounce("the pool doesn't trade the reserve asset"); |
| 269 | $get_param = ($name, $default) => { |
| 270 | $value = var[$oswap_aa][$name]; |
| 271 | exists($value) ? $value : (exists($oswap_params[$name]) ? $oswap_params[$name] : $default) |
| 272 | }; |
| 273 | $alpha = $get_param('alpha'); |
| 274 | $beta = 1 - $alpha; |
| 275 | $balances = var[$oswap_aa]['balances']; |
| 276 | $get_shifts = () => { |
| 277 | $mid_price = $get_param('mid_price', 0); |
| 278 | if ($mid_price){ |
| 279 | $gamma = $get_param('price_deviation', 0); |
| 280 | $lp_shares = var[$oswap_aa]['lp_shares']; |
| 281 | $s_curve = $lp_shares.linear * $lp_shares.coef; |
| 282 | $x0 = $s_curve / $mid_price^$beta / $gamma; |
| 283 | $y0 = $x0 * $mid_price; |
| 284 | } |
| 285 | {x0: $x0, y0: $y0} |
| 286 | }; |
| 287 | $sh = $get_shifts(); |
| 288 | |
| 289 | $pBA = ($in_label == 'x') |
| 290 | ? $alpha/$beta * ($balances.y + $sh.y0) / ($balances.x + $sh.x0) |
| 291 | : $beta/$alpha * ($balances.x + $sh.x0) / ($balances.y + $sh.y0); |
| 292 | |
| 293 | $denom = $get_denom($balances, var[$oswap_aa]['leveraged_balances'], $sh, $pBA, $alpha, $in_label); |
| 294 | $balanceB = $get_available_balance($arb_aa, $assetB); |
| 295 | $final_pAB = 1/$pBA * (1 + $balanceB/$denom * $share); |
| 296 | { |
| 297 | data: {final_price: $final_pAB}, |
| 298 | assetB: $assetB, |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | }", |
| 304 | "messages": [ |
| 305 | { |
| 306 | "app": "state", |
| 307 | "state": "{ |
| 308 | $h = $calc_arb_params(); |
| 309 | bounce("library only"); |
| 310 | }" |
| 311 | } |
| 312 | ] |
| 313 | } |
| 314 | ] |