[
"autonomous agent",
{
"init": "{
$CT_ADDRESS = "
T2I6RLGGJ25KSJZKRNZZH52VMEAQ3722";
$OWNER = params.issuer;
$PRICE = var["price"];
$PRICE_ASSET = var["currency"] OTHERWISE "base";
$LICENSE_ASSET = var["licenseAsset"];
$ONE_DAY = 7600;
$CUT = 0.03;
$DEPOSIT_PERCENT = 0.05; //5% of a loan value is used as a deposit that will be paid back to the renter if they close their own loan and will otherwise be paid to any claimer that claims it after 24h have passed since the expiry date
$method = unit[trigger.initial_unit].messages[[.app="data"]].payload["method"];
$isHelper = var["helper_" || trigger.initial_address];
if (NOT $method)
bounce("method field is mandatory");
}",
"messages": {
"cases": [
{
"if": "{
trigger.initial_address == $OWNER AND
($method == "init" OR
$method == "toggleHelper" OR
$method == "payout")
}",
"messages": {
"cases": [
{
"if": "{$method == "init" AND NOT $LICENSE_ASSET}",
"init": "{
if (NOT trigger.data["price"])
bounce("price field is mandatory");
if (NOT is_valid_amount(trigger.data["price"]))
bounce("price is not a valid amount");
if (trigger.data["asset"])
if (NOT asset[trigger.data["asset"]].exists)
bounce("That asset does not exist");
}",
"messages": [
{
"app": "asset",
"payload": {
"is_private": false,
"is_transferrable": "{NOT NOT trigger.data["tradeable"]}",
"auto_destroy": false,
"fixed_denominations": false,
"issued_by_definer_only": true,
"cosigned_by_definer": false,
"spender_attested": false
}
},
{
"app": "state",
"state": "{
var["price"] = trigger.data["price"];
var["currency"] = trigger.data["asset"];
var["licenseAsset"] = response_unit;
}"
}
]
},
{
"if": "{$method == "toggleHelper"}",
"init": "{
if (NOT trigger.data["helper"])
bounce("helper field is mandatory");
if (NOT is_valid_address(trigger.data["helper"]))
bounce("The helper address is not a valid address");
}",
"messages": [
{
"app": "state",
"state": "{
var["helper_" || trigger.data["helper"]] = NOT $isHelper;
}"
}
]
},
{
"if": "{$method == "payout"}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{trigger.data.asset OTHERWISE $PRICE_ASSET}",
"outputs": [
{
"address": "{$OWNER}"
}
]
}
}
]
}
]
}
},
{
"if": "{($isHelper OR trigger.initial_address == $OWNER) AND $method == "changePrice"}",
"init": "{
if (NOT trigger.data["price"])
bounce("price field is mandatory");
if (NOT is_valid_amount(trigger.data["price"]))
bounce("price is not a valid amount");
if (trigger.data["asset"])
if (NOT asset[trigger.data["asset"]].exists)
bounce("That asset does not exist");
}",
"messages": [
{
"app": "state",
"state": "{
var['price'] = trigger.data["price"];
if (trigger.data["asset"])
var["currency"] = trigger.data["asset"];
}"
}
]
},
{
"if": "{$method == "buy"}",
"init": "{
$copies = floor(trigger.output[[asset=$PRICE_ASSET]]/$PRICE, 0);
if ($copies == 0)
bounce("You cannot buy less than 1 license");
$hasReferral = (trigger.data["r"] AND is_valid_address(trigger.data["r"]));
$cut = $hasReferral ? $CUT/2 : $CUT;
$cutAmount = round(trigger.output[[asset=$PRICE_ASSET]] * $cut, 0);
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$LICENSE_ASSET}",
"outputs": [
{
"address": "{trigger.initial_address}",
"amount": "{$copies}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$PRICE_ASSET}",
"outputs": [
{
"address": "{$CT_ADDRESS}",
"amount": "{$cutAmount}"
},
{
"if": "{$hasReferral}",
"address": "{trigger.data["r"]}",
"amount": "{$cutAmount}"
}
]
}
}
]
},
{
"if": "{$method == "lend"}",
"init": "{
if (NOT trigger.data["address"])
bounce("address field is mandatory");
if (trigger.output[[asset=$LICENSE_ASSET]].amount == 0)
bounce("You have to lend at least 1 license unit");
if (NOT trigger.data["period"])
bounce("period field is mandatory");
if (trigger.data["period"] <= 0)
bounce("period must be a positive integer");
if (NOT trigger.data["price"])
bounce("price field is mandatory");
if (NOT is_valid_amount(trigger.data["price"]))
bounce("price field is not a valid amount");
if (trigger.data["asset"])
if (NOT asset[trigger.data["asset"]].exists)
bounce("That asset does not exist");
}",
"messages": [
{
"app": "state",
"state": "{
var["lend_" || response_unit] = {price: trigger.data["price"], by: trigger.initial_address, asset: trigger.data["asset"] OTHERWISE "base", period: trigger.data["period"]};
}"
}
]
},
{
"if": "{$method == "loan"}",
"init": "{
if (NOT trigger.data["id"])
bounce("id field is mandatory");
$offer = var["lend_" || trigger.data["id"]];
if (NOT $offer)
bounce("Offer not found");
$total = round($offer.price * (1 + $DEPOSIT_PERCENT));
$deposit = $total - $offer.price;
if (trigger.output[[asset=$offer.asset]].amount < $total)
bounce("You need to pay at least " || $total || " units of asset " || $offer.asset);
if (var[trigger.initial_address] >= timestamp)
bounce("You cannot renew your license yet");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$offer.asset}",
"outputs": [
{
"address": "{$offer.by}",
"amount": "{$offer.price}"
}
]
}
},
{
"app": "state",
"state": "{
var[trigger.initial_address] = timestamp + $offer.period;
var["loan_" || $offer.by || "_" || trigger.data["id"]] = {deadline: timestamp + $offer.period, rentedBy: trigger.initial_address, deposit: $deposit, asset: $PRICE_ASSET};
}"
}
]
},
{
"if": "{$method == "claim"}",
"init": "{
if (NOT trigger.data["id"])
bounce("id field is mandatory");
$by = trigger.data["by"] OTHERWISE trigger.initial_address;
$offer = var["loan_" || $by || "_" || trigger.data["id"]];
if (NOT $offer)
bounce("That loan was not found");
if ($offer.deadline >= timestamp)
bounce("That loan has not ended yet");
if ($offer.rentedBy != trigger.initial_address)
if ($offer.deadline > timestamp - $ONE_DAY)
bounce("Only the renter can claim that loan until 24 hours have passed");
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$LICENSE_ASSET}",
"outputs": [
{
"address": "{$by}",
"amount": 1
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$offer.asset}",
"outputs": [
{
"address": "{trigger.initial_address}",
"amount": "{$offer.deposit}"
}
]
}
},
{
"app": "state",
"state": "{
var["loan_" || $offer.by || "_" || trigger.data["id"]] = false;
}"
}
]
}
]
}
}
]