[
"autonomous agent",
{
"getters": "{
$getPrice = ()=>{
$priceStr = data_feed[[oracles=params.issuer, feed_name=this_address, ifseveral="last", type="string"]];
$chunks = split($priceStr, " ");
$price = $chunks[0];
return $price;
};
$getPriceAsset = ()=>{
$priceStr = data_feed[[oracles=params.issuer, feed_name=this_address, ifseveral="last", type="string"]];
$chunks = split($priceStr, " ");
$asset = $chunks[0];
return $asset;
};
$getSales = ()=>var["sales"];
}",
"init": "{
$CT_ADDRESS = "
ZIT3YPDTLGAO7MVO2ONNDMSEYS2VELX6";
$OWNER = params.issuer;
$LICENSE_ASSET = var["licenseAsset"];
if ($LICENSE_ASSET){
$priceStr = data_feed[[oracles=$OWNER, feed_name=this_address, ifseveral="last", type="string"]];
$chunks = split($priceStr, " ");
require(length($chunks) == 2, "Malformed data_feed. It should have only 2 items");
$PRICE = $chunks[0];
require(is_integer($PRICE), "Price must be an integer");
require($PRICE > 0, "Price must be positive");
require(is_valid_amount($PRICE), "Price must be a valid amount");
$PRICE_ASSET = $chunks[1];
require(asset[$PRICE_ASSET].exists, "Price asset does not exist");
}
$method = trigger.data["method"];
response["method"] = $method;
response["who"] = trigger.data["to"] OTHERWISE trigger.address;
$CUT = 0.03;
if ($LICENSE_ASSET AND NOT $method)
bounce("method field is mandatory");
}",
"messages": {
"cases": [
{
"if": "{NOT $LICENSE_ASSET}",
"init": "{
response["method"] = "initialize";
}",
"messages": [
{
"if": "{params.cap}",
"app": "asset",
"payload": {
"is_private": false,
"cap": "{params.cap OTHERWISE 0}",
"is_transferrable": "{NOT NOT params.tradeable}",
"auto_destroy": false,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"if": "{NOT params.cap}",
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": "{NOT NOT params.tradeable}",
"auto_destroy": false,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var["licenseAsset"] = response_unit;
}"
}
]
},
{
"if": "{trigger.address == $OWNER AND $method == "payout"}",
"messages": {
"cases": [
{
"if": "{$method == "payout"}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{trigger.data.asset OTHERWISE $PRICE_ASSET}",
"outputs": [
{
"address": "{$OWNER}"
}
]
}
}
]
}
]
}
},
{
"if": "{$method == "buy"}",
"init": "{
$copies = floor(trigger.output[[asset=$PRICE_ASSET]]/$PRICE, 0);
require($copies > 0, "You cannot buy less than 1 license");
$hasReferral = (trigger.data["r"] AND is_valid_address(trigger.data["r"]));
$cut = $hasReferral ? $CUT/2 : $CUT;
$cutAmount = round(trigger.output[[asset=$PRICE_ASSET]] * $cut, 0);
response["amount"] = $copies;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$LICENSE_ASSET}",
"outputs": [
{
"address": "{trigger.data["to"] OTHERWISE trigger.initial_address}",
"amount": "{$copies}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$PRICE_ASSET}",
"outputs": [
{
"address": "{$CT_ADDRESS}",
"amount": "{$cutAmount}"
},
{
"if": "{$hasReferral}",
"address": "{trigger.data["r"]}",
"amount": "{$cutAmount}"
}
]
}
},
{
"app": "state",
"state": "{
var["sales"]+=1;
}"
}
]
}
]
}
}
]