Definition: [
    "autonomous agent",
    {
        "bounce_fees": {
            "base": 10000
        },
        "init": "{
        $balance = var['balance'] otherwise 0;
        $id = var['last_id'] otherwise 0;
    }",
        "messages": {
            "cases": [
                {
                    "if": "{
                    trigger.data.type == 'make_order_sell' 
                    AND trigger.data.price_in_eth
                }",
                    "init": "{
                    if(trigger.output[[asset=base]].amount < 10000)
                        bounce('Not enough money');
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$id || '_owner'] = trigger.address;
                            var[$id || '_type'] = 'sell';
                            var[$id || '_amount'] = trigger.output[[asset=base]].amount;
                            var[$id || '_price_in_eth'] = trigger.data.price_in_eth;
                            var[$id || '_status'] = 'open';
                            var['last_id'] = $id + 1;
                            response['message'] = 'Oreder created';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    trigger.data.type == 'make_order_buy' 
                    AND trigger.data.amount
                    AND trigger.data.price_in_gb
                }",
                    "init": "{
                    if(trigger.data.amount <= 0)
                        bounce('The amount must be greater than 0');
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$id || '_owner'] = trigger.address;
                            var[$id || '_type'] = 'buy';
                            var[$id || '_amount'] = trigger.data.amount;
                            var[$id || '_price_in_gb'] = trigger.data.price_in_gb;
                            var[$id || '_status'] = 'open';
                            var['last_id'] = $id + 1;
                            response['message'] = 'Oreder created';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    $thisId = trigger.data.id;
                    trigger.data.type == 'redeem'
                    AND trigger.data.id
                    AND trigger.data.hash
                    AND trigger.data.address
                }",
                    "init": "{
                    if(!var[$thisId || '_owner'])
                        bounce('Order not found');
                    if(var[$thisId || '_type'] != 'buy')
                        bounce('This order is not for purchase');
                    if(var[$thisId || '_status'] != 'open')
                        bounce('Order it not opened');
                        
                    if(trigger.output[[asset=base]].amount != var[$thisId || '_price_in_gb'])
                        bounce('Amount is incorrect');
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$thisId || '_status'] = 'lock';
                            var[$thisId || '_address'] = trigger.data.address;
                            var[$thisId || '_hash'] = trigger.data.hash;
                            var[$thisId || '_unlock_time'] = timestamp + 3600;
                            response['message'] = 'done';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    $thisId = trigger.data.id;
                    trigger.data.type == 'lock'
                    AND trigger.data.id
                    AND trigger.data.address
                    AND trigger.data.hash
                }",
                    "init": "{
                    if(trigger.address != var[$thisId || '_owner'])
                        bounce("You're not the owner");
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$thisId || '_status'] = 'lock';
                            var[$thisId || '_address'] = trigger.data.address;
                            var[$thisId || '_hash'] = trigger.data.hash;
                            var[$thisId || '_unlock_time'] = timestamp + 3600;
                            response['message'] = 'done';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    $thisId = trigger.data.id;
                    trigger.data.type == 'unlock'
                    AND trigger.data.id
                }",
                    "init": "{
                    if(trigger.address != var[$thisId || '_owner'])
                        bounce("You're not the owner");
                    if(timestamp < var[$thisId || '_unlock_time'])
                        bounce('Lock time has not yet expired');
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$thisId || '_status'] = 'open';
                            var[$thisId || '_address'] = false;
                            var[$thisId || '_hash'] = false;
                            var[$thisId || '_unlock_time'] = false;
                            response['message'] = 'done';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    $thisId = trigger.data.id;
                    trigger.data.type == 'withdraw'
                    AND trigger.data.id
                    AND trigger.data.key
                }",
                    "init": "{
                    if (trigger.address != var[$thisId || '_address']){
                        bounce('Incorrect address');
                    }
                    if (sha256(trigger.data.key) != var[$thisId || '_hash']){
                        bounce('Incorrect key');
                    }
                    $fees_aa = var[$thisId || '_amount'] * 0.25 / 100;
                }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "outputs": [
                                    {
                                        "address": "{trigger.address}",
                                        "amount": "{ceil(var[$thisId || '_amount'] - $fees_aa)}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "data",
                            "payload": {
                                "id": "{$thisId}",
                                "timestamp": "{timestamp}",
                                "type": "{var[$thisId || '_type']}",
                                "amount": "{var[$thisId || '_amount']}",
                                "price": "{(var[$thisId || '_price_in_eth']) ? var[$thisId || '_price_in_eth'] : var[$thisId || 'price_in_gb']}"
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var['balance'] = $balance + $fees_aa;
                            var[$thisId || '_owner'] = false;
                            var[$thisId || '_type'] = false;
                            var[$thisId || '_amount'] = false;
                            var[$thisId || '_price_in_eth'] = false;
                            var[$thisId || '_price_in_gb'] = false;
                            var[$thisId || '_status'] = false;
                            var[$thisId || '_address'] = false;
                            var[$thisId || '_hash'] = false;
                            var[$thisId || '_unlock_time'] = false;
                            response['message'] = 'done';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    trigger.data.type == 'remove'
                    AND trigger.data.id
                }",
                    "init": "{
                    $orderId = trigger.data.id;
                    if(!var[$orderId || '_owner'])
                        bounce('Order not found');
                    
                    if(var[$orderId || '_owner'] != trigger.address)
                        bounce('You not owner');
                    
                    if(var[$orderId || '_status'] == 'lock')
                        bounce('Order locked');
                }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                            var[$thisId || '_owner'] = false;
                            var[$thisId || '_type'] = false;
                            var[$thisId || '_amount'] = false;
                            var[$thisId || '_price_in_eth'] = false;
                            var[$thisId || '_price_in_gb'] = false;
                            var[$thisId || '_status'] = false;
                            response['message'] = 'done';
                        }"
                        }
                    ]
                },
                {
                    "if": "{
                    trigger.data.type == 'withdraw_balance'
                }",
                    "init": "{
                    if(trigger.address != '2UDX4Q6BPDHVIXIAWZA3RR5OQGKOS6SA')
                        bounce('You not ownder');
                }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "outputs": [
                                    {
                                        "address": "{trigger.address}",
                                        "amount": "{ceil(var['balance'])}"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var['balance'] = var['balance'] - ceil(var['balance']);
                        }"
                        }
                    ]
                }
            ]
        }
    }
]