GHZL7TY2KJOIJK3K2OQWH6F45Q6JMLVY
[
"autonomous agent",
{
"init": "{
$pool_aa = params.pool_aa;
$fee = 2000;
$pool_params = definition[$pool_aa][1].params;
$asset0 = $pool_params.asset0;
$asset1 = $pool_params.asset1;
$amount0 = trigger.output[[asset=$asset0]] - ($asset0 == 'base' ? $fee : 0);
$amount1 = trigger.output[[asset=$asset1]] - ($asset1 == 'base' ? $fee : 0);
if ($amount0 <= 0)
bounce("amount0 = " || $amount0);
if ($amount1 <= 0)
bounce("amount1 = " || $amount1);
$reserve0 = balance[$pool_aa][$asset0];
$reserve1 = balance[$pool_aa][$asset1];
$ratio = $reserve1 / $reserve0;
$expectedAmount1 = round($ratio * $amount0);
if ($amount1 > $expectedAmount1){
$change0 = 0;
$change1 = $amount1 - $expectedAmount1;
}
else if ($amount1 < $expectedAmount1){ // amount0 is too large
$used_amount0 = floor($amount1 / $ratio);
if ($used_amount0 > $amount0)
bounce("BUG: " || $used_amount0 || " > " || $amount0);
$newExpectedAmount1 = round($ratio * $used_amount0);
if ($amount1 < $newExpectedAmount1)
bounce("rounding issue: adjusted amount0 from " || $amount0 || " to " || $used_amount0 || " but amount1 " || $amount1 || " is still less than the new expected amount1 " || $newExpectedAmount1);
$change0 = $amount0 - $used_amount0;
$change1 = $amount1 - $newExpectedAmount1;
}
else{
$change0 = 0;
$change1 = 0;
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset0}",
"outputs": [
{
"address": "{$pool_aa}",
"amount": "{$amount0 - $change0}"
},
{
"address": "{trigger.address}",
"amount": "{$change0}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset1}",
"outputs": [
{
"address": "{$pool_aa}",
"amount": "{$amount1 - $change1}"
},
{
"address": "{trigger.address}",
"amount": "{$change1}"
}
]
}
},
{
"if": "{$asset0 != 'base' AND $asset1 != 'base'}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$pool_aa}"
}
]
}
}
]
}
]
AAs based on this template Show transactions in assets:
No transactions found, it may be worth changing the filter
Unit ID
Date
From
Direction
To
Amount