| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $owner = "MYESSCFGDERS3YIEGNZDOG2BI5HKQHLU"; |
| 6 | }", |
| 7 | "messages": { |
| 8 | "cases": [ |
| 9 | { |
| 10 | "if": "{ trigger.data.create }", |
| 11 | "init": "{ |
| 12 | $name = trigger.data.create; |
| 13 | $other_party = trigger.data.other_party otherwise bounce ("need 'other_party' address!"); |
| 14 | }", |
| 15 | "messages": [ |
| 16 | { |
| 17 | "app": "state", |
| 18 | "state": "{ |
| 19 | var[$name||"_party1"] = trigger.address; |
| 20 | var[$name||"_party2"] = $other_party; |
| 21 | var[$name||"_balance"] = trigger.output[[asset=base]]; |
| 22 | |
| 23 | if (var["blocked_balance"]) |
| 24 | var["blocked_balance"] += trigger.output[[asset=base]]; |
| 25 | else |
| 26 | var["blocked_balance"] = trigger.output[[asset=base]]; |
| 27 | |
| 28 | response['message'] = "Done! Later, the parties will have to agree on how to spread the balance ("||trigger.output[[asset=base]]||") by sending the 'amount' they allow the other party to receive, if the sum of the 2 amounts is equal to the balance then the payment will be executed."; |
| 29 | }" |
| 30 | } |
| 31 | ] |
| 32 | }, |
| 33 | { |
| 34 | "if": "{ trigger.data.name }", |
| 35 | "init": "{ |
| 36 | $name = trigger.data.name; |
| 37 | $balance = var[$name||"_balance"] otherwise bounce ("Guarantee name do not exist!"); |
| 38 | $amount = trigger.data.amount otherwise bounce ("Need the 'amount' you allow the other party to receive!"); |
| 39 | if ($amount > $balance) bounce ("The amount is bigger than the full balance!"); |
| 40 | |
| 41 | if (var[$name||"_party1"] == trigger.address) |
| 42 | { |
| 43 | $proposition1 = $amount; |
| 44 | $proposition2 = var[$name||"_proposition2"] otherwise $balance; |
| 45 | } |
| 46 | if (var[$name||"_party2"] == trigger.address) |
| 47 | { |
| 48 | $proposition1 = var[$name||"_proposition1"] otherwise $balance; |
| 49 | $proposition2 = $amount; |
| 50 | } |
| 51 | |
| 52 | if (!($proposition1)) bounce ("You are not part of the agreement!"); |
| 53 | |
| 54 | $agreement_found = ( $proposition1 + $proposition2 ) == $balance; |
| 55 | }", |
| 56 | "messages": [ |
| 57 | { |
| 58 | "if": "{ $agreement_found }", |
| 59 | "app": "payment", |
| 60 | "payload": { |
| 61 | "asset": "base", |
| 62 | "outputs": [ |
| 63 | { |
| 64 | "address": "{var[$name||"_party1"]}", |
| 65 | "amount": "{ $proposition2 - 500 }" |
| 66 | }, |
| 67 | { |
| 68 | "address": "{var[$name||"_party2"]}", |
| 69 | "amount": "{ $proposition1 - 500 }" |
| 70 | }, |
| 71 | { |
| 72 | "address": "{trigger.address}", |
| 73 | "amount": "{ trigger.output[[asset=base]]-2000 }" |
| 74 | } |
| 75 | ] |
| 76 | } |
| 77 | }, |
| 78 | { |
| 79 | "app": "state", |
| 80 | "state": "{ |
| 81 | if ($agreement_found) |
| 82 | { |
| 83 | var[$name||"_party1"] = false; |
| 84 | var[$name||"_party2"] = false; |
| 85 | var[$name||"_balance"] = false; |
| 86 | var[$name||"_proposition1"] = false; |
| 87 | var[$name||"_proposition2"] = false; |
| 88 | |
| 89 | var["blocked_balance"] -= ($proposition1 + $proposition2 - 1000); |
| 90 | |
| 91 | response['message'] = "Agreement found and funds sent back to parties ^^"; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | if (var[$name||"_party1"] == trigger.address) |
| 96 | var[$name||"_proposition1"] = $amount; |
| 97 | else |
| 98 | var[$name||"_proposition2"] = $amount; |
| 99 | |
| 100 | response['message'] = "Proposition recorded ("||$amount||" bytes), the other party should set 'amount' to "|| ($balance - $amount)||" bytes to find agreement ^^"; |
| 101 | } |
| 102 | }" |
| 103 | } |
| 104 | ] |
| 105 | }, |
| 106 | { |
| 107 | "init": "{ |
| 108 | if (trigger.address != $owner) |
| 109 | bounce ("you need to read the doc!"); |
| 110 | }", |
| 111 | "messages": [ |
| 112 | { |
| 113 | "app": "payment", |
| 114 | "payload": { |
| 115 | "asset": "base", |
| 116 | "outputs": [ |
| 117 | { |
| 118 | "address": "{$owner}", |
| 119 | "amount": "{ balance[base] - var["blocked_balance"] - 1000 }" |
| 120 | } |
| 121 | ] |
| 122 | } |
| 123 | }, |
| 124 | { |
| 125 | "app": "state", |
| 126 | "state": "{ |
| 127 | response['message'] = "Dust sent to owner"; |
| 128 | }" |
| 129 | } |
| 130 | ] |
| 131 | } |
| 132 | ] |
| 133 | } |
| 134 | } |
| 135 | ] |