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