| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "messages": { |
| 5 | "cases": [ |
| 6 | { |
| 7 | "if": "{trigger.data.withdraw AND trigger.data.asset AND trigger.data.amount}", |
| 8 | "init": "{ |
| 9 | $key = 'balance_'||trigger.address||'_'||trigger.data.asset; |
| 10 | $balance = var[$key] + 0; |
| 11 | if (trigger.data.to){ |
| 12 | if (!is_valid_address(trigger.data.to)) |
| 13 | bounce("invalid withdrawal address: " || trigger.data.to); |
| 14 | $address = trigger.data.to; |
| 15 | } |
| 16 | else |
| 17 | $address = trigger.address; |
| 18 | if (trigger.data.amount == 'all') |
| 19 | $amount = $balance; |
| 20 | else if (trigger.data.amount > $balance) |
| 21 | bounce("withdrawal amount too large, balance: " || $balance); |
| 22 | else |
| 23 | $amount = trigger.data.amount; |
| 24 | }", |
| 25 | "messages": [ |
| 26 | { |
| 27 | "app": "payment", |
| 28 | "payload": { |
| 29 | "asset": "{trigger.data.asset}", |
| 30 | "outputs": [ |
| 31 | { |
| 32 | "address": "{$address}", |
| 33 | "amount": "{$amount}" |
| 34 | } |
| 35 | ] |
| 36 | } |
| 37 | }, |
| 38 | { |
| 39 | "app": "state", |
| 40 | "state": "{ |
| 41 | var[$key] -= $amount; |
| 42 | response[$address || '_' || trigger.data.asset] = -$amount; |
| 43 | response['event'] = 'withdrawal'; |
| 44 | }" |
| 45 | } |
| 46 | ] |
| 47 | }, |
| 48 | { |
| 49 | "if": "{ |
| 50 | $order1 = trigger.data.order1.signed_message; |
| 51 | $order2 = trigger.data.order2.signed_message; |
| 52 | if (!$order1.sell_asset OR !$order2.sell_asset) |
| 53 | return false; |
| 54 | if ($order1.sell_asset != $order2.buy_asset OR $order1.buy_asset != $order2.sell_asset) |
| 55 | bounce('assets do not match'); |
| 56 | |
| 57 | if ($order1.matcher != trigger.address) |
| 58 | bounce('wrong matcher in order1'); |
| 59 | if ($order2.matcher != trigger.address) |
| 60 | bounce('wrong matcher in order2'); |
| 61 | |
| 62 | if ($order1.aa != this_address) |
| 63 | bounce('wrong aa in order1'); |
| 64 | if ($order2.aa != this_address) |
| 65 | bounce('wrong aa in order2'); |
| 66 | |
| 67 | if ($order1.expiry_ts AND $order1.expiry_ts <= timestamp) |
| 68 | bounce("order1 expired"); |
| 69 | if ($order2.expiry_ts AND $order2.expiry_ts <= timestamp) |
| 70 | bounce("order2 expired"); |
| 71 | |
| 72 | $sell_key1 = 'balance_' || $order1.address || '_' || $order1.sell_asset; |
| 73 | $sell_key2 = 'balance_' || $order2.address || '_' || $order2.sell_asset; |
| 74 | |
| 75 | $id1 = sha256($order1.address || $order1.sell_asset || $order1.buy_asset || $order1.sell_amount || $order1.price || ($order1.nonce otherwise '') || trigger.data.order1.last_ball_unit); |
| 76 | $id2 = sha256($order2.address || $order2.sell_asset || $order2.buy_asset || $order2.sell_amount || $order2.price || ($order2.nonce otherwise '') || trigger.data.order2.last_ball_unit); |
| 77 | |
| 78 | if (var['executed_' || $id1]) |
| 79 | bounce('order1 already executed'); |
| 80 | if (var['executed_' || $id2]) |
| 81 | bounce('order2 already executed'); |
| 82 | |
| 83 | $amount_left1 = var['amount_left_' || $id1] otherwise $order1.sell_amount; |
| 84 | $amount_left2 = var['amount_left_' || $id2] otherwise $order2.sell_amount; |
| 85 | |
| 86 | |
| 87 | if ($amount_left1 > var[$sell_key1] + 0) |
| 88 | bounce('not sufficient balance in sell asset to complete order1'); |
| 89 | if ($amount_left2 > var[$sell_key2] + 0) |
| 90 | bounce('not sufficient balance in sell asset to complete order2'); |
| 91 | |
| 92 | |
| 93 | $maker_price = $order1.price; |
| 94 | $buy_amount1 = round($amount_left1 * $order1.price); |
| 95 | if ($buy_amount1 > $amount_left2){ |
| 96 | $order_smaller = $order2; |
| 97 | $order_larger = $order1; |
| 98 | $id_smaller = $id2; |
| 99 | $id_larger = $id1; |
| 100 | $amount_left_smaller = $amount_left2; |
| 101 | $amount_left_larger = $amount_left1; |
| 102 | $buy_amount2 = round($amount_left2 / $maker_price); |
| 103 | $buy_amount_smaller = $buy_amount2; |
| 104 | $amount_sold2 = $amount_left2; |
| 105 | $amount_sold1 = $buy_amount2; |
| 106 | } |
| 107 | else{ |
| 108 | $order_smaller = $order1; |
| 109 | $order_larger = $order2; |
| 110 | $id_smaller = $id1; |
| 111 | $id_larger = $id2; |
| 112 | $amount_left_smaller = $amount_left1; |
| 113 | $amount_left_larger = $amount_left2; |
| 114 | $buy_amount_smaller = $buy_amount1; |
| 115 | $amount_sold1 = $amount_left1; |
| 116 | $amount_sold2 = $buy_amount1; |
| 117 | } |
| 118 | |
| 119 | if (round($amount_left_smaller * ($order1.price * $order2.price)) < $amount_left_smaller) |
| 120 | bounce("price mismatch"); |
| 121 | $expected_buy_amount_larger = round($buy_amount_smaller * $order_larger.price); |
| 122 | if ($expected_buy_amount_larger > $amount_left_smaller) |
| 123 | bounce("price mismatch: larger user doesn't like the price, he gets less than expects"); |
| 124 | |
| 125 | |
| 126 | $max_matcher_fee1 = round($order1.matcher_fee * $amount_sold1/$order1.sell_amount); |
| 127 | $max_matcher_fee2 = round($order2.matcher_fee * $amount_sold2/$order2.sell_amount); |
| 128 | $matcher_fee1 = (typeof(trigger.data.matcher_fee1) == 'boolean' AND !trigger.data.matcher_fee1 OR trigger.data.matcher_fee1 == 'default') ? $max_matcher_fee1 : trigger.data.matcher_fee1; |
| 129 | $matcher_fee2 = (typeof(trigger.data.matcher_fee2) == 'boolean' AND !trigger.data.matcher_fee2 OR trigger.data.matcher_fee2 == 'default') ? $max_matcher_fee2 : trigger.data.matcher_fee2; |
| 130 | |
| 131 | if ($matcher_fee1 > $max_matcher_fee1) |
| 132 | bounce('matcher_fee1 is too large'); |
| 133 | if ($matcher_fee2 > $max_matcher_fee2) |
| 134 | bounce('matcher_fee2 is too large'); |
| 135 | |
| 136 | |
| 137 | if ($order1.affiliate){ |
| 138 | if (!$order1.affiliate_fee_asset) |
| 139 | bounce('no affiliate_fee_asset in order1'); |
| 140 | if ($order1.affiliate_fee < 0) |
| 141 | bounce('affiliate_fee < 0 in order1'); |
| 142 | $affiliate_fee1 = round($order1.affiliate_fee * $amount_sold1/$order1.sell_amount); |
| 143 | } |
| 144 | if ($order2.affiliate){ |
| 145 | if (!$order2.affiliate_fee_asset) |
| 146 | bounce('no affiliate_fee_asset in order2'); |
| 147 | if ($order2.affiliate_fee < 0) |
| 148 | bounce('affiliate_fee < 0 in order2'); |
| 149 | $affiliate_fee2 = round($order2.affiliate_fee * $amount_sold2/$order2.sell_amount); |
| 150 | } |
| 151 | |
| 152 | if (!is_valid_signed_package(trigger.data.order1, $order1.address)) |
| 153 | bounce('bad signature of order1'); |
| 154 | if (!is_valid_signed_package(trigger.data.order2, $order2.address)) |
| 155 | bounce('bad signature of order2'); |
| 156 | |
| 157 | true |
| 158 | }", |
| 159 | "messages": [ |
| 160 | { |
| 161 | "app": "state", |
| 162 | "state": "{ |
| 163 | $buy_key1 = 'balance_' || $order1.address || '_' || $order1.buy_asset; |
| 164 | $buy_key2 = 'balance_' || $order2.address || '_' || $order2.buy_asset; |
| 165 | var[$sell_key1] -= $amount_sold1; |
| 166 | var[$sell_key2] -= $amount_sold2; |
| 167 | var[$buy_key1] += $amount_sold2; |
| 168 | var[$buy_key2] += $amount_sold1; |
| 169 | |
| 170 | |
| 171 | $matcher_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.matcher_fee_asset; |
| 172 | $matcher_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.matcher_fee_asset; |
| 173 | $matcher_fee_matcher_key1 = 'balance_' || $order1.matcher || '_' || $order1.matcher_fee_asset; |
| 174 | $matcher_fee_matcher_key2 = 'balance_' || $order2.matcher || '_' || $order2.matcher_fee_asset; |
| 175 | var[$matcher_fee_matcher_key1] += $matcher_fee1; |
| 176 | var[$matcher_fee_matcher_key2] += $matcher_fee2; |
| 177 | var[$matcher_fee_user_key1] -= $matcher_fee1; |
| 178 | var[$matcher_fee_user_key2] -= $matcher_fee2; |
| 179 | if (var[$matcher_fee_user_key1] < 0) |
| 180 | bounce("not enough user1 balance for matcher fees"); |
| 181 | if (var[$matcher_fee_user_key2] < 0) |
| 182 | bounce("not enough user2 balance for matcher fees"); |
| 183 | |
| 184 | if (var[$matcher_fee_matcher_key1] < 0) |
| 185 | bounce("not enough matcher order1.matcher_fee_asset balance for matcher fees"); |
| 186 | if (var[$matcher_fee_matcher_key2] < 0) |
| 187 | bounce("not enough matcher order2.matcher_fee_asset balance for matcher fees"); |
| 188 | |
| 189 | |
| 190 | if ($order1.affiliate AND $affiliate_fee1){ |
| 191 | $affiliate_fee_user_key1 = 'balance_' || $order1.address || '_' || $order1.affiliate_fee_asset; |
| 192 | $affiliate_fee_affiliate_key1 = 'balance_' || $order1.affiliate || '_' || $order1.affiliate_fee_asset; |
| 193 | var[$affiliate_fee_user_key1] -= $affiliate_fee1; |
| 194 | var[$affiliate_fee_affiliate_key1] += $affiliate_fee1; |
| 195 | if (var[$affiliate_fee_user_key1] < 0) |
| 196 | bounce("not enough user1 balance for affiliate fees"); |
| 197 | } |
| 198 | if ($order2.affiliate AND $affiliate_fee2){ |
| 199 | $affiliate_fee_user_key2 = 'balance_' || $order2.address || '_' || $order2.affiliate_fee_asset; |
| 200 | $affiliate_fee_affiliate_key2 = 'balance_' || $order2.affiliate || '_' || $order2.affiliate_fee_asset; |
| 201 | var[$affiliate_fee_user_key2] -= $affiliate_fee2; |
| 202 | var[$affiliate_fee_affiliate_key2] += $affiliate_fee2; |
| 203 | if (var[$affiliate_fee_user_key2] < 0) |
| 204 | bounce("not enough user2 balance for affiliate fees"); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | $aa_fee = 1000; |
| 209 | $base_key1 = 'balance_' || $order1.address || '_base'; |
| 210 | $base_key2 = 'balance_' || $order2.address || '_base'; |
| 211 | var[$base_key1] -= $aa_fee; |
| 212 | var[$base_key2] -= $aa_fee; |
| 213 | if (var[$base_key1] < 0 OR var[$base_key2] < 0) |
| 214 | bounce('not enough balance for AA fees'); |
| 215 | |
| 216 | var['executed_' || $id_smaller] = 1; |
| 217 | var['amount_left_' || $id_smaller] = false; |
| 218 | $new_amount_left_larger = $amount_left_larger - $buy_amount_smaller; |
| 219 | if ($new_amount_left_larger < 0) |
| 220 | bounce("panic: new_amount_left_larger < 0"); |
| 221 | if ($new_amount_left_larger) |
| 222 | var['amount_left_' || $id_larger] = $new_amount_left_larger; |
| 223 | else{ |
| 224 | var['executed_' || $id_larger] = 1; |
| 225 | var['amount_left_' || $id_larger] = false; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | response[$order1.address || '_' || $order1.sell_asset] = -$amount_sold1; |
| 230 | response[$order2.address || '_' || $order2.buy_asset] = $amount_sold1; |
| 231 | response[$order1.address || '_' || $order1.buy_asset] = $amount_sold2; |
| 232 | response[$order2.address || '_' || $order2.sell_asset] = -$amount_sold2; |
| 233 | response['event'] = 'trade'; |
| 234 | }" |
| 235 | } |
| 236 | ] |
| 237 | }, |
| 238 | { |
| 239 | "if": "{!trigger.data OR trigger.data.to}", |
| 240 | "messages": [ |
| 241 | { |
| 242 | "app": "state", |
| 243 | "state": "{ |
| 244 | $asset = trigger.output[[asset!=base]].asset; |
| 245 | if ($asset == 'ambiguous') |
| 246 | bounce('ambiguous asset'); |
| 247 | if (trigger.data.to){ |
| 248 | if (!is_valid_address(trigger.data.to)) |
| 249 | bounce("invalid deposit address: " || trigger.data.to); |
| 250 | $address = trigger.data.to; |
| 251 | } |
| 252 | else |
| 253 | $address = trigger.address; |
| 254 | $base_key = 'balance_'||$address||'_'||'base'; |
| 255 | var[$base_key] = var[$base_key] + trigger.output[[asset=base]]; |
| 256 | $response_base = trigger.output[[asset=base]] || ' bytes'; |
| 257 | response[$address || '_base'] = trigger.output[[asset=base]]; |
| 258 | if ($asset != 'none'){ |
| 259 | $asset_key = 'balance_'||$address||'_'||$asset; |
| 260 | var[$asset_key] = var[$asset_key] + trigger.output[[asset=$asset]]; |
| 261 | $response_asset = ' and ' || trigger.output[[asset=$asset]] || ' of ' || $asset; |
| 262 | response[$address || '_' || $asset] = trigger.output[[asset=$asset]]; |
| 263 | } |
| 264 | response['message'] = 'accepted coins: ' || $response_base || ($response_asset otherwise ''); |
| 265 | response['event'] = 'deposit'; |
| 266 | }" |
| 267 | } |
| 268 | ] |
| 269 | } |
| 270 | ] |
| 271 | } |
| 272 | } |
| 273 | ] |