[
    "autonomous agent",
    {
        "doc_url": "https://obyte.org/prediction-factory.json",
        "bounce_fees": {
            "base": 20000
        },
        "init": "{
        $prediction_base_aa = 'DC7MCUB55NEKKMSHH7ZL5KACTJRXLSNN';
        $define_asset_forwarder = 'E4BAASPOCW6WHXSUOY2XEKXKA42RRD5I';
    }",
        "messages": {
            "cases": [
                {
                    "if": "{(trigger.data.yes_asset OR trigger.data.no_asset) AND var['prediction_' || trigger.address]}",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "define": 1,
                                "factory": "{this_address}",
                                "to": "{trigger.address}"
                            }
                        },
                        {
                            "if": "{trigger.data.yes_asset AND trigger.data.allow_draw}",
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$define_asset_forwarder}",
                                        "amount": 4000
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            $params = var['prediction_' || trigger.address];
                            if (trigger.data.yes_asset) {
                                $params.yes_asset = trigger.data.yes_asset;
                                $params.no_asset = trigger.unit;
                            }
                            if (trigger.data.no_asset) {
                                $params.draw_asset = trigger.unit;
                            }
                            var['prediction_' || trigger.address] = $params;
                        }"
                        }
                    ]
                },
                {
                    "init": "{
                    $event = trigger.data.event OTHERWISE '';
                    require(length($event) >= 5 AND length($event) <= 128, "additional description cannot be over 128 chars");
                    $oracle = trigger.data.oracle;
                    require($oracle AND is_valid_address($oracle), "oracle isn't valid");
                    $comparison = trigger.data.comparison OTHERWISE "==";
                    require($comparison == "==" OR $comparison == ">" OR $comparison == ">=" OR $comparison == "<" OR $comparison == "<=" OR $comparison == "!=", "operation is unknown");
                    $feed_name = trigger.data.feed_name OTHERWISE '';
                    require(length($feed_name) > 0 AND $feed_name != 'none', "feed_name isn't valid");
                    $reserve_asset = trigger.data.reserve_asset OTHERWISE 'base';
                    require(asset[$reserve_asset].exists, "no such asset: " || $reserve_asset);
                    $allow_draw = trigger.data.allow_draw OTHERWISE false;
                    $datafeed_value = trigger.data.datafeed_value;
                    require(exists($datafeed_value), "datafeed_value does not exist");        
                    $end_of_trading_period = trigger.data.end_of_trading_period OTHERWISE 0;
                    require(is_integer($end_of_trading_period) AND $end_of_trading_period > timestamp, "end_of_trading_period must be integer");
                    $waiting_period_length = trigger.data.waiting_period_length OTHERWISE 5 * 24 * 3600;
                    require(is_integer($waiting_period_length) AND $waiting_period_length >= 0, "trading period must be longer than a day");
                    $issue_fee = exists(trigger.data.issue_fee) ? trigger.data.issue_fee : 0.01;
                    require($issue_fee >= 0 AND $issue_fee < 1, "issue_fee isn't valid");        
                    $redeem_fee = exists(trigger.data.redeem_fee) ? trigger.data.redeem_fee : 0.02;
                    require($redeem_fee >= 0 AND $redeem_fee < 1, "redeem_fee isn't valid");
                    $arb_profit_tax = exists(trigger.data.arb_profit_tax) ? trigger.data.arb_profit_tax : 0.9;
                    require($arb_profit_tax >= 0 AND $arb_profit_tax < 1, "arb_profit_tax isn't valid");
                    $params = {
                        event: $event,
                        oracle: $oracle,
                        comparison: $comparison,
                        feed_name: $feed_name,
                        reserve_asset: $reserve_asset,
                        allow_draw: $allow_draw,
                        datafeed_value: $datafeed_value,
                        end_of_trading_period: $end_of_trading_period,
                        waiting_period_length: $waiting_period_length,
                        arb_profit_tax: $arb_profit_tax,
                        issue_fee: $issue_fee,
                        redeem_fee: $redeem_fee
                    };
                    if ($allow_draw) {
                        $params.datafeed_draw_value = trigger.data.datafeed_draw_value OTHERWISE 'none';
                    }
                    $prediction_aa = [
                        'autonomous agent',
                        {
                            base_aa: $prediction_base_aa,
                            params: $params
                        }
                    ];
                    $prediction_aa_address = chash160($prediction_aa);
                    require(!definition[$prediction_aa_address], "such a prediction already exists: " || $prediction_aa_address);
                }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $prediction_aa_address }",
                                        "amount": 12000
                                    }
                                ]
                            }
                        },
                        {
                            "app": "definition",
                            "payload": {
                                "definition": "{$prediction_aa}"
                            }
                        },
                        {
                            "app": "data",
                            "payload": {
                                "define": 1,
                                "factory": "{this_address}"
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            $params.created_at = timestamp;
                            $params.creator = trigger.initial_address;
                            var['prediction_' || $prediction_aa_address] = $params;
                            response["prediction_address"] =  $prediction_aa_address;
                        }"
                        }
                    ]
                }
            ]
        }
    }
]