| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | if(trigger.output[[asset!=base]].asset != 'none') |
| 6 | $asset = trigger.output[[asset!=base]].asset; |
| 7 | if($asset){ |
| 8 | if($asset == 'ambiguous') |
| 9 | bounce("only 1 asset per tx"); |
| 10 | if(trigger.output[[asset!=base]] == 0) |
| 11 | bounce("zero asset tx"); |
| 12 | $asset_inp = trigger.output[[asset!=base]]; |
| 13 | } |
| 14 | else{ |
| 15 | bounce('asset not found'); |
| 16 | } |
| 17 | if(trigger.output[[asset=base]] < 7000) bounce('not enough funds for fees'); |
| 18 | }", |
| 19 | "messages": { |
| 20 | "cases": [ |
| 21 | { |
| 22 | "messages": [ |
| 23 | { |
| 24 | "app": "payment", |
| 25 | "payload": { |
| 26 | "asset": "{$asset}", |
| 27 | "outputs": [ |
| 28 | { |
| 29 | "address": "{trigger.address}", |
| 30 | "amount": "{$asset_inp}" |
| 31 | } |
| 32 | ] |
| 33 | } |
| 34 | }, |
| 35 | { |
| 36 | "app": "payment", |
| 37 | "payload": { |
| 38 | "asset": "base", |
| 39 | "outputs": [ |
| 40 | { |
| 41 | "address": "{trigger.address}" |
| 42 | } |
| 43 | ] |
| 44 | } |
| 45 | }, |
| 46 | { |
| 47 | "app": "definition", |
| 48 | "payload": { |
| 49 | "definition": [ |
| 50 | "autonomous agent", |
| 51 | { |
| 52 | "{"init"}": "{ |
| 53 | "{$asset_ct = '"||$asset||"';" |
| 54 | || "if(!var['_ Last price'] and !trigger.data.price) bounce('Price must be defined before first exchange');" |
| 55 | |
| 56 | || "if(trigger.data.price){" |
| 57 | |
| 58 | || "$ratio = round(trigger.data.price);" |
| 59 | || "if($ratio <= 0) bounce('Price not allowed');" |
| 60 | || "}" |
| 61 | || "else $ratio = var['_ Last price'];" |
| 62 | |
| 63 | |
| 64 | || "if(trigger.output[[asset!=base]].asset != 'none')" |
| 65 | || "$asset = trigger.output[[asset!=base]].asset;" |
| 66 | || "if($asset){" |
| 67 | || "if($asset == 'ambiguous') bounce('Only 1 asset per tx');" |
| 68 | || "if($asset != $asset_ct) bounce('Asset not allowed, please send ' || $asset_ct);" |
| 69 | || "$asset_inp = (trigger.output[[asset!=base]] != 0) ? trigger.output[[asset!=base]] : bounce('Zero asset tx');" |
| 70 | || "}" |
| 71 | |
| 72 | || "$addr = trigger.address;" |
| 73 | || "$base_inp = trigger.output[[asset=base]] - 700;" |
| 74 | || "if($base_inp <= 0) bounce('Not enough bytes for fees');" |
| 75 | |
| 76 | |
| 77 | || "$base_key = '_' || $addr || $ratio || 'b';" |
| 78 | || "$asset_key = '_' || $addr || $ratio;" |
| 79 | |
| 80 | |
| 81 | || "$total_key = 'b' || $ratio;" |
| 82 | || "$total_asset_key = 'a' || $ratio;" |
| 83 | |
| 84 | |
| 85 | || "$exch_key = 'b_' || $ratio ;" |
| 86 | || "$exch_asset_key = 'a_' || $ratio ;" |
| 87 | |
| 88 | |
| 89 | || "$max_b_to_exchange = var[$total_key] - var[$exch_key];" |
| 90 | || "$max_a_to_exchange = var[$total_asset_key] - var[$exch_asset_key];" |
| 91 | |
| 92 | |
| 93 | || "$base_inp_round = ($base_inp - ($base_inp % $ratio));}" |
| 94 | |
| 95 | }", |
| 96 | "messages": { |
| 97 | "{"cases"}": [ |
| 98 | { |
| 99 | "{"if"}": "{ |
| 100 | |
| 101 | "{$withdraw_asset = var[$exch_asset_key] and var[$base_key] and var[$exch_asset_key] > 0 and var[$base_key] > 0;" |
| 102 | || "$withdraw_base = var[$exch_key] and var[$asset_key] and var[$exch_key] > 0 and var[$asset_key] > 0;" |
| 103 | |
| 104 | || "$withdraw_asset or $withdraw_base}" |
| 105 | }", |
| 106 | "{"init"}": "{ |
| 107 | "{if($asset) bounce('You must withdraw, send byte only transaction please');" |
| 108 | || "$withdraw_key = $withdraw_asset ? $exch_asset_key : $exch_key;" |
| 109 | || "$w_total_key = $withdraw_asset ? $total_asset_key : $total_key;" |
| 110 | || "$max_to_withdraw = $withdraw_asset ? (var[$base_key] / $ratio) : (var[$asset_key] * $ratio);" |
| 111 | || "$key = $withdraw_asset ? $base_key : $asset_key;" |
| 112 | |
| 113 | || "$withdraw_amount = (var[$withdraw_key] <= $max_to_withdraw) ? var[$withdraw_key] : $max_to_withdraw;}" |
| 114 | }", |
| 115 | "messages": [ |
| 116 | { |
| 117 | "app": "payment", |
| 118 | "payload": { |
| 119 | "asset": "base", |
| 120 | "outputs": [ |
| 121 | { |
| 122 | "address": "{"{$addr}"}", |
| 123 | "amount": "{ "{$withdraw_asset ? $base_inp : ($withdraw_amount + $base_inp)}" }" |
| 124 | } |
| 125 | ] |
| 126 | } |
| 127 | }, |
| 128 | { |
| 129 | "{"if"}": "{"{$withdraw_asset}"}", |
| 130 | "app": "payment", |
| 131 | "payload": { |
| 132 | "asset": "{"{$asset_ct}"}", |
| 133 | "outputs": [ |
| 134 | { |
| 135 | "address": "{"{$addr}"}", |
| 136 | "amount": "{"{$withdraw_amount}"}" |
| 137 | } |
| 138 | ] |
| 139 | } |
| 140 | }, |
| 141 | { |
| 142 | "app": "state", |
| 143 | "{"state"}": "{ |
| 144 | |
| 145 | "{var[$w_total_key] -= $withdraw_amount;" |
| 146 | || "if(var[$w_total_key] == 0) var[$w_total_key] = false;" |
| 147 | |
| 148 | |
| 149 | || "var[$withdraw_key] -= $withdraw_amount;" |
| 150 | || "if(var[$withdraw_key] == 0) var[$withdraw_key] = false;" |
| 151 | |
| 152 | |
| 153 | || "var[$key] -= $withdraw_asset ? ($withdraw_amount * $ratio) : ($withdraw_amount / $ratio);" |
| 154 | || "if(var[$key] == 0) var[$key] = false;" |
| 155 | |
| 156 | || "response['message'] = 'withdrawn exchanged amount ('||($withdraw_asset ? 'assets': 'bytes')||') at '||$ratio||' bytes per token';}" |
| 157 | }" |
| 158 | } |
| 159 | ] |
| 160 | }, |
| 161 | { |
| 162 | "{"if"}": "{ |
| 163 | |
| 164 | "{$exchange_asset = $asset and $max_b_to_exchange > 0;" |
| 165 | || "$exchange_base = !$asset and $max_a_to_exchange > 0;" |
| 166 | |
| 167 | || "!trigger.data.cancel and ($exchange_asset or $exchange_base)}" |
| 168 | }", |
| 169 | "{"init"}": "{ |
| 170 | "{$max_to_ex = $asset ? $max_b_to_exchange : $max_a_to_exchange;" |
| 171 | || "$inp_in_other = $asset ? $asset_inp*$ratio : $base_inp_round/$ratio;" |
| 172 | || "if($inp_in_other > $max_to_ex){" |
| 173 | |
| 174 | || "$byte_amount = $asset ? $max_to_ex : (($inp_in_other - $max_to_ex) * $ratio);" |
| 175 | || "$asset_amount = $asset ? ((($inp_in_other - $max_to_ex) - (($inp_in_other - $max_to_ex) % $ratio)) / $ratio) : $max_to_ex;" |
| 176 | || "}" |
| 177 | || "else{" |
| 178 | |
| 179 | || "if($inp_in_other == 0) bounce('Not enough funds for minimum');" |
| 180 | || "$byte_amount = $asset ? $inp_in_other : 0;" |
| 181 | || "$asset_amount = $asset ? 0 : $inp_in_other;}" |
| 182 | || "}" |
| 183 | }", |
| 184 | "messages": [ |
| 185 | { |
| 186 | "{"if"}": "{"{$exchange_asset or $byte_amount > 0}"}", |
| 187 | "app": "payment", |
| 188 | "payload": { |
| 189 | "asset": "base", |
| 190 | "outputs": [ |
| 191 | { |
| 192 | "address": "{"{$addr}"}", |
| 193 | "amount": "{"{$exchange_asset ? ($byte_amount + $base_inp) : $byte_amount}"}" |
| 194 | } |
| 195 | ] |
| 196 | } |
| 197 | }, |
| 198 | { |
| 199 | "{"if"}": "{"{$exchange_base or $asset_amount > 0}"}", |
| 200 | "app": "payment", |
| 201 | "payload": { |
| 202 | "asset": "{"{$asset_ct}"}", |
| 203 | "outputs": [ |
| 204 | { |
| 205 | "address": "{"{$addr}"}", |
| 206 | "amount": "{"{$asset_amount}"}" |
| 207 | } |
| 208 | ] |
| 209 | } |
| 210 | }, |
| 211 | { |
| 212 | "app": "state", |
| 213 | "{"state"}": "{ |
| 214 | "{var['_ Last price'] = $ratio;" |
| 215 | || "if($exchange_asset){" |
| 216 | |
| 217 | || "var[$total_asset_key] += $asset_inp - $asset_amount;" |
| 218 | || "var[$exch_asset_key] += $asset_inp - $asset_amount;" |
| 219 | |
| 220 | |
| 221 | || "var[$total_key] -= $byte_amount;" |
| 222 | || "if(var[$total_key] == 0) var[$total_key] = false;" |
| 223 | || "response['message'] = 'sold '||($asset_inp - $asset_amount)||' assets for '||($byte_amount)||' bytes';" |
| 224 | || "}" |
| 225 | || "else{" |
| 226 | |
| 227 | || "var[$total_key] += $base_inp_round - $byte_amount;" |
| 228 | || "var[$exch_key] += $base_inp_round - $byte_amount;" |
| 229 | |
| 230 | |
| 231 | || "var[$total_asset_key] -= $asset_amount;" |
| 232 | || "if(var[$total_asset_key] == 0) var[$total_asset_key] = false;" |
| 233 | || "response['message'] = 'bought '||($asset_amount)||' assets for '||($base_inp_round - $byte_amount)||' bytes';}" |
| 234 | || "}" |
| 235 | }" |
| 236 | } |
| 237 | ] |
| 238 | }, |
| 239 | { |
| 240 | "{"if"}": "{ |
| 241 | |
| 242 | "{$cancel_asset = var[$asset_key] and var[$asset_key] > 0;" |
| 243 | || "$cancel_base = var[$base_key] and var[$base_key] > 0;" |
| 244 | |
| 245 | || "trigger.data.cancel and ($cancel_asset or $cancel_base)}" |
| 246 | }", |
| 247 | "{"init"}": "{ |
| 248 | "{if($asset) bounce('Send byte only transaction please');" |
| 249 | || "$max_to_cancel = $cancel_asset ? $max_a_to_exchange : $max_b_to_exchange;" |
| 250 | || "$key = $cancel_asset ? $asset_key : $base_key;" |
| 251 | || "$cancel_tot_key = $cancel_asset ? $total_asset_key : $total_key;" |
| 252 | |
| 253 | || "if($max_to_cancel <= 0) bounce('Not allowed');" |
| 254 | |
| 255 | || "$cancel_amount = ($max_to_cancel <= var[$key]) ? $max_to_cancel : var[$key];}" |
| 256 | }", |
| 257 | "messages": [ |
| 258 | { |
| 259 | "app": "payment", |
| 260 | "payload": { |
| 261 | "asset": "base", |
| 262 | "outputs": [ |
| 263 | { |
| 264 | "address": "{"{$addr}"}", |
| 265 | "amount": "{"{$cancel_asset ? $base_inp : ($cancel_amount + $base_inp)}"}" |
| 266 | } |
| 267 | ] |
| 268 | } |
| 269 | }, |
| 270 | { |
| 271 | "{"if"}": "{"{$cancel_asset}"}", |
| 272 | "app": "payment", |
| 273 | "payload": { |
| 274 | "asset": "{"{$asset_ct}"}", |
| 275 | "outputs": [ |
| 276 | { |
| 277 | "address": "{"{$addr}"}", |
| 278 | "amount": "{"{$cancel_amount}"}" |
| 279 | } |
| 280 | ] |
| 281 | } |
| 282 | }, |
| 283 | { |
| 284 | "app": "state", |
| 285 | "{"state"}": "{ |
| 286 | "{var[$cancel_tot_key] -= $cancel_amount;" |
| 287 | || "if(var[$cancel_tot_key] == 0)var[$cancel_tot_key] = false;" |
| 288 | || "var[$key] -= $cancel_amount;" |
| 289 | || "if(var[$key] == 0)var[$key] = false;" |
| 290 | || "response['message'] = 'canceled order ('||($cancel_asset?'sell':'buy')||' at '||$ratio||' bytes per token)';}" |
| 291 | }" |
| 292 | } |
| 293 | ] |
| 294 | }, |
| 295 | { |
| 296 | "messages": [ |
| 297 | { |
| 298 | "{"if"}": "{"{$asset}"}", |
| 299 | "app": "payment", |
| 300 | "payload": { |
| 301 | "asset": "base", |
| 302 | "outputs": [ |
| 303 | { |
| 304 | "address": "{"{$addr}"}", |
| 305 | "amount": "{"{$base_inp}"}" |
| 306 | } |
| 307 | ] |
| 308 | } |
| 309 | }, |
| 310 | { |
| 311 | "app": "state", |
| 312 | "{"state"}": "{ |
| 313 | "{if($asset){" |
| 314 | || "var[$total_asset_key] += $asset_inp;" |
| 315 | || "var[$asset_key] += $asset_inp;" |
| 316 | || "response['message'] = 'deposited '||$asset_inp||' assets to sell at '||$ratio||' bytes per token';" |
| 317 | || "}" |
| 318 | || "else{" |
| 319 | || "var[$total_key] += $base_inp_round;" |
| 320 | || "var[$base_key] += $base_inp_round;" |
| 321 | || "response['message'] = 'deposited '||$base_inp_round||' bytes to buy at '||$ratio||' bytes per token';" |
| 322 | || "}}" |
| 323 | }" |
| 324 | } |
| 325 | ] |
| 326 | } |
| 327 | ] |
| 328 | } |
| 329 | } |
| 330 | ] |
| 331 | } |
| 332 | } |
| 333 | ] |
| 334 | } |
| 335 | ] |
| 336 | } |
| 337 | } |
| 338 | ] |