Definition: [
"autonomous agent",
{
"doc_url": "https://raw.githubusercontent.com/jldevelops/aa-len/master/description.json",
"getters": "{
$OSWAP_FACT = 'B22543LKSS35Z55ROU4GDN26RT6MDKWU';
$chechAddr = $addr =>{
if(!is_valid_address($addr))
bounce('not valid '||$addr);
if(!var[$OSWAP_FACT]['pools.'||$addr||'.asset'])
bounce('not oswap pool'||$addr);
true
};
$chechAddresses = $addr =>{
if(!is_array($addr))
bounce('addresses invalid');
if(array_length($addr)<2 OR array_length($addr)>4)
bounce('min 2 max 4 addresses');
foreach($addr,4,$ad => $chechAddr($ad));
true
};
$getPool = $ass => {
if(!$ass)
bounce('asset param not found');
if($ass != 'base' AND !asset[$ass].exists)
bounce('asset invalid');
$data = {};
$data.asset = $ass;
$data.tokens = 0;//actual amount of tokens
$data.t_lm = 0;//last month amount of tokens
$data.t_lm_upd = 0;
var[$ass] OTHERWISE $data
};
$getPoolAmount = $ass => balance[$ass]-($ass == 'base'?storage_size:0);
$getInterestAmount= $inp,$pool =>{
$pct = $inp /(($pool.t_lm_upd+(60*60*48) < timestamp)?$pool.t_lm:$pool.t_llm);
round($getPoolAmount($pool.asset)*$pct)-$inp
};
}",
"init": "{
$loan_fee = 1.01;
$month = 60*60*24*31;
$today = split(timestamp_to_string(timestamp,'date'),'-');
$day_one = $today[2] == '01';
if(trigger.output[[asset!=base]].asset != 'none'){
$asset = trigger.output[[asset!=base]].asset;
if($asset and $asset == 'ambiguous')
bounce('Only 1 asset per tx');
}
else
$asset = 'base';
$to = is_valid_address(trigger.data.to)?trigger.data.to:trigger.address;
}",
"messages": {
"cases": [
{
"if": "{var['addresses']}",
"init": "{
$end = var['index'] == var['last_index'];
if($end){
$pool = var['pool'];
if($pool.asset != $asset)
bounce('failed, not same asset returned');
$diff = trigger.output[[asset=$asset]]-round(var['amount']*$loan_fee);
if($diff <= 0)
bounce('failed, not enough funds');
}
else
$addrs = var['addresses'];
}",
"messages": [
{
"if": "{!$end}",
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$addrs[var['index']]}",
"amount": "{trigger.output[[asset=$asset]]}"
}
]
}
},
{
"if": "{!$end}",
"app": "data",
"payload": {
"to": "{this_address}"
}
},
{
"if": "{$end}",
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{var['to']}",
"amount": "{$diff}"
}
]
}
},
{
"app": "state",
"state": "{
if($end){
var['pool'] = false;
var['amount'] = false;
var['addresses'] = false;
var['index'] = false;
var['to'] = false;
var['last_index'] = false;
response['event'] = 'end_loan';
}
else{
var['index'] += 1;
response['event'] = 'loan';
}
}"
}
]
},
{
"if": "{trigger.data.interest and $day_one}",
"init": "{
$pool = $getPool(trigger.data.asset);
if(var[trigger.address||'_t'||$pool.asset]+$month > timestamp)
bounce('31 days min since last deposit to accrue interest');
if(var[trigger.address||'i_'||$pool.asset] AND var[trigger.address||'i_'||$pool.asset]+(60*60*48)>timestamp)
bounce('already triggered');
$tot = $getInterestAmount(var[trigger.address||'_'||$pool.asset]+0,$pool);
$act = trigger.data.withdraw == 1;
}",
"messages": [
{
"if": "{$act}",
"app": "payment",
"payload": {
"asset": "{$pool.asset}",
"outputs": [
{
"address": "{$to}",
"amount": "{$tot}"
}
]
}
},
{
"app": "state",
"state": "{
if($pool.t_lm == 0){
$pool.t_lm = $pool.tokens;
$pool.t_llm = $pool.tokens;
$pool.t_lm_upd = timestamp;
}
else{
if($pool.t_lm_upd+(60*60*48) < timestamp){
$pool.t_llm = $pool.t_lm;
$pool.t_lm = $pool.tokens;
$pool.t_lm_upd = timestamp;
}
}
if(!$act){
$pool.tokens = $pool.tokens + $tot;
var[trigger.address||'_'||$pool.asset] += $tot;
}
var[$pool.asset] = $pool;
var[trigger.address||'i_'||$pool.asset] = timestamp;
response['event'] = 'interest';
}"
}
]
},
{
"if": "{trigger.data.withdraw}",
"init": "{
$pool = $getPool(trigger.data.asset);
$inp = var[trigger.address||'_'||$pool.asset];
if(trigger.data.amount)
$am = trigger.data.amount < $inp?trigger.data.amount:$inp;
else
$am = $inp;
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$pool.asset}",
"outputs": [
{
"address": "{$to}",
"amount": "{$inp}"
}
]
}
},
{
"app": "state",
"state": "{
$pool.tokens = $pool.tokens - $inp;
var[trigger.address||'_'||$pool.asset] = false;
var[trigger.address||'_t'||$pool.asset] = false;
var[trigger.address||'i_'||$pool.asset] = false;
var[$pool.asset] = $pool;
response['event'] = 'withdraw';
}"
}
]
},
{
"if": "{trigger.data.deposit}",
"init": "{
$data = $getPool($asset);
}",
"messages": [
{
"app": "state",
"state": "{
$data.tokens = $data.tokens + trigger.output[[asset=$asset]];
var[trigger.address||'_'||$asset] += trigger.output[[asset=$asset]];
var[$asset] = $data;
var[trigger.address||'_t'||$asset] = timestamp;
response['event'] = 'deposit';
}"
}
]
},
{
"init": "{
if($asset != 'base')
bounce('no assets here');
if(trigger.data.asset)
$pool = $getPool(trigger.data.asset);
else
$pool = $getPool('base');
if(trigger.data.amount)
$am = trigger.data.amount < $getPoolAmount($pool.asset)?trigger.data.amount:$getPoolAmount($pool.asset);
else
$am = $getPoolAmount($pool.asset);
if($am < 100)
bounce('min loan 100');
$addresses = json_parse(trigger.data.addresses);
$shallnopass = $chechAddresses($addresses);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$pool.asset}",
"outputs": [
{
"address": "{$addresses[0]}",
"amount": "{$am}"
}
]
}
},
{
"app": "data",
"payload": {
"to": "{this_address}"
}
},
{
"app": "state",
"state": "{
var['addresses'] = $addresses;
var['index'] = 1;
var['last_index'] = array_length($addresses);
var['pool'] = $pool;
var['amount'] = $am;
var['to'] = is_valid_address(trigger.data.to)?trigger.data.to:trigger.initial_address;
response['event'] = 'start_loan';
}"
}
]
}
]
}
}
]