[
"autonomous agent",
{
"init": "{
$oracles = 'F4KHJUCLJKY4JV7M5F754LAJX4EB7M4N:K237YYRMBYWCJBLSZGLJTXLZVVEXLI2Y'; // Add your address here to simulate another rate
$feed_name = 'GBYTE_USD';
$decimal_multiplier_base = 1e9; // Multiplier GB to bytes
$decimal_multiplier_ousd = 1e3; // Multiplier for OUSD (1 OUSD = 1,000)
$liquidation_ratio = 1.5; // Require $1.5 worth of bytes as collateral to lend 1 OUSD
$auction_period = 60 * 30; // 30 minutes
$bounce = 1e4;
$asset = var['asset'];
$supply = var['supply'];
$rate = data_feed[[oracles=$oracles, feed_name=$feed_name, ifnone=false]];
$is_active = ($asset AND $rate);
$cdp = trigger.data.cdp;
if ($cdp) {
$owner = var['cdp:' || $cdp || ':owner'];
$collateral = var['cdp:' || $cdp || ':collateral'];
$debt = var['cdp:' || $cdp || ':debt'];
$liquidation = var['cdp:' || $cdp || ':liquidation'];
$auction_end = var['cdp:' || $cdp || ':auction:end'];
$auction_best_bid = var['cdp:' || $cdp || ':auction:best-bid'];
$auction_best_bid_from = var['cdp:' || $cdp || ':auction:best-bid:from'];
$is_valid = var['cdp:' || $cdp];
$is_owner = (trigger.address == $owner);
}
}",
"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['message'] = 'Asset created';
response['asset'] = response_unit;
}"
}
]
},
{
"if": "{$is_active AND trigger.data.open}",
"init": "{
$cdp_index = (var['cdp:index'] + 1);
}",
"messages": [
{
"app": "state",
"state": "{
var['cdp:' || $cdp_index] = true;
var['cdp:' || $cdp_index || ':owner'] = trigger.address;
var['cdp:index'] = $cdp_index;
response['message'] = 'Opened CDP ' || $cdp_index;
response['cdp'] = $cdp_index;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.close}",
"init": "{
if (!$is_owner) bounce('You need to be the owner of the CDP');
if ($liquidation) bounce('You can not close a CDP in liquidation');
if ($debt != 0) bounce('You must have 0 debt, got ' || $debt);
}",
"messages": [
{
"app": "payment",
"if": "{$collateral > 0}",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$owner}",
"amount": "{$collateral}"
}
]
}
},
{
"app": "state",
"state": "{
var['cdp:' || $cdp] = false;
var['cdp:' || $cdp || ':owner'] = false;
var['cdp:' || $cdp || ':collateral'] = false;
var['cdp:' || $cdp || ':debt'] = false;
var['cdp:' || $cdp || ':liquidation'] = false;
var['cdp:' || $cdp || ':auction:end'] = false;
var['cdp:' || $cdp || ':auction:best-bid'] = false;
var['cdp:' || $cdp || ':auction:best-bid:from'] = false;
response['message'] = 'Closed CDP ' || $cdp;
response['cdp'] = $cdp;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.lock}",
"init": "{
$amount = (trigger.output[[asset=base]] - $bounce);
if (!$is_owner) 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": "{$is_valid AND trigger.data.free AND trigger.data.amount}",
"init": "{
$amount = trigger.data.amount;
if (!$is_owner) 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 > 0) {
$collateralization_ratio = (($collateral - $amount) * $rate / $decimal_multiplier_base) / ($debt / $decimal_multiplier_ousd);
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": "{$is_valid AND trigger.data.draw AND trigger.data.amount}",
"init": "{
$amount = trigger.data.amount;
if (!$is_owner) 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 = ($collateral * $rate / $decimal_multiplier_base) / (($debt + $amount) / $decimal_multiplier_ousd);
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 OUSD for ' || trigger.address;
response['collateralization_ratio'] = $collateralization_ratio;
response['rate'] = $rate;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.wipe}",
"init": "{
$amount = trigger.output[[asset=$asset]];
if (!$is_owner) 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 OUSD ' || $asset);
if (!$debt) bounce('The CDP must have debt');
if ($amount > $debt) bounce('You need to send an amount of OUSD equal or less than current CDP debt');
$collateralization_ratio = ($rate / $decimal_multiplier_base * $collateral) / (($debt - $amount) / $decimal_multiplier_ousd);
}",
"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;
response['rate'] = $rate;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.bite}",
"init": "{
$amount = trigger.output[[asset=$asset]];
if ($liquidation) bounce('The CDP is already in liquidation');
$collateralization_ratio = ($rate / $decimal_multiplier_base * $collateral) / ($debt / $decimal_multiplier_ousd);
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;
var['cdp:' || $cdp || ':liquidation:initiator'] = trigger.address;
response['message'] = 'Triggered the liquidation process for CDP ' || $cdp;
response['collateralization_ratio'] = $collateralization_ratio;
response['rate'] = $rate;
response['cdp'] = $cdp;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.bid AND trigger.data.bid_amount}",
"init": "{
$amount = trigger.output[[asset=$asset]];
$bid_amount = trigger.data.bid_amount;
if ($amount != $debt) bounce('You must send exactly ' || $debt || ' ' || $asset);
if (!$liquidation) bounce('This CDP is not in liquidation');
if ($auction_end > 0 AND timestamp > $auction_end) bounce('Auction ended');
if ($auction_best_bid AND $bid_amount >= $auction_best_bid) bounce('You must have the best bid, lower than ' || $auction_best_bid || ' bytes');
if ($bid_amount > $collateral) bounce('Bid amount must be lower or equal to CDP collateral');
}",
"messages": [
{
"app": "payment",
"if": "{$auction_best_bid}",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$auction_best_bid_from}",
"amount": "{$debt}"
}
]
}
},
{
"app": "state",
"state": "{
if (!$auction_end) {
var['cdp:' || $cdp || ':auction:start'] = timestamp;
var['cdp:' || $cdp || ':auction:end'] = timestamp + $auction_period;
}
var['cdp:' || $cdp || ':auction:best-bid'] = $bid_amount;
var['cdp:' || $cdp || ':auction:best-bid:from'] = trigger.address;
response['message'] = 'Bidded on CDP ' || $cdp;
response['cdp'] = $cdp;
}"
}
]
},
{
"if": "{$is_valid AND trigger.data.settle}",
"init": "{
if (!$liquidation)
bounce('The CDP must be in liquidation');
if (timestamp <= $auction_end)
bounce('The auction has not expired yet');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$auction_best_bid_from}",
"amount": "{$auction_best_bid}"
},
{
"if": "{$collateral > $auction_best_bid}",
"address": "{$owner}",
"amount": "{($collateral - $auction_best_bid)}"
}
]
}
},
{
"app": "state",
"state": "{
var['cdp:' || $cdp] = false;
var['cdp:' || $cdp || ':owner'] = false;
var['cdp:' || $cdp || ':collateral'] = false;
var['cdp:' || $cdp || ':debt'] = false;
var['cdp:' || $cdp || ':liquidation'] = false;
var['cdp:' || $cdp || ':auction:end'] = false;
var['cdp:' || $cdp || ':auction:best-bid'] = false;
var['cdp:' || $cdp || ':auction:best-bid:from'] = false;
response['message'] = 'Settle auction and closed CDP ' || $cdp;
response['cdp'] = $cdp;
}"
}
]
}
]
}
}
]