[
"autonomous agent",
{
"init": "{
// FOREVER CONSTANTE
$AA_NAME = "JAAA";
$LIST_HOLDER_AA = "
YNB262XRUZYZTG5YSTS4QCWE4C6ZEEP2";
$INSTRUCTIONS = "Every operation need 'account_name= <account name>'except if you received a 'pay_id', then just use it, 'pay_id = <received pay_id> optionaly with 'cancel = true' to deny the payment. To initiate a payment, define 'pay_to' and 'amount'. To create a new account, add users with 'add_user = <address>' multiple times then 'activate_account = true' and choose 'hierarchical/flat = true'.";
$TRICK_TO_ORDER_FIELD_PROPOSITION_IN_WALLET = trigger.data.pay_id||trigger.data.account_name||trigger.data.pay_to||trigger.data.amount||trigger.data.communication||trigger.data.add_user||trigger.data.nickname||trigger.data.active_account||trigger.data.hierarchical||trigger.data.flat;
//inputs
$inputs = "For the moment I received: "||json_stringify(trigger.data)||json_stringify(trigger.output[[asset=base]]);
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.cancel}",
"init": "{
$account_name = trigger.data.account_name otherwise bounce ($INSTRUCTIONS);
if (!var[$LIST_HOLDER_AA][$account_name||"_users_"||trigger.address])
bounce ("This address is not listed as actor of this account");
$payment_id = trigger.data.token otherwise bounce ("'Token' needed !");
$message = "Payment cancelled by "||trigger.address||", "||var[$payment_id];
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{ $message }"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ var[$payment_id||"_initiator"] }",
"amount": 6
}
]
}
},
{
"app": "state",
"state": "{
var[$payment_id] = false;
var[$payment_id||"_initiator"] = false;
var[$payment_id||"_validations_needed"] = false;
var[$payment_id||"_communication"] = false;
var[$payment_id||"_destination"] = false;
var[$payment_id||"_amount"] = false;
var[$payment_id||"_account"] = false;
var[$payment_id||"_needs_"||trigger.address] = false;
var[$payment_id||"_destination"] = false;
var[$payment_id||"_amount"] = false;
response['message'] = $message;
}"
}
]
},
{
"if": "{ !!trigger.data.pay_id }",
"init": "{
$payment_id = trigger.data.pay_id;
$account_name = var[$payment_id||"_account"];
// bouncings
if (!var[$payment_id])
bounce ("Token "||$payment_id||" do not exist!");
if (!var[$LIST_HOLDER_AA][$account_name||"_users_"||trigger.address])
bounce ("This address is not listed as actor of this account");
if (!var[$payment_id||"_needs_"||trigger.address])
bounce ("Your validation is not needed!");
$number_of_validations_needed = var[$payment_id||"_validations_needed"] - 1;
$is_validated = ($number_of_validations_needed == 0);
$forwarding_needed = !$is_validated and var[$account_name] == "hierarchical";
}",
"messages": [
{
"if": "{ $is_validated }",
"app": "data",
"payload": {
"message": "{ var[$payment_id||"_communication"] }"
}
},
{
"if": "{ $is_validated }",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ var[$payment_id||"_destination"] }",
"amount": "{ var[$payment_id||"_amount"] }"
}
]
}
},
{
"if": "{ $forwarding_needed }",
"app": "data",
"payload": {
"list_name": "{ $account_name||"_users" }",
"token": "{ $payment_id }",
"get_previous": "{ trigger.address }"
}
},
{
"if": "{ $forwarding_needed }",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $LIST_HOLDER_AA }",
"amount": "{ 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[$payment_id||"_needs_"||trigger.address] = false;
var[$payment_id||"_validations_needed"] = $number_of_validations_needed;
if ($is_validated) // clean the payment state
{
var[$payment_id] = false;
var[$payment_id||"_initiator"] = false;
var[$payment_id||"_communication"] = false;
var[$payment_id||"_destination"] = false;
var[$payment_id||"_amount"] = false;
var[$payment_id||"_account"] = false;
response['message'] = "Payment executed";
}
else
response['message'] = "Payment validated, next validation in progress ^^";
}"
}
]
},
{
"if": "{ !!trigger.data.pay_to}",
"init": "{
// bouncing
$account_name = trigger.data.account_name otherwise bounce ($INSTRUCTIONS);
if (!var[$account_name]) bounce ("Account "||$account_name||" do not exist!");
if (!var[$LIST_HOLDER_AA][$account_name||"_users_"||trigger.address])
bounce ("This address is not listed as actor of this account");
if (!is_valid_address(trigger.data.pay_to)) bounce ("Invalid address in 'pay_to'!");
if (!is_valid_amount(trigger.data.amount)) bounce ("amount must be positive !");
// initiate payment request
$payment_id = sha256($account_name||'>'||$pay_address||': '||$pay_amount);
}",
"messages": [
{
"app": "data",
"payload": {
"list_name": "{ $account_name||"_users" }",
"get_previous": "{ true }",
"token": "{ $payment_id }"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $LIST_HOLDER_AA }",
"amount": 1000
}
]
}
},
{
"app": "state",
"state": "{
$nickname = var[$LIST_HOLDER_AA][$account_name||"_users_"||trigger.address];
var[$payment_id] = "Payment of "||trigger.data.amount||" bytes from "||$account_name||" to "||trigger.data.pay_to||" initiated by "||trigger.address||"("||$nickname||")";
var[$payment_id||"_initiator"] = trigger.address;
var[$payment_id||"_destination"] = trigger.data.pay_to;
var[$payment_id||"_amount"] = trigger.data.amount;
var[$payment_id||"_account"] = $account_name;
var[$payment_id||"_communication"] = trigger.data.communication otherwise "none";
var[$payment_id||"_validations_needed"] = var[$LIST_HOLDER_AA][$account_name||"_users_items_count"]-1;
response['message'] = "Payment "||$payment_id||" created and approvals requested";
}"
}
]
},
{
"if": "{ !!trigger.data.add_user }",
"init": "{
$account_name = var[trigger.address] otherwise trigger.data.account_name otherwise bounce ($INSTRUCTIONS);
if (!!var[$account_name||"_owner"])
if (trigger.address != var[$account_name||"_owner"])
bounce ("Account "||$account_name||" already exist!");
if (!is_valid_address(trigger.data.add_user)) bounce ("Invalid address in 'add_user'!");
}",
"messages": [
{
"app": "data",
"payload": {
"list_name": "{ $account_name||"_users" }",
"add": "{ trigger.data.add_user }",
"value": "{ trigger.data.nick_name }"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $LIST_HOLDER_AA }",
"amount": 250
}
]
}
},
{
"app": "state",
"state": "{
// temporary store creator to avoid to enter account name for each new user added
var[trigger.address] = $account_name;
// temporary store owner to avoid abuse during creation of the account
var[$account_name||"_owner"] = trigger.address;
var[$account_name||"_balance"] += trigger.output[[asset=base]].amount - 1000;
response['message'] = 'One user added ^^';
}"
}
]
},
{
"if": "{ !!trigger.data.activate_account }",
"init": "{
$account_name = var[trigger.address] otherwise trigger.data.account_name otherwise bounce ($INSTRUCTIONS);
if (!!var[$account_name||"_owner"])
if (trigger.address != var[$account_name||"_owner"])
bounce ($account_name||" already active!");
if (!trigger.data.hierarchical and !trigger.data.flat)
bounce ("you must specify 'hierarchical = true' or 'flat = true'");
}",
"messages": [
{
"app": "state",
"state": "{
var[$account_name] = !!trigger.data.hierarchical ? "hierarchical": "flat";
var[$account_name||"_owner"] = false;
var[trigger.address] = false;
var[$account_name||"_balance"] += trigger.output[[asset=base]].amount;
response['message'] = $account_name||" activated ^^";
}"
}
]
},
{
"if": "{ trigger.address == $LIST_HOLDER_AA }",
"init": "{
// initiate payment request
$payment_id = trigger.data.token; // get it back from HOLLAA
$account_name = var[$payment_id||"_account"];
$party = trigger.data.item_name;
// validation from party needed?
$actual_user_validation_needed = $party != var[$payment_id||"_initiator"];
//
$another_party_is_needed = $party != var[$LIST_HOLDER_AA][$account_name||"_users_first_item"];
$forwarding_needed = $another_party_is_needed
and (var[$account_name] == "flat" or !$actual_user_validation_needed);
// trick to be able to put condition on the payment and avoid 'already have base payment'
$amount_for_AA = $forwarding_needed? 1000 : 0;
}",
"messages": [
{
"if": "{ $actual_user_validation_needed }",
"app": "data",
"payload": {
"message": "{"JAAA: Your approval is required: "||var[$payment_id]||", please send me back the pay_id linked to validate the transaction. And add a 'cancel = true' field to refuse it."}",
"me": "{ this_address }",
"pay_id": "{ $payment_id }"
}
},
{
"if": "{ $forwarding_needed }",
"app": "data",
"payload": {
"list_name": "{ $account_name||"_users" }",
"get_previous": "{ $party }",
"token": "{ $payment_id }"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"if": "{ $forwarding_needed }",
"address": "{ $LIST_HOLDER_AA }",
"amount": "{ 1000 }"
},
{
"if": "{ $actual_user_validation_needed }",
"address": "{ $party }",
"amount": "{ 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[$payment_id||"_needs_"||$party] = $actual_user_validation_needed;
if ( $actual_user_validation_needed )
response["message"] = $party||"has been requested for validation";
if ( $forwarding_needed )
response["message"] = "Forwarding info to other parties";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{ bounce ($INSTRUCTIONS); }"
}
]
}
]
}
}
]