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