Definition: [
"autonomous agent",
{
"init": "{
// JAAAWS, Joint Account Agent
$aa_name = "JAAAWS";
$owner = "O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; //office_testnet_wallet
$holaa = "5BRALFFKNML53X4MJVTWBSBYSDSNRIHQ";
$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
}
]
}
},
{
"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 = 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||" to "||trigger.data.pay_to||", please send me back the pay_id linked to validate the transaction. And add a cancel = true field to refuse it."}",
"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 ^^";
}"
}
]
},
{
"if": "{ trigger.data.validate }",
"init": "{
// bounces
$pay_id = trigger.data.validate;
if (!(var[$pay_id||"_account"]))
bounce ("Payment ID "||$pay_id||" do not exist!");
$validator_waited_for = var[$pay_id||"_waiting_for"];
if (var[$validator_waited_for]!=trigger.address)
bounce ("Waiting first for validation by "||$validator_waited_for);
$account = var[$pay_id||"_account"];
if (!(var[$list][$account||"_"||trigger.address]))
bounce ("You are not party of "||$account);
// need more validation?
$last_validator_num = var[$list][$account||"_count"];
$last_validator = var[$list][$account||"_"||$last_validator_num];
if (trigger.address == $last_validator)
$payment_complete_validated = true;
else
{
$next_validator_num = (var[$list][$account||"_"||$validator_waited_for]) +1 ;
$next_validator = var[$list][$account||"_"||$next_validator_num];
}
// creation of messages
$amount = var[$pay_id||"_amount"];
$address = var[$pay_id||"_address"];
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 send me back the pay_id linked to validate the transaction. And add a cancel = true field to refuse it.";
}
}",
"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;
}"
}
]
}
]
}
}
]