[
"autonomous agent",
{
"init": "{
// JAAAWS, Joint Account Agent
$owner = "
O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ"; //office_testnet_wallet
$input_pid = trigger.data.pay_id otherwise trigger.data.pi otherwise "";
if ($input_pid != "") // for validation steps and payment step
{
$account = var[$input_pid||"_account"];
$amount_to_pay = var[$input_pid||"_amount"];
$address_to_pay = var[$input_pid||"_address"];
$pay_id = $input_pid;
$pay_initiator = var[$input_pid||"_initiator"];
}
else // for account creation and init
{
$account = trigger.data.account otherwise trigger.data.ac otherwise "";
$amount_to_pay = trigger.data.amount_to_pay otherwise trigger.data.am otherwise 0;
$address_to_pay = trigger.data.address_to_pay otherwise trigger.data.ad otherwise "";
$pay_id = sha256($account||'>'||$pay_address||':'||$pay_amount);
$pay_initiator = trigger.address;
}
if ($account=="") bounce ("need account name");
$party1 = trigger.data.address_to_pay1 otherwise trigger.data.a1 otherwise var[$account||"_1"] otherwise "";
$party2 = trigger.data.address_to_pay2 otherwise trigger.data.a2 otherwise var[$account||"_2"] otherwise "";
$party3 = trigger.data.address_to_pay3 otherwise trigger.data.a3 otherwise var[$account||"_3"] otherwise "";
$trg = trigger.address;
$b_is_a_party = ($trg == $party1) or ($trg == $party2) or ($trg == $party3) or ($trg == $address_to_pay);
if (trigger.address == $party1) $i_party_num = 1;
if (trigger.address == $party2) $i_party_num = 2;
if (trigger.address == $party3) $i_party_num = 3;
if ($input_pid != "") // for validation steps and payment step
{
$b_pay_needed_v_from_1 = var[$input_pid||"_need_1"];
$b_pay_needed_v_from_2 = var[$input_pid||"_need_2"];
$b_pay_needed_v_from_3 = var[$input_pid||"_need_3"];
}
else // for account creation and init
{
$b_pay_needed_v_from_1 = (trigger.address == $party1);
$b_pay_needed_v_from_2 = (trigger.address == $party2);
$b_pay_needed_v_from_3 = (trigger.address == $party3);
}
$b_initizalization = trigger.data.address_to_pay or trigger.data.ad or trigger.data.amount_to_pay or trigger.data.am;
}",
"messages": {
"cases": [
{
"if": "{(trigger.data.address_to_pay1 or trigger.data.a1) and (trigger.data.address_to_pay2 or trigger.data.a2)}",
"init": "{
if (var[$account]) bounce ("Account "||$account||" already exist!");
}",
"messages": [
{
"app": "state",
"state": "{
var[$account] = trigger.output[[asset=base]];
if ($party1 != "")var[$account||"_1"] = $party1;
if ($party2 != "")var[$account||"_2"] = $party2;
if ($party3 != "")var[$account||"_3"] = $party3;
response['message'] = "Joint Account set for "||$account||", balance = "||var[$account];
}"
}
]
},
{
"if": "{($input_pid!= "") or $b_initizalization}",
"init": "{
// global checks
if (!var[$account]) bounce ("Account "||$account||" do not exist!");
if (!$is_a_party) bounce ("You are not a party of "||$account);
if (!$b_is_a_party) bounce ("You must be a party to interract!");
if (trigger.data.cancel==true) // cancel
{
$state_pay_need_v_from_1 = false;
$state_pay_need_v_from_2 = false;
$state_pay_need_v_from_3 = false;
$address = var[$pay_id||"_initiator"];
$amount = 1000;
$state_account = false;
$state_amount = false;
$state_address = false;
$state_initiator = false;
$message = "Payment of "||$amount||" to "||$address||" is cancelled by "||$trg;
}
else
{
if ($input_pid != "") // validation
{
$state_pay_need_v_from_1 = $b_pay_needed_v_from_1 and ($trg != $party1);
$state_pay_need_v_from_2 = $b_pay_needed_v_from_2 and ($trg != $party2);
$state_pay_need_v_from_3 = $b_pay_needed_v_from_3 and ($trg != $party3);
}
else // initialization
{
$state_pay_need_v_from_1 = !(($party1 == "") or ($trg != $party1)) ;
$state_pay_need_v_from_2 = !(($party2 == "") or ($trg != $party2)) ;
$state_pay_need_v_from_3 = !(($party3 == "") or ($trg != $party3)) ;
}
$b_all_valid = ($state_pay_need_v_from_1 or $state_pay_need_v_from_2 or $state_pay_need_v_from_2);
if ($b_all_valid) // payment is final
{
$address = var[$pay_id||"_address"];
$amount = var[$pay_id||"_amount"];
$state_account = false;
$state_amount = false;
$state_address = false;
$state_initiator = false;
$message = "Payment of "||$amount||" to "||$address||" has been proceed";
}
else // validation is partial
{
if ($dummy) $address = $dummy;
else if ($state_pay_need_v_from_1) $address = $party1;
else if ($state_pay_need_v_from_2) $address = $party2;
else if ($state_pay_need_v_from_3) $address = $party3;
$amount = 1000;
$state_account = $account;
$state_amount = $amount_to_pay;
$state_address = $address_to_pay;
$state_initiator = $pay_initiator;
$message = "JAAAWS: Your approval is required for a payment from the account '"||$account||" to pay "||$pay_amount||" to "||$pay_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": "{$message}",
"pay_id": "{$pay_id}"
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
var[$pay_id||"_need_1"] = $state_pay_need_v_from_1;
var[$pay_id||"_need_2"] = $state_pay_need_v_from_2;
var[$pay_id||"_need_3"] = $state_pay_need_v_from_3;
var[$pay_id||"_account"] = $state_account;
var[$pay_id||"_amount"] = $state_amount;
var[$pay_id||"_address"] = $state_address;
var[$pay_id||"_initiator"] = $state_initiator;
response['message'] = $message;
}"
}
]
},
{
"messages": [
{
"init": "{
if ($account=="") bounce ("need account name");
$input = trigger.output[[asset=base]];
}",
"app": "state",
"state": "{
var[$account] += $input;
response['message'] = $input||" added to the account "||$account;
}"
}
]
}
]
}
}
]