Definition: [
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
// admin stuff
$this = {
name: "swapbook",
use_case_1: "Declare your intention by locking money",
owner:"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 : "QRHA5GXYHDBXCHSU2FPTMXBRRUXUNDUT",
};
}",
"messages": {
"cases": [
{
"if": "{ !!trigger.data.initiate }",
"init": "{
$hash = trigger.data.initiate;
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.destination_address 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 }",
"init": "{
$hash = trigger.data.cancel;
if (length($hash) != 44) bounce ("cancel hash must be 44 bytes long");
$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.owner }",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{ $this.owner }"
}
]
}
}
]
},
{
"if": "{ !!trigger.data.secret_hash }",
"init": "{
$hash = trigger.data.secret_hash;
if (length($hash) != 44) bounce ("hash must be 44 bytes long");
if (!!var[$hash]) bounce ("this secret is already in use");
}",
"messages": [
{
"app": "state",
"state": "{
$offer = {
owner: trigger.data.owner otherwise trigger.address,
deadline: trigger.data.deadline 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",
};
var[$hash] = json_stringify($offer);
var["number of offer"] = $this.number_of_offers + 1;
response['message'] = "Offer recorded";
}"
}
]
}
]
}
}
]