[
"autonomous agent",
{
"doc_url": "https://oswap-frontend.herokuapp.com/pair.json",
"init": "{
$asset0 = params.asset0;
$asset1 = params.asset1;
$liquidity = var['liquidity'];
$supply = var['supply'];
$amount0 = trigger.output[[asset=$asset0]];
$amount1 = trigger.output[[asset=$asset1]];
$reserve0 = balance[$asset0] - $amount0;
$reserve1 = balance[$asset1] - $amount1;
if (trigger.data.to AND !is_valid_address(trigger.data.to))
bounce('invalid recipient address');
$to = trigger.data.to OTHERWISE trigger.address;
$deadline = trigger.data.deadline;
$amount_out_min = trigger.data.amount_out_min;
$min0 = $asset0 == 'base' ? 1e5 : 0;
$min1 = $asset1 == 'base' ? 1e5 : 0;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.initialize AND !$liquidity AND $asset0 AND $asset1}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": true,
"auto_destroy": false,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var['liquidity'] = response_unit;
response['liquidity'] = response_unit;
}"
}
]
},
{
"if": "{$liquidity AND $amount0 > $min0 AND $amount1 > $min1}",
"init": "{
if ($reserve1 == 0 OR $reserve0 == 0) {
$issue_amount = balance[$asset0];
return;
}
$ratio = $reserve1 / $reserve0;
$expected_amount1 = round($ratio * $amount0);
if ($expected_amount1 != $amount1)
bounce('wrong ratio of amounts, expected ' || $expected_amount1 || ' of ' || $asset1);
$investor_share = $amount0 / $reserve0;
$issue_amount = round($investor_share * $supply);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$liquidity}",
"outputs": [
{
"address": "{$to}",
"amount": "{$issue_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['supply'] += $issue_amount;
response['liquidity_minted'] = $issue_amount;
}"
}
]
},
{
"if": "{$liquidity AND trigger.output[[asset=$liquidity]]}",
"init": "{
$liquidity_amount = trigger.output[[asset=$liquidity]];
$investor_share = $liquidity_amount / $supply;
$minted0 = round($investor_share * balance[$asset0]);
$minted1 = round($investor_share * balance[$asset1]);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset0}",
"outputs": [
{
"address": "{$to}",
"amount": "{$minted0}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset1}",
"outputs": [
{
"address": "{$to}",
"amount": "{$minted1}"
}
]
}
},
{
"app": "state",
"state": "{
var['supply'] -= $liquidity_amount;
}"
}
]
},
{
"if": "{
$asset0_to_asset1 = ($supply AND $amount0 > $min0 AND $amount1 == ($asset1 == 'base' ? 1e4 : 0));
$asset1_to_asset0 = ($supply AND $amount1 > $min1 AND $amount0 == ($asset0 == 'base' ? 1e4 : 0));
$asset0_to_asset1 || $asset1_to_asset0
}",
"init": "{
if ($deadline AND $deadline < timestamp) bounce('expired');
$asset_out = $asset0_to_asset1 ? $asset1 : $asset0;
$numerator = $asset0_to_asset1 ? $amount0 * $reserve1 * 997 : $amount1 * $reserve0 * 997;
$denominator = $asset0_to_asset1 ? $reserve0 * 1000 + $amount0 * 997 : $reserve1 * 1000 + $amount1 * 997;
$amount_out = round($numerator / $denominator);
if ($amount_out_min AND $amount_out < $amount_out_min) bounce('insufficient output amount');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset_out}",
"outputs": [
{
"address": "{$to}",
"amount": "{$amount_out}"
}
]
}
}
]
}
]
}
}
]