[
"autonomous agent",
{
"init": "{
//oracle stablecoin
//day 2 of every month users could deposit to be guarantors. guarantors earn a percentage of total exchanged amount in return.
//all days except day 1 anyone could deposit bytes in exchange for a token (centUSD) and viceversa
//day 1 of every month is the only day allowed to take their part of the fees
//guarantors cant retire if byte value goes below certain threshold
$FEE = 1000;
$COLAT = 4;//how many dollars (in bytes) backs up a deposited dollar, min 1
$STABLE_FEE = 0.01;//percent of bytes from users going to hands of guarantors
$addr = trigger.address;
$base_inp = trigger.output[[asset=base]];
$balance = balance[base]-$base_inp;
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['stable']){
if($asset and var['stable'] != $asset)
bounce("not a allowed asset");
}
$gb_usd = data_feed[[oracles='F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N', feed_name='GBYTE_USD']];
$ratio = round(10000000/$gb_usd);//bytes per 1 asset-centUSD-
$total_val = $balance*$gb_usd/10000000;
$stable_val = var['stable_issued']/100;
$day = substring(timestamp_to_string(timestamp), 8, 2);
$allowed_fee = $day == '01';
$allowed_deposit = $day == '02';
}",
"messages": {
"cases": [
{
"if": "{!var['stable']}",
"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['stable'] = response_unit;
var['stable_issued'] = 0;
var['stable_max'] = 0;
var['colat_issued'] = 0;
}"
}
]
},
{
"if": "{
var['colat_'||$addr] and var['colat_'||$addr] > 0 and trigger.data.take_fees
}",
"init": "{
if(!$allowed_fee) bounce('Fee collection only allowed first day of month');
$tot_fees = var['total_fees_lm'] otherwise var['total_fees'];
$FEEs_for_me = round(var['colat'||$addr]/var['colat_issued']*$tot_fees);
$last_pay = attestation[[attestors=this_address, address=$addr]].last_pay;
if($last_pay and timestamp_to_string(timestamp,'date') == $last_pay){
bounce('Fees for you already taken');
}
$byte_amount = $base_inp - $FEE + $FEEs_for_me-$alr_wd;
}",
"messages": [
{
"app": "attestation",
"payload": {
"address": "{$addr}",
"profile": {
"last_pay": "{timestamp_to_string(timestamp,'date')}"
}
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$byte_amount}"
}
]
}
},
{
"app": "state",
"state": "{
if(!var['total_fees_lm'] and var['total_fees'] > 0){
var['total_fees_lm'] = var['total_fees'];
var['total_fees'] = 0;
}
}"
}
]
},
{
"if": "{
trigger.data.colat == 'withdraw'
}",
"init": "{
if(var['stable_issued'] >= var['stable_max'])
bounce('Cant retire colat, please retire centUSD from circulation first');
$max_to_retire = round((var['stable_max'] - var['stable_issued'])/$gb_usd*10000000/$COLAT);
$state = $total_val - (var['stable_issued']/100*($COLAT+1));
if($state <= 0)
bounce('Cant retire colat, not enough edge for amount issued');
$byte_amount = (var['colat_'||$addr]*4);
if($byte_amount > $max_to_retire)
bounce('what to do on this case');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$base_inp+$byte_amount-$FEE}"
}
]
}
},
{
"app": "state",
"state": "{
var['colat_issued'] -= var['colat_'||$addr];
var['colat_'||$addr] = false;
var['stable_max'] -= $byte_amount*$gb_usd/10000000;
response['message'] = 'redeemed '||$byte_amount||' colat';
}"
}
]
},
{
"if": "{
!$asset AND trigger.data.colat == 'deposit'
}",
"init": "{
if(!$allowed_deposit) bounce('Deposit only allowed second day of month');
$issue_amount = round($base_inp/$COLAT);
}",
"messages": [
{
"app": "state",
"state": "{
if(var['total_fees_lm']) var['total_fees_lm'] = false;
var['colat_issued'] += $issue_amount;
var['colat_'||$addr] += $issue_amount;
var['stable_max'] += $issue_amount*$gb_usd;
response['message'] = 'issued '||$issue_amount||' colat';
}"
}
]
},
{
"if": "{
$asset
}",
"messages": [
{
"init": "{
if($day == '01') bounce('Service unavaliable first day of month');
$byte_amount = round($asset_inp*((1-$STABLE_FEE)*$ratio));
if($byte_amount < $balance)
$am = $base_inp-$FEE+$byte_amount;
else
$am = $balance-$FEE;
}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$am}"
}
]
}
},
{
"app": "state",
"state": "{
if(var['total_fees_lm']) var['total_fees_lm'] = false;
var['stable_issued'] -= $asset_inp;
var['total_fees'] += round($asset_inp*($STABLE_FEE*$ratio));
response['message'] = 'redeemed '||$asset_inp||' centUSD';
}"
}
]
},
{
"if": "{
!$asset
}",
"init": "{
if($day == '01') bounce('Service unavaliable first day of month');
if(var['stable_issued'] >= var['stable_max'])
bounce('Not enough liquidity, please add collateral');
$issue_amount = round((($base_inp-$FEE)/$ratio)*(1-$STABLE_FEE));
if((var['stable_issued']+$issue_amount) >= var['stable_max'])
bounce('Not enough liquidity, max amount available, send less bytes');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{var['stable']}",
"outputs": [
{
"address": "{$addr}",
"amount": "{$issue_amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['stable_issued'] += $issue_amount;
if(var['total_fees_lm']) var['total_fees_lm'] = false;
var['total_fees'] += round(($base_inp-$FEE)/$ratio*$STABLE_FEE);
response['message'] = 'issued '||$issue_amount||' centUSD';
}"
}
]
}
]
}
}
]