[
"autonomous agent",
{
"init": "{
// JAAAWS, Joint Account Agent
$aa_name = "JAAAWS";
$owner = "
O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ";
$holaa = "
FVNMVTIJYAOQJ4LKV65VT23VXIL6ZHF6"; // Holder Of List AA for the list of parties in the account
$list = $holaa;
$pay_id_received = trigger.data.pay_id otherwise trigger.data.pi otherwise "";
// use cases
$validate_pay = $pay_id_received != "";
$cancel = ($pay_id_received != "") AND (trigger.data.cancel==true);
}",
"messages": {
"cases": [
{
"if": "{trigger.data.create}",
"init": "{
}",
"messages": [
{
"app": "data",
"payload": {
"create": "{trigger.data.create}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$list}",
"amount": 1
},
{
"address": "{trigger.address}",
"amount": "{ trigger.output[[asset=base]] - 1000 }"
}
]
}
},
{
"app": "state",
"state": "{
var[trigger.data.create] = trigger.output[[asset=base]];
var[trigger.data.create||"_in_creation"] = trigger.address; // to avoid abuse;
response['message'] = trigger.data.create||" create, please add parties.";
}"
}
]
},
{
"if": "{trigger.data.add_party}",
"init": "{
if (!trigger.data.account) bounce ("Need 'account'!");
if (!(var[$list][trigger.data.account])) bounce (trigger.data.account||" do not exist");
if (!(var[trigger.data.account||"_in_creation"])) bounce ("The account is final");
if (var[trigger.data.account||"_in_creation"] != trigger.address) bounce ("You are not the account creator!");
}",
"messages": [
{
"app": "data",
"payload": {
"list_name": "{trigger.data.account}",
"add": "{trigger.data.add_party}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$list}",
"amount": 1
}
]
}
},
{
"app": "state",
"state": "{
response['message'] = trigger.data.add_party||" added ^^";
}"
}
]
},
{
"if": "{trigger.data.finalize}",
"init": "{
$account= trigger.data.finalize;
if (!(var[$list][$account])) bounce ($account||" do not exist");
if (!(var[$account||"_in_creation"])) bounce ("The account is already final");
if (var[$account||"_in_creation"] != trigger.address) bounce ("You are not the account creator!");
if (var[$holaa][$account||"_count"] < 2) bounce ("the minimum is 2 parties, use 'add_party'");
}",
"messages": [
{
"app": "state",
"state": "{
var[$account||"_in_creation"] = false;
response['message'] = $account||" finalized ^^";
}"
}
]
},
{
"if": "{ trigger.data.pay_to }",
"init": "{
$account = trigger.data.account otherwise bounce("Need 'account'!");
$amount = trigger.data.amount otherwise bounce ("Need 'amount'!");
$account_balance = var[$list][$account] otherwise bounce ($account||" do not exist");
$party_number = var[$list][$account||"_"||trigger.address] otherwise
bounce ("You are not party of "||$account);
if (var[$account||"_in_creation"])
if (var[$account||"_in_creation"] != trigger.address)
bounce ("Account not finalized!");
$pay_id = "payment_id_"||sha256($account||'>'||trigger.data.pay_to||':'||trigger.data.amount);
// validators
$next_validator_number = ($party_number == 1)? 2: 1;
$next_validator = var[$list][$account||"_"||$next_validator_number];
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{"JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||trigger.data.amount||" bytes to "||trigger.data.pay_to||", please validate or cancel it by send me back 'validate = payment_id_xx...xx' or 'cancel = payment_id_xx...xx'.."}",
"pay_id": "{$pay_id}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$next_validator}",
"amount": 33
}
]
}
},
{
"app": "state",
"state": "{
var[$pay_id||"_account"] = $account;
var[$pay_id||"_amount"] = $pay_amount;
var[$pay_id||"_address"] = $pay_address;
var[$pay_id||"_initiator"] = trigger.address;
var[$pay_id||"_waiting_for"] = $next_validator;
response['message'] = "Payment "||$pay_id||" created and validation request sent to "||$next_validator;
}"
}
]
},
{
"if": "{ trigger.data.validate }",
"init": "{
// bounces
$pay_id = trigger.data.validate;
$account = var[$pay_id||"_account"] otherwise bounce ("Payment ID "||$pay_id||" do not exist!");
$amount = var[$pay_id||"_amount"] otherwise bounce ("No amount available !");
$address = var[$pay_id||"_address"] otherwise bounce ("No address to pay !");
$initiator = var[$pay_id||"_initiator"] otherwise bounce ("No initiator !");
$validator_waited_for = var[$pay_id||"_waiting_for"] otherwise bounce ("Not waiting for validation");
$party_number = var[$list][$account||"_"||trigger.address] otherwise bounce ("You are not party of "||$account);
if ($validator_waited_for!=trigger.address) bounce ("Waiting first for validation by "||$validator_waited_for);
// need more validation?
$last_validator_num = var[$list][$account||"_count"];
$last_validator = var[$list][$account||"_"||$last_validator_num] otherwise bounce ("error");
if (trigger.address == $last_validator) // validation rounds complete
$payment_complete_validated = true;
else
{ // find next valisator
$last_validator_num = (var[$list][$account||"_"||$validator_waited_for]);
$jump = (var[$list][$account||"_"||($last_validator_num+1)] == $initiator)? 2:1;
$next_validator = var[$list][$account||"_"||($last_validator_num+$jump)];
}
// creation of messages
if ($payment_complete_validated)
{ // lets do the payment
$next_address = $address;
$next_amount = $amount;
$next_message = "Payment of "||$next_amount||" done to "||$next_address||" from account "||$account;
}
else
{ // prepare message for next valisator
$next_address = $next_validator;
$next_amount = 33;
$next_message = "JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||$amount||" to "||$address||", please validate or cancel it by send me back 'validate = payment_id_xx...xx' or 'cancel = payment_id_xx...xx'.";
}
}",
"messages": [
{
"app": "data",
"payload": {
"message": "{$next_message}",
"pay_id": "{$pay_id_received}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$next_address}",
"amount": "{$next_amount}"
}
]
}
},
{
"app": "state",
"state": "{
if ($payment_complete_validated)
{
var[$pay_id||"_account"] = false;
var[$pay_id||"_amount"] = false;
var[$pay_id||"_address"] = false;
var[$pay_id||"_initiator"] = false;
var[$pay_id||"_waiting_for"] = false;
response['message'] = "Payment of "||$amount||" to "||$address||" is finally sent to "||$next_address;
}
else
{
var[$pay_id||"_waiting_for"] = $next_validator;
response['message'] = "Next validation request has been sent to "||$next_validator;
}
}"
}
]
},
{
"if": "{ trigger.data.cancel}",
"init": "{
// bounces
$pay_id = trigger.data.cancel;
if (!(var[$pay_id||"_account"]))
bounce ("Payment ID "||$pay_id||" do not exist!");
$account = var[$pay_id||"_account"];
if (!(var[$list][$account||"_"||trigger.address]))
bounce ("You are not party of "||$account);
}",
"messages": [
{
"app": "state",
"state": "{
var[$pay_id||"_account"] = false;
var[$pay_id||"_amount"] = false;
var[$pay_id||"_address"] = false;
var[$pay_id||"_initiator"] = false;
var[$pay_id||"_waiting_for"] = false;
response['message'] = "Payment of "||$amount||" to "||$address||" is cancelled by "||trigger.address;
}"
}
]
},
{
"messages": [
{
"init": "{
if (!(trigger.data.account)) bounce ("need account name");
$input = trigger.output[[asset=base]];
}",
"app": "state",
"state": "{
var[$account] += $input;
response['message'] = $input||" added to the account "||$account;
}"
}
]
}
]
}
}
]