[
    "autonomous agent",
    {
        "init": "{
        $asset_ct = 'hd936lRtPN11pkgNv8qVnqN2OHMyAp8P/NsLp6KsuqA=';
        if(!var['_ Last price'] and !trigger.data.price)    bounce('Price must be defined before first exchange');
        if(trigger.data.price){
            //bytes per 1 asset
            $ratio = round(trigger.data.price);
            if($ratio <= 0) bounce('Price not allowed');
        }
        else $ratio = var['_ Last price'];
        //asset detection and checks
        if(trigger.output[[asset!=base]].asset != 'none')
            $asset = trigger.output[[asset!=base]].asset;
        if($asset){
            if($asset == 'ambiguous') bounce('Only 1 asset per tx');
            if($asset != $asset_ct)    bounce('Asset not allowed, please send ' || $asset_ct);
            $asset_inp = (trigger.output[[asset!=base]] != 0) ? trigger.output[[asset!=base]] : bounce('Zero asset tx');
        }
        
        $addr = trigger.address;
        $base_inp = trigger.output[[asset=base]] - 700;
        //user amounts-price
        $base_key =  '_' || $addr || $ratio || 'b';
        $asset_key = '_' || $addr || $ratio;
        //total amounts-price
        $total_key = 'b' || $ratio;
        $total_asset_key = 'a' || $ratio;
        //exchanged amounts, allow users to withdraw afterwards
        $exch_key = 'b_' || $ratio ;
        $exch_asset_key = 'a_' || $ratio ;
        //exchanged amounts are 'protected' until owners withdraw, defines also max amount to cancel
        $max_b_to_exchange = var[$total_key] - var[$exch_key];
        $max_a_to_exchange = var[$total_asset_key] - var[$exch_asset_key];
        //base_inp without leftover bytes
        $base_inp_round = ($base_inp - ($base_inp % $ratio));
    }",
        "messages": {
            "cases": [
                {
                    "if": "{
                        //allowed if there is exchanged amount and user deposited previously
                        $withdraw_asset = var[$exch_asset_key] and var[$base_key] and var[$exch_asset_key] > 0 and var[$base_key] > 0;
                        $withdraw_base = var[$exch_key] and var[$asset_key] and var[$exch_key] > 0 and var[$asset_key] > 0;
                        $withdraw_asset or $withdraw_base 
                    }",
                    "init": "{
                        if($asset) bounce('You must withdraw, send byte only transaction please');
                        $withdraw_key = $withdraw_asset ? $exch_asset_key : $exch_key;
                        $w_total_key = $withdraw_asset ? $total_asset_key : $total_key;
                        $max_to_withdraw = $withdraw_asset ? (var[$base_key] / $ratio) : (var[$asset_key] * $ratio);
                        $key = $withdraw_asset ? $base_key : $asset_key;
                        //user can withdraw all their exchanged amount or a part of their exchanged amount (rest of order could be cancel)
                        $withdraw_amount = (var[$withdraw_key] <= $max_to_withdraw) ? var[$withdraw_key] : $max_to_withdraw;
                    }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{ $withdraw_asset ? $base_inp : ($withdraw_amount + $base_inp) }"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$withdraw_asset}",
                            "app": "payment",
                            "payload": {
                                "asset": "{$asset_ct}",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$withdraw_amount}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            //substract withdrawn amount
                            var[$w_total_key] -= $withdraw_amount;
                            if(var[$w_total_key] == 0) var[$w_total_key] = false;
                            //substract exchanged amount
                            var[$withdraw_key] -= $withdraw_amount;
                            if(var[$withdraw_key] == 0) var[$withdraw_key] = false;
                            //substract user deposit
                            var[$key] -= $withdraw_asset ? ($withdraw_amount * $ratio) : ($withdraw_amount / $ratio);
                            if(var[$key] == 0) var[$key] = false;
                            response['message'] = 'withdrawn exchanged amount ('||($withdraw_asset ? 'assets': 'bytes')||') at '||$ratio||' bytes per token';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                        //allowed if there is amount to exchange
                        $exchange_asset = $asset and $max_b_to_exchange > 0;
                        $exchange_base = !$asset and $max_a_to_exchange > 0;
                        
                        !trigger.data.cancel and ($exchange_asset or $exchange_base)
                    }",
                    "init": "{
                        $max_to_ex = $asset ? $max_b_to_exchange : $max_a_to_exchange;
                        $inp_in_other = $asset ? $asset_inp*$ratio : $base_inp_round/$ratio;
                        if($inp_in_other > $max_to_ex){
                            //user sent more amount than allowed, exchange to max allowed/return leftover if any
                            $byte_amount =  $asset ? $max_to_ex : (($inp_in_other - $max_to_ex) * $ratio);
                            $asset_amount = $asset ? ((($inp_in_other - $max_to_ex) - (($inp_in_other - $max_to_ex) % $ratio)) / $ratio) : $max_to_ex;
                        }
                        else{
                            //user sent less or equal amount than allowed, exchange to user input
                            if($inp_in_other == 0)    bounce('Not enough funds for minimum');
                            $byte_amount = $asset ? $inp_in_other : 0;
                            $asset_amount = $asset ? 0 : $inp_in_other;
                        }
                    }",
                    "messages": [
                        {
                            "if": "{$exchange_asset or $byte_amount > 0}",
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$exchange_asset ? ($byte_amount + $base_inp) : $byte_amount}"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$exchange_base or $asset_amount > 0}",
                            "app": "payment",
                            "payload": {
                                "asset": "{$asset_ct}",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$asset_amount}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var['_ Last price'] = $ratio;
                            if($exchange_asset){
                                //sum exchanged amount
                                var[$total_asset_key] += $asset_inp - $asset_amount;
                                var[$exch_asset_key] +=  $asset_inp - $asset_amount;
                                
                                //substract taken amount
                                var[$total_key] -= $byte_amount;
                                if(var[$total_key] == 0) var[$total_key] = false;
                                response['message'] = 'sold '||($asset_inp - $asset_amount)||' assets for '||($byte_amount)||' bytes';
                            }
                            else{
                                //sum exchanged amount
                                var[$total_key] += $base_inp_round - $byte_amount;
                                var[$exch_key] += $base_inp_round - $byte_amount;
                                //substract taken amount
                                var[$total_asset_key] -= $asset_amount;
                                if(var[$total_asset_key] == 0) var[$total_asset_key] = false;
                                response['message'] = 'bought '||($asset_amount)||' assets for '||($base_inp_round - $byte_amount)||' bytes';
                            }
                        }"
                        }
                    ]
                },
                {
                    "if": "{    
                        //allowed if user has non-filled or partial filled orders to cancel
                        $cancel_asset = var[$asset_key] and var[$asset_key] > 0;
                        $cancel_base = var[$base_key] and var[$base_key] > 0;
                        //if we are here, means user does not fulfill conditions to withdraw
                        trigger.data.cancel and ($cancel_asset or $cancel_base)
                    }",
                    "init": "{
                        $max_to_cancel = $cancel_asset ? $max_a_to_exchange : $max_b_to_exchange;
                        $key = $cancel_asset ? $asset_key : $base_key;
                        $cancel_tot_key = $cancel_asset ? $total_asset_key : $total_key;
                        //trying to cancel a executed order
                        if($max_to_cancel <= 0) bounce('Not allowed');
                        //cancel all amount if possible
                        $cancel_amount = ($max_to_cancel <= var[$key]) ? $max_to_cancel : var[$key];
                    }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$cancel_asset ? $base_inp : ($cancel_amount + $base_inp)}"
                                    }
                                ]
                            }
                        },
                        {
                            "if": "{$cancel_asset}",
                            "app": "payment",
                            "payload": {
                                "asset": "{$asset_ct}",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$cancel_amount}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var[$cancel_tot_key] -= $cancel_amount;
                            if(var[$cancel_tot_key] == 0)var[$cancel_tot_key] = false;
                            var[$key] -= $cancel_amount;
                            if(var[$key] == 0)var[$key] = false;
                            response['message'] = 'canceled order ('||($cancel_asset?'sell':'buy')||' at '||$ratio||' bytes per token)';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                        (!trigger.data.cancel and ($asset ? $max_b_to_exchange == 0 : $max_a_to_exchange == 0 )) otherwise bounce('Deposit not allowed, please cancel order first')
                    }",
                    "messages": [
                        {
                            "if": "{$asset}",
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{$addr}",
                                        "amount": "{$base_inp}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            if($asset){
                                var[$total_asset_key] += $asset_inp;
                                var[$asset_key] += $asset_inp;
                                response['message'] = 'deposited '||$asset_inp||' assets to sell at '||$ratio||' bytes per token';
                            }
                            else{
                                var[$total_key] += $base_inp_round;
                                var[$base_key] += $base_inp_round;
                                response['message'] = 'deposited '||$base_inp_round||' bytes to buy at '||$ratio||' bytes per token';
                            }
                        }"
                        }
                    ]
                }
            ]
        }
    }
]