| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 11000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | $aa_name = "CODAAA"; |
| 9 | $aa_asset_creation_and_issuing = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; |
| 10 | |
| 11 | $DAY = 60 * 60 * 24; |
| 12 | $WEEK = 7 * $DAY; |
| 13 | $YEAR = 364.25 * $DAY; |
| 14 | |
| 15 | $TOTAL_CODS_AMOUNT=1e15; |
| 16 | |
| 17 | $DEBUG = true; |
| 18 | |
| 19 | $GIVEAWAY_PERIOD = $DEBUG ? 15*60 : 0.5 * $YEAR; |
| 20 | |
| 21 | $MAX_BONUS_P100 = 200; |
| 22 | |
| 23 | $DAILY_BONUS_REDUCTION_P100 = 1; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | $ACCOUNTING_PERIOD = 1 * $YEAR; |
| 31 | |
| 32 | $YEARLY_INTEREST_P100_FROM_POOL = 4 / 52; |
| 33 | |
| 34 | |
| 35 | $temp_next_accounting_time = json_parse(var["next_accounting_time"]) otherwise 0; |
| 36 | $temp_interest_next_accounting = json_parse(var["interest_next_accounting"]) otherwise 0; |
| 37 | $temp_pool_balance = json_parse(var["pool_balance"]) otherwise $TOTAL_CODS_AMOUNT; |
| 38 | |
| 39 | $account_name = trigger.data.account_name otherwise bounce ("Please specify an account name"); |
| 40 | $user_bytes = json_parse(var[trigger.address||"_"||$account_name||"_bytes"]) otherwise 0; |
| 41 | $user_cods = json_parse(var[trigger.address||"_"||$account_name||"_cods"]) otherwise 0; |
| 42 | $user_stake_start = json_parse(var[trigger.address||"_"||$account_name||"_stake_start"]) otherwise timestamp; |
| 43 | $user_predicted_lock_time = json_parse(var[trigger.address||"_"||$account_name||"_predicted_lock_time"]) otherwise trigger.data.lock_time; |
| 44 | |
| 45 | |
| 46 | |
| 47 | if ( timestamp > $temp_next_accounting_time ) |
| 48 | { |
| 49 | $cods_available_from_pool = round(json_parse(var["pool_balance"]) * $YEARLY_INTEREST_P100_FROM_POOL / 100); |
| 50 | $pool_balance = $temp_pool_balance - $cods_available_from_pool; |
| 51 | $next_accounting_time = $temp_next_accounting_time + $ACCOUNTING_PERIOD; |
| 52 | $interest_next_accounting = 0; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | $pool_balance = $temp_pool_balance; |
| 57 | $next_accounting_time = $temp_next_accounting_time; |
| 58 | $interest_next_accounting = $temp_interest_next_accounting; |
| 59 | } |
| 60 | }", |
| 61 | "messages": { |
| 62 | "cases": [ |
| 63 | { |
| 64 | "if": "{ !!trigger.data.create}", |
| 65 | "init": "{ |
| 66 | }", |
| 67 | "messages": [ |
| 68 | { |
| 69 | "app": "asset", |
| 70 | "payload": { |
| 71 | "cap": "{$TOTAL_CODS_AMOUNT}", |
| 72 | "is_private": false, |
| 73 | "is_transferrable": true, |
| 74 | "fixed_denominations": false, |
| 75 | "auto_destroy": false, |
| 76 | "cosigned_by_definer": false, |
| 77 | "issued_by_definer_only": true, |
| 78 | "spender_attested": false |
| 79 | } |
| 80 | }, |
| 81 | { |
| 82 | "app": "state", |
| 83 | "state": "{ |
| 84 | var["cods_id"] = response_unit; |
| 85 | var["block_until"] = timestamp + $GIVEAWAY_PERIOD; |
| 86 | var["block_until_nice"] = timestamp_to_string(timestamp + $GIVEAWAY_PERIOD); |
| 87 | var["next_accounting_time"] = timestamp + $ACCOUNTING_PERIOD; |
| 88 | var["pool_balance"] = $TOTAL_CODS_AMOUNT; |
| 89 | }" |
| 90 | } |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | "if": "{ !!trigger.data.claim }", |
| 95 | "init": "{ |
| 96 | |
| 97 | if (!!$user_bytes or !!$user_cods) |
| 98 | bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !"); |
| 99 | |
| 100 | |
| 101 | $received_bytes = trigger.output[[asset=base]].amount; |
| 102 | |
| 103 | |
| 104 | $remaining_time = json_parse(var["block_until"]) - timestamp; |
| 105 | if ($remaining_time<0) |
| 106 | bounce ("Sorry, The free CODs offering is finished !"); |
| 107 | $elapsed_time = $GIVEAWAY_PERIOD - $remaining_time; |
| 108 | $elapsed_days = round($elapsed_time/$DAY); |
| 109 | $remaining_days = round($remaining_time/$DAY); |
| 110 | $bonus = $MAX_BONUS_P100 - ($elapsed_days * $DAILY_BONUS_REDUCTION_P100); |
| 111 | $unfair_free_cods = $received_bytes * ($bonus / 100); |
| 112 | |
| 113 | |
| 114 | $free_cods = round($unfair_free_cods * (-0.07 * ln($received_bytes) +2.5)); |
| 115 | |
| 116 | $new_interest = $free_cods * (0.7 * ln($user_predicted_lock_time) + 1.5); |
| 117 | }", |
| 118 | "messages": [ |
| 119 | { |
| 120 | "app": "state", |
| 121 | "state": "{ |
| 122 | var["interest_next_accounting"] = $interest_next_accounting + $new_interest; |
| 123 | var["pool_balance"] = $pool_balance - $free_cods; |
| 124 | $key = trigger.address||"_"||$account_name; |
| 125 | var[$key||"_bytes"] = $received_bytes; |
| 126 | var[$key||"_cods"] = $free_cods; |
| 127 | var[$key||"_stake_start"] = $user_stake_start; |
| 128 | var[$key||"_predicted_lock_time"] = $user_predicted_lock_time; |
| 129 | |
| 130 | response["message"] = "Your "||$received_bytes||" bytes are safe with us until: "||var["block_until_nice"]||". You will receive "||$free_cods||" free CODs. You still have "||$remaining_days||" days to claim more CODs. Actual bonus is "||$bonus||"% of your bytes."; |
| 131 | }" |
| 132 | } |
| 133 | ] |
| 134 | }, |
| 135 | { |
| 136 | "if": "{ !!trigger.data.cancel_claim }", |
| 137 | "init": "{ |
| 138 | if (!$user_bytes) |
| 139 | bounce ("Nothing to cancel ! are you using the right address ?"); |
| 140 | $predicted_interest = $user_cods * (0.7 * ln($user_predicted_lock_time) + 1.5); |
| 141 | }", |
| 142 | "messages": [ |
| 143 | { |
| 144 | "app": "payment", |
| 145 | "payload": { |
| 146 | "asset": "base", |
| 147 | "outputs": [ |
| 148 | { |
| 149 | "address": "{trigger.address}", |
| 150 | "amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }" |
| 151 | } |
| 152 | ] |
| 153 | } |
| 154 | }, |
| 155 | { |
| 156 | "app": "state", |
| 157 | "state": "{ |
| 158 | var["interest_next_accounting"] = $interest_next_accounting - $predicted_interest; |
| 159 | var["pool_balance"] = $pool_balance + $user_cods; |
| 160 | |
| 161 | var[$key||"_bytes"] = false; |
| 162 | var[$key||"_cods"] = false; |
| 163 | var[$key||"_stake_start"] = false; |
| 164 | |
| 165 | response["message"] = "Your requested your "||$user_bytes||" bytes back. YOu have lost your "||$user_cods||" CODs."; |
| 166 | }" |
| 167 | } |
| 168 | ] |
| 169 | }, |
| 170 | { |
| 171 | "if": "{ !!trigger.data.get_bytes_back }", |
| 172 | "init": "{ |
| 173 | if (!$user_bytes) |
| 174 | bounce ($account_name||" with the address "||trigger.address||" do NOT exist !"); |
| 175 | if ( timestamp < var["block_until"] ) |
| 176 | bounce ("Too early to get your bytes back, except if you ready to loose your CODs, in this case use 'cancel_claim'."); |
| 177 | }", |
| 178 | "messages": [ |
| 179 | { |
| 180 | "app": "payment", |
| 181 | "payload": { |
| 182 | "asset": "base", |
| 183 | "outputs": [ |
| 184 | { |
| 185 | "address": "{ trigger.address }", |
| 186 | "amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }" |
| 187 | } |
| 188 | ] |
| 189 | } |
| 190 | }, |
| 191 | { |
| 192 | "app": "state", |
| 193 | "state": "{ |
| 194 | var[trigger.address||"_"||$account_name||"_bytes"] = false; |
| 195 | response["message"] = "Your "||$user_bytes||" bytes have been sent back to you, you have now "||$user_cods||" CODs staking since "||$user_stake_start||"."; |
| 196 | }" |
| 197 | } |
| 198 | ] |
| 199 | }, |
| 200 | { |
| 201 | "if": "{ !!trigger.data.stake }", |
| 202 | "init": "{ |
| 203 | if (trigger.output[[asset!=base]].asset != $cods ) |
| 204 | bounce ("You can only stake CODs ('"||$cods||"'."); |
| 205 | if (!!$user_cods) |
| 206 | bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !"); |
| 207 | $received_cods = trigger.output[[asset!=base]].amount; |
| 208 | $new_interest = $received_cods * (0.7 * ln($user_predicted_lock_time) + 1.5); |
| 209 | }", |
| 210 | "messages": [ |
| 211 | { |
| 212 | "app": "state", |
| 213 | "state": "{ |
| 214 | var["interest_next_accounting"] = $interest_next_accounting + $new_interest; |
| 215 | $key = trigger.address||"_"||$account_name; |
| 216 | var[$key||"_cods"] = $received_cods; |
| 217 | var[$key||"_stake_start"] = $user_stake_start; |
| 218 | var[$key||"_predicted_lock_time"] = $user_predicted_lock_time; |
| 219 | response["message"] = $received_cods||" CODs in stake until "||timestamp_to_string($user_stake_start + $user_predicted_lock_time)||"."; |
| 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; |
| 232 | if ( !!$too_early and !trigger.data.use_force ) |
| 233 | bounce ("Your defined staking period ends in "||($user_stake_start + $user_predicted_lock_time-timestamp)/24/60/60||" days. To force the unstaking and loose a big part of your interests use 'use_force' !"); |
| 234 | |
| 235 | |
| 236 | $predicted_interest = $user_cods * (0.7 * ln($user_predicted_lock_time) + 1.5); |
| 237 | }", |
| 238 | "messages": [ |
| 239 | { |
| 240 | "app": "state", |
| 241 | "state": "{ |
| 242 | var["interest_next_accounting"] = $interest_next_accounting - $predicted_interest; |
| 243 | $key = trigger.address||"_"||$account_name; |
| 244 | var[$key||"_cods"] = false; |
| 245 | var[$key||"_stake_start"] = false; |
| 246 | var[$key||"_predicted_lock_time"] = false; |
| 247 | response["message"] = "Your "||$user_cods||" CODs have been sent to you, you have now."; |
| 248 | }" |
| 249 | } |
| 250 | ] |
| 251 | }, |
| 252 | { |
| 253 | "init": "{ |
| 254 | if (!$user_cods) |
| 255 | bounce ($account_name||" with the address "||trigger.address||" do NOT exist !"); |
| 256 | |
| 257 | $too_early = timestamp < $user_stake_start + $user_predicted_lock_time; |
| 258 | |
| 259 | |
| 260 | $predicted_interest = $user_cods * (0.7 * ln($user_predicted_lock_time) + 1.5); |
| 261 | }", |
| 262 | "messages": [ |
| 263 | { |
| 264 | "app": "state", |
| 265 | "state": "{ |
| 266 | var["interest_next_accounting"] = 0; |
| 267 | |
| 268 | var["pool_balance"] = $pool_balance + $user_cods; |
| 269 | |
| 270 | $key = trigger.address||"_"||$account_name; |
| 271 | var[$key||"_cods"] = $received_cods; |
| 272 | var[$key||"_stake_start"] = $user_stake_start; |
| 273 | var[$key||"_predicted_lock_time"] = $user_predicted_lock_time; |
| 274 | response["message"] = $received_cods||" CODs in stake until "||timestamp_to_string($user_stake_start + $user_predicted_lock_time)||"."; |
| 275 | }" |
| 276 | } |
| 277 | ] |
| 278 | } |
| 279 | ] |
| 280 | } |
| 281 | } |
| 282 | ] |