Definition: [
"autonomous agent",
{
"init": "{
$owner = 'JBGMJI7XSKLKNHZF26BUBWHVUZSDV3P5';
$addr = trigger.address;
$base_inp = trigger.output[[asset=base]];
if(trigger.output[[asset!=base]].asset != 'none')
$asset = trigger.output[[asset!=base]].asset;
if($asset){
if($asset == 'ambiguous'){
bounce("only 1 asset per tx");
}
if(trigger.output[[asset!=base]] ==0)
bounce("zero asset tx");
$asset_inp = trigger.output[[asset!=base]];
}
if(var['asset']){
if($asset and var['asset'] != $asset)
bounce("not a previously defined asset");
}
$ratio = round(10000000/data_feed[[oracles='F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N', feed_name='GBYTE_USD']]);
//bytes per 1 asset-centUSD-
if(var['amount'] and var['amount'] != 0)
$max_ratio = round((balance[base]-9000)/var['amount']);
}",
"messages": {
"cases": [
{
"if": "{!var['asset']}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": true,
"auto_destroy": true,
"fixed_denominations": false,
"issued_by_definer_only": false,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var['asset'] = response_unit;
var['amount'] = 0;
}"
}
]
},
{
"if": "{
$asset
}",
"messages": [
{
"init": "{
if($ratio > $max_ratio){
bounce('Not enough liquidity to cover, please hodl');
}
$byte_amount = round($asset_inp*0.998*$ratio);
}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$byte_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['amount'] -= $asset_inp;
response['message'] = 'redeemed '||$asset_inp||' centUSD at'||(0.998*$ratio)||' bytes per token';
}"
}
]
},
{
"if": "{
!$asset
}",
"init": "{
$issue_amount = ($base_inp - ($base_inp % $ratio)) / $ratio;
if($issue_amount <= 0){
bounce('Minimum: '||$ratio);
}
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{var['asset']}",
"outputs": [
{
"address": "{$addr}",
"amount": "{$issue_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['amount'] += $issue_amount;
response['message'] = 'issued '||$issue_amount||' centUSD at '||$ratio||' bytes per token';
}"
}
]
}
]
}
}
]