[
"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["indice_hash"] AND !!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["temp_creator"])
{
if (var["temp_creator"] != $user)
bounce("Only the creator of the indice is allowed to define it!");
}
// an asset requires at least 2 underlying assets
if (!!var["internal_link_1"])
{
$first_chain_link = var["internal_link_1"];
if (!var["internal_link_"||$first_chain_link])
bounce("the indice requires at least 2 underlying assets, please send the second underlying asset in the desired amount!");
}
else
bounce("To create a new indice, start sending the wanted amount of the first underlying asset!");
$asset_name = trigger.data.define;
}",
"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["indice_hash"] = response_unit;
var["indice_name"] = $asset_name;
var["temp_previous_underlying_asset"] = false;
}"
}
]
},
{
"if": "{!var["indice_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["temp_creator"])
if (var["temp_creator"] != $user)
bounce("The indice is being created, and you are not the creator, try again later!");
$previous_underlying_asset_hash = var["temp_previous_underlying_asset"] otherwise "1";
$received_underlying_asset_hash = $received_asset ;
$received_underlying_asset_amount = $received_amount;
}",
"messages": [
{
"app": "state",
"state": "{
var["temp_creator"] = $user;
// store in the definition, the quantity of asset
var["amount_of_"||$received_underlying_asset_hash] = $received_underlying_asset_amount;
// create the next chain link
var["internal_link_"||$previous_underlying_asset_hash] = $received_underlying_asset_hash;
var["temp_previous_underlying_asset"] = $received_underlying_asset_hash;
response["defined"] = "underlying asset received, waiting for next one or data.define=<indice name>";
}"
}
]
},
{
"if": "{!!var["temp_creator"] and var["temp_creator"] == $user}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{var["indice_hash"]}",
"outputs": [
{
"address": "{var["temp_creator"] }",
"amount": "{ 1 }"
}
]
}
},
{
"app": "state",
"state": "{
response["defined"] = "Creator got the first token and do not have any role to play";
var["temp_creator"] = false; // we can remove the creator
}"
}
]
},
{
"if": "{($received_asset == var["indice_hash"]) or $is_aa}",
"init": "{
if (!$is_aa)
{
$requester = $user;
$underlying_asset_hash = var["internal_link_1"];
$amount = $received_amount;
}
else
{
$requester = trigger.data.aa_reserved_requester;
$underlying_asset_hash = trigger.data.aa_reserved_underlying_asset_hash;
$amount = json_parse(trigger.data.aa_reserved_asset_amount);
}
// use the link chain to get the next underlying asset
$next_underlying_asset_hash = var["internal_link_"||$underlying_asset_hash];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$underlying_asset_hash }",
"outputs": [
{
"address": "{$requester}",
"amount": "{$amount * json_parse(var["amount_of_"||$underlying_asset_hash])}"
}
]
}
},
{
"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 }"
}
]
}
},
{
"app": "state",
"state": "{
response["redemption"] = "indice token has been excahnge for the underlying assets.";
}"
}
]
},
{
"if": "{ // send the first unerlying asset or the one needed
(!!var["temp_"||$user||"_underlying_asset"] and ($received_asset == var["temp_"||$user||"_underlying_asset"])) OR
( !var["temp_"||$user||"_underlying_asset"] and ($received_asset == var["internal_link_1"]))
}",
"init": "{
if (!!var["temp_"||$user||"_underlying_asset"])
{
$underlying_asset_hash = var["temp_"||$user||"_underlying_asset"];
$etf_amount = var["temp_"||$user||"_etf_amount"];
$needed_mount = $etf_amount * var["amount_of_"||$underlying_asset_hash];
if ($received_amount != $needed_mount)
bounce ("you should send "||$needed_mount|| " of "||$underlying_asset_hash);
}
else
{
$underlying_asset_hash = var["internal_link_1"];
$needed_mount = var["amount_of_"||$underlying_asset_hash];
//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["internal_link_"||$underlying_asset_hash];
$all_underlying_asset_received = !$next_underlying_asset_hash;
}",
"messages": [
{
"if": "{$all_underlying_asset_received}",
"app": "payment",
"payload": {
"asset": "{var["indice_hash"] }",
"outputs": [
{
"address": "{$user}",
"amount": "{$etf_amount}"
}
]
}
},
{
"app": "state",
"state": "{
if ($all_underlying_asset_received)
{
var["temp_"||$user||"_etf_amount"] = false;
var["temp_"||$user||"_underlying_asset"] = false;
response["processed"] = var["indice_name"]||" sent";
}
else
{
var["temp_"||$user||"_etf_amount"] = $etf_amount;
var["temp_"||$user||"_underlying_asset"] = $next_underlying_asset_hash;
response["in progress"] = $underlying_asset_hash ||" received and waiting for "||$next_underlying_asset_hash;
}
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
if (!!var["temp_"||$user||"_underlying_asset"])
{
$underlying_asset_hash = var["temp_"||$user||"_underlying_asset"];
$etf_amount = var["temp_"||$user||"_etf_amount"];
$needed_mount = $etf_amount * var["amount_of_"||$underlying_asset_hash];
bounce ("you should send "||$needed_mount|| " of "||$underlying_asset_hash);
}
else
{
$underlying_asset_hash = var["internal_link_1"];
$needed_mount = var["amount_of_"||$underlying_asset_hash];
bounce ("you should send a multiple of "||$needed_mount|| " of "||$underlying_asset_hash);
}
}"
}
]
}
]
}
}
]