[
"autonomous agent",
{
"doc_url": "https://oswap-frontend.herokuapp.com/oscript/exchange.json",
"init": "{
// $asset = params.asset;
$asset = 'WhsD5Y/1NeKROgQojGwfe9EBC7NH4KnAk4CorCrVRhs=';
$mm_asset = var['mm_asset'];
$recipient = is_valid_address(trigger.data.recipient) ? trigger.data.recipient : trigger.address;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.define AND !$mm_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": "{
$asset_balance = balance[$asset] - trigger.output[[asset=$asset]];
$bytes_balance = balance[base] - trigger.output[[asset=base]];
if ($asset_balance == 0 OR $bytes_balance == 0){ // initial deposit
$issue_amount = balance[base];
return;
}
$current_ratio = $asset_balance / $bytes_balance;
$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_of_prev_balance = trigger.output[[asset=base]] / $bytes_balance;
$issue_amount = round($investor_share_of_prev_balance * 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;
}"
}
]
},
{
"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'];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$recipient}",
"amount": "{round($investor_share * balance[$asset])}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$recipient}",
"amount": "{round($investor_share * balance[base])}"
}
]
}
},
{
"app": "state",
"state": "{
var['mm_asset_outstanding'] -= trigger.output[[asset=$mm_asset]];
}"
}
]
},
{
"if": "{trigger.output[[asset=base]] > 2e4 AND trigger.output[[asset=$asset]] == 0 AND var['mm_asset_outstanding']}",
"init": "{
$asset_balance = balance[$asset] - trigger.output[[asset=$asset]];
$bytes_balance = balance[base] - trigger.output[[asset=base]];
// other formula can be used for product, e.g. $asset_balance * $bytes_balance ^ 2
$p = $asset_balance * $bytes_balance;
$new_asset_balance = round($p / balance[base]);
$amount = round(($asset_balance - $new_asset_balance) / 1000 * 997); // we can deduct exchange fees here
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$amount}"
}
]
}
}
]
},
{
"if": "{trigger.output[[asset=$asset]] > 0 AND var['mm_asset_outstanding']}",
"init": "{
$asset_balance = balance[$asset] - trigger.output[[asset=$asset]];
$bytes_balance = balance[base] - trigger.output[[asset=base]]; // 10Kb fee
// other formula can be used for product, e.g. $asset_balance * $bytes_balance ^ 2
$p = $asset_balance * $bytes_balance;
$new_bytes_balance = round($p / balance[$asset]);
$amount = round(($bytes_balance - $new_bytes_balance) / 1000 * 997); // we can deduct exchange fees here
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$recipient}",
"amount": "{$amount}"
}
]
}
}
]
}
]
}
}
]