| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | $i = trigger.data; |
| 6 | |
| 7 | |
| 8 | $INSTRUCTIONS = "Create a locked guarantee with 'create' = <unique account name> and 'owner' = <address of the owner for who you should lock a guarantee for>. Once locked, it will require consensus between the 2 parties on how to distribute it to unlock it. Both parties should use 'proposition' = <part of the guarantee you are agree to release for the other party>. When the sum of the 2 propositions is equal to the guarantee amount, the guarantee is unlock and distribute accordingly to the 2 parties."; |
| 9 | |
| 10 | |
| 11 | $AA_OWNER = "MYESSCFGDERS3YIEGNZDOG2BI5HKQHLU"; |
| 12 | $AA_NAME = "ARGAA"; |
| 13 | |
| 14 | |
| 15 | $account_name = $i.account_name otherwise $i.create; |
| 16 | $key = $account_name||"_"; |
| 17 | |
| 18 | |
| 19 | $owner = var[$key||"owner"] otherwise $i.owner otherwise $i.o otherwise bounce ("need 'owner' address!"); |
| 20 | $renter = var[$key||"renter"] otherwise trigger.address; |
| 21 | |
| 22 | |
| 23 | $guarantee = var[$key||"guarantee"] otherwise trigger.output[[asset=base]]-1000; |
| 24 | |
| 25 | |
| 26 | $owner_proposition = var[$key||"owner_proposition"] otherwise 0; |
| 27 | $renter_proposition = var[$key||"renter_proposition"] otherwise 0; |
| 28 | $trigger_proposition = $i.proposition otherwise $i.p otherwise false; |
| 29 | |
| 30 | |
| 31 | $total = var["total"] otherwise 0; |
| 32 | }", |
| 33 | "messages": { |
| 34 | "cases": [ |
| 35 | { |
| 36 | "if": "{ !!$i.create and !!$owner}", |
| 37 | "init": "{ if (var[$key||"guarantee"]) bounce ("Already a guarantee with this name"); }", |
| 38 | "messages": [ |
| 39 | { |
| 40 | "app": "data", |
| 41 | "payload": { |
| 42 | "account_name": "{ $account_name }", |
| 43 | "guarantee": "{ $guarantee }", |
| 44 | "aa": "{ this_address }" |
| 45 | } |
| 46 | }, |
| 47 | { |
| 48 | "app": "payment", |
| 49 | "payload": { |
| 50 | "asset": "base", |
| 51 | "outputs": [ |
| 52 | { |
| 53 | "address": "{ $owner }", |
| 54 | "amount": "{ 1 }" |
| 55 | } |
| 56 | ] |
| 57 | } |
| 58 | }, |
| 59 | { |
| 60 | "app": "state", |
| 61 | "state": "{ |
| 62 | var[$key||"owner"] = $owner; var[$key||"guarantee"] = $guarantee; |
| 63 | var[$key||"renter"] = $renter; var["total"] = $total + $guarantee; |
| 64 | |
| 65 | response['message'] = $guarantee||" bytes locked as guarantee."; |
| 66 | }" |
| 67 | } |
| 68 | ] |
| 69 | }, |
| 70 | { |
| 71 | "if": "{ !!$i.account_name and !!$trigger_proposition }", |
| 72 | "init": "{ |
| 73 | |
| 74 | if ($trigger_proposition > $guarantee) bounce ("The proposition is larger than the full guarantee!"); |
| 75 | if (trigger.address != $renter and trigger.address != $owner) bounce ("You are not part of the agreement!"); |
| 76 | |
| 77 | |
| 78 | $new_owner_proposition = trigger.address == $owner ? $trigger_proposition : $owner_proposition; |
| 79 | $new_renter_proposition = trigger.address == $renter ? $trigger_proposition : $renter_proposition; |
| 80 | |
| 81 | |
| 82 | $agreement_found = ( $new_owner_proposition + $new_renter_proposition ) == $guarantee; |
| 83 | |
| 84 | |
| 85 | $amount_to_send_to_renter = $agreement_found ? $new_owner_proposition : trigger.address == $owner ? 1:0; |
| 86 | $amount_to_send_to_owner = $agreement_found ? $new_renter_proposition : trigger.address == $renter ? 1:0; |
| 87 | }", |
| 88 | "messages": [ |
| 89 | { |
| 90 | "app": "data", |
| 91 | "payload": { |
| 92 | "account_name": "{ $account_name }", |
| 93 | "guarantee": "{ $guarantee }", |
| 94 | "agreement": "{ $agreement_found }", |
| 95 | "renter_proposition": "{ $new_renter_proposition }", |
| 96 | "owner_proposition": "{ $new_owner_proposition }", |
| 97 | "aa": "{ this_address }" |
| 98 | } |
| 99 | }, |
| 100 | { |
| 101 | "app": "payment", |
| 102 | "payload": { |
| 103 | "asset": "base", |
| 104 | "outputs": [ |
| 105 | { |
| 106 | "address": "{ $renter }", |
| 107 | "amount": "{ $amount_to_send_to_renter }" |
| 108 | }, |
| 109 | { |
| 110 | "address": "{ $owner }", |
| 111 | "amount": "{ $amount_to_send_to_owner }" |
| 112 | }, |
| 113 | { |
| 114 | "address": "{ trigger.address }", |
| 115 | "amount": "{ trigger.output[[asset=base]]-2000 }" |
| 116 | } |
| 117 | ] |
| 118 | } |
| 119 | }, |
| 120 | { |
| 121 | "app": "state", |
| 122 | "state": "{ |
| 123 | if ($agreement_found) |
| 124 | { |
| 125 | var[$key||"renter"] = false; var[$key||"renter_proposition"] = false; |
| 126 | var[$key||"owner"] = false; var[$key||"owner_proposition"] = false; |
| 127 | var[$key||"guarantee"] = false; var["total"] = $total - $guarantee; |
| 128 | |
| 129 | response['message'] = "Agreement found and funds have been sent back to parties ^^"; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | var[$key||"renter_proposition"] = $new_renter_proposition; |
| 134 | var[$key||"owner_proposition"] = $new_owner_proposition; |
| 135 | |
| 136 | response['message'] = "Proposition recorded ("||$trigger_proposition||" bytes), the other party should set 'amount' to "|| ($guarantee - $trigger_proposition)||" bytes to find agreement ^^"; |
| 137 | } |
| 138 | }" |
| 139 | } |
| 140 | ] |
| 141 | }, |
| 142 | { |
| 143 | "init": "{ if (trigger.address != $AA_OWNER) bounce ($INSTRUCTIONS); }", |
| 144 | "messages": [ |
| 145 | { |
| 146 | "app": "payment", |
| 147 | "payload": { |
| 148 | "asset": "base", |
| 149 | "outputs": [ |
| 150 | { |
| 151 | "address": "{ $AA_OWNER }", |
| 152 | "amount": "{ balance[base] - $total - 1000 }" |
| 153 | } |
| 154 | ] |
| 155 | } |
| 156 | }, |
| 157 | { |
| 158 | "app": "state", |
| 159 | "state": "{ response['message'] = "Dust sent to AA owner"; }" |
| 160 | } |
| 161 | ] |
| 162 | } |
| 163 | ] |
| 164 | } |
| 165 | } |
| 166 | ] |