[
"autonomous agent",
{
"init": "{
$oracles = 'F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N:K237YYRMBYWCJBLSZGLJTXLZVVEXLI2Y'; // Add judge address
$feed_name = 'GBYTE_USD';
$decimal_multiplier_base = 1e9;
$decimal_multiplier_stablecoin = 1e3;
$liquidation_ratio = 1.5;
$liquidation_penalty = 0.1;
$bounce = 1e4;
$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);
$owner = var['cdp:' || $cdp || ':owner'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
if ($owner != trigger.address) bounce('You need to be the owner of the CDP');
if ($liquidation) bounce('The CDP is in liquidation');
if ($amount <= 0) bounce('You need to send more than the bounce fees');
}",
"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;
$amount = trigger.data.amount;
$owner = var['cdp:' || $cdp || ':owner'];
$collateral = var['cdp:' || $cdp || ':collateral'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
$debt = var['cdp:' || $cdp || ':debt'];
if ($owner != trigger.address) bounce('You need to be the owner of the CDP');
if ($liquidation) bounce('The CDP is in liquidation');
if ($amount <= 0) bounce('The amount must be positive');
if ($collateral <= 0) bounce('The CDP collateral must be positive');
if (($collateral - $amount) < 0) bounce('The amount must be lower or equal to the current CDP collateral');
if ($debt) {
$collateralization_ratio = ($rate / $decimal_multiplier_base * ($collateral - $amount)) / ($debt / $decimal_multiplier_stablecoin);
if ($collateralization_ratio >= $liquidation_ratio)
bounce('You need a collateralization ratio over ' || $liquidation_ratio || ', found ' || $collateralization_ratio);
}
}",
"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 ($collateralization_ratio) response['collateralization_ratio'] = $collateralization_ratio;
}"
}
]
},
{
"if": "{trigger.data.draw AND trigger.data.cdp AND trigger.data.amount}",
"init": "{
$cdp = trigger.data.cdp;
$amount = trigger.data.amount;
$owner = var['cdp:' || $cdp || ':owner'];
$debt = var['cdp:' || $cdp || ':debt'];
$collateral = var['cdp:' || $cdp || ':collateral'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
if ($owner != trigger.address) bounce('You need to be the owner of the CDP');
if ($liquidation) bounce('The CDP is in liquidation');
if ($amount <= 0) bounce('The amount must be positive');
if ($collateral <= 0) bounce('The CDP collateral must be positive');
$collateralization_ratio = ($rate / $decimal_multiplier_base * $collateral) / (($debt + $amount) / $decimal_multiplier_stablecoin);
if ($collateralization_ratio >= $liquidation_ratio)
bounce('You need a collateralization ratio over ' || $liquidation_ratio || ', found ' || $collateralization_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;
response['collateralization_ratio'] = $collateralization_ratio;
}"
}
]
},
{
"if": "{trigger.data.wipe AND trigger.data.cdp}",
"init": "{
$cdp = trigger.data.cdp;
$amount = trigger.output[[asset=$asset]];
$owner = var['cdp:' || $cdp || ':owner'];
$debt = var['cdp:' || $cdp || ':debt'];
$collateral = var['cdp:' || $cdp || ':collateral'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
if ($owner != trigger.address) bounce('You need to be the owner of the CDP');
if ($liquidation) bounce('The CDP is in liquidation');
if ($amount <= 0) bounce('You must send stablecoin ' || $asset);
if (!$debt) bounce('The CDP must have debt');
if ($amount > $debt) bounce('You need to send an amount of stablecoin equal or less than current CDP debt');
$collateralization_ratio = ($rate / $decimal_multiplier_base * $collateral) / (($debt + $amount) / $decimal_multiplier_stablecoin);
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':debt'] -= $amount;
var['supply'] -= $amount;
response['message'] = 'Debt reduced by ' || $amount || ' on CDP ' || $cdp;
response['collateralization_ratio'] = $collateralization_ratio;
}"
}
]
},
{
"if": "{trigger.data.bite AND trigger.data.cdp}",
"init": "{
$cdp = trigger.data.cdp;
$amount = trigger.output[[asset=$asset]];
$owner = var['cdp:' || $cdp || ':owner'];
$debt = var['cdp:' || $cdp || ':debt'];
$collateral = var['cdp:' || $cdp || ':collateral'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
if ($liquidation) bounce('The CDP is already in liquidation');
$collateralization_ratio = ($rate / $decimal_multiplier_base * $collateral) / ($debt / $decimal_multiplier_stablecoin);
if ($collateralization_ratio < $liquidation_ratio)
bounce('Require a collateralization ratio lower than ' || $liquidation_ratio || ', found ' || $collateralization_ratio);
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp || ':liquidation'] = true;
response['message'] = 'Triggered the liquidation process for CDP ' || $cdp;
response['cdp'] = $cdp;
}"
}
]
}
]
}
}
]