Definition: [
"autonomous agent",
{
"doc_url": "https://oswap.io/initial-sale-pool.json",
"getters": "{
$get_prices = () => {
$r = var['total'];
$s = params.token_aa#7.$get_tokens($r);
$final_price = params.token_aa#7.$get_price($s);
$avg_price = $r/$s;
{final_price: $final_price, avg_price: $avg_price}
};
}",
"init": "{
$reserve_asset = params.reserve_asset OTHERWISE 'base';
$token_aa = params.token_aa;
$buy_freeze_period = params.buy_freeze_period OTHERWISE 1; // in days
$buy_freeze_period_in_seconds = $buy_freeze_period * 24 * 3600;
$launch_date = var['launch_date'] OTHERWISE params.launch_date;
$launch_ts = parse_date($launch_date);
// reserve
$min_contribution = ($reserve_asset == 'base') ? 99999 : 0;
$network_fee = ($reserve_asset == 'base') ? 1000 : 0;
if (trigger.data.to AND !is_valid_address(trigger.data.to))
bounce("bad to address");
$to = trigger.data.to OTHERWISE trigger.address;
}",
"messages": {
"cases": [
{
"if": "{ trigger.output[[asset=$reserve_asset]] > $min_contribution }",
"init": "{
$ok = timestamp < $launch_ts - $buy_freeze_period_in_seconds;
if (!$ok){
$error = "no more deposits accepted";
if (!trigger.data.soft_bounce)
bounce($error);
}
}",
"messages": [
{
"if": "{$error}",
"app": "payment",
"payload": {
"asset": "{$reserve_asset}",
"outputs": [
{
"address": "{$to}",
"amount": "{trigger.output[[asset=$reserve_asset]] - 10000}"
}
]
}
},
{
"app": "state",
"state": "{
if ($error) return;
$amount = trigger.output[[asset=$reserve_asset]] - $network_fee;
var['user_'||$to] += $amount;
var['total'] += $amount;
response['added'] = $amount;
}"
}
]
},
{
"if": "{ trigger.data.withdraw }",
"init": "{
require(timestamp < $launch_ts, "withdrawals not allowed any more");
$balance = var['user_'||trigger.address];
require($balance, "you have no balance");
$amount = trigger.data.amount OTHERWISE $balance;
require($amount <= $balance, "amount cannot exceed your balance "||$balance);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$reserve_asset}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['user_'||trigger.address] -= $amount;
var['total'] -= $amount;
response['withdrawn'] = $amount;
}"
}
]
},
{
"if": "{ trigger.data.buy }",
"init": "{
require(timestamp >= $launch_ts, "too early");
require(!var['tokens'], "already bought");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$reserve_asset}",
"outputs": [
{
"address": "{$token_aa}",
"amount": "{var['total'] + $network_fee}"
}
]
}
},
{
"app": "state",
"state": "{
response['message'] = 'bought';
}"
}
]
},
{
"if": "{ trigger.address == $token_aa }",
"init": "{
$constants = var[$token_aa]['constants'];
$tokens = trigger.output[[asset=$constants.asset]];
require($tokens > 0, "no tokens received");
require(!var['tokens'], "already bought");
}",
"messages": [
{
"app": "state",
"state": "{
var['tokens'] = $tokens;
response['tokens'] = $tokens;
}"
}
]
},
{
"if": "{ trigger.data.stake AND trigger.data.group_key AND trigger.data.percentages }",
"init": "{
require(var['tokens'], "not bought yet");
$balance = var['user_'||trigger.address];
require($balance, "you have no balance");
$amount = floor($balance / var['total'] * var['tokens']);
$constants = var[$token_aa]['constants'];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$constants.asset}",
"outputs": [
{
"address": "{$token_aa}",
"amount": "{$amount}"
}
]
}
},
{
"app": "data",
"payload": {
"stake": 1,
"term": "{4*360}",
"group_key": "{trigger.data.group_key}",
"percentages": "{trigger.data.percentages}",
"to": "{trigger.address}"
}
},
{
"app": "state",
"state": "{
var['user_'||trigger.address] = false;
response['sent'] = $amount;
}"
}
]
},
{
"if": "{ params.admin_address AND trigger.address == params.admin_address AND trigger.data.launch_date }",
"init": "{
require(!$launch_ts OR timestamp < $launch_ts, "already launched");
$new_launch_ts = parse_date(trigger.data.launch_date);
require($new_launch_ts > 0, "invalid date");
require(timestamp < $new_launch_ts - $buy_freeze_period_in_seconds, "new launch date should be at least buy freeze period in the future");
}",
"messages": [
{
"app": "state",
"state": "{
var['launch_date'] = trigger.data.launch_date;
response['new launch date'] = trigger.data.launch_date;
}"
}
]
}
]
}
}
]