Definition: [
    "autonomous agent",
    {
        "bounce_fees": {
            "base": 10000
        },
        "init": "{
    // admin stuff
    $this = {
        name: "swapbook", 
        use_case_1: "Declare your intention by locking money",
        dust_collector:"O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ",  // to collect the dust when the aa is not in use.
        default_duration : 10*24*60*60,
        number_of_offers : json_parse(var["number of offers"] otherwise 0),
        htlc_creator_aa : "TF23TD5WEBWEYPM63GQY7VYDYXSF7ZS6",
    }; 
}",
        "messages": {
            "cases": [
                {
                    "if": "{ !!trigger.data.accept_offer }",
                    "init": "{ 
                $hash = trigger.data.accept_offer;
                if (length($hash) != 44) bounce ("initiation hash must be 44 bytes long");
                $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer"));
                
                if (timestamp > $offer.deadline) bounce ("Too late");
            }",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "owner": "{$offer.owner}",
                                "destination_address": "{trigger.data.accept_with_destination_address_optional otherwise trigger.initial_address}",
                                "hash_of_secret": "{$offer.secret_hash}",
                                "amount": "{$offer.swap_amount}",
                                "asset_id": "{$offer.swap_asset}",
                                "deadline": "{$offer.deadline}"
                            }
                        },
                        {
                            "if": "{ $offer.swap_asset != "base" }",
                            "app": "payment",
                            "payload": {
                                "asset": "{$offer.reward_asset}",
                                "outputs": [
                                    {
                                        "address": "{ $this.htlc_creator_aa }",
                                        "amount": "{ $offer.swap_amount }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $offer.owner }",
                                        "amount": "{ 1 }"
                                    },
                                    {
                                        "if": "{ $offer.swap_asset == "base" }",
                                        "address": "{ $this.htlc_creator_aa }",
                                        "amount": "{ $offer.swap_amount }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{ 
                        var[$hash] = false;   
                        var["number of offers"] = $this.number_of_offers -1;
                        response['message'] = "swap initiated"; 
                    }"
                        }
                    ]
                },
                {
                    "if": "{ !!trigger.data.cancel_offer }",
                    "init": "{ 
                $hash = trigger.data.cancel_offer;
                if (length($hash) != 44) bounce ("cancel hash must be 44 bytes long, and finish with '='");
                $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer"));
                if (trigger.initial_address != $offer.owner) bounce ("You are not the owner");
            }",
                    "messages": [
                        {
                            "if": "{ $offer.swap_asset != "base" }",
                            "app": "payment",
                            "payload": {
                                "asset": "{$offer.reward_asset}",
                                "outputs": [
                                    {
                                        "address": "{ $offer.owner }",
                                        "amount": "{ $offer.swap_amount }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "if": "{ $offer.swap_asset == "base" }",
                                        "address": "{ $offer.owner }",
                                        "amount": "{ $offer.swap_amount }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{ 
                        var[$hash] = false;   
                        var["number of offers"] = $this.number_of_offers -1;
                        response['message'] = "offer cancelled"; 
                    }"
                        }
                    ]
                },
                {
                    "if": "{ $this.number_of_offers < 1 and trigger.address == $this.dust_collector  }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $this.owner }"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    "if": "{ !!trigger.data.offer_secret_hash }",
                    "init": "{ 
                $hash = trigger.data.offer_secret_hash;
                if (length($hash) != 44) bounce ("hash must be 44 bytes long and finish with '='");
                if (!!var[$hash]) bounce ("this secret is already in use");
            }",
                    "messages": [
                        {
                            "app": "state",
                            "state": "{
                        $offer = {
                            owner: trigger.data.offer_owner otherwise trigger.address,
                            deadline: trigger.data.offer_deadline_optional otherwise timestamp + $this.default_duration,
                            secret_hash: $hash,
                            swap_amount: trigger.output[[asset!=base]].amount otherwise trigger.output[[asset=base]].amount-2000,
                            swap_asset: trigger.output[[asset!=base]].asset != "none"? trigger.output[[asset!=base]].asset: "base",
                            htlc_id_on_other_chain: trigger.data.offer_htlc_id_on_other_chain,
                        };
                        var[$hash] = json_stringify($offer);
                        var["number of offers"] = $this.number_of_offers + 1;
                        response['message'] = "Offer recorded"; 
                    }"
                        }
                    ]
                }
            ]
        }
    }
]