| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | |
| 6 | $aa_name = "JAAAWS"; |
| 7 | $owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 8 | $holaa = "FVNMVTIJYAOQJ4LKV65VT23VXIL6ZHF6"; |
| 9 | $list = $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": "{$list}", |
| 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[$list][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": "{$list}", |
| 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[$list][$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[$account||"_in_creation"] = false; |
| 102 | response['message'] = $account||" finalized ^^"; |
| 103 | }" |
| 104 | } |
| 105 | ] |
| 106 | }, |
| 107 | { |
| 108 | "if": "{ trigger.data.pay_to }", |
| 109 | "init": "{ |
| 110 | $account = trigger.data.account otherwise bounce("Need 'account'!"); |
| 111 | $amount = trigger.data.amount otherwise bounce ("Need 'amount'!"); |
| 112 | $account_balance = var[$list][$account] otherwise bounce ($account||" do not exist"); |
| 113 | $party_number = var[$list][$account||"_"||trigger.address] otherwise |
| 114 | bounce ("You are not party of "||$account); |
| 115 | if (var[$account||"_in_creation"]) |
| 116 | if (var[$account||"_in_creation"] != trigger.address) |
| 117 | bounce ("Account not finalized!"); |
| 118 | |
| 119 | $pay_id = "payment_id_"||sha256($account||'>'||trigger.data.pay_to||':'||trigger.data.amount); |
| 120 | |
| 121 | |
| 122 | $next_validator_number = ($party_number == 1)? 2: 1; |
| 123 | $next_validator = var[$list][$account||"_"||$next_validator_number]; |
| 124 | }", |
| 125 | "messages": [ |
| 126 | { |
| 127 | "app": "data", |
| 128 | "payload": { |
| 129 | "message": "{"JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||trigger.data.amount||" bytes to "||trigger.data.pay_to||", please validate or cancel it by send me back 'validate = payment_id_xx...xx' or 'cancel = payment_id_xx...xx'.."}", |
| 130 | "pay_id": "{$pay_id}" |
| 131 | } |
| 132 | }, |
| 133 | { |
| 134 | "app": "payment", |
| 135 | "payload": { |
| 136 | "asset": "base", |
| 137 | "outputs": [ |
| 138 | { |
| 139 | "address": "{$next_validator}", |
| 140 | "amount": 33 |
| 141 | } |
| 142 | ] |
| 143 | } |
| 144 | }, |
| 145 | { |
| 146 | "app": "state", |
| 147 | "state": "{ |
| 148 | var[$pay_id||"_account"] = $account; |
| 149 | var[$pay_id||"_amount"] = $pay_amount; |
| 150 | var[$pay_id||"_address"] = $pay_address; |
| 151 | var[$pay_id||"_initiator"] = trigger.address; |
| 152 | var[$pay_id||"_waiting_for"] = $next_validator; |
| 153 | |
| 154 | response['message'] = "Payment "||$pay_id||" created ^^"; |
| 155 | }" |
| 156 | } |
| 157 | ] |
| 158 | }, |
| 159 | { |
| 160 | "if": "{ trigger.data.validate }", |
| 161 | "init": "{ |
| 162 | |
| 163 | $pay_id = trigger.data.validate; |
| 164 | $account = var[$pay_id||"_account"] otherwise bounce ("Payment ID "||$pay_id||" do not exist!"); |
| 165 | $amount = var[$pay_id||"_amount"] otherwise bounce ("No amount available !"); |
| 166 | $address = var[$pay_id||"_address"] otherwise bounce ("No address to pay !"); |
| 167 | $initiator = var[$pay_id||"_initiator"] otherwise bounce ("No initiator !"); |
| 168 | $validator_waited_for = var[$pay_id||"_waiting_for"] otherwise bounce ("Not waiting for validation"); |
| 169 | $party_number = var[$list][$account||"_"||trigger.address] otherwise bounce ("You are not party of "||$account); |
| 170 | if ($validator_waited_for!=trigger.address) bounce ("Waiting first for validation by "||$validator_waited_for); |
| 171 | |
| 172 | |
| 173 | $last_validator_num = var[$list][$account||"_count"]; |
| 174 | $last_validator = var[$list][$account||"_"||$last_validator_num] otherwise bounce ("error"); |
| 175 | if (trigger.address == $last_validator) |
| 176 | $payment_complete_validated = true; |
| 177 | else |
| 178 | { |
| 179 | $last_validator_num = (var[$list][$account||"_"||$validator_waited_for]); |
| 180 | $jump = (var[$list][$account||"_"||($last_validator_num+1)] == $initiator)? 2:1; |
| 181 | $next_validator = var[$list][$account||"_"||($last_validator_num+$jump)]; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | if ($payment_complete_validated) |
| 186 | { |
| 187 | $next_address = $address; |
| 188 | $next_amount = $amount; |
| 189 | $next_message = "Payment of "||$next_amount||" done to "||$next_address||" from account "||$account; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | $next_address = $next_validator; |
| 194 | $next_amount = 33; |
| 195 | $next_message = "JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||$amount||" to "||$address||", please validate or cancel it by send me back 'validate = payment_id_xx...xx' or 'cancel = payment_id_xx...xx'."; |
| 196 | } |
| 197 | }", |
| 198 | "messages": [ |
| 199 | { |
| 200 | "app": "data", |
| 201 | "payload": { |
| 202 | "message": "{$next_message}", |
| 203 | "pay_id": "{$pay_id_received}" |
| 204 | } |
| 205 | }, |
| 206 | { |
| 207 | "app": "payment", |
| 208 | "payload": { |
| 209 | "asset": "base", |
| 210 | "outputs": [ |
| 211 | { |
| 212 | "address": "{$next_address}", |
| 213 | "amount": "{$next_amount}" |
| 214 | } |
| 215 | ] |
| 216 | } |
| 217 | }, |
| 218 | { |
| 219 | "app": "state", |
| 220 | "state": "{ |
| 221 | if ($payment_complete_validated) |
| 222 | { |
| 223 | var[$pay_id||"_account"] = false; |
| 224 | var[$pay_id||"_amount"] = false; |
| 225 | var[$pay_id||"_address"] = false; |
| 226 | var[$pay_id||"_initiator"] = false; |
| 227 | var[$pay_id||"_waiting_for"] = false; |
| 228 | response['message'] = "Payment of "||$amount||" to "||$address||" is finally sent to "||$next_address; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | var[$pay_id||"_waiting_for"] = $next_validator; |
| 233 | response['message'] = "Next validation request has been sent to "||$next_validator; |
| 234 | } |
| 235 | }" |
| 236 | } |
| 237 | ] |
| 238 | }, |
| 239 | { |
| 240 | "if": "{ trigger.data.cancel}", |
| 241 | "init": "{ |
| 242 | |
| 243 | $pay_id = trigger.data.cancel; |
| 244 | if (!(var[$pay_id||"_account"])) |
| 245 | bounce ("Payment ID "||$pay_id||" do not exist!"); |
| 246 | |
| 247 | $account = var[$pay_id||"_account"]; |
| 248 | if (!(var[$list][$account||"_"||trigger.address])) |
| 249 | bounce ("You are not party of "||$account); |
| 250 | |
| 251 | }", |
| 252 | "messages": [ |
| 253 | { |
| 254 | "app": "state", |
| 255 | "state": "{ |
| 256 | var[$pay_id||"_account"] = false; |
| 257 | var[$pay_id||"_amount"] = false; |
| 258 | var[$pay_id||"_address"] = false; |
| 259 | var[$pay_id||"_initiator"] = false; |
| 260 | var[$pay_id||"_waiting_for"] = false; |
| 261 | response['message'] = "Payment of "||$amount||" to "||$address||" is cancelled by "||trigger.address; |
| 262 | }" |
| 263 | } |
| 264 | ] |
| 265 | }, |
| 266 | { |
| 267 | "messages": [ |
| 268 | { |
| 269 | "init": "{ |
| 270 | if (!(trigger.data.account)) bounce ("need account name"); |
| 271 | $input = trigger.output[[asset=base]]; |
| 272 | }", |
| 273 | "app": "state", |
| 274 | "state": "{ |
| 275 | var[$account] += $input; |
| 276 | response['message'] = $input||" added to the account "||$account; |
| 277 | }" |
| 278 | } |
| 279 | ] |
| 280 | } |
| 281 | ] |
| 282 | } |
| 283 | } |
| 284 | ] |