[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$INVESTMENT_CUT = 0.01; //We take a 1% cut from all maecenas investments
$RESERVE_AMOUNT = 200000; //We will always keep > 200.000 in the AA
if (NOT trigger.data["method"])
bounce("method field is mandatory");
$method = trigger.data["method"];
$spendableFunds = balance["base"] - var["locked"] - storage_size - $RESERVE_AMOUNT;
$owner = var["owner"];
$calculateSharesToBuy = ($price, $pool, $myShare)=>ln($price / ($pool * $myShare)) / ln($myShare);
$_maecenasCurve = $x=>6969420 * $x^2;
}",
"getters": "{
/*
** Attested profile (or just the address if not found)
*/
$profileOf = $artist=>{
$att = attestation[[attestors=this_address, address=$artist, ifseveral='last', type="auto"]]["data"];
$ret = ($att ? json_parse($att) : {}) || {address: $artist};
return $ret;
};
$artistInfoOf = $artist=>var["artist_" || $artist];
}",
"messages": {
"cases": [
{
"if": "{
NOT $owner
}",
"messages": [
{
"app": "state",
"state": "{
var["owner"] = "
IUU43O7TS2TBYKAPGKUARDZHOTAE275A"; //Set the Owner to my address
var["helper"] = "
IUU43O7TS2TBYKAPGKUARDZHOTAE275A";
var["locked"] = 0; //Bytes locked in investments are not withdrawable by the Owner
}"
}
]
},
{
"if": "{
trigger.address == $owner
AND ($method == "payout"
OR $method == "transferOwnership"
OR $method == "setHelper"
OR $method == "setAA"
OR $method == "delAA"
OR $method == "unverify")
}",
"messages": {
"cases": [
{
"if": "{
$method == "payout"
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$spendableFunds}"
}
]
}
}
]
},
{
"if": "{
$method == "unverify"
}",
"init": "{
if (NOT trigger.data["artist"])
bounce("artist field is mandatory");
$_ = $profileOf(trigger.data["artist"]);
$oldUsername = $_["username"];
$prof = $_ || {verified: false, username: false};
}",
"messages": [
{
"app": "attestation",
"init": "{
$profileString = json_stringify($prof);
if (length($profileString) > 4096)
bounce("The profile is too long, the user has to remove some fields and retry verification");
}",
"payload": "{
{
address: trigger.data["artist"],
profile: {
data: $profileString
}
}
}"
},
{
"app": "state",
"state": "{
var["user_" || $oldUsername] = false; //Free username
}"
}
]
},
{
"if": "{
$method == "transferOwnership"
}",
"init": "{
if (NOT trigger.data["newOwner"])
bounce("newOwner field is mandatory");
if (NOT is_valid_address(trigger.data["newOwner"]))
bounce("newOwner field is not a valid address");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$spendableFunds}"
}
]
}
},
{
"app": "state",
"state": "{
var["owner"] = trigger.data["newOwner"];//Ownership is transferred
}"
}
]
},
{
"if": "{
$method == "setHelper"
}",
"init": "{
if (NOT trigger.data["helper"])
bounce("helper field is mandatory");
}",
"messages": [
{
"app": "state",
"state": "{
var["helper"] = trigger.data["helper"];
}"
}
]
},
{
"if": "{
trigger.data["method"] == "setAA"
}",
"init": "{
if (NOT is_valid_address(trigger.data["AA"]))
bounce("AA is not a valid address");
}",
"messages": [
{
"app": "state",
"state": "{
var["AA_" || trigger.data["AA"]] = true;
}"
}
]
},
{
"if": "{
trigger.data["method"] == "delAA"
}",
"init": "{
if (NOT var["AA_" || trigger.data["AA"]])
bounce("AA is not a valid address");
}",
"messages": [
{
"app": "state",
"state": "{
var["AA_" || trigger.data["AA"]] = false;
}"
}
]
}
]
}
},
{
"if": "{
(trigger.address == $owner
OR trigger.address == var["helper"])
AND $method == "verifyProfile"
}",
"init": "{
if (NOT trigger.data["artist"])
bounce("artist field is mandatory");
$prof = $profileOf(trigger.data["artist"]);
}",
"messages": [
{
"app": "attestation",
"payload": "{
{
address: trigger.data["artist"],
profile: { //Wrapper to be able to query the whole object at once since you can only query attestations by fields
data: json_stringify($prof || {verified: true})
}
}
}"
},
{
"app": "state",
"state": "{
var["user_" || $prof.username] = trigger.data["artist"]; //Username is now taken
}"
}
]
},
{
"if": "{
$method == "DIVEST"
}",
"init": "{
$artist = $profileOf(trigger.data["artist"]);
if (NOT trigger.data["artist"])
bounce("artist field is mandatory");
if (NOT is_valid_address(trigger.data["artist"]))
bounce("artist's address is invalid");
if (NOT $artist)
bounce("That artist never accepted maecenas");
if (NOT exists(trigger.data["amount"]))
bounce("amount field is mandatory");
if (NOT is_integer(trigger.data["amount"]) OR trigger.data["amount"] <= 0)
bounce("amount must be an integer > 0");
$investedAmount = var["maecenas_" || trigger.address || "_" || trigger.data["artist"]];
if (NOT $investedAmount)
bounce("You have no shares of that artist");
if (trigger.data["amount"] > $investedAmount)
bounce("You only have " || $investedAmount || " shares of that artist");
$yourShare = floor($artist.share * trigger.data["amount"] / $artist.supply, 0);
if ($yourShare < 20000)
bounce("It is not worth divesting your share");
$allowsMaecenas = ($artist.sharePercent >= 0.01) ? true : false;
$hasMaecenas = $allowsMaecenas
? $artist.supply > 0
? true
: false
: false;
if ($allowsMaecenas){
$maecenasShare = 0;
$sharesInThePool = $artist.supply - trigger.data["amount"]; //If divesting we need to substract divested shares
$p = $artist.share - floor((trigger.data["amount"] / $artist.supply) * $artist.share, 0); //Current share minus what was just divested
$k = 1 / ($sharesInThePool + 1); //The fraction of the pool 1 share represents
$price = $_maecenasCurve($sharesInThePool + 1); //Price of buying one share
$totalSpendable = floor($spendableFunds + $price * $INVESTMENT_CUT, 0);
$sharesToBuy = $sharesInThePool == 0 ? 0 : ceil($calculateSharesToBuy($price, $p, $k), 0);//Can be 0 and I would still afford 1 share if profitable
if ($k == 1){
$sharesICanAfford = $price < $totalSpendable ? 1 : 0;
}
else {
if ($totalSpendable > $price AND $sharesToBuy >= 1){
$sharesICanAfford = floor($totalSpendable / ($price * $sharesToBuy * (1 - $INVESTMENT_CUT)), 0); //Do not buy more shares than what I can afford
}
else{
$sharesICanAfford = 0;
}
}
}
if ($allowsMaecenas AND $sharesICanAfford > 0){
$buyShares = true;
}
else{
$buyShares = false;
}
$shareIWillfinallyBuy = min($sharesToBuy, $sharesICanAfford); //Maybe I can afford more shares than what is profitable to buy
$myProfit = floor($buyShares ? $p - $p * $k^$shareIWillfinallyBuy : 0, 0); //0 if no shares are bought
$poolFinalValue = $p - $myProfit; //Not actual profit but what I got out of the pool
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$yourShare - 10000}"
}
]
}
},
{
"app": "state",
"state": "{
var["artist_" || trigger.data["artist"]] ||= {
share: $artist.share - $yourShare, //Decrease the share by the amount just divested
supply: $artist.supply - trigger.data["amount"] //Decrease the supply by the shares used to divest
};
var["maecenas_" || trigger.address || "_" || trigger.data["artist"]] -= trigger.data["amount"];
var["locked"] -= ($yourShare + $myProfit);
}"
}
]
},
{
"if": "{
$method == "ENABLE_INVESTING"
}",
"init": "{
if (var["artist_" || trigger.address])
bounce("You are already accepting maecenas");
if (NOT trigger.data["share"])
bounce("share field is mandatory");
if (NOT is_integer(trigger.data["share"]) OR trigger.data["share"] < 1 OR trigger.data["share"] > 50)
bounce("share must be an integer between 1 - 50");
if (exists(trigger.data["selfBuy"])){
if (NOT is_integer(trigger.data["selfBuy"]))
bounce("selfBuy field must be an integer");
if (trigger.data["selfBuy"] < 0)
bounce("selfBuy must be a positive integer");
$cost = (1161570 * trigger.data["selfBuy"] * (1 + trigger.data["selfBuy"]) * (1 + 2 * trigger.data["selfBuy"])) * $INVESTMENT_CUT;
if (trigger.output[[asset="base"]].amount < $cost)
bounce("You need to send at least " || $cost || " bytes");
}
}",
"messages": [
{
"app": "state",
"state": "{
var["artist_" || trigger.address] = {
supply: trigger.data["selfBuy"] OTHERWISE 0,
share: 0, //Pooled amount availabe to investors
sharePercent: trigger.data["share"] / 100
};
if (trigger.data["selfBuy"])
var["maecenas_" || trigger.address || "_" || trigger.address] = trigger.data["selfBuy"];
}"
}
]
},
{
"if": "{
$method == "DISABLE_INVESTING"
}",
"init": "{
$info = $profileOf(trigger.address);
if (NOT $info)
bounce("You are already not accepting maecenas");
if ($info.supply > 0)
bounce("You cannot stop accepting maecenas right now as you already have some");
}",
"messages": [
{
"app": "state",
"state": "{
var["artist_" || trigger.address] = false;
}"
}
]
},
{
"if": "{
$method == "INVEST"
}",
"init": "{
if (NOT trigger.data["artist"])
bounce("artist field is mandatory");
$artist = $profileOf(trigger.data["artist"]);
if (NOT $artist)
bounce("That artist is not accepting maecenas");
if (NOT trigger.data["amount"])
bounce("amount field is mandatory");
if (NOT is_integer(trigger.data["amount"]) OR trigger.data["amount"] <= 0)
bounce("amount must be a positive integer");
$x0 = $artist.supply;
$x1 = $artist.supply + trigger.data["amount"];
$upToFirstToken = 1161570 * $x0 * (1 + $x0) * (1 + 2 * $x0);
$upToTargetPurchase = 1161570 * $x1 * (1 + $x1) * (1 + 2 * $x1);
$totalCost = round($upToTargetPurchase - $upToFirstToken, 0);
if (trigger.output[[asset="base"]].amount < $totalCost)
bounce("Your payment is not enough for that purchase");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.data["artist"]}",
"amount": "{floor($totalCost * (1 - $INVESTMENT_CUT) - 10000, 0)}"
}
]
}
},
{
"app": "state",
"state": "{
var["artist_" || trigger.data["artist"]] ||= {supply: $artist.supply + trigger.data["amount"]};
if (var["maecenas_" || trigger.address || "_" || trigger.data["artist"]])
var["maecenas_" || trigger.address || "_" || trigger.data["artist"]] += trigger.data["amount"];
else
var["maecenas_" || trigger.address || "_" || trigger.data["artist"]] = trigger.data["amount"];
}"
}
]
},
{
"if": "{
$method == "PROFILE"
}",
"init": "{
$prof = $profileOf(trigger.address);
$oldUsername = $prof.username;
$newUsername = trigger.data["username"] OTHERWISE $oldUsername;
if (NOT $oldUsername){
if (NOT trigger.data["username"])
bounce("username field is mandatory");
}
if (var["user_" || $newUsername]){
if ($oldUsername){
if ($oldUsername != $newUsername){
bounce("username already taken by a verified user");
}
}
}
if (exists(trigger.data["username"]) OR exists(trigger.data["name"]) OR exists(trigger.data["company"]))
$unverify = true;
else
$unverify = false;
if ($prof)
$profileObj = $prof || trigger.data || {username: $newUsername, verified: $unverify};
else
$profileObj = trigger.data || {username: $newUsername, verified: $unverify};
delete($profileObj, "method");
}",
"messages": [
{
"app": "attestation",
"init": "{
$profileString = json_stringify($profileObj);
if (length($profileString) > 4096)
bounce("Your profile is too big, try removing some fields");
}",
"payload": "{
{
address: trigger.address,
profile: {
data: $profileString
}
}
}"
},
{
"app": "state",
"state": "{
//var["profile_" || trigger.address] = $profileObj;
if ($oldUsername){
if ($oldUsername != $newUsername)
var["user_" || $oldUsername] = false; //Free old username
}
//Do not lock new username here. It will be locked upon verification to prevent username squatting
}"
}
]
},
{
"if": "{
$method == "addShare"
}",
"init": "{
//Failsafe measures against a bug being found on one of our AAs
if (NOT var["AA_" || trigger.address])
bounce("You have no power here!");
}",
"messages": [
{
"app": "state",
"state": "{
$current = $artistInfoOf(trigger.data["artist"]);
var[trigger.data["artist"]] = $current || {share: ($current.share OTHERWISE 0) + trigger.output[[asset="base"]].amount};
}"
}
]
}
]
}
}
]