[
"autonomous agent",
{
"messages": {
"cases": [
{
"if": "{
$salt = "blue";
// select random registered address and optionally send a payment message to it, by
[email protected]
// usage:
// 1) interested parties sends 10000 to this AA to register address for future selection, they will be paid back when selected
// 2) another AA send 11000 + size of data.message which could include a message, a link or an address to send to the choosen address
// 3) data.nosend if true doesn't send message from this AA, used to defer the payment sending to primary AA which might add more data
if( trigger.data.message ){
$size = length( trigger.data.message ) * 4 + 1000; // 1000 for transaction fee
if( !var[ "count" ] ) bounce( "There is currently no registered address to choose from");
if( !trigger.data.nosend AND trigger.output[ [ asset=base ] ] < ( 10000 + $size ) ) bounce( "need to send " || ( 10000 + $size ) || " with this data.message");
if( var[ "count" ] == 1 ) $choose = 1; // special case, number from seed doesn't work with start = end
else $choose = number_from_seed( $salt || trigger.address || timestamp || mc_unit , 1 , var[ "count" ] ); // SEMI RANDOM CHOOSE
$choosen = var[ $choose || "" ];
}
trigger.data.message
}",
"messages": [
{
"if": "{ trigger.data.nosend }",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ trigger.address }",
"amount": "{ trigger.output[[asset=base]] - $size }"
}
]
}
},
{
"if": "{ !trigger.data.nosend }",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $choosen }",
"amount": 10000
},
{
"address": "{ trigger.address }",
"amount": "{ trigger.output[[asset=base]] - 10000 - $size }"
}
]
}
},
{
"app": "data",
"payload": {
"message": "{trigger.data.message}",
"choosen": "{ $choosen }"
}
},
{
"app": "state",
"state": "{ // RESPONSE LISTENED BY NODES
response[ "message" ] = trigger.data.message;
response[ "choosen" ] = $choosen;
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
if( var[ trigger.initial_address ] ) bounce( trigger.initial_address || " had registered with " || var [ trigger.initial_address ] || ", there are total of " || var[ "count" ] || " addresses to chose from ");
// NEW REGISTRATION
var[ "count" ] += 1;
var[ var[ "count" ] || "" ] = trigger.initial_address;
var[ trigger.initial_address ] = trigger.output[ [ asset = base ] ] ;
response[ 'message' ] = trigger.initial_address || " registered, there are now " || var[ "count" ] || " addresses";
}"
}
]
}
]
}
}
]