| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | if (trigger.output[[asset!=base]].asset != 'none') |
| 6 | bounce('foreign coins'); |
| 7 | $payment_amount = trigger.output[[asset=base]]; |
| 8 | $min_reward = 1e5; |
| 9 | $coef = 1.5; |
| 10 | $overpayment_fee = 1000; |
| 11 | $min_delay_before_deadline = 15*60; |
| 12 | $period_length = 3600; |
| 13 | |
| 14 | $question = trigger.data.question; |
| 15 | $additional_description = trigger.data.description; |
| 16 | $question_id = trigger.data.question_id; |
| 17 | $outcome = trigger.data.outcome; |
| 18 | |
| 19 | if ($question AND trigger.data.question_id) |
| 20 | bounce("Question and question_id not allowed together"); |
| 21 | |
| 22 | if (!$question AND !trigger.data.question_id) |
| 23 | bounce("No question nor key"); |
| 24 | |
| 25 | if ($question AND length($question) > 256) |
| 26 | bounce("Question cannot be over 256 chars"); |
| 27 | |
| 28 | if ($question AND contains($question, '_')) |
| 29 | bounce('Question cannot contain underscore'); |
| 30 | |
| 31 | if ($additional_description AND length($additional_description) > 256) |
| 32 | bounce("Additional description cannot be over 256 chars"); |
| 33 | |
| 34 | if ($additional_description AND contains($additional_description, '_')) |
| 35 | bounce('Additional description cannot contain underscore'); |
| 36 | |
| 37 | if ($question_id AND !var[$question_id]) |
| 38 | bounce("This question doesn't exist"); |
| 39 | |
| 40 | if ($outcome AND $outcome != "no" AND $outcome != "yes") |
| 41 | bounce("Outcome must be yes or no"); |
| 42 | |
| 43 | if ($question_id AND !$outcome AND !trigger.data.withdraw AND !trigger.data.commit) |
| 44 | bounce("You must either provide an outcome, commit or withdraw"); |
| 45 | |
| 46 | }", |
| 47 | "messages": { |
| 48 | "cases": [ |
| 49 | { |
| 50 | "if": "{$question}", |
| 51 | "init": "{ |
| 52 | if ($payment_amount < $min_reward) |
| 53 | bounce("Reward must be at least "||$min_reward||" bytes"); |
| 54 | |
| 55 | $key = "k_"||substring(sha256($question||' '||$additional_description||' '||$payment_amount),0,16); |
| 56 | if (var[$key]) |
| 57 | bounce("This question already exists"); |
| 58 | |
| 59 | if (!trigger.data.deadline) |
| 60 | bounce("You must specify a deadline"); |
| 61 | |
| 62 | $deadline = json_parse(trigger.data.deadline); |
| 63 | if (!is_integer($deadline)) |
| 64 | bounce("Deadline must be an integer"); |
| 65 | |
| 66 | if ($deadline < timestamp + $min_delay_before_deadline) |
| 67 | bounce("Deadline must be at least "||$min_delay_before_deadline||" seconds from now"); |
| 68 | }", |
| 69 | "messages": [ |
| 70 | { |
| 71 | "app": "state", |
| 72 | "state": "{ |
| 73 | var[$key] = "created"; |
| 74 | var[$key||"_question"] = $question; |
| 75 | var[$key||"_deadline"] = $deadline; |
| 76 | var[$key||"_reward"] = $payment_amount; |
| 77 | response['your_address'] = trigger.address; |
| 78 | response['new_question'] = $question; |
| 79 | response['question_id'] = $key; |
| 80 | |
| 81 | }" |
| 82 | } |
| 83 | ] |
| 84 | }, |
| 85 | { |
| 86 | "if": "{$outcome AND $question_id}", |
| 87 | "init": "{ |
| 88 | $key = $question_id; |
| 89 | if (var[$key||"_deadline"] > timestamp) |
| 90 | bounce("Deadline isn't reached, cannot be graded yet"); |
| 91 | |
| 92 | if(var[$key||'_outcome']){ |
| 93 | if(var[$key||'_outcome'] == $outcome) |
| 94 | bounce("Question is already graded with this outcome"); |
| 95 | if (timestamp - var[$key || '_countdown_start'] > $period_length) |
| 96 | bounce('challenging period expired'); |
| 97 | $current_outcome = var[$key || '_outcome']; |
| 98 | |
| 99 | $stake_on_current_outcome = var[$key || '_total_staked_on_' || $current_outcome]; |
| 100 | $stake_on_proposed_outcome = var[$key || '_total_staked_on_' || $outcome]; |
| 101 | $required_to_challenge = round($coef * $stake_on_current_outcome); |
| 102 | $would_override_current_outcome = ($stake_on_proposed_outcome + $amount >= $required_to_challenge); |
| 103 | if ($would_override_current_outcome) |
| 104 | $excess = $stake_on_proposed_outcome + $amount - $required_to_challenge; |
| 105 | } else { |
| 106 | if ($payment_amount < var[$key||"_reward"]) |
| 107 | bounce("You must stake at least "||$min_reward||" bytes to grade this question"); |
| 108 | $excess = $payment_amount - $min_reward; |
| 109 | $bInitialStake = true; |
| 110 | } |
| 111 | |
| 112 | }", |
| 113 | "messages": [ |
| 114 | { |
| 115 | "if": "{$excess}", |
| 116 | "app": "payment", |
| 117 | "payload": { |
| 118 | "asset": "base", |
| 119 | "outputs": [ |
| 120 | { |
| 121 | "address": "{trigger.address}", |
| 122 | "amount": "{$excess - $overpayment_fee}" |
| 123 | } |
| 124 | ] |
| 125 | } |
| 126 | }, |
| 127 | { |
| 128 | "app": "state", |
| 129 | "state": "{ |
| 130 | var[$key] = "being_graded"; |
| 131 | if ($bInitialStake || $would_override_current_outcome) |
| 132 | var[$key||'_outcome'] = $outcome; |
| 133 | $accepted_amount = $payment_amount - $excess; |
| 134 | var[$key||'_total_staked_on_'||$outcome] += $accepted_amount; |
| 135 | var[$key || '_total_staked'] += $accepted_amount; |
| 136 | var[$key || '_total_staked_on_' || $outcome || '_by_' || trigger.address] = $accepted_amount; |
| 137 | |
| 138 | var[$key || '_countdown_start'] = timestamp; |
| 139 | if ($bInitialStake){ |
| 140 | var[$key || '_initial_reporter'] = trigger.address; |
| 141 | response['expected_reward'] = var[$key||"_reward"]; |
| 142 | } |
| 143 | |
| 144 | response['your_address'] = trigger.address; |
| 145 | response['your_stake'] = var[$key || '_total_staked_on_' || $outcome || '_by_' || trigger.address]; |
| 146 | response['total_staked_on_yes'] = var[$key || '_total_staked_on_yes'] otherwise 0; |
| 147 | response['total_staked_on_no'] = var[$key || '_total_staked_on_no'] otherwise 0; |
| 148 | response['question_id'] = $key; |
| 149 | response['reporting'] = $outcome; |
| 150 | }" |
| 151 | } |
| 152 | ] |
| 153 | }, |
| 154 | { |
| 155 | "if": "{trigger.data.commit AND $question_id}", |
| 156 | "init": "{ |
| 157 | $key = $question_id; |
| 158 | if (var[$key] == 'committed') |
| 159 | bounce('already committed'); |
| 160 | if (timestamp - var[$key || '_countdown_start'] <= $period_length) |
| 161 | bounce('challenge period is still running'); |
| 162 | |
| 163 | $address = var[$key || '_initial_reporter']; |
| 164 | $initial_reporter_stake = var[$key || '_total_staked_on_' || $outcome || '_by_' || $address]; |
| 165 | |
| 166 | if ($initial_reporter_stake){ |
| 167 | $reward = var[$key||"_reward"]; |
| 168 | $total_winning_stake = var[$key || '_total_staked_on_' || $outcome]; |
| 169 | $total_stake = var[$key || '_total_staked']; |
| 170 | $full_amount = round($initial_reporter_stake / $total_winning_stake * ($total_stake + $rewar)); |
| 171 | } |
| 172 | }", |
| 173 | "messages": [ |
| 174 | { |
| 175 | "app": "data_feed", |
| 176 | "payload": { |
| 177 | "{$key}": "{$outcome}" |
| 178 | } |
| 179 | }, |
| 180 | { |
| 181 | "if": "{$initial_reporter_stake}", |
| 182 | "app": "payment", |
| 183 | "payload": { |
| 184 | "asset": "base", |
| 185 | "outputs": [ |
| 186 | { |
| 187 | "address": "{$address}", |
| 188 | "amount": "{$full_amount}" |
| 189 | } |
| 190 | ] |
| 191 | } |
| 192 | }, |
| 193 | { |
| 194 | "app": "state", |
| 195 | "state": "{ |
| 196 | var[$key] = 'committed'; |
| 197 | var[$pair||"_committed_outcome"] = $outcome; |
| 198 | if ($initial_reporter_stake){ |
| 199 | var[$key || '_total_staked_on_' || $outcome || '_by_' || $address] = false; |
| 200 | response['paid_out_amount'] = $full_amount; |
| 201 | response['paid_out_address'] = $address; |
| 202 | } |
| 203 | response['question_id'] = $key; |
| 204 | response['committed_outcome'] = $outcome; |
| 205 | }" |
| 206 | } |
| 207 | ] |
| 208 | }, |
| 209 | { |
| 210 | "if": "{trigger.data.withdraw AND $question_id}", |
| 211 | "init": "{ |
| 212 | if (var[$key] != 'committed') |
| 213 | bounce('not committed yet'); |
| 214 | $address = trigger.data.address otherwise trigger.address; |
| 215 | $outcome = var[$key || '_outcome']; |
| 216 | $my_stake = var[$key || '_total_staked_on_' || $outcome || '_by_' || $address]; |
| 217 | if (!$my_stake) |
| 218 | bounce("you didn't stake on the winning outcome or you already withdrew"); |
| 219 | $reward = var[$key||"_reward"]; |
| 220 | $total_winning_stake = var[$key || '_total_staked_on_' || $outcome]; |
| 221 | $total_stake = var[$key || '_total_staked']; |
| 222 | $full_amount = round($my_stake / $total_winning_stake * ($total_stake + $reward)); |
| 223 | }", |
| 224 | "messages": [ |
| 225 | { |
| 226 | "app": "payment", |
| 227 | "payload": { |
| 228 | "asset": "base", |
| 229 | "outputs": [ |
| 230 | { |
| 231 | "address": "{$address}", |
| 232 | "amount": "{$full_amount}" |
| 233 | } |
| 234 | ] |
| 235 | } |
| 236 | }, |
| 237 | { |
| 238 | "app": "state", |
| 239 | "state": "{ |
| 240 | var[$key || '_total_staked_on_' || $outcome || '_by_' || $address] = false; |
| 241 | response['message'] = "paid " || $amount || " bytes"; |
| 242 | response['question_id'] = $key; |
| 243 | response['paid_out_amount'] = $full_amount; |
| 244 | response['paid_out_address'] = $address; |
| 245 | }" |
| 246 | } |
| 247 | ] |
| 248 | }, |
| 249 | { |
| 250 | "if": "{trigger.data.nickname}", |
| 251 | "messages": [ |
| 252 | { |
| 253 | "app": "state", |
| 254 | "state": "{ |
| 255 | var['nickname_' || trigger.address] = trigger.data.nickname; |
| 256 | response['your_address'] = trigger.address; |
| 257 | response['nickname'] = trigger.data.nickname; |
| 258 | response['message'] = "Nickname changed for " || trigger.data.nickname; |
| 259 | }" |
| 260 | } |
| 261 | ] |
| 262 | } |
| 263 | ] |
| 264 | } |
| 265 | } |
| 266 | ] |