[
"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_htlc_duration : 2*24*60*60, // 2 days
default_duration : 10*24*60*60, // 10 days
number_of_offers : json_parse(var["number of offers"] otherwise 0),
htlc_creator_aa : "
GALNCFV3O34RAEMI3YCKNCGQKJWDZ6L5",
};
}",
"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"));
$destination_address = trigger.data.accept_with_destination_address_optional otherwise trigger.initial_address;
if (timestamp > $offer.deadline) bounce ("Too late");
// calculating the address of the HTLC taht will be create:
$aa_definition = ['autonomous agent', {
base_aa: $this.htlc_base_aa ,
params: {
destination_address: $destination_address,
hash_of_secret: $offer.secret_hash,
amount: $offer.swap_amount,
asset_id: $offer.swap_asset,
duration: $offer.htlc_duration,
}
}
];
}",
"messages": [
{
"app": "data",
"payload": {
"owner": "{$offer.owner}",
"destination_address": "{$destination_address}",
"hash_of_secret": "{$offer.secret_hash}",
"amount": "{$offer.swap_amount}",
"asset_id": "{$offer.swap_asset}",
"duration": "{$offer.duration}"
}
},
{
"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";
response['HTLC_address'] = chash160($aa_definition);
}"
}
]
},
{
"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,
htlc_duration: trigger.data.offer_htlc_duration_optional otherwise $this.default_htlc_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",
deadline: timestamp + (trigger.data.offer_duration_optional otherwise $this.default_duration),
};
var[$hash] = json_stringify($offer);
var["number of offers"] = $this.number_of_offers + 1;
response['message'] = "Offer recorded";
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
response['message'] = "No case true";
}"
}
]
}
]
}
}
]