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