[
"autonomous agent",
{
"doc_url": "https://oswap-frontend.herokuapp.com/pair.json",
"init": "{
$factory = params.factory;
$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;
$toPair = trigger.data.toPair;
$deadline = trigger.data.deadline;
$amountOutMin = trigger.data.amountOutMin;
$min0 = $asset0 == 'base' ? 1e5 : 0;
$min1 = $asset1 == 'base' ? 1e5 : 0;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.initialize AND !$liquidity}",
"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;
}"
}
]
},
{
"if": "{$liquidity AND $amount0 > $min0 AND $amount1 > $min1}",
"init": "{
if ($reserve1 == 0 OR $reserve0 == 0) {
$issueAmount = balance[$asset0];
return;
}
$ratio = $reserve1 / $reserve0;
$expectedAmount1 = round($ratio * $amount0);
if ($expectedAmount1 != $amount1)
bounce('wrong ratio of amounts, expected ' || $expectedAmount1 || ' of ' || $asset1);
$investorShare = $amount0 / $reserve0;
$issueAmount = round($investorShare * $supply);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$liquidity}",
"outputs": [
{
"address": "{$to}",
"amount": "{$issueAmount}"
}
]
}
},
{
"app": "state",
"state": "{
var['supply'] += $issueAmount;
}"
}
]
},
{
"if": "{$liquidity AND trigger.output[[asset=$liquidity]]}",
"init": "{
$liquidityAmount = trigger.output[[asset=$liquidity]];
$investorShare = $liquidityAmount / $supply;
$minted0 = round($investorShare * balance[$asset0]);
$minted1 = round($investorShare * 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'] -= $liquidityAmount;
}"
}
]
},
{
"if": "{
$asset0ToAsset1 = ($supply AND $amount0 > $min0 AND $amount1 == ($asset1 == 'base' ? 1e4 : 0));
$asset1ToAsset0 = ($supply AND $amount1 > $min1 AND $amount0 == ($asset0 == 'base' ? 1e4 : 0));
$liquidity AND ($asset0ToAsset1 || $asset1ToAsset0)
}",
"init": "{
if ($deadline AND $deadline < timestamp) bounce('expired');
$assetOut = $asset0ToAsset1 ? $asset1 : $asset0;
$numerator = $asset0ToAsset1 ? $amount0 * $reserve1 * 997 : $amount1 * $reserve0 * 997;
$denominator = $asset0ToAsset1 ? $reserve0 * 1000 + $amount0 * 997 : $reserve1 * 1000 + $amount1 * 997;
$amountOut = round($numerator / $denominator);
if ($amountOutMin AND $amountOut < $amountOutMin) bounce('insufficient output amount');
$pair = var[$factory]['all_pairs.' || $assetOut || '_' || $toAsset];
if ($toAsset AND !$pair) bounce('invalid asset');
$address = $toAsset ? $pair : $to;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$assetOut}",
"outputs": [
{
"address": "{$address}",
"amount": "{$amountOut}"
}
]
}
},
{
"if": "{$toAsset}",
"app": "data",
"payload": {
"to": "{$to}"
}
}
]
}
]
}
}
]