| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | |
| 9 | $AA_OWNER = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 10 | $AA_NAME = "THAANKS"; |
| 11 | $LOPAVAA = "K6XFY4PODYMWPUELDOYG6UOJ3GQLMDMT"; |
| 12 | $DANAA = var[$LOPAVAA]["DANAA_last_version"]; |
| 13 | |
| 14 | |
| 15 | $INSTRUCTIONS_USE_CASES = "Be first to use 'token = <received token>' after the request time and before the acceptable delay to receive the associated reward. To subscribe as an helper and have a chance to be picked up later use: 'subscribe = true'. To unsubscribe use: 'unsubscribe = true'. If your are an AA use at minimum 'ask_help = <true>'. "; |
| 16 | $INSTRUCTIONS_ASK_HELP = "Use 'ask_help = <true>' with optionaly 'helper = <address>' if you want to propose an helper. Use 'private_mode = true' to force the triggering to be done by the specific helper. If you need to be trigger after a given time, use 'request_time = <timestamp>' and if you want to give a dead line use 'acceptable_delay = <delay in seconds>'. "; |
| 17 | |
| 18 | $DEFAULT_ACCEPTABLE_DELAY = 10*24*60*60; |
| 19 | |
| 20 | $user = trigger.address; |
| 21 | $i = trigger.data; |
| 22 | }", |
| 23 | "messages": { |
| 24 | "cases": [ |
| 25 | { |
| 26 | "if": "{ !!$i.ask_help }", |
| 27 | "init": "{ |
| 28 | if ($i.ask_help != "true") bounce ($INSTRUCTIONS_ASK_HELP); |
| 29 | |
| 30 | $aa = $i.aa otherwise $user; |
| 31 | $requested_time = $i.requested_time otherwise timestamp; |
| 32 | $acceptable_delay = !!$i.acceptable_delay ? $i.acceptable_delay : $DEFAULT_ACCEPTABLE_DELAY; |
| 33 | $received_amount = trigger.output[[asset!=base]].amount otherwise trigger.output[[asset=base]].amount; |
| 34 | $reward_amount = $received_amount - 1000; |
| 35 | $reward_asset = trigger.output[[asset!=base]].asset != "none" ? trigger.output[[asset!=base]].asset : "base"; |
| 36 | $asset_nice_name = var[$DANAA][$reward_asset||"_shortName"] otherwise $reward_asset; |
| 37 | |
| 38 | $good_guy_count = var["good_guy_count"] otherwise 0; |
| 39 | $helper = $i.helper otherwise $good_guy_count > 0 ? |
| 40 | var["good_guy_"||number_from_seed(timestamp,0, $good_guy_count)] : bounce ("No good guy available, use 'helper = <address>'!"); |
| 41 | |
| 42 | $token = sha256($aa||$requested_time); |
| 43 | }", |
| 44 | "messages": [ |
| 45 | { |
| 46 | "app": "data", |
| 47 | "payload": { |
| 48 | "message": "{"Get "||$reward_amount||" "||$asset_nice_name||" if you are first to send back this 'token = <token received below>'' to me between "||timestamp_to_string($requested_time)||" and "||timestamp_to_string($requested_time+$acceptable_delay, 'datetime')}", |
| 49 | "me": "{this_address}", |
| 50 | "token": "{$token}" |
| 51 | } |
| 52 | }, |
| 53 | { |
| 54 | "app": "payment", |
| 55 | "payload": { |
| 56 | "asset": "base", |
| 57 | "outputs": [ |
| 58 | { |
| 59 | "address": "{$helper}", |
| 60 | "amount": "{4}" |
| 61 | } |
| 62 | ] |
| 63 | } |
| 64 | }, |
| 65 | { |
| 66 | "app": "state", |
| 67 | "state": "{ |
| 68 | var[$token||"_aa"] = $aa; |
| 69 | var[$token||"_helper"] = $helper; var[$token||"_private_mode"] = $i.private_mode; |
| 70 | var[$token||"_reward_amount"] = $reward_amount; var[$token||"_reward_asset"] = $reward_asset; |
| 71 | var[$token||"_requested_time"] = $requested_time; var[$token||"_acceptable_delay"] = $acceptable_delay; |
| 72 | response['message'] = $helper||" has been asked for help ^^"; |
| 73 | }" |
| 74 | } |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "if": "{ $i.token }", |
| 79 | "init": "{ |
| 80 | |
| 81 | $token = $i.token otherwise bounce ($INSTRUCTIONS_TRIGGER_FOR_REWARD); |
| 82 | |
| 83 | $aa = var[$token||"_aa"]; |
| 84 | if (!$aa) bounce ("Too late someone was quicker (or you mistype the token)!"); |
| 85 | |
| 86 | |
| 87 | $requested_time = var[$token||"_requested_time"]; |
| 88 | if ($requested_time > timestamp)bounce ("Too early, try at "||timestamp_to_string($requested_time)); |
| 89 | |
| 90 | |
| 91 | $too_late = timestamp > $requested_time + var[$token||"_acceptable_delay"]; |
| 92 | |
| 93 | |
| 94 | if (!!var[$token||"_private_mode"]) |
| 95 | if (var[$token||"_helper"] != $user) bounce ("You are not allow to get this private reward !"); |
| 96 | |
| 97 | |
| 98 | $bytes = trigger.output[[asset=base]].amount - 2000; |
| 99 | $reward_in_other = var[$token||"_reward_asset"] != "base" ? var[$token||"_reward_amount"] : false; |
| 100 | if ($too_late) |
| 101 | { |
| 102 | $bytes_for_user = $bytes + 2000; |
| 103 | $bytes_for_aa = var[$token||"_reward_asset"] == "base" ? var[$token||"_reward_amount"]-1000 : 0; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | $bytes_for_user = var[$token||"_reward_asset"] == "base" ? var[$token||"_reward_amount"] + $bytes: $bytes; |
| 108 | $bytes_for_aa = 1000; |
| 109 | } |
| 110 | }", |
| 111 | "messages": [ |
| 112 | { |
| 113 | "if": "{ !!$reward_in_other }", |
| 114 | "app": "payment", |
| 115 | "payload": { |
| 116 | "asset": "{ var[$token||"_reward_asset"] }", |
| 117 | "outputs": [ |
| 118 | { |
| 119 | "address": "{ $too_late ? $aa : $user }", |
| 120 | "amount": "{ $reward_in_other }" |
| 121 | } |
| 122 | ] |
| 123 | } |
| 124 | }, |
| 125 | { |
| 126 | "app": "payment", |
| 127 | "payload": { |
| 128 | "asset": "base", |
| 129 | "outputs": [ |
| 130 | { |
| 131 | "address": "{ $user }", |
| 132 | "amount": "{ $bytes_for_user }" |
| 133 | }, |
| 134 | { |
| 135 | "address": "{ $aa }", |
| 136 | "amount": "{ $bytes_for_aa }" |
| 137 | } |
| 138 | ] |
| 139 | } |
| 140 | }, |
| 141 | { |
| 142 | "app": "state", |
| 143 | "state": "{ |
| 144 | var[$token||"_aa"] = false; |
| 145 | var[$token||"_helper"] = false; var[$token||"_private_mode"] = false; |
| 146 | var[$token||"_reward_amount"] = false; var[$token||"_reward_asset"] = false; |
| 147 | var[$token||"_requested_time"] = false; var[$token||"_acceptable_delay"] = false; |
| 148 | response['message'] = $too_late ? "Too late, sorry !" : $user||" has been rewarded ^^"; |
| 149 | }" |
| 150 | } |
| 151 | ] |
| 152 | }, |
| 153 | { |
| 154 | "if": "{ $i.subscribe }", |
| 155 | "init": "{ if (var[$user]) bounce ("You are motivated, but once is enough!"); }", |
| 156 | "messages": [ |
| 157 | { |
| 158 | "app": "state", |
| 159 | "state": "{ |
| 160 | var["good_guy_count"] += 1; |
| 161 | var["good_guy_"||$user||"_id"] = var["good_guy_count"]; |
| 162 | var["good_guy_"||var["good_guy_count"]] = $user; |
| 163 | response['message'] = "Subscribed ^^. "; |
| 164 | }" |
| 165 | } |
| 166 | ] |
| 167 | }, |
| 168 | { |
| 169 | "if": "{ $i.unsubscribe }", |
| 170 | "init": "{ if (!var[$user]) bounce ("You were not register!"); }", |
| 171 | "messages": [ |
| 172 | { |
| 173 | "app": "state", |
| 174 | "state": "{ |
| 175 | var["good_guy_count"] -= 1; |
| 176 | $id = var["good_guys_"||$user||"_id"]; |
| 177 | var["good_guy_"||$user||"_id"] = false; |
| 178 | var["good_guy_"||$id] = false; |
| 179 | response['message'] = "Unsubscribed ^^. "; |
| 180 | }" |
| 181 | } |
| 182 | ] |
| 183 | }, |
| 184 | { |
| 185 | "init": "{ |
| 186 | if ($user != $AA_OWNER) bounce ($INSTRUCTIONS_USE_CASES); |
| 187 | }", |
| 188 | "messages": [ |
| 189 | { |
| 190 | "app": "payment", |
| 191 | "payload": { |
| 192 | "asset": "{'base'}", |
| 193 | "outputs": [ |
| 194 | { |
| 195 | "address": "{$AA_OWNER}", |
| 196 | "amount": "{$reward}" |
| 197 | } |
| 198 | ] |
| 199 | } |
| 200 | } |
| 201 | ] |
| 202 | } |
| 203 | ] |
| 204 | } |
| 205 | } |
| 206 | ] |