[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
// cste
$dust_collector = "
B2UMBICLZSBVDHRC7BYM6BBU4AQEXL2F";
// params
$htlc_destination_address = params.destination_address;
$htlc_hash_of_secret = params.hash_of_secret;
$htlc_amount = params.amount;
$htlc_asset = params.asset_id;
$htlc_duration = json_parse(params.duration);
// states
$htlc_refunded = var["refunded"];
$htlc_withdrawn = var["withdrawn"];
$htlc_funded = var["funded"];
$htlc_funder = var["funder"];
$htlc_fund_amount = json_parse(var["fund_amount"]);
// trigger values
$user = trigger.initial_address;
$temp = trigger.output[[asset!=base]].asset;
$received_asset = $temp != "none" ? $temp : "base";
$received_amount = trigger.output[[asset=$received_asset]].amount;
// case selection
$is_timed_out = timestamp > $htlc_duration ;
$is_recipient = $user == $htlc_destination_address;
$is_help_from_funder = ($user == $htlc_funder) and !!trigger.data.open;
// fund the HTLC
}",
"messages": {
"cases": [
{
"if": "{ !!$htlc_refunded or !!$htlc_withdrawn}",
"messages": [
{
"if": "{$user == $dust_collector}",
"app": "payment",
"payload": {
"asset": "{"base"}",
"outputs": [
{
"address": "{$dust_collector}",
"amount": "{balance[base]-600}"
}
]
}
},
{
"app": "state",
"state": "{ response['message'] = 'Thank you for this donation!'; }"
}
]
},
{
"if": "{ !$htlc_funded}",
"messages": [
{
"app": "state",
"state": "{
// tests
if ($user == $htlc_destination_address)
bounce ("As the htlc recipient, you should not fund it");
if ($received_asset != $htlc_asset)
bounce ("Wrong asset, "||$htlc_asset||" wanted (not "||$received_asset||").");
if ($htlc_amount!= 0) if ($received_amount != $htlc_amount)
bounce ("Wrong amount, "||$htlc_amount||" wanted (not "||$received_amount||").");
$estimated_storage_fee = 100;
// states
var["funded"] = true;
var["funder"] = $user;
var["fund_amount"] = $received_amount - $estimated_storage_fee;
var["deadline"] = timestamp + $htlc_duration;
// responses
response['message'] = 'HTLC funded';
}"
}
]
},
{
"if": "{ !!$is_timed_out or !!$is_recipient or !!$is_help_from_funder}",
"init": "{
if (!!$is_recipient)
{
$message = "You have been recognized but ";
if (!!$is_timed_out)
bounce ($message||"the time is out");
if (!trigger.data.secret)
bounce ($message||"you have to provide the 'secret' revealed on the other chain");
if (sha256(trigger.data.secret) != params.hash_of_secret)
bounce ($message||"the secret is not good.");
}
$transfert_recipient = !!$is_recipient? $htlc_destination_address: $htlc_funder;
$transfert_asset = $htlc_asset;
$transfert_amount = $htlc_fund_amount;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$transfert_asset}",
"outputs": [
{
"address": "{$transfert_recipient}",
"amount": "{$transfert_amount}"
}
]
}
},
{
"if": "{ $transfert_asset != 'base'}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$transfert_recipient}",
"amount": "{balance[base]-1000}"
}
]
}
},
{
"app": "state",
"state": "{
var["withdrawn"] = $is_recipient;
var["refunded"] = !$is_recipient;
response['message'] = !!$is_recipient ? 'Withdrawn' : 'Refunded';
response['donate'] = 'Please donate by sending Gbytes to this address to support developpers';
}"
}
]
},
{
"messages": [
{
"app": "state",
"state": "{
bounce("Please find instructions");
}"
}
]
}
]
}
}
]