Definition: [
    "autonomous agent",
    {
        "init": "{
        $CT_ADDRESS = "T2I6RLGGJ25KSJZKRNZZH52VMEAQ3722";
        $OWNER = params.issuer;
        $LICENSE_ASSET = var["licenseAsset"];
        if ($LICENSE_ASSET){
            $priceStr = data_feed[[oracles=$OWNER, feed_name=this_address, ifseveral="last", type="string"]];
            $chunks = split($priceStr, " ");
            if (length($chunks) != 2)
                bounce("Malformed data_feed. It should have only 2 items");
            $PRICE = $chunks[0];
            if ($PRICE < 0)
                bounce("Price must be positive");
            if (NOT is_valid_amount($PRICE))
                bounce("Price must be a valid amount");
            $PRICE_ASSET = $chunks[1];
            if (NOT asset[$PRICE_ASSET].exists)
                bounce("Invalid price asset");
        }
        $method = unit[trigger.initial_unit].messages[[.app="data"]].payload["method"];
        $CUT = 0.03;
        if ($LICENSE_ASSET AND NOT $method)
            bounce("method field is mandatory");
    }",
        "messages": {
            "cases": [
                {
                    "if": "{NOT $LICENSE_ASSET}",
                    "messages": [
                        {
                            "if": "{params.cap}",
                            "app": "asset",
                            "payload": {
                                "is_private": false,
                                "cap": "{params.cap OTHERWISE 0}",
                                "is_transferrable": "{NOT NOT params.tradeable}",
                                "auto_destroy": false,
                                "fixed_denominations": false,
                                "issued_by_definer_only": true,
                                "cosigned_by_definer": false,
                                "spender_attested": false
                            }
                        },
                        {
                            "if": "{NOT params.cap}",
                            "app": "asset",
                            "payload": {
                                "is_private": false,
                                "is_transferrable": "{NOT NOT params.tradeable}",
                                "auto_destroy": false,
                                "fixed_denominations": false,
                                "issued_by_definer_only": true,
                                "cosigned_by_definer": false,
                                "spender_attested": false
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var["licenseAsset"] = response_unit;
                        }"
                        }
                    ]
                },
                {
                    "if": "{trigger.initial_address == $OWNER AND $method == "payout"}",
                    "messages": {
                        "cases": [
                            {
                                "if": "{$method == "payout"}",
                                "messages": [
                                    {
                                        "app": "payment",
                                        "payload": {
                                            "asset": "{trigger.data.asset OTHERWISE $PRICE_ASSET}",
                                            "outputs": [
                                                {
                                                    "address": "{$OWNER}"
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                },
                {
                    "if": "{$method == "buy"}",
                    "init": "{
                    $copies = floor(trigger.output[[asset=$PRICE_ASSET]]/$PRICE, 0);
                    if ($copies == 0)
                        bounce("You cannot buy less than 1 license");
                    $hasReferral = (trigger.data["r"] AND is_valid_address(trigger.data["r"]));
                    $cut = $hasReferral ? $CUT/2 : $CUT;
                    $cutAmount = round(trigger.output[[asset=$PRICE_ASSET]] * $cut, 0);
                }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "{$LICENSE_ASSET}",
                                "outputs": [
                                    {
                                        "address": "{trigger.initial_address}",
                                        "amount": "{$copies}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "{$PRICE_ASSET}",
                                "outputs": [
                                    {
                                        "address": "{$CT_ADDRESS}",
                                        "amount": "{$cutAmount}"
                                    },
                                    {
                                        "if": "{$hasReferral}",
                                        "address": "{trigger.data["r"]}",
                                        "amount": "{$cutAmount}"
                                    }
                                ]
                            }
                        }
                    ]
                }
            ]
        }
    }
]