[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$aa_name = "CODAA";
$howto = "check out: https://certificateofdeposit.000webhostapp.com/";
$MINUTES = 60;
$HOUR = 60 * $MINUTES;
$DAY = 24 * $HOUR;
$WEEK = 7 * $DAY;
$YEAR = 364.25 * $DAY;
$TOTAL_CODS_AMOUNT=1e15;
// BONUS RELATED STUFF
// There is a give away period for people ready to stake their bytes:
$GIVEAWAY_PERIOD = 10 * $MINUTES;
// at the beginning of the give away period the bonus is max:
$MAX_BONUS_PERCENTAGE = 200;
// then after every period the bonus is reducted of given percentage:
$BONUS_REDUCTION_PERIOD = 1 * $MINUTE; // irl must be 1 week or 1 day
$BONUS_REDUCTION_PERCENTAGE = 20; // irl must be 0.5% - 2%
// ACCOUNTING RELATED STUFF
// Every given time interest should be computed and paied by each user
$ACCOUNTING_PERIOD = 10* $MINUTES; // irl 1 * $YEAR;
// from the pool of unclaim coins, we can allow a percentage per accountign period
$INTEREST_PERCENTAGE_FROM_POOL = 4; // 4% per year
// load state concerning the CODAA
$total_interest_next_accounting = json_parse(var["total_interest_next_accounting"]) otherwise 0;
$pool_balance = json_parse(var["pool_balance"]) otherwise $TOTAL_CODS_AMOUNT;
// load state concerning the user
$account_name = trigger.data.account_name otherwise bounce ("Please specify an account name");
$address_name = trigger.address||"_"||$account_name;
$user_bytes = json_parse(var[$address_name||"_bytes"]) otherwise 0;
$user_cods = json_parse(var[$address_name||"_cods"]) otherwise 0;
$user_stake_start = parse_date(var[$address_name||"_stake_start"]) otherwise timestamp;
$user_predicted_lock_time_in_sec = json_parse(var[$address_name||"_predicted_lock_time_in_sec"]) otherwise trigger.data.lock_time;
$user_predicted_interest = var[$address_name||"_predicted_interest"] otherwise 0;
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.create}",
"init": "{
}",
"messages": [
{
"app": "asset",
"payload": {
"cap": "{$TOTAL_CODS_AMOUNT}",
"is_private": false,
"is_transferrable": true,
"fixed_denominations": false,
"auto_destroy": false,
"cosigned_by_definer": false,
"issued_by_definer_only": true,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var["cods_id"] = response_unit;
var["block_until"] = timestamp_to_string(timestamp + $GIVEAWAY_PERIOD);
var["distributing_time"] = timestamp_to_string(timestamp);
var["next_accounting_time"] = timestamp_to_string(timestamp + $ACCOUNTING_PERIOD);
var["total_interest_to_distribute"] = 0;
var["total_interest_next_accounting"] = 0;
var["pool_balance"] = $TOTAL_CODS_AMOUNT;
}"
}
]
},
{
"if": "{ !!trigger.data.claim }",
"init": "{
if (!!$user_bytes or !!$user_cods)
bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !");
if (!trigger.data.lock_time)
bounce("Set lock time");
// Amount received to be blocked to receive free coins
$received_bytes = trigger.output[[asset=base]].amount;
// free coin computation
$remaining_time = parse_date(var["block_until"]) - timestamp;
if ($remaining_time<0)
bounce ("Sorry, The free CODs offering is finished !");
$elapsed_time = $GIVEAWAY_PERIOD - $remaining_time;
$elapsed_periods = round($elapsed_time/$BONUS_REDUCTION_PERIOD);
$remaining_days = round($remaining_time/$DAY);
$bonus = $MAX_BONUS_PERCENTAGE - ($elapsed_days * $BONUS_REDUCTION_PERCENTAGE);
$unfair_free_cods = $received_bytes * ($bonus / 100);
// robin hood effect, steal the riches and give to the ... pool, 1Gb = 100% 10Gb = 88%, 72, 56,40,24,8%
$uncorrected_robin_percentage = -0.07 * ln($received_bytes) +2.5;
// if more than 100% bring it back to 100%
$robin_percentage = $uncorrected_robin_percentage>1 ? 1 : $uncorrected_robin_percentage;
$free_cods = round($unfair_free_cods * $robin_percentage);
$predicted_interest = $received_cods * (0.7 * ln($user_predicted_lock_time_in_sec) + 1.5); // longer stake => bigger return
}",
"messages": [
{
"app": "state",
"state": "{
var["total_interest_next_accounting"] = $total_interest_next_accounting + $predicted_interest;
var["pool_balance"] = $pool_balance - $free_cods;
var[$address_name||"_bytes"] = $received_bytes;
var[$address_name||"_cods"] = $free_cods;
var[$address_name||"_predicted_interest"] = $predicted_interest;
var[$address_name||"_distributed_time"] = timestamp_to_string(timestamp);
var[$address_name||"_stake_start"] = timestamp_to_string($user_stake_start);
var[$address_name||"_predicted_lock_time_in_sec"] = $user_predicted_lock_time_in_sec;
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.";
}"
}
]
},
{
"if": "{ !!trigger.data.cancel_claim }",
"init": "{
if (!$user_bytes)
bounce ("Nothing to cancel ! are you using the right address ?");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var["total_interest_next_accounting"] = $total_interest_next_accounting - $user_predicted_interest;
var["pool_balance"] = $pool_balance + $user_cods;
var[$address_name||"_bytes"] = false; // back to user
var[$address_name||"_cods"] = false; // back to the pool
var[$address_name||"_predicted_interest"] = false;
var[$address_name||"_stake_start"] = false;
response["message"] = "Your requested your "||$user_bytes||" bytes back. You have lost your "||$user_cods||" CODs.";
}"
}
]
},
{
"if": "{ !!trigger.data.get_bytes_back }",
"init": "{
if (!$user_bytes)
bounce ($account_name||" with the address "||trigger.address||" do NOT exist !");
if ( timestamp < parse_date(var["block_until"]))
bounce ("Too early to get your bytes back, except if you ready to loose your CODs, in this case use 'cancel_claim'.");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{ $user_bytes + trigger.output[[asset=base]].amount - 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[trigger.address||"_"||$account_name||"_bytes"] = false;
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)||".";
}"
}
]
},
{
"if": "{ !!trigger.data.stake }",
"init": "{
if (trigger.output[[asset!=base]].asset != var["cods_id"] )
bounce ("You can only stake CODs ('"||$cods||"'.");
if (!!$user_cods)
bounce ($account_name||" for the address "||trigger.address||" already exist, use another address or another 'account_name' !");
$received_cods = trigger.output[[asset!=base]].amount;
$predicted_interest = $received_cods * (0.7 * ln($user_predicted_lock_time_in_sec) + 1.5); // longer stake => bigger return
}",
"messages": [
{
"app": "state",
"state": "{
var["total_interest_next_accounting"] = $total_interest_next_accounting + $predicted_interest;
var[$address_name||"_cods"] = $received_cods;
var[$address_name||"_predicted_interest"]= $predicted_interest;
var[$address_name||"_stake_start"] = timestamp_to_string($user_stake_start);
var[$address_name||"_predicted_lock_time_in_sec"] = $user_predicted_lock_time_in_sec;
var[$address_name||"_distributed_time"] = var["distributing_time"];
response["message"] = $received_cods||" CODs in stake until "||timestamp_to_string($user_stake_start + $user_predicted_lock_time_in_sec)||".";
}"
}
]
},
{
"if": "{ !!trigger.data.unstake }",
"init": "{
if (!$user_cods)
bounce ($account_name||" with the address "||trigger.address||" do NOT exist !");
if (!!$user_bytes)
bounce ("Cannot unstake while having bytes in the system, please use 'get_bytes_back' or 'cancel_claim'!");
$too_early = timestamp < $user_stake_start + $user_predicted_lock_time_in_sec;
if ( !!$too_early and !trigger.data.use_force )
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' !");
if (!!$too_early)
{
$amount_to_send = $user_cods;
}
else
{
$amount_to_send = $user_cods + var[$address_name||"_interest"];
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{ var["cods_id"] }",
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{ $amount_to_send }"
}
]
}
},
{
"app": "state",
"state": "{
var["total_interest_next_accounting"] = $total_interest_next_accounting - $user_predicted_interest;
var[$address_name||"_cods"] = false;
var[$address_name||"_stake_start"] = false;
var[$address_name||"_predicted_interest"] = false;
var[$address_name||"_predicted_lock_time_in_sec"] = false;
response["message"] = "Your "||$amount_to_send||" CODs have been sent to you.";
}"
}
]
},
{
"if": "{ !!trigger.data.do_accounting }",
"init": "{
if (!$user_cods)
bounce ($account_name||" with the address "||trigger.address||" do NOT exist !");
if (!!$user_bytes)
bounce ("Cannot do accounting while having bytes in the system, please use 'get_bytes_back' if the bonus period ended or 'cancel_claim'!");
if (var[$address_name||"_distributed_time"] == var["distributing_time"])
bounce ("You got your interest already, see you on "||var["next_accounting_time"]);
// accounting time? (removing from the pool, the needed coin reserved to pay interets)
$actual_next_accounting_time = parse_date(var["next_accounting_time"]);
$next_accounting_time_triggered = timestamp > $actual_next_accounting_time;
$cods_available_from_pool = json_parse(var["cods available_from_pool"]);
if (!!$next_accounting_time_triggered)
{
$total_interest_to_distribute = json_parse(var["total_interest_to_distribute"]);
}
// computing
$user_interest_in_parts =
json_parse(var[$address_name||"_predicted_interest"]) /
json_parse(var["total_interest_to_distribute"]);
$user_interest_in_cods = $user_interest_in_parts * $cods_available_from_pool;
}",
"messages": [
{
"app": "state",
"state": "{
if(!!$next_accounting_time_triggered)
{
var["total_interest_to_distribute"] = $total_interest_to_distribute;
var["total_interest_next_accounting"] = 0;
var["pool_balance"] -= $cods_available_from_pool;
var["cods available_from_pool"] =
round(json_parse(var["pool_balance"]) * $INTEREST_PERCENTAGE_FROM_POOL / 100);
var["distributing_time"] = var["next_accounting_time"];
var["next_accounting_time"] = timestamp_to_string(
parse_date(var["next_accounting_time"]) + $ACCOUNTING_PERIOD);
}
else
{
var["total_interest_next_accounting"] = $total_interest_next_accounting - $predicted_interest;
}
var[$address_name||"_distributed_time"] = var["distributing_time"];
var[$address_name||"_interest"] += $user_interest_in_cods;
response["message"] = "Your "||$user_cods||" CODs have been sent to you, you have now.";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
bounce($howto);
}"
}
]
}
]
}
}
]