[
"autonomous agent",
{
"init": "{
$oracles = 'F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N';
$feed_name = 'GBYTE_USD';
$liquidation_ratio = 1.5;
$liquidation_penalty = 0.1;
$fee = 0.5;
$bounce = 10000;
$asset = var['asset'];
$supply = var['supply'];
$rate = data_feed[[oracles=$oracles, feed_name=$feed_name, ifnone=false]];
$is_active = ($asset AND $rate);
}",
"messages": {
"cases": [
{
"if": "{trigger.data.define AND !$asset}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": true,
"auto_destroy": true,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var['asset'] = response_unit;
response['asset'] = response_unit;
}"
}
]
},
{
"if": "{$is_active AND trigger.data.open}",
"init": "{
$cdp = (var['cdp:index'] + 1);
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':owner'] = trigger.address;
var['cdp:index'] = $cdp;
response['message'] = 'Opened CDP ' || $cdp;
response['cdp'] = $cdp;
}"
}
]
},
{
"if": "{trigger.data.lock AND trigger.data.cdp}",
"init": "{
$cdp = trigger.data.cdp;
$amount = (trigger.output[[asset=base]] - $bounce);
if ($amount <= 0)
bounce('You need to send more than the bounce fees');
if (var['cdp:' || $cdp || ':owner'] != trigger.address)
bounce('You need to be the owner of the CDP');
if (var['cdp:' || $cdp || ':liquidation'])
bounce('The CDP is in liquidation');
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':collateral'] += $amount;
response['message'] = 'Added ' || $amount || ' as collateral for CDP ' || $cdp;
response['cdp'] = $cdp;
response['amount'] = $amount;
}"
}
]
},
{
"if": "{trigger.data.free AND trigger.data.cdp AND trigger.data.amount}",
"init": "{
$cdp = trigger.data.cdp;
if (var['cdp:' || $cdp || ':owner'] != trigger.address)
bounce('You need to be the owner of the CDP');
if (var['cdp:' || $cdp || ':liquidation'])
bounce('The CDP is in liquidation');
// @TODO Bounce request if the collateral ratio is under the liquidation ratio
$amount = trigger.data.amount;
$collateral = var['cdp:' || $cdp || ':collateral'];
$debt = var['cdp:' || $cdp || ':debt'];
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':collateral'] -= $amount;
response['message'] = 'Reclaimed ' || $amount || ' bytes from CDP ' || $cdp;
}"
}
]
},
{
"if": "{trigger.data.draw AND trigger.data.cdp AND trigger.data.amount}",
"init": "{
$cdp = trigger.data.cdp;
if (var['cdp:' || $cdp || ':owner'] != trigger.address)
bounce('You need to be the owner of the CDP');
if (var['cdp:' || $cdp || ':liquidation'])
bounce('The CDP is in liquidation');
$amount = trigger.data.amount;
if ($amount <= 0)
bounce('The amount must be positive');
// @TODO Bounce request if the collateral ratio is under the liquidation ratio
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{$amount}"
}
]
}
},
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':debt'] += $amount;
var['supply'] += $amount;
response['message'] = 'Sent fresh stablecoin for ' || trigger.address;
}"
}
]
},
{
"if": "{trigger.data.wipe AND trigger.data.cdp}",
"init": "{
$cdp = trigger.data.cdp;
$amount = trigger.output[[asset=$asset]];
if (var['cdp:' || $cdp || ':owner'] != trigger.address)
bounce('You need to be the owner of the CDP');
if (var['cdp:' || $cdp || ':liquidation'])
bounce('The CDP is in liquidation');
// @TODO Check amount
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':debt'] -= $amount;
var['supply'] -= $amount;
response['message'] = 'Debt reduced by ' || $amount || ' on CDP ' || $cdp;
}"
}
]
}
]
}
}
]