[
"autonomous agent",
{
"init": "{
$owner = params.owner;
if(!$owner)
bounce('no owner');
if($owner != trigger.address)
bounce('You are not the owner');
$curve_address = trigger.data.curve_address;
if (!exists($curve_address) OR !is_valid_address($curve_address))
bounce('bad curve_address');
$asset1 = var[$curve_address]['asset1'];
$asset2 = var[$curve_address]['asset2'];
if (!$asset1 OR !$asset2)
bounce('curve not found');
}",
"messages": {
"cases": [
{
"if": "{ trigger.data.withdraw }",
"init": "{
$tokens1 = trigger.data.tokens1 OTHERWISE 0;
$tokens2 = trigger.data.tokens2 OTHERWISE 0;
$balance1 = balance[$asset1];
$balance2 = balance[$asset2];
$withdraw_all = $tokens1 == 0 AND $tokens2 == 0;
}",
"messages": [
{
"if": "{ $withdraw_all OR $tokens1 > 0 }",
"app": "payment",
"payload": {
"asset": "{$asset1}",
"outputs": [
{
"address": "{$owner}",
"amount": "{ $tokens1 > 0 ? $tokens1 : $balance1 }"
}
]
}
},
{
"if": "{ $withdraw_all OR $tokens2 > 0}",
"app": "payment",
"payload": {
"asset": "{$asset2}",
"outputs": [
{
"address": "{$owner}",
"amount": "{ $tokens2 > 0 ? $tokens2 : $balance2 }"
}
]
}
},
{
"if": "{ $withdraw_all OR $tokens1 > 0 OR $tokens2 > 0 }",
"app": "state",
"state": "{
response['action'] = "withdraw";
}"
}
]
},
{
"init": "{
if(!exists(var[$curve_address]['p2']))
bounce('p2 not initialized');
$canBeDone = balance[$asset1] > 0 AND balance[$asset2] > 0;
$curve_params = definition[$curve_address][1].params;
if(trigger.output[[asset=$asset1]] > 0){
response['message'] = "Added " || (trigger.output[[asset=$asset1]] / 10^$curve_params.decimals1) || " T1 tokens";
}
if (trigger.output[[asset=$asset2]] > 0){
response['message'] = "Added " || (trigger.output[[asset=$asset2]] / 10^$curve_params.decimals2) || " T2 tokens";
}
if($canBeDone){
$initial_p2 = var[$curve_address]['p2'];
$dilution_factor = var[$curve_address]['dilution_factor'];
$m = $curve_params.m;
$n = $curve_params.n;
$auto_withdraw = trigger.data.auto_withdraw;
$p2 = var[$curve_address]['p2'];
$new_supply1 = var[$curve_address]['supply1'] - balance[$asset1];
$new_supply2 = var[$curve_address]['supply2'] - balance[$asset2];
$s1 = $new_supply1 / 10^$curve_params.decimals1;
$s2 = $new_supply2 / 10^$curve_params.decimals2;
$expectT1 = (($p2 / ($dilution_factor * $n * (is_integer($n*2) ? sqrt($s2^(($n-1)*2)) : $s2^($n-1))))^(1 / $m)) - (var[$curve_address]['supply1'] / 10^$curve_params.decimals1);
$expectT2 = (($p2 / ($dilution_factor * ($s1^$m) * $n)) ^ (1 / ($n - 1))) - var[$curve_address]['supply2'] / 10 ^ $curve_params.decimals2;
$expectT1WithoutDecimals = abs(round($expectT1 * 10^$curve_params.decimals1));
$expectT2WithoutDecimals = abs(round($expectT2 * 10^$curve_params.decimals2));
if(abs($expectT2WithoutDecimals) < balance[$asset2]) {
$count1 = balance[$asset1];
$count2 = $expectT2WithoutDecimals;
$change1 = 0;
$change2 = balance[$asset2] - $count2;
} else if(abs($expectT1WithoutDecimals) < balance[$asset1]){
$count1 = $expectT1WithoutDecimals;
$count2 = balance[$asset2];
$change1 = balance[$asset1] - $count1;
$change2 = 0;
} else {
bounce("Couldn't calculate optimal quantity");
}
response['count1'] = $count1;
response['count2'] = $count2;
response['change1'] = $change1;
response['change2'] = $change2;
}
}",
"messages": [
{
"if": "{$canBeDone}",
"app": "payment",
"payload": {
"asset": "{$asset1}",
"outputs": [
{
"address": "{$curve_address}",
"amount": "{ $count1 }"
},
{
"address": "{$owner}",
"amount": "{ $change1 }",
"if": "{ $auto_withdraw }"
}
]
}
},
{
"if": "{$canBeDone}",
"app": "payment",
"payload": {
"asset": "{$asset2}",
"outputs": [
{
"address": "{$curve_address}",
"amount": "{ $count2 }"
},
{
"address": "{$owner}",
"amount": "{ $change2 }",
"if": "{ $auto_withdraw }"
}
]
}
},
{
"if": "{$canBeDone}",
"app": "data",
"payload": {
"to": "{$owner}"
}
},
{
"app": "state",
"state": "{
}"
}
]
}
]
}
}
]