| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | |
| 6 | $aa_name = "JAAAWS"; |
| 7 | $owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 8 | $holaa = "5BRALFFKNML53X4MJVTWBSBYSDSNRIHQ"; |
| 9 | $parties = $holaa; |
| 10 | |
| 11 | $pay_id_received = trigger.data.pay_id otherwise trigger.data.pi otherwise ""; |
| 12 | |
| 13 | |
| 14 | $validate_pay = $pay_id_received != ""; |
| 15 | $cancel = ($pay_id_received != "") AND (trigger.data.cancel==true); |
| 16 | }", |
| 17 | "messages": { |
| 18 | "cases": [ |
| 19 | { |
| 20 | "if": "{trigger.data.create}", |
| 21 | "init": "{ |
| 22 | }", |
| 23 | "messages": [ |
| 24 | { |
| 25 | "app": "data", |
| 26 | "payload": { |
| 27 | "create": "{trigger.data.create}" |
| 28 | } |
| 29 | }, |
| 30 | { |
| 31 | "app": "payment", |
| 32 | "payload": { |
| 33 | "asset": "base", |
| 34 | "outputs": [ |
| 35 | { |
| 36 | "address": "{$holaa}", |
| 37 | "amount": 1 |
| 38 | } |
| 39 | ] |
| 40 | } |
| 41 | }, |
| 42 | { |
| 43 | "app": "state", |
| 44 | "state": "{ |
| 45 | var[trigger.data.create] = trigger.output[[asset=base]]; |
| 46 | var[trigger.data.create||"_in_creation"] = trigger.address; |
| 47 | response['message'] = trigger.data.create||" create, please add parties."; |
| 48 | }" |
| 49 | } |
| 50 | ] |
| 51 | }, |
| 52 | { |
| 53 | "if": "{trigger.data.add_party}", |
| 54 | "init": "{ |
| 55 | if (!trigger.data.account) bounce ("Need 'account'!"); |
| 56 | if (!(var[$parties][trigger.data.account])) bounce (trigger.data.account||" do not exist"); |
| 57 | if (!(var[trigger.data.account||"_in_creation"])) bounce ("The account is final"); |
| 58 | if (var[trigger.data.account||"_in_creation"] != trigger.address) bounce ("You are not the account creator!"); |
| 59 | }", |
| 60 | "messages": [ |
| 61 | { |
| 62 | "app": "data", |
| 63 | "payload": { |
| 64 | "list_name": "{trigger.data.account}", |
| 65 | "add": "{trigger.data.add_party}" |
| 66 | } |
| 67 | }, |
| 68 | { |
| 69 | "app": "payment", |
| 70 | "payload": { |
| 71 | "asset": "base", |
| 72 | "outputs": [ |
| 73 | { |
| 74 | "address": "{$parties}", |
| 75 | "amount": 1 |
| 76 | } |
| 77 | ] |
| 78 | } |
| 79 | }, |
| 80 | { |
| 81 | "app": "state", |
| 82 | "state": "{ |
| 83 | response['message'] = trigger.data.add_party||" added ^^"; |
| 84 | }" |
| 85 | } |
| 86 | ] |
| 87 | }, |
| 88 | { |
| 89 | "if": "{trigger.data.finalize}", |
| 90 | "init": "{ |
| 91 | $account= trigger.data.finalize; |
| 92 | if (!(var[$parties][$account])) bounce ($account||" do not exist"); |
| 93 | if (!(var[$account||"_in_creation"])) bounce ("The account is already final"); |
| 94 | if (var[$account||"_in_creation"] != trigger.address) bounce ("You are not the account creator!"); |
| 95 | if (var[$holaa][$account||"_count"] < 2) bounce ("the minimum is 2 parties, use 'add_party'"); |
| 96 | }", |
| 97 | "messages": [ |
| 98 | { |
| 99 | "app": "state", |
| 100 | "state": "{ |
| 101 | var[trigger.data.account||"_in_creation"] = false; |
| 102 | response['message'] = trigger.data.account||" finalized ^^"; |
| 103 | }" |
| 104 | } |
| 105 | ] |
| 106 | }, |
| 107 | { |
| 108 | "if": "{ trigger.data.pay_to }", |
| 109 | "init": "{ |
| 110 | $account = trigger.data.account; |
| 111 | if (!($account)) bounce ("Need 'account'!"); |
| 112 | if (!(trigger.data.amount)) bounce ("Need 'amount'!"); |
| 113 | if (!(var[$parties][$account])) bounce ($account||" do not exist"); |
| 114 | if (var[$account||"_in_creation"] != trigger.address) bounce ("Account not finalized!"); |
| 115 | |
| 116 | $pay_id_created = sha256($account||'>'||trigger.data.pay_to||':'||trigger.data.amount); |
| 117 | |
| 118 | |
| 119 | if (trigger.address == $party1) |
| 120 | { |
| 121 | $need_party1_validation = false; |
| 122 | $initiator = 1; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | if (trigger.address == $party2) |
| 127 | { |
| 128 | $need_party2_validation = false; |
| 129 | $initiator = 2; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | if (trigger.address == $party3) |
| 134 | { |
| 135 | $need_party3_validation = false; |
| 136 | $initiator = 3; |
| 137 | } |
| 138 | else |
| 139 | bounce ("You are not a party of "||$account); |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | if ($initiator==1) $next_validator_num = 2; else $next_validator_num = 1; |
| 144 | |
| 145 | }", |
| 146 | "messages": [ |
| 147 | { |
| 148 | "app": "data", |
| 149 | "payload": { |
| 150 | "message": "{"JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||trigger.data.amount||" to "||trigger.data.pay_to||", please send me back the pay_id linked to validate the transaction. And add a cancel = true field to refuse it."}", |
| 151 | "pay_id": "{$pay_id_created}" |
| 152 | } |
| 153 | }, |
| 154 | { |
| 155 | "app": "payment", |
| 156 | "payload": { |
| 157 | "asset": "base", |
| 158 | "outputs": [ |
| 159 | { |
| 160 | "address": "{var[$account||"_"||$next_validator_num]}", |
| 161 | "amount": 1000 |
| 162 | } |
| 163 | ] |
| 164 | } |
| 165 | }, |
| 166 | { |
| 167 | "app": "state", |
| 168 | "state": "{ |
| 169 | var[$pay_id_created||"_account"] = $account; |
| 170 | var[$pay_id_created||"_amount"] = $pay_amount; |
| 171 | var[$pay_id_created||"_address"] = $pay_address; |
| 172 | var[$pay_id_created||"_initiator"] = trigger.address; |
| 173 | |
| 174 | if ( $need_party1_validation) var[$pay_id_created||"_required_v1"] = true; |
| 175 | if ( $need_party2_validation) var[$pay_id_created||"_required_v2"] = true; |
| 176 | if ( $need_party3_validation) var[$pay_id_created||"_required_v3"] = true; |
| 177 | |
| 178 | response['message'] = "Payment "||$pay_id_created||" created and approvals requested"; |
| 179 | }" |
| 180 | } |
| 181 | ] |
| 182 | }, |
| 183 | { |
| 184 | "if": "{$validate_pay}", |
| 185 | "init": "{ |
| 186 | $account_received = var[$pay_id_received||"_account"] otherwise ""; |
| 187 | $pay_amount_to_valid = var[$pay_id_received||"_amount"] otherwise 0; |
| 188 | $pay_address_to_valid = var[$pay_id_received||"_address"] otherwise ""; |
| 189 | |
| 190 | if ($account_received == "") bounce ("Payment ID "||$pay_id_received||" do not exist!"); |
| 191 | |
| 192 | |
| 193 | if ($trigger == var[$account_received||"_1"]) $validator_num = 1; |
| 194 | if ($trigger == var[$account_received||"_2"]) $validator_num = 2; |
| 195 | if ($trigger == var[$account_received||"_3"]) $validator_num = 3; |
| 196 | |
| 197 | $is_this_validation_needed = var[$pay_id_created||"_required_v"||$validator_num] otherwise false; |
| 198 | if ($is_this_validation_needed ==false) |
| 199 | bounce ("Your validation by validator "||$validator_num||"is not required!"); |
| 200 | |
| 201 | $is_next_needed = var[$pay_id_created||"_required_v"||($validator_num+1)] otherwise false; |
| 202 | if ($is_next_needed == true) |
| 203 | { |
| 204 | $next_address = var[$account_received||"_"||($validator_num+1)]; |
| 205 | $next_amount = 1000; |
| 206 | $next_message = "JAAAWS: Your approval is required for a payment from the account '"||$account_received||" to pay "||$pay_amount_to_valid||" to "||$pay_address_to_valid||", please send me back the pay_id linked to validate the transaction. And add a cancel = true field to refuse it."; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | $next_address = $pay_address_to_valid; |
| 211 | $next_amount = $pay_amount_to_valid; |
| 212 | $next_message = "Payment of "||$next_amount||" done to "||$next_address||" from account "||$account_received; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | }", |
| 217 | "messages": [ |
| 218 | { |
| 219 | "app": "data", |
| 220 | "payload": { |
| 221 | "message": "{$next_message}", |
| 222 | "pay_id": "{$pay_id_received}" |
| 223 | } |
| 224 | }, |
| 225 | { |
| 226 | "app": "payment", |
| 227 | "payload": { |
| 228 | "asset": "base", |
| 229 | "outputs": [ |
| 230 | { |
| 231 | "address": "{$next_address}", |
| 232 | "amount": "{$next_amount}" |
| 233 | } |
| 234 | ] |
| 235 | } |
| 236 | }, |
| 237 | { |
| 238 | "app": "state", |
| 239 | "state": "{ |
| 240 | var[$pay_id_created||"_required_v"||$validator_num] = false; |
| 241 | if ($is_next_needed != true) |
| 242 | { |
| 243 | var[$account_received] = false; |
| 244 | var[$pay_address_to_valid] = false; |
| 245 | var[$pay_amount_to_valid] = false; |
| 246 | } |
| 247 | }" |
| 248 | } |
| 249 | ] |
| 250 | }, |
| 251 | { |
| 252 | "if": "{$cancel}", |
| 253 | "init": "{ |
| 254 | $account_received = var[$pay_id_received||"_account"] otherwise ""; |
| 255 | $amount = var[$pay_id_received||"_amount"] otherwise 0; |
| 256 | $address = var[$pay_id_received||"_address"] otherwise ""; |
| 257 | |
| 258 | if (trigger.address == var[$account_received||"_1"]) $cancel_valid = true; |
| 259 | if (trigger.address == var[$account_received||"_2"]) $cancel_valid = true; |
| 260 | if (trigger.address == var[$account_received||"_2"]) $cancel_valid = true; |
| 261 | |
| 262 | if (trigger.address == $address) $cancel_valid = true; |
| 263 | if ($cancel_valid==false) bounce ("You must be a party to cancel!"); |
| 264 | }", |
| 265 | "messages": [ |
| 266 | { |
| 267 | "app": "state", |
| 268 | "state": "{ |
| 269 | var[$account_received] = false; |
| 270 | var[$address] = false; |
| 271 | var[$amount] = false; |
| 272 | var[$pay_id_created||"_required_v1"] = false; |
| 273 | var[$pay_id_created||"_required_v2"] = false; |
| 274 | var[$pay_id_created||"_required_v3"] = false; |
| 275 | response['message'] = "Payment of "||$amount||" to "||$address||" is cancelled by "||trigger.address; |
| 276 | }" |
| 277 | } |
| 278 | ] |
| 279 | }, |
| 280 | { |
| 281 | "messages": [ |
| 282 | { |
| 283 | "init": "{ |
| 284 | if ($account=="") bounce ("need account name"); |
| 285 | $input = trigger.output[[asset=base]]; |
| 286 | }", |
| 287 | "app": "state", |
| 288 | "state": "{ |
| 289 | var[$account] += $input; |
| 290 | response['message'] = $input||" added to the account "||$account; |
| 291 | }" |
| 292 | } |
| 293 | ] |
| 294 | } |
| 295 | ] |
| 296 | } |
| 297 | } |
| 298 | ] |