[
"autonomous agent",
{
"bounce_fees": {
"base": 10000
},
"init": "{
$POOL_BASE_AA = "
5DNICJLNOBPANH536Y7YVBN5ZDBLQ4LE";
$asset = trigger.output[[asset!="base"]].asset == "none" ? "base" : trigger.output[[asset!="base"]].asset;
if (NOT trigger.data["_fwto"])
bounce("_fwto field is mandatory");
if (NOT is_valid_address(trigger.data["_fwto"]))
bounce("_fwto field is not a valid address");
if (trigger.output[[asset!="base"]].asset == "ambiguous")
bounce("AAs cannot handle payments with multiple assets");
if (NOT trigger.data["_opr"])
bounce("_opr field is mandatory");
if (NOT is_valid_amount(trigger.data["_opr"]))
bounce("_opr must be a positive amount");
if (NOT trigger.data["_pl"])
bounce("_pl field is mandatory");
if (NOT is_aa(trigger.data["_pl"]))
bounce("_pl is not a valid oswap pool");
$definition = definition[trigger.data["_pl"]][1];
if ($definition["base_aa"] != $POOL_BASE_AA)
bounce("That pool version is unsupported");
$getAmount = ($outputAmount, $outputReserve, $inputReserve, $fee)=>{
$swapNoFee = 1e11 - $fee;
$numerator = $outputAmount * $inputReserve * 1e11;
$denominator = ($outputReserve - $outputAmount) * $swapNoFee;
return ceil($numerator / $denominator, 0);
};
$calculateNeededAmount = ()=>{
$asset0 = $definition["params"]["asset0"];
$asset1 = $definition["params"]["asset1"];
$fee = $definition["params"]["swap_fee"];
$reserve0 = balance[trigger.data["_pl"]][$asset0];
$reserve1 = balance[trigger.data["_pl"]][$asset1];
if ($asset == $asset0)
return $getAmount(trigger.data["_opr"], $reserve1, $reserve0, $fee);
return $getAmount(trigger.data["_opr"], $reserve0, $reserve1, $fee);
};
$price = $calculateNeededAmount();
if ($price > ($asset=="base" ? trigger.output[[asset="base"]].amount - 5000 : trigger.output[[asset=$asset]].amount))
bounce("Not enough amount");
$remaining = $asset=="base" ? trigger.output[[asset="base"]].amount - $price - 5000 : trigger.output[[asset=$asset]].amount - $price;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.data["_loa"] OTHERWISE trigger.initial_address}",
"amount": "{$remaining}"
},
{
"address": "{trigger.data["_pl"]}",
"amount": "{$price}"
}
]
}
},
{
"app": "data",
"payload": {
"to": "{trigger.data["_fwto"]}"
}
}
]
}
]