[
"autonomous agent",
{
"doc_url": "https://oswap-frontend.herokuapp.com/oscript/exchange.json",
"init": "{
$asset = params.asset;
$mm_asset = var['mm_asset'];
$asset_reserve = balance[$asset] - trigger.output[[asset=$asset]];
$bytes_reserve = balance[base] - trigger.output[[asset=base]];
if (trigger.data.recipient AND !is_valid_address(trigger.data.recipient))
bounce('wrong recipient address');
$recipient = trigger.data.recipient OTHERWISE trigger.address;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.define AND !$mm_asset AND $asset}",
"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['mm_asset'] = response_unit;
response['mm_asset'] = response_unit;
}"
}
]
},
{
"if": "{$mm_asset AND trigger.output[[asset=base]] > 2e4 AND trigger.output[[asset=$asset]] > 0}",
"init": "{
if ($asset_reserve == 0 OR $bytes_reserve == 0){
$issue_amount = balance[base];
return;
}
$current_ratio = $asset_reserve / $bytes_reserve;
$expected_asset_amount = round($current_ratio * trigger.output[[asset=base]]);
if ($expected_asset_amount != trigger.output[[asset=$asset]])
bounce('wrong ratio of amounts, expected ' || $expected_asset_amount || ' of asset');
$investor_share = trigger.output[[asset=base]] / $bytes_reserve;
$issue_amount = round($investor_share * var['mm_asset_outstanding']);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$mm_asset}",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$issue_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['mm_asset_outstanding'] += $issue_amount;
response['provider'] = trigger.address;
response['byte_amount'] = trigger.output[[asset=base]];
response['asset_amount'] = trigger.output[[asset=$asset]];
response['liquidity_minted'] = $issue_amount;
}"
}
]
},
{
"if": "{$mm_asset AND trigger.output[[asset=$mm_asset]]}",
"init": "{
$mm_asset_amount = trigger.output[[asset=$mm_asset]];
$investor_share = $mm_asset_amount / var['mm_asset_outstanding'];
$byte_amount = round($investor_share * balance[base]);
$asset_amount = round($investor_share * balance[$asset]);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$byte_amount}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$asset_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['mm_asset_outstanding'] -= trigger.output[[asset=$mm_asset]];
response['provider'] = trigger.address;
response['byte_amount'] = trigger.output[[asset=base]];
response['asset_amount'] = trigger.output[[asset=$asset]];
}"
}
]
},
{
"if": "{trigger.output[[asset=base]] > 2e4 AND trigger.output[[asset=$asset]] == 0 AND var['mm_asset_outstanding']}",
"init": "{
$numerator = trigger.output[[asset=base]] * $asset_reserve * 997;
$denominator = $bytes_reserve * 1000 + trigger.output[[asset=base]] * 997;
$amount = round($numerator / $denominator);
response['buyer'] = trigger.address;
response['bytes_sold'] = trigger.output[[asset=base]];
response['assets_bought'] = $amount;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$amount}"
}
]
}
}
]
},
{
"if": "{trigger.output[[asset=$asset]] > 0 AND var['mm_asset_outstanding']}",
"init": "{
$numerator = trigger.output[[asset=$asset]] * $bytes_reserve * 997;
$denominator = $asset_reserve * 1000 + trigger.output[[asset=$asset]] * 997;
$amount = round($numerator / $denominator);
response['buyer'] = trigger.address;
response['assets_sold'] = trigger.output[[asset=$asset]];
response['bytes_bought'] = $amount;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$amount}"
}
]
}
}
]
}
]
}
}
]