[
"autonomous agent",
{
"bounce_fees": {
"base": 11000
},
"init": "{
$user = trigger.address;
$is_aa = $user == this_address;
$received_asset = trigger.output[[asset!=base]].asset;
$received_amount = trigger.output[[asset!=base]].amount;
}",
"messages": {
"cases": [
{
"if": "{!var["def_asset_hash"] }",
"init": "{
// if the creation of the asset have started,
// only the creator can interract with tha AA until the asset is defined
if (!!var["def_creator"])
if (var["def_creator"] != $user)
bounce("The asset is being created, and you are not the creator, try again later!");
$previous_underlying_asset_hash = var["previous_underlying_asset"] otherwise "first";
$received_underlying_asset_hash = $received_asset ;
$received_underlying_asset_amount = $received_amount;
}",
"messages": [
{
"app": "state",
"state": "{
var["def_creator"] = $user;
// store in the definition, the quantity of asset
var["def_"||$received_underlying_asset_hash||"_amount"] = $received_underlying_asset_amount;
// create the next chain link
var["def_"||$previous_underlying_asset_hash] = $received_underlying_asset_hash;
var["previous_underlying_asset"] = $received_underlying_asset_hash;
}"
}
]
},
{
"if": "{!!trigger.data.define}",
"init": "{
// if the creation of the asset have started,
// only the creator can interract with tha AA until the asset is defined
if (!!var["def_creator"])
{
if (var["def_creator"] != $user)
bounce("Only the creator of the asset is allowed to define it!");
}
// an asset requires at least 2 underlying assets
if (!!var["first"])
{
$first_chain_link = var["first"];
if (!var[$first_chain_link])
bounce("an asset requires at least 2 underlying assets, please send the second underlying asset in the desired amount!");
}
else
bounce("To create a new asset, start sending the wanted amount of the first underlying asset!");
$asset_name = trigger.data.define;
$previous_underlying_asset = var["previous_underlying_asset"];
}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": true,
"auto_destroy": true,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
response["defined"] = $asset_name ||" is defined as "||response_unit;
var["def_asset_hash"] = response_unit;
var["def_asset_name"] = trigger.data.define;
}"
}
]
},
{
"if": "{!!var["def_creator"] and var["def_creator"] == $user}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{var["def_asset_hash"]}",
"outputs": [
{
"address": "{var["def_creator"] }",
"amount": "{ 1 }"
}
]
}
},
{
"app": "state",
"state": "{
response["defined"] = $asset_name ||"Creator got one asset and do not have any role to play";
var["def_creator"] = false; // we can remove the creator
}"
}
]
},
{
"if": "{($received_asset == var["def_asset_hash"]) or $is_aa}",
"init": "{
if (!$is_aa)
{
$requester = $user;
$underlying_asset_hash = var["def_first"];
$amount = $received_amount;
}
else
{
$requester = trigger.data.aa_reserved_requester;
$underlying_asset_hash = trigger.data.aa_reserved_underlying_asset_hash;
$amount = trigger.data.aa_reserved_asset_amount;
}
// use the link chain to get the next underlying asset
$next_underlying_asset_hash = var["def_"||$underlying_asset_hash];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$underlying_asset_hash }",
"outputs": [
{
"address": "{$requester}",
"amount": "{$amount * var["def_"||$underlying_asset_hash]||"_amount"}"
}
]
}
},
{
"if": "{!!$next_underlying_asset_hash}",
"app": "data",
"payload": {
"aa_reserved_requester": "{$requester}",
"aa_reserved_underlying_asset_hash": "{$next_underlying_asset_hash}",
"aa_reserved_asset_amount": "{$amount}"
}
},
{
"if": "{!!$next_underlying_asset_hash}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{this_address}",
"amount": "{ 1 }"
}
]
}
}
]
},
{
"if": "{ // send the first unerlying asset or the one needed
(!!var[$user||"_underlying_asset"] and ($received_asset == var[$user||"_underlying_asset"])) OR
( !var[$user||"_underlying_asset"] and ($received_asset == var["def_first"]))
}",
"init": "{
if (!!var[$user||"_underlying_asset"])
{
$underlying_asset_hash = var[$user||"_underlying_asset"];
$etf_amount = var[$user||"_etf_amount"];
$needed_mount = $etf_amount * var["def_"||$underlying_asset_hash||"_amount"];
if ($received_amount != $needed_mount)
bounce ("you should send "||$needed_mount|| " of "||$underlying_asset_hash);
}
else
{
$underlying_asset_hash = var["def_first"];
$needed_mount = var["def_"||$underlying_asset_hash||"_amount"];
//check if is a multiple of the first underlying asset amount
if (($received_amount % $needed_mount) != 0)
bounce ("you should send a multiple of "||$needed_mount|| " of "||$underlying_asset_hash);
else
$etf_amount = $received_amount / $needed_mount;
}
// use the link chain to get the next underlying asset
$next_underlying_asset_hash = var["def_"||$underlying_asset_hash];
$all_underlying_asset_received = !$next_underlying_asset_hash;
}",
"messages": [
{
"if": "{$all_underlying_asset_received}",
"app": "payment",
"payload": {
"asset": "{$underlying_asset_hash }",
"outputs": [
{
"address": "{$requester}",
"amount": "{$amount * var["def_"||$underlying_asset_hash]||"_amount"}"
}
]
}
},
{
"app": "state",
"state": "{
if ($all_underlying_asset_received)
{
var[$user||"_etf_amount"] = false;
var[$user||"_underlying_asset"] = false;
response["processed"] = var["def_asset_name"]||"sent";
}
else
{
var[$user||"_etf_amount"] = $etf_amount;
var[$user||"_underlying_asset"] = $next_underlying_asset_hash;
response["in progress"] = $underlying_asset_hash ||"received and waiting for "||$next_underlying_asset_hash;
}
}"
}
]
}
]
}
}
]