[
"autonomous agent",
{
"init": "{
// $asset = params.asset;
$asset = 'WhsD5Y/1NeKROgQojGwfe9EBC7NH4KnAk4CorCrVRhs=';
$mm_asset = var['mm_asset'];
$total_supply = var['total_supply'];
}",
"messages": {
"cases": [
{
"if": "{trigger.data.setup 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]] > 1e5 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": "{trigger.address}",
"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": "{trigger.address}",
"amount": "{ round($investor_share * balance[$asset]) }"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{ round($investor_share * balance[base]) }"
}
]
}
},
{
"app": "state",
"state": "{
var['mm_asset_outstanding'] -= trigger.output[[asset=$mm_asset]];
}"
}
]
},
{
"if": "{trigger.output[[asset=base]] > 1e5 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 = $asset_balance - $new_asset_balance; // we can deduct exchange fees here
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.address}",
"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 = $bytes_balance - $new_bytes_balance; // we can deduct exchange fees here
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{ $amount }"
}
]
}
}
]
}
]
}
}
]