Definition: [
    "autonomous agent",
    {
        "bounce_fees": {
            "base": 10000
        },
        "init": "{
            // RAAdomoney: the more money, the more random. 
            // Base on the functionnal funds you provide to RAAdomoney, it will trigger several rounds of randomisation with  RAAndomore at a pace depending on the DAG confirmation time. 
            // As RAAndomore uses several data feeds that vary in time and as it is triggered at a time that cannot be predicted because depending of the speed of the DAG, the randomness is insure.
            $RAAndomore = "MR3GL4JFWF3UTTSN42LRPCPWZSABO7WR";
            $to_raa_price = 6000;
            $from_raa_price = 1000; 
            $to_user_price = 1000;
            // rooting;
            $from_raa = trigger.address == $RAAndomore;
            $from_user = !$from_randomiser;
            $user = ($from_raa) ? trigger.data.address : trigger.address;
            
            // check if wealthy enough for another round;
            $existing_user_balance = var['balance_'||$user] otherwise 0;
            $user_balance = $existing_balance + trigger.output[[asset=base]];
            $wealthy = ( $user_balance > $to_raa_price + $from_raa_price + $to_user_price);
        }",
        "messages": {
            "cases": [
                {
                    "if": "{$from_user}",
                    "init": "{
                        if (var['rounds_'||$user]) bounce ("You have a randomization in progress, round is "||var['rounds_'||$user]);
                        if (!$wealthy) bounce ('gimme more bytes (and an optional seed)');
                        $seed = trigger.data.seed otherwise sha256($user);
                    }",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "seed": "{$seed}",
                                "address": "{$user}"
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$RAAndomore}",
                                        "amount": "{$from_raa_price}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{ 
                                var['balance_'||$user] = $user_balance - $to_raa_price - $from_raa_price; 
                                var['rounds_'||$user] = 1;
                                response['message'] = 'Randomisation in progress by $RAAndomore';
                            }"
                        }
                    ]
                },
                {
                    "if": "{$from_raa AND $wealthy}",
                    "init": "{ }",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "seed": "{trigger.data.rhash}",
                                "address": "{$user}"
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$RAAndomore}",
                                        "amount": "{$from_raa_price}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                                var['rounds_'||$user] += 1;
                            }"
                        }
                    ]
                },
                {
                    "init": "{}",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "rhash": "{trigger.data.rhash}"
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$user}",
                                        "amount": "{$user_balance}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                                var['balance_'||$user] =false;
                                response['message'] = 'rash after '||var['rounds_'||$user]||' round(s) is '|| trigger.data.rhash;
                                var['rounds_'||$user] = false;
                            }"
                        }
                    ]
                }
            ]
        }
    }
]