| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "init": "{ |
| 5 | if (trigger.output[[asset!=base]].asset != 'none') |
| 6 | bounce('foreign coins'); |
| 7 | $fee = 1000; |
| 8 | $min_stake = 1e6; |
| 9 | $coef = 1.5; |
| 10 | $period_length = 3600; |
| 11 | |
| 12 | $exchange = trigger.data.exchange; |
| 13 | $remove_wallet_id = trigger.data.remove_wallet_id; |
| 14 | $add_wallet_id = trigger.data.add_wallet_id; |
| 15 | $forced_key = trigger.data.challenge_id; |
| 16 | if ($remove_wallet_id AND $add_wallet_id) |
| 17 | bounce('cannot remove and add'); |
| 18 | $pair = 'p_' || sha256($exchange || '=' || ($remove_wallet_id ? $remove_wallet_id : $add_wallet_id)); |
| 19 | }", |
| 20 | "messages": { |
| 21 | "cases": [ |
| 22 | { |
| 23 | "if": "{$exchange AND ($remove_wallet_id OR $add_wallet_id)}", |
| 24 | "init": "{ |
| 25 | if ($forced_key AND !var[$forced_key]) |
| 26 | bounce("unknown challenge"); |
| 27 | if ($forced_key AND var[$forced_key] != 'committed') |
| 28 | bounce('this challenge is not committed'); |
| 29 | if ($forced_key){ |
| 30 | $key = $forced_key; |
| 31 | } |
| 32 | else { |
| 33 | $num = var[$pair || '_number'] otherwise 0; |
| 34 | $old_key = $pair || '_' || $num; |
| 35 | if (!$num OR var[$old_key] == 'committed') |
| 36 | $key = $pair || '_' || ($num + 1); |
| 37 | else |
| 38 | $key = $old_key; |
| 39 | } |
| 40 | }", |
| 41 | "messages": { |
| 42 | "cases": [ |
| 43 | { |
| 44 | "if": "{ |
| 45 | if (trigger.output[[asset=base]] < $min_stake) |
| 46 | return false; |
| 47 | if ($forced_key) |
| 48 | bounce("you can specify challenge id only for withdrawal"); |
| 49 | $amount = trigger.output[[asset=base]] - $fee; |
| 50 | $outcome = $add_wallet_id ? 'yes' : 'no'; |
| 51 | $bInitialStake = !var[$key]; |
| 52 | if ($bInitialStake){ |
| 53 | if(var[$pair] AND var[$pair] == 'committed') { |
| 54 | if($outcome == var[$pair||"_committed_outcome"]) |
| 55 | bounce("committed outcome for this pair is already " || $outcome); |
| 56 | } |
| 57 | $pool_id = trigger.data.pool_id; |
| 58 | if (!$pool_id) |
| 59 | bounce('please specify a reward pool id'); |
| 60 | $number_of_rewards = var['pool_' || $pool_id || '_number_of_rewards']; |
| 61 | if (!$number_of_rewards) |
| 62 | bounce("pool " || $pool_id || " doesn't exist or is empty"); |
| 63 | $pool_feed_name = var['pool_' || $pool_id || '_feed_name']; |
| 64 | if ($pool_feed_name AND $pool_feed_name != $exchange) |
| 65 | bounce("pool " || $pool_id || " is for exchange " || $pool_feed_name || " only"); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | if (timestamp - var[$key || '_countdown_start'] > $period_length) |
| 70 | bounce('challenging period expired'); |
| 71 | $current_outcome = var[$key || '_outcome']; |
| 72 | if ($current_outcome == $outcome) |
| 73 | bounce('staking on the current outcome is not allowed'); |
| 74 | $stake_on_current_outcome = var[$key || '_total_staked_on_' || $current_outcome]; |
| 75 | $stake_on_proposed_outcome = var[$key || '_total_staked_on_' || $outcome]; |
| 76 | $required_to_challenge = round($coef * $stake_on_current_outcome); |
| 77 | $would_override_current_outcome = ($stake_on_proposed_outcome + $amount >= $required_to_challenge); |
| 78 | if ($would_override_current_outcome) |
| 79 | $excess = $stake_on_proposed_outcome + $amount - $required_to_challenge; |
| 80 | true |
| 81 | }", |
| 82 | "messages": [ |
| 83 | { |
| 84 | "if": "{$excess}", |
| 85 | "app": "payment", |
| 86 | "payload": { |
| 87 | "asset": "base", |
| 88 | "outputs": [ |
| 89 | { |
| 90 | "address": "{trigger.address}", |
| 91 | "amount": "{$excess}" |
| 92 | } |
| 93 | ] |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | "app": "state", |
| 98 | "state": "{ |
| 99 | if ($bInitialStake){ |
| 100 | var[$pair || '_exchange'] = $exchange; |
| 101 | var[$pair || '_wallet_id'] = $wallet_id; |
| 102 | var[$pair || '_number'] += 1; |
| 103 | var[$key] = 'onreview'; |
| 104 | var[$key || '_initial_reporter'] = trigger.address; |
| 105 | var['pool_' || $pool_id || '_number_of_rewards'] -= 1; |
| 106 | var[$key || '_pool_id'] = $pool_id; |
| 107 | response['pool_id'] = $pool_id; |
| 108 | response['expected_reward'] = var['pool_' || $pool_id || '_reward_amount']; |
| 109 | } |
| 110 | if ($bInitialStake OR $would_override_current_outcome){ |
| 111 | var[$key || '_countdown_start'] = timestamp; |
| 112 | var[$key || '_outcome'] = $outcome; |
| 113 | response['countdown_started'] = true; |
| 114 | response['outcome'] = $outcome; |
| 115 | } |
| 116 | else |
| 117 | response['outcome'] = $current_outcome; |
| 118 | if (!$bInitialStake){ |
| 119 | response['challenge_id'] = $key; |
| 120 | } |
| 121 | $accepted_amount = $amount - $excess; |
| 122 | var[$key || '_total_staked'] += $accepted_amount; |
| 123 | var[$key || '_total_staked_on_' || $outcome] += $accepted_amount; |
| 124 | var[$key || '_total_staked_on_' || $outcome || '_by_' || trigger.address] += $accepted_amount; |
| 125 | response['staked_on_yes'] = var[$key || '_total_staked_on_yes']; |
| 126 | response['staked_on_no'] = var[$key || '_total_staked_on_no'] otherwise 0; |
| 127 | response['your_stake'] = var[$key || '_total_staked_on_' || $outcome || '_by_' || trigger.address]; |
| 128 | }" |
| 129 | } |
| 130 | ] |
| 131 | }, |
| 132 | { |
| 133 | "if": "{ |
| 134 | if (!trigger.data.commit) |
| 135 | return false; |
| 136 | if ($forced_key) |
| 137 | bounce("you can specify challenge id only for withdrawal"); |
| 138 | if (!var[$key]) |
| 139 | bounce('unknown feed'); |
| 140 | if (var[$key] == 'committed') |
| 141 | bounce('already committed'); |
| 142 | if (timestamp - var[$key || '_countdown_start'] <= $period_length) |
| 143 | bounce('challenge period is still running'); |
| 144 | $pool_id = var[$key || '_pool_id']; |
| 145 | $outcome = var[$key || '_outcome']; |
| 146 | |
| 147 | $address = var[$key || '_initial_reporter']; |
| 148 | $initial_reporter_stake = var[$key || '_total_staked_on_' || $outcome || '_by_' || $address]; |
| 149 | |
| 150 | if ($initial_reporter_stake){ |
| 151 | $reward = var['pool_' || $pool_id || '_reward_amount']; |
| 152 | $total_winning_stake = var[$key || '_total_staked_on_' || $outcome]; |
| 153 | $total_stake = var[$key || '_total_staked']; |
| 154 | $amount = round($initial_reporter_stake / $total_winning_stake * $total_stake); |
| 155 | $full_amount = $amount + $reward; |
| 156 | } |
| 157 | true |
| 158 | }", |
| 159 | "messages": [ |
| 160 | { |
| 161 | "if": "{$outcome == 'yes'}", |
| 162 | "app": "data_feed", |
| 163 | "payload": { |
| 164 | "{$exchange}": "{$wallet_id}" |
| 165 | } |
| 166 | }, |
| 167 | { |
| 168 | "if": "{$outcome == 'no'}", |
| 169 | "app": "data_feed", |
| 170 | "payload": { |
| 171 | "{$exchange}": "{"-"||$wallet_id}" |
| 172 | } |
| 173 | }, |
| 174 | { |
| 175 | "if": "{$initial_reporter_stake}", |
| 176 | "app": "payment", |
| 177 | "payload": { |
| 178 | "asset": "base", |
| 179 | "outputs": [ |
| 180 | { |
| 181 | "address": "{$address}", |
| 182 | "amount": "{$full_amount}" |
| 183 | } |
| 184 | ] |
| 185 | } |
| 186 | }, |
| 187 | { |
| 188 | "app": "state", |
| 189 | "state": "{ |
| 190 | var[$key] = 'committed'; |
| 191 | var[$pair||"_committed_outcome"] = $outcome; |
| 192 | if ($initial_reporter_stake){ |
| 193 | var[$key || '_paid_out_' || $address] = 1; |
| 194 | } |
| 195 | else |
| 196 | var['pool_' || $pool_id || '_number_of_rewards'] += 1; |
| 197 | response['message'] = "committed"; |
| 198 | }" |
| 199 | } |
| 200 | ] |
| 201 | }, |
| 202 | { |
| 203 | "if": "{ |
| 204 | if (!trigger.data.withdraw) |
| 205 | return false; |
| 206 | if (!var[$key]) |
| 207 | bounce('unknown feed'); |
| 208 | if (var[$key] != 'committed') |
| 209 | bounce('not committed yet'); |
| 210 | $address = trigger.data.address otherwise trigger.address; |
| 211 | if (var[$key || '_paid_out_' || $address]) |
| 212 | bounce("you were already paid"); |
| 213 | $outcome = var[$key || '_outcome']; |
| 214 | $my_stake = var[$key || '_total_staked_on_' || $outcome || '_by_' || $address]; |
| 215 | if (!$my_stake) |
| 216 | bounce("you didn't stake on the winning outcome"); |
| 217 | $total_winning_stake = var[$key || '_total_staked_on_' || $outcome]; |
| 218 | $total_stake = var[$key || '_total_staked']; |
| 219 | $amount = round($my_stake / $total_winning_stake * $total_stake); |
| 220 | true |
| 221 | }", |
| 222 | "messages": [ |
| 223 | { |
| 224 | "app": "payment", |
| 225 | "payload": { |
| 226 | "asset": "base", |
| 227 | "outputs": [ |
| 228 | { |
| 229 | "address": "{$address}", |
| 230 | "amount": "{$amount}" |
| 231 | } |
| 232 | ] |
| 233 | } |
| 234 | }, |
| 235 | { |
| 236 | "app": "state", |
| 237 | "state": "{ |
| 238 | var[$key || '_paid_out_' || $address] = 1; |
| 239 | response['message'] = "paid " || $amount || " bytes"; |
| 240 | }" |
| 241 | } |
| 242 | ] |
| 243 | } |
| 244 | ] |
| 245 | } |
| 246 | }, |
| 247 | { |
| 248 | "if": "{trigger.data.reward_amount AND trigger.data.number_of_rewards}", |
| 249 | "init": "{ |
| 250 | $expected_amount = trigger.data.reward_amount * trigger.data.number_of_rewards; |
| 251 | if (trigger.output[[asset=base]] != $expected_amount) |
| 252 | bounce('wrong amount received, expected: ' || $expected_amount); |
| 253 | }", |
| 254 | "messages": [ |
| 255 | { |
| 256 | "app": "state", |
| 257 | "state": "{ |
| 258 | var['pool_id'] += 1; |
| 259 | $pool_id = var['pool_id']; |
| 260 | var['pool_' || $pool_id || '_sponsor'] = trigger.address; |
| 261 | var['pool_' || $pool_id || '_reward_amount'] = trigger.data.reward_amount; |
| 262 | var['pool_' || $pool_id || '_number_of_rewards'] = trigger.data.number_of_rewards; |
| 263 | if ($exchange) |
| 264 | var['pool_' || $pool_id || '_exchange'] = $exchange; |
| 265 | response['message'] = "created a reward pool " || $pool_id || " of " || trigger.output[[asset=base]] || " bytes"; |
| 266 | }" |
| 267 | } |
| 268 | ] |
| 269 | }, |
| 270 | { |
| 271 | "if": "{trigger.data.withdraw_pool AND trigger.data.pool_id}", |
| 272 | "init": "{ |
| 273 | $sponsor = var['pool_' || $pool_id || '_sponsor']; |
| 274 | if (!$sponsor) |
| 275 | bounce('no such pool: ' || $pool_id); |
| 276 | if ($sponsor != trigger.address) |
| 277 | bounce('not your pool: ' || $pool_id); |
| 278 | $number_of_rewards = var['pool_' || $pool_id || '_number_of_rewards']; |
| 279 | if (!$number_of_rewards) |
| 280 | bounce('pool ' || $pool_id || ' is already empty'); |
| 281 | $reward_amount = var['pool_' || $pool_id || '_reward_amount']; |
| 282 | }", |
| 283 | "messages": [ |
| 284 | { |
| 285 | "app": "payment", |
| 286 | "payload": { |
| 287 | "asset": "base", |
| 288 | "outputs": [ |
| 289 | { |
| 290 | "address": "{trigger.address}", |
| 291 | "amount": "{$number_of_rewards * $reward_amount}" |
| 292 | } |
| 293 | ] |
| 294 | } |
| 295 | }, |
| 296 | { |
| 297 | "app": "state", |
| 298 | "state": "{ |
| 299 | var['pool_' || $pool_id || '_number_of_rewards'] = false; |
| 300 | response['message'] = "destroyed reward pool " || $pool_id; |
| 301 | }" |
| 302 | } |
| 303 | ] |
| 304 | } |
| 305 | ] |
| 306 | } |
| 307 | } |
| 308 | ] |