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