| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | |
| 6 | $owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 7 | |
| 8 | $account = trigger.data.account otherwise trigger.data.ac otherwise ""; |
| 9 | |
| 10 | $party1 = trigger.data.address1 otherwise trigger.data.a1 otherwise ""; $p1_exist = ($party1 !=""); |
| 11 | $party2 = trigger.data.address2 otherwise trigger.data.a2 otherwise ""; $p2_exist = ($party2 !=""); |
| 12 | $party3 = trigger.data.address3 otherwise trigger.data.a3 otherwise ""; $p3_exist = ($party3 !=""); |
| 13 | |
| 14 | $trigger = trigger.address; |
| 15 | $pay_amount = trigger.data.amount otherwise trigger.data.am otherwise 0; |
| 16 | $pay_address = trigger.data.address otherwise trigger.data.ad otherwise ""; |
| 17 | $pay_id_received = trigger.data.pay_id otherwise trigger.data.pi otherwise ""; |
| 18 | |
| 19 | |
| 20 | $setup = ($p1_exist) AND ($p2_exist); |
| 21 | $init_pay = ($pay_amount>0) AND ($pay_address !=""); |
| 22 | $validate_pay = $pay_id_received != ""; |
| 23 | $cancel = ($pay_id_received != "") AND (trigger.data.cancel==true); |
| 24 | }", |
| 25 | "messages": { |
| 26 | "cases": [ |
| 27 | { |
| 28 | "if": "{$setup}", |
| 29 | "init": "{ |
| 30 | if ($account=="") bounce ("need account name"); |
| 31 | if (var[$account]) bounce ("Account "||$account||" already exist!"); |
| 32 | }", |
| 33 | "messages": [ |
| 34 | { |
| 35 | "app": "state", |
| 36 | "state": "{ |
| 37 | var[$account] = trigger.output[[asset=base]]; |
| 38 | if ($p1_exist )var[$account||"_1"] = $party1; |
| 39 | if ($p2_exist )var[$account||"_2"] = $party2; |
| 40 | if ($p3_exist )var[$account||"_3"] = $party3; |
| 41 | response['message'] = 'Multi-sig account set for '||$account||'. '; |
| 42 | }" |
| 43 | } |
| 44 | ] |
| 45 | }, |
| 46 | { |
| 47 | "if": "{$init_pay}", |
| 48 | "init": "{ |
| 49 | if ($account=="") bounce ("need account name"); |
| 50 | if (!var[$account]) bounce ("Account "||$account||" do not exist!"); |
| 51 | $pay_id_created = sha256($account||'>'||$pay_address||': '||$pay_amount); |
| 52 | |
| 53 | |
| 54 | if (trigger.address == $party1) |
| 55 | { |
| 56 | $need_party1_validation = false; |
| 57 | $initiator = 1; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | if (trigger.address == $party2) |
| 62 | { |
| 63 | $need_party2_validation = false; |
| 64 | $initiator = 2; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | if (trigger.address == $party3) |
| 69 | { |
| 70 | $need_party3_validation = false; |
| 71 | $initiator = 3; |
| 72 | } |
| 73 | else |
| 74 | bounce ("You are not a party of "||$account); |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | if ($initiator==1) $next_validator_num = 2; else $next_validator_num = 1; |
| 79 | |
| 80 | }", |
| 81 | "messages": [ |
| 82 | { |
| 83 | "app": "data", |
| 84 | "payload": { |
| 85 | "message": "{"JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||$pay_amount||" to "||$pay_address||", please send me back the pay_id linked to validate the transaction. And add a cancel = true field to refuse it."}", |
| 86 | "pay_id": "{$pay_id_created}" |
| 87 | } |
| 88 | }, |
| 89 | { |
| 90 | "app": "payment", |
| 91 | "payload": { |
| 92 | "asset": "base", |
| 93 | "outputs": [ |
| 94 | { |
| 95 | "address": "{var[$account||"_"||$next_validator_num]}", |
| 96 | "amount": 1000 |
| 97 | } |
| 98 | ] |
| 99 | } |
| 100 | }, |
| 101 | { |
| 102 | "app": "state", |
| 103 | "state": "{ |
| 104 | var[$pay_id_created||"_account"] = $account; |
| 105 | var[$pay_id_created||"_amount"] = $pay_amount; |
| 106 | var[$pay_id_created||"_address"] = $pay_address; |
| 107 | var[$pay_id_created||"_initiator"] = trigger.address; |
| 108 | |
| 109 | if ( $need_party1_validation) var[$pay_id_created||"_required_v1"] = true; |
| 110 | if ( $need_party2_validation) var[$pay_id_created||"_required_v2"] = true; |
| 111 | if ( $need_party3_validation) var[$pay_id_created||"_required_v3"] = true; |
| 112 | |
| 113 | response['message'] = "Payment "||$pay_id_created||" created and approvals requested"; |
| 114 | }" |
| 115 | } |
| 116 | ] |
| 117 | }, |
| 118 | { |
| 119 | "if": "{$validate_pay}", |
| 120 | "init": "{ |
| 121 | $account_received = var[$pay_id_received||"_account"] otherwise ""; |
| 122 | $pay_amount_to_valid = var[$pay_id_received||"_amount"] otherwise 0; |
| 123 | $pay_address_to_valid = var[$pay_id_received||"_address"] otherwise ""; |
| 124 | |
| 125 | if ($account_received == "") bounce ("Payment ID "||$pay_id_received||" do not exist!"); |
| 126 | |
| 127 | |
| 128 | if ($trigger == var[$account_received||"_1"]) $validator_num = 1; |
| 129 | if ($trigger == var[$account_received||"_2"]) $validator_num = 2; |
| 130 | if ($trigger == var[$account_received||"_3"]) $validator_num = 3; |
| 131 | |
| 132 | $is_this_validation_needed = var[$pay_id_created||"_required_v"||$validator_num] otherwise false; |
| 133 | if ($is_this_validation_needed ==false) |
| 134 | bounce ("Your validation by validator "||$validator_num||"is not required!"); |
| 135 | |
| 136 | $is_next_needed = var[$pay_id_created||"_required_v"||($validator_num+1)] otherwise false; |
| 137 | if ($is_next_needed == true) |
| 138 | { |
| 139 | $next_address = var[$account_received||"_"||($validator_num+1)]; |
| 140 | $next_amount = 1000; |
| 141 | $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."; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | $next_address = $pay_address_to_valid; |
| 146 | $next_amount = $pay_amount_to_valid; |
| 147 | $next_message = "Payment of "||$next_amount||" done to "||$next_address||" from account "||$account_received; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | }", |
| 152 | "messages": [ |
| 153 | { |
| 154 | "app": "data", |
| 155 | "payload": { |
| 156 | "message": "{$next_message}", |
| 157 | "pay_id": "{$pay_id_received}" |
| 158 | } |
| 159 | }, |
| 160 | { |
| 161 | "app": "payment", |
| 162 | "payload": { |
| 163 | "asset": "base", |
| 164 | "outputs": [ |
| 165 | { |
| 166 | "address": "{$next_address}", |
| 167 | "amount": "{$next_amount}" |
| 168 | } |
| 169 | ] |
| 170 | } |
| 171 | }, |
| 172 | { |
| 173 | "app": "state", |
| 174 | "state": "{ |
| 175 | var[$pay_id_created||"_required_v"||$validator_num] = false; |
| 176 | if ($is_next_needed != true) |
| 177 | { |
| 178 | var[$account_received] = false; |
| 179 | var[$pay_address_to_valid] = false; |
| 180 | var[$pay_amount_to_valid] = false; |
| 181 | } |
| 182 | }" |
| 183 | } |
| 184 | ] |
| 185 | }, |
| 186 | { |
| 187 | "if": "{$cancel}", |
| 188 | "init": "{ |
| 189 | $account_received = var[$pay_id_received||"_account"] otherwise ""; |
| 190 | $amount = var[$pay_id_received||"_amount"] otherwise 0; |
| 191 | $address = var[$pay_id_received||"_address"] otherwise ""; |
| 192 | |
| 193 | if (trigger.address == var[$account_received||"_1"]) $cancel_valid = true; |
| 194 | if (trigger.address == var[$account_received||"_2"]) $cancel_valid = true; |
| 195 | if (trigger.address == var[$account_received||"_2"]) $cancel_valid = true; |
| 196 | |
| 197 | if (trigger.address == $address) $cancel_valid = true; |
| 198 | if ($cancel_valid==false) bounce ("You must be a party to cancel!"); |
| 199 | }", |
| 200 | "messages": [ |
| 201 | { |
| 202 | "app": "state", |
| 203 | "state": "{ |
| 204 | var[$account_received] = false; |
| 205 | var[$address] = false; |
| 206 | var[$amount] = false; |
| 207 | var[$pay_id_created||"_required_v1"] = false; |
| 208 | var[$pay_id_created||"_required_v2"] = false; |
| 209 | var[$pay_id_created||"_required_v3"] = false; |
| 210 | response['message'] = "Payment of "||$amount||" to "||$address||" is cancelled by "||trigger.address; |
| 211 | }" |
| 212 | } |
| 213 | ] |
| 214 | }, |
| 215 | { |
| 216 | "messages": [ |
| 217 | { |
| 218 | "init": "{ |
| 219 | if ($account=="") bounce ("need account name"); |
| 220 | $input = trigger.output[[asset=base]]; |
| 221 | }", |
| 222 | "app": "state", |
| 223 | "state": "{ |
| 224 | var[$account] += $input; |
| 225 | response['message'] = $input||" added to the account "||$account; |
| 226 | }" |
| 227 | } |
| 228 | ] |
| 229 | } |
| 230 | ] |
| 231 | } |
| 232 | } |
| 233 | ] |