| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | |
| 9 | $dust_collector = "B2UMBICLZSBVDHRC7BYM6BBU4AQEXL2F"; |
| 10 | |
| 11 | |
| 12 | $htlc_destination_address = params.destination_address; |
| 13 | $htlc_hash_of_secret = params.hash_of_secret; |
| 14 | $htlc_amount = params.amount; |
| 15 | $htlc_asset = params.asset_id; |
| 16 | $htlc_duration = json_parse(params.duration); |
| 17 | |
| 18 | |
| 19 | $htlc_refunded = var["refunded"]; |
| 20 | $htlc_withdrawn = var["withdrawn"]; |
| 21 | $htlc_funded = var["funded"]; |
| 22 | $htlc_funder = var["funder"]; |
| 23 | $htlc_fund_amount = json_parse(var["fund_amount"]); |
| 24 | |
| 25 | |
| 26 | $user = trigger.initial_address; |
| 27 | $temp = trigger.output[[asset!=base]].asset; |
| 28 | $received_asset = $temp != "none" ? $temp : "base"; |
| 29 | $received_amount = trigger.output[[asset=$received_asset]].amount; |
| 30 | |
| 31 | |
| 32 | $is_timed_out = timestamp > $htlc_duration ; |
| 33 | $is_recipient = $user == $htlc_destination_address; |
| 34 | $is_help_from_funder = ($user == $htlc_funder) and !!trigger.data.open; |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | }", |
| 41 | "messages": { |
| 42 | "cases": [ |
| 43 | { |
| 44 | "if": "{ !!$htlc_refunded or !!$htlc_withdrawn}", |
| 45 | "messages": [ |
| 46 | { |
| 47 | "if": "{$user == $dust_collector}", |
| 48 | "app": "payment", |
| 49 | "payload": { |
| 50 | "asset": "{"base"}", |
| 51 | "outputs": [ |
| 52 | { |
| 53 | "address": "{$dust_collector}", |
| 54 | "amount": "{balance[base]-600}" |
| 55 | } |
| 56 | ] |
| 57 | } |
| 58 | }, |
| 59 | { |
| 60 | "app": "state", |
| 61 | "state": "{ response['message'] = 'Thank you for this donation!'; }" |
| 62 | } |
| 63 | ] |
| 64 | }, |
| 65 | { |
| 66 | "if": "{ !$htlc_funded}", |
| 67 | "messages": [ |
| 68 | { |
| 69 | "app": "state", |
| 70 | "state": "{ |
| 71 | |
| 72 | if ($user == $htlc_destination_address) |
| 73 | bounce ("As the htlc recipient, you should not fund it"); |
| 74 | if ($received_asset != $htlc_asset) |
| 75 | bounce ("Wrong asset, "||$htlc_asset||" wanted (not "||$received_asset||")."); |
| 76 | if ($htlc_amount!= 0) if ($received_amount != $htlc_amount) |
| 77 | bounce ("Wrong amount, "||$htlc_amount||" wanted (not "||$received_amount||")."); |
| 78 | |
| 79 | $estimated_storage_fee = 100; |
| 80 | |
| 81 | var["funded"] = true; |
| 82 | var["funder"] = $user; |
| 83 | var["fund_amount"] = $received_amount - $estimated_storage_fee; |
| 84 | var["deadline"] = timestamp + $htlc_duration; |
| 85 | |
| 86 | |
| 87 | response['message'] = 'HTLC funded'; |
| 88 | }" |
| 89 | } |
| 90 | ] |
| 91 | }, |
| 92 | { |
| 93 | "if": "{ !!$is_timed_out or !!$is_recipient or !!$is_help_from_funder}", |
| 94 | "init": "{ |
| 95 | if (!!$is_recipient) |
| 96 | { |
| 97 | $message = "You have been recognized but "; |
| 98 | if (!!$is_timed_out) |
| 99 | bounce ($message||"the time is out"); |
| 100 | if (!trigger.data.secret) |
| 101 | bounce ($message||"you have to provide the 'secret' revealed on the other chain"); |
| 102 | if (sha256(trigger.data.secret) != params.hash_of_secret) |
| 103 | bounce ($message||"the secret is not good."); |
| 104 | } |
| 105 | $transfert_recipient = !!$is_recipient? $htlc_destination_address: $htlc_funder; |
| 106 | $transfert_asset = $htlc_asset; |
| 107 | $transfert_amount = $htlc_fund_amount; |
| 108 | }", |
| 109 | "messages": [ |
| 110 | { |
| 111 | "app": "payment", |
| 112 | "payload": { |
| 113 | "asset": "{$transfert_asset}", |
| 114 | "outputs": [ |
| 115 | { |
| 116 | "address": "{$transfert_recipient}", |
| 117 | "amount": "{$transfert_amount}" |
| 118 | } |
| 119 | ] |
| 120 | } |
| 121 | }, |
| 122 | { |
| 123 | "if": "{ $transfert_asset != 'base'}", |
| 124 | "app": "payment", |
| 125 | "payload": { |
| 126 | "asset": "base", |
| 127 | "outputs": [ |
| 128 | { |
| 129 | "address": "{$transfert_recipient}", |
| 130 | "amount": "{balance[base]-1000}" |
| 131 | } |
| 132 | ] |
| 133 | } |
| 134 | }, |
| 135 | { |
| 136 | "app": "state", |
| 137 | "state": "{ |
| 138 | var["withdrawn"] = $is_recipient; |
| 139 | var["refunded"] = !$is_recipient; |
| 140 | response['message'] = !!$is_recipient ? 'Withdrawn' : 'Refunded'; |
| 141 | response['donate'] = 'Please donate by sending Gbytes to this address to support developpers'; |
| 142 | }" |
| 143 | } |
| 144 | ] |
| 145 | }, |
| 146 | { |
| 147 | "messages": [ |
| 148 | { |
| 149 | "app": "state", |
| 150 | "state": "{ |
| 151 | bounce("Please find instructions"); |
| 152 | }" |
| 153 | } |
| 154 | ] |
| 155 | } |
| 156 | ] |
| 157 | } |
| 158 | } |
| 159 | ] |