[
    "autonomous agent",
    {
        "init": "{
        $i = trigger.data;
        // How is works
            $INSTRUCTIONS = "Create a locked guarantee with 'create' = <unique account name> and 'owner' = <address of the owner for who you should lock a guarantee for>. Once locked, it will require consensus between the 2 parties on how to distribute it to unlock it. Both parties should use 'proposition' = <part of the guarantee you are agree to release for the other party>. When the sum of the 2 propositions is equal to the guarantee amount, the guarantee is unlock and distribute accordingly to  the 2 parties.";
        // Owner of the AA can withdraw the dust
            $AA_OWNER = "
MYESSCFGDERS3YIEGNZDOG2BI5HKQHLU";
            $AA_NAME = "ARGAA";  // A Renting Guarantee Autonomous Agent
        
        // Account name is used to store de related information
            $account_name = $i.account_name otherwise $i.create;
            $key = $account_name||"_";
        // There is 2 parties, owner and renter, known from the AA state or from the input during creation of the account
            $owner = var[$key||"owner"] otherwise $i.owner otherwise $i.o otherwise bounce ("need 'owner' address!");
            $renter = var[$key||"renter"] otherwise trigger.address;
        // the object of the account is the guarantee, known a existing account or equal to the funds sent during the creation
            $guarantee = var[$key||"guarantee"] otherwise trigger.output[[asset=base]]-1000;
        // For the gurantee to be release, both parties should send proposition of release for the other party:
            $owner_proposition =  var[$key||"owner_proposition"] otherwise 0;
            $renter_proposition = var[$key||"renter_proposition"] otherwise 0;
            $trigger_proposition = $i.proposition otherwise $i.p otherwise false;
        // the AA is keeping in mind the total amount of guarantee to have a way to now the dust available in its balance:
            $total = var["total"] otherwise 0;
    }",
        "messages": {
            "cases": [
                {
                    "if": "{ !!$i.create and !!$owner}",
                    "init": "{ if (var[$key||"guarantee"]) bounce ("Already a guarantee with this name"); }",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "account_name": "{ $account_name }",
                                "guarantee": "{ $guarantee }",
                                "aa": "{ this_address }"
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $owner }",
                                        "amount": "{ 1 }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            var[$key||"owner"] = $owner;       var[$key||"guarantee"] = $guarantee; 
                            var[$key||"renter"] = $renter;     var["total"] = $total + $guarantee;
                            
                            response['message'] = $guarantee||" bytes locked as guarantee.";
                        }"
                        }
                    ]
                },
                {
                    "if": "{ !!$i.account_name and !!$trigger_proposition }",
                    "init": "{ 
                // Exceptions
                    if ($trigger_proposition > $guarantee) bounce ("The proposition is larger than the full guarantee!");
                    if (trigger.address != $renter and trigger.address != $owner) bounce ("You are not part of the agreement!");
                    
                // Lets find which party is giving a new proposition
                    $new_owner_proposition = trigger.address == $owner ? $trigger_proposition : $owner_proposition;
                    $new_renter_proposition = trigger.address == $renter ? $trigger_proposition : $renter_proposition;
                    
                // Agreement is found if both propositions have been received and their sum is equal to the guarantee
                    $agreement_found = ( $new_owner_proposition + $new_renter_proposition ) == $guarantee;
                    
                // Distribute the guarantee? send a symbolic byte to make a party aware of the new proposition?
                    $amount_to_send_to_renter = $agreement_found ? $new_owner_proposition : trigger.address == $owner  ? 1:0;
                    $amount_to_send_to_owner = $agreement_found ? $new_renter_proposition : trigger.address == $renter ? 1:0;
                }",
                    "messages": [
                        {
                            "app": "data",
                            "payload": {
                                "account_name": "{ $account_name }",
                                "guarantee": "{ $guarantee }",
                                "agreement": "{ $agreement_found }",
                                "renter_proposition": "{ $new_renter_proposition }",
                                "owner_proposition": "{ $new_owner_proposition }",
                                "aa": "{ this_address }"
                            }
                        },
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $renter         }",
                                        "amount": "{ $amount_to_send_to_renter         }"
                                    },
                                    {
                                        "address": "{ $owner          }",
                                        "amount": "{ $amount_to_send_to_owner          }"
                                    },
                                    {
                                        "address": "{ trigger.address }",
                                        "amount": "{ trigger.output[[asset=base]]-2000 }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{
                            if ($agreement_found)
                            {   // clear the full state
                                var[$key||"renter"] = false;       var[$key||"renter_proposition"] = false;    
                                var[$key||"owner"] = false;        var[$key||"owner_proposition"] = false;
                                var[$key||"guarantee"] =  false;   var["total"] = $total - $guarantee;
                                response['message'] = "Agreement found and funds have been sent back to parties ^^";
                            }
                            else
                            { // safe the proposition
                                var[$key||"renter_proposition"] = $new_renter_proposition;
                                var[$key||"owner_proposition"] = $new_owner_proposition;
                                response['message'] = "Proposition recorded ("||$trigger_proposition||" bytes), the other party should set 'amount' to "|| ($guarantee - $trigger_proposition)||" bytes to find agreement ^^";
                            }
                        }"
                        }
                    ]
                },
                {
                    "init": "{ if (trigger.address != $AA_OWNER) bounce ($INSTRUCTIONS); }",
                    "messages": [
                        {
                            "app": "payment",
                            "payload": {
                                "asset": "base",
                                "outputs": [
                                    {
                                        "address": "{ $AA_OWNER }",
                                        "amount": "{ balance[base] - $total - 1000 }"
                                    }
                                ]
                            }
                        },
                        {
                            "app": "state",
                            "state": "{ response['message'] = "Dust sent to AA owner"; }"
                        }
                    ]
                }
            ]
        }
    }
]