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