| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $aa_name = "CODAA"; |
| 9 | $howto = "check out: https://certificateofdeposit.000webhostapp.com/"; |
| 10 | |
| 11 | |
| 12 | $PERCENT = 0.01; |
| 13 | $SECOND = 1; |
| 14 | $MINUTES = 60; |
| 15 | $HOUR = 60 * $MINUTES; |
| 16 | $DAY = 24 * $HOUR; |
| 17 | $WEEK = 7 * $DAY; |
| 18 | $YEAR = 364.25 * $DAY; |
| 19 | $TOTAL_CODS_AMOUNT=1e15; |
| 20 | |
| 21 | |
| 22 | |
| 23 | $GIVEAWAY_PERIOD = 10 * $MINUTES; |
| 24 | |
| 25 | $MAX_BONUS_PERCENTAGE = 200 * $PERCENT; |
| 26 | |
| 27 | $BONUS_REDUCTION_PERIOD = 20 * $SECOND; |
| 28 | $BONUS_REDUCTION_PERCENTAGE = 20 * $PERCENT; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | $ACCOUNTING_PERIOD = 10* $MINUTES; |
| 34 | |
| 35 | $INTEREST_PERCENTAGE_FROM_POOL = 4 * $PERCENT; |
| 36 | |
| 37 | |
| 38 | $total_interest_next_accounting = json_parse(var["total_interest_next_accounting"]) otherwise 0; |
| 39 | $pool_balance = json_parse(var["pool_balance"]) otherwise $TOTAL_CODS_AMOUNT; |
| 40 | |
| 41 | |
| 42 | $account_name = trigger.data.account_name otherwise bounce ("Please specify an account name"); |
| 43 | $address_name = trigger.address||"_"||$account_name; |
| 44 | $user_bytes = json_parse(var[$address_name||"_bytes"]) otherwise 0; |
| 45 | $user_cods = json_parse(var[$address_name||"_cods"]) otherwise 0; |
| 46 | $user_stake_start = parse_date(var[$address_name||"_stake_start"]) otherwise timestamp; |
| 47 | $user_predicted_lock_time_in_sec = json_parse(var[$address_name||"_predicted_lock_time_in_sec"]) otherwise trigger.data.lock_time; |
| 48 | $user_predicted_interest = var[$address_name||"_predicted_interest"] otherwise 0; |
| 49 | |
| 50 | }", |
| 51 | "messages": { |
| 52 | "cases": [ |
| 53 | { |
| 54 | "if": "{ !!trigger.data.create}", |
| 55 | "init": "{ |
| 56 | if (!!var["cods_id"]) |
| 57 | bounce ("cods asset already exist now, take advantage of the bonus period to get free ones, just by staking bytes for a moment."); |
| 58 | }", |
| 59 | "messages": [ |
| 60 | { |
| 61 | "app": "asset", |
| 62 | "payload": { |
| 63 | "cap": "{$TOTAL_CODS_AMOUNT}", |
| 64 | "is_private": false, |
| 65 | "is_transferrable": true, |
| 66 | "fixed_denominations": false, |
| 67 | "auto_destroy": false, |
| 68 | "cosigned_by_definer": false, |
| 69 | "issued_by_definer_only": true, |
| 70 | "spender_attested": false |
| 71 | } |
| 72 | }, |
| 73 | { |
| 74 | "app": "state", |
| 75 | "state": "{ |
| 76 | var["cods_id"] = response_unit; |
| 77 | var["block_until"] = timestamp_to_string(timestamp + $GIVEAWAY_PERIOD); |
| 78 | var["distributing_time"] = timestamp_to_string(timestamp); |
| 79 | var["next_accounting_time"] = timestamp_to_string(timestamp + $ACCOUNTING_PERIOD); |
| 80 | var["total_interest_to_distribute"] = 0; |
| 81 | var["total_interest_next_accounting"] = 0; |
| 82 | var["pool_balance"] = $TOTAL_CODS_AMOUNT; |
| 83 | }" |
| 84 | } |
| 85 | ] |
| 86 | }, |
| 87 | { |
| 88 | "if": "{ !!trigger.data.claim }", |
| 89 | "init": "{ |
| 90 | if (!!$user_bytes or !!$user_cods) |
| 91 | bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !"); |
| 92 | if (!trigger.data.lock_time) |
| 93 | bounce("Set lock time"); |
| 94 | |
| 95 | |
| 96 | $received_bytes = trigger.output[[asset=base]].amount; |
| 97 | |
| 98 | |
| 99 | $remaining_time = parse_date(var["block_until"]) - timestamp; |
| 100 | if ($remaining_time<0) |
| 101 | bounce ("Sorry, The free CODs offering is finished !"); |
| 102 | $elapsed_time = $GIVEAWAY_PERIOD - $remaining_time; |
| 103 | $elapsed_periods = round($elapsed_time/$BONUS_REDUCTION_PERIOD); |
| 104 | $remaining_days = round($remaining_time/$DAY); |
| 105 | $bonus = $MAX_BONUS_PERCENTAGE - ($elapsed_days * $BONUS_REDUCTION_PERCENTAGE); |
| 106 | $unfair_free_cods = $received_bytes * ($bonus); |
| 107 | |
| 108 | |
| 109 | $uncorrected_robin_percentage = -0.08 * ln($received_bytes) +2.7; |
| 110 | |
| 111 | $robin_percentage = $uncorrected_robin_percentage>1 ? 1 : $uncorrected_robin_percentage; |
| 112 | $free_cods = round($unfair_free_cods * $robin_percentage); |
| 113 | $predicted_interest = $received_cods * (0.7 * ln($user_predicted_lock_time_in_sec) + 1.5); |
| 114 | }", |
| 115 | "messages": [ |
| 116 | { |
| 117 | "app": "state", |
| 118 | "state": "{ |
| 119 | var["total_interest_next_accounting"] += $predicted_interest; |
| 120 | var["pool_balance"] = $pool_balance - $free_cods; |
| 121 | var[$address_name||"_bytes"] = $received_bytes; |
| 122 | var[$address_name||"_cods"] = $free_cods; |
| 123 | var[$address_name||"_predicted_interest"] = $predicted_interest; |
| 124 | var[$address_name||"_distributed_time"] = timestamp_to_string(timestamp); |
| 125 | var[$address_name||"_stake_start"] = timestamp_to_string($user_stake_start); |
| 126 | var[$address_name||"_predicted_lock_time_in_sec"] = $user_predicted_lock_time_in_sec; |
| 127 | |
| 128 | response["message"] = "Your "||$received_bytes||" bytes are safe with us and blocked until "||var["block_until"]||". You will receive "||$free_cods||" free CODs. You still have "||$remaining_days||" days to claim more CODs. Actual bonus is "||$bonus||"% of your bytes."; |
| 129 | }" |
| 130 | } |
| 131 | ] |
| 132 | }, |
| 133 | { |
| 134 | "if": "{ !!trigger.data.cancel_claim }", |
| 135 | "init": "{ |
| 136 | if (!$user_bytes) |
| 137 | bounce ("Nothing to cancel ! are you using the right address ?"); |
| 138 | if ( timestamp > parse_date(var["block_until"])) |
| 139 | bounce ("Too late to cancel you win free cods and you can already get your bytes back using 'get_bytes_back=true'."); |
| 140 | }", |
| 141 | "messages": [ |
| 142 | { |
| 143 | "app": "payment", |
| 144 | "payload": { |
| 145 | "asset": "base", |
| 146 | "outputs": [ |
| 147 | { |
| 148 | "address": "{trigger.address}", |
| 149 | "amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }" |
| 150 | } |
| 151 | ] |
| 152 | } |
| 153 | }, |
| 154 | { |
| 155 | "app": "state", |
| 156 | "state": "{ |
| 157 | var["total_interest_next_accounting"] = $total_interest_next_accounting - $user_predicted_interest; |
| 158 | var["pool_balance"] = $pool_balance + $user_cods; |
| 159 | var[$address_name||"_bytes"] = false; |
| 160 | var[$address_name||"_cods"] = false; |
| 161 | var[$address_name||"_predicted_interest"] = false; |
| 162 | var[$address_name||"_stake_start"] = false; |
| 163 | |
| 164 | response["message"] = "Your requested your "||$user_bytes||" bytes back. You have lost your "||$user_cods||" CODs."; |
| 165 | }" |
| 166 | } |
| 167 | ] |
| 168 | }, |
| 169 | { |
| 170 | "if": "{ !!trigger.data.get_bytes_back }", |
| 171 | "init": "{ |
| 172 | if (!$user_bytes) |
| 173 | bounce ($account_name||" with the address "||trigger.address||" do NOT exist !"); |
| 174 | if ( timestamp < parse_date(var["block_until"])) |
| 175 | bounce ("Too early to get your bytes back, except if you ready to loose your CODs, in this case use 'cancel_claim'."); |
| 176 | }", |
| 177 | "messages": [ |
| 178 | { |
| 179 | "app": "payment", |
| 180 | "payload": { |
| 181 | "asset": "base", |
| 182 | "outputs": [ |
| 183 | { |
| 184 | "address": "{ trigger.address }", |
| 185 | "amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }" |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | }, |
| 190 | { |
| 191 | "app": "state", |
| 192 | "state": "{ |
| 193 | var[trigger.address||"_"||$account_name||"_bytes"] = false; |
| 194 | response["message"] = "Your "||$user_bytes||" bytes have been sent back to you, you have now "||$user_cods||" CODs staking since "||timestamp_to_string($user_stake_start)||"."; |
| 195 | }" |
| 196 | } |
| 197 | ] |
| 198 | }, |
| 199 | { |
| 200 | "if": "{ !!trigger.data.stake }", |
| 201 | "init": "{ |
| 202 | if (trigger.output[[asset!=base]].asset != var["cods_id"] ) |
| 203 | bounce ("You can only stake CODs ('"||$cods||"'."); |
| 204 | if (!!$user_cods) |
| 205 | bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !"); |
| 206 | $received_cods = trigger.output[[asset!=base]].amount; |
| 207 | $predicted_interest = $received_cods * (0.7 * ln($user_predicted_lock_time_in_sec) + 1.5); |
| 208 | }", |
| 209 | "messages": [ |
| 210 | { |
| 211 | "app": "state", |
| 212 | "state": "{ |
| 213 | var["total_interest_next_accounting"] += $predicted_interest; |
| 214 | var[$address_name||"_cods"] = $received_cods; |
| 215 | var[$address_name||"_predicted_interest"]= $predicted_interest; |
| 216 | var[$address_name||"_stake_start"] = timestamp_to_string($user_stake_start); |
| 217 | var[$address_name||"_predicted_lock_time_in_sec"] = $user_predicted_lock_time_in_sec; |
| 218 | var[$address_name||"_distributed_time"] = var["distributing_time"]; |
| 219 | response["message"] = $received_cods||" CODs in stake until "||timestamp_to_string($user_stake_start + $user_predicted_lock_time_in_sec)||"."; |
| 220 | }" |
| 221 | } |
| 222 | ] |
| 223 | }, |
| 224 | { |
| 225 | "if": "{ !!trigger.data.unstake }", |
| 226 | "init": "{ |
| 227 | if (!$user_cods) |
| 228 | bounce ($account_name||" with the address "||trigger.address||" do NOT exist !"); |
| 229 | if (!!$user_bytes) |
| 230 | bounce ("Cannot unstake while having bytes in the system, please use 'get_bytes_back' or 'cancel_claim'!"); |
| 231 | $too_early = timestamp < $user_stake_start + $user_predicted_lock_time_in_sec; |
| 232 | if ( !!$too_early and !trigger.data.use_force ) |
| 233 | bounce ("Your defined staking period ends in "||($user_stake_start + $user_predicted_lock_time_in_sec-timestamp)/24/60/60||" days. To force the unstaking and loose a big part of your interests use 'use_force' !"); |
| 234 | |
| 235 | if (!!$too_early) |
| 236 | { |
| 237 | $amount_to_send = $user_cods; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | $amount_to_send = $user_cods + var[$address_name||"_interest"]; |
| 242 | } |
| 243 | |
| 244 | }", |
| 245 | "messages": [ |
| 246 | { |
| 247 | "app": "payment", |
| 248 | "payload": { |
| 249 | "asset": "{ var["cods_id"] }", |
| 250 | "outputs": [ |
| 251 | { |
| 252 | "address": "{ trigger.address }", |
| 253 | "amount": "{ $amount_to_send }" |
| 254 | } |
| 255 | ] |
| 256 | } |
| 257 | }, |
| 258 | { |
| 259 | "app": "state", |
| 260 | "state": "{ |
| 261 | var["total_interest_next_accounting"] -= $user_predicted_interest; |
| 262 | var[$address_name||"_cods"] = false; |
| 263 | var[$address_name||"_stake_start"] = false; |
| 264 | var[$address_name||"_predicted_interest"] = false; |
| 265 | var[$address_name||"_predicted_lock_time_in_sec"] = false; |
| 266 | response["message"] = "Your "||$amount_to_send||" CODs have been sent to you."; |
| 267 | }" |
| 268 | } |
| 269 | ] |
| 270 | }, |
| 271 | { |
| 272 | "if": "{ !!trigger.data.do_accounting }", |
| 273 | "init": "{ |
| 274 | if (!$user_cods) |
| 275 | bounce ($account_name||" with the address "||trigger.address||" do NOT exist !"); |
| 276 | if (!!$user_bytes) |
| 277 | bounce ("Cannot do accounting while having bytes in the system, please use 'get_bytes_back' if the bonus period ended or 'cancel_claim'!"); |
| 278 | if (var[$address_name||"_distributed_time"] == var["distributing_time"]) |
| 279 | bounce ("You got your interest already, see you on "||var["next_accounting_time"]); |
| 280 | |
| 281 | |
| 282 | $actual_next_accounting_time = parse_date(var["next_accounting_time"]); |
| 283 | $next_accounting_time_triggered = timestamp > $actual_next_accounting_time; |
| 284 | $cods_available_from_pool = json_parse(var["cods available_from_pool"]); |
| 285 | if (!!$next_accounting_time_triggered) |
| 286 | { |
| 287 | $total_interest_to_distribute = json_parse(var["total_interest_next_accounting"]); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | $total_interest_to_distribute = json_parse(var["total_interest_to_distribute"]); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | $user_interest_in_parts = |
| 296 | json_parse(var[$address_name||"_predicted_interest"]) / $total_interest_to_distribute; |
| 297 | $user_interest_in_cods = $user_interest_in_parts * $cods_available_from_pool; |
| 298 | |
| 299 | }", |
| 300 | "messages": [ |
| 301 | { |
| 302 | "app": "state", |
| 303 | "state": "{ |
| 304 | if(!!$next_accounting_time_triggered) |
| 305 | { |
| 306 | var["total_interest_to_distribute"] = $total_interest_to_distribute; |
| 307 | var["total_interest_next_accounting"] = 0; |
| 308 | var["pool_balance"] -= $cods_available_from_pool; |
| 309 | var["cods available_from_pool"] = |
| 310 | round(json_parse(var["pool_balance"]) * $INTEREST_PERCENTAGE_FROM_POOL); |
| 311 | var["distributing_time"] = var["next_accounting_time"]; |
| 312 | var["next_accounting_time"] = timestamp_to_string( |
| 313 | parse_date(var["next_accounting_time"]) + $ACCOUNTING_PERIOD); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | var["total_interest_next_accounting"] = $total_interest_next_accounting - $predicted_interest; |
| 318 | } |
| 319 | var[$address_name||"_distributed_time"] = var["distributing_time"]; |
| 320 | var[$address_name||"_interest"] += $user_interest_in_cods; |
| 321 | |
| 322 | |
| 323 | response["message"] = "Your "||$user_cods||" CODs have been sent to you, you have now."; |
| 324 | }" |
| 325 | } |
| 326 | ] |
| 327 | }, |
| 328 | { |
| 329 | "if": "{ !!trigger.data.buy_some_cods_for_test }", |
| 330 | "init": "{ |
| 331 | $buying_amount = round(trigger.output[[asset=base]].amount/100); |
| 332 | }", |
| 333 | "messages": [ |
| 334 | { |
| 335 | "app": "payment", |
| 336 | "payload": { |
| 337 | "asset": "{ var["cods_id"] }", |
| 338 | "outputs": [ |
| 339 | { |
| 340 | "address": "{ trigger.address }", |
| 341 | "amount": "{ $buying_amount }" |
| 342 | } |
| 343 | ] |
| 344 | } |
| 345 | }, |
| 346 | { |
| 347 | "app": "state", |
| 348 | "state": "{ |
| 349 | var["pool_balance"] -= $buying_amount; |
| 350 | response["message"] = "You just bough "||$buying_amount||" CODs."; |
| 351 | }" |
| 352 | } |
| 353 | ] |
| 354 | }, |
| 355 | { |
| 356 | "messages": [ |
| 357 | { |
| 358 | "app": "state", |
| 359 | "state": "{ |
| 360 | bounce($howto); |
| 361 | }" |
| 362 | } |
| 363 | ] |
| 364 | } |
| 365 | ] |
| 366 | } |
| 367 | } |
| 368 | ] |