[
"autonomous agent",
{
"doc_url": "https://kivach.org/cascading-donations.json",
"getters": "{
$attestor = 'OYW2XTDKSNKGSEZ27LMGNOPJSYIXHBHC'; // mainnet
// $attestor = 'QBIAZQZWO3YK2I2C3X37DCVY643S4NI4'; // testnet
// $attestor = 'QMWQRJV6NCYGUI6X7BWRKLLBQVQYLZ27'; // testkit tests
$attestor_aa = 'NPTNZFOTBQ7DVTR4OVV7SEMBNYFP2ZZS'; // mainnet
// $attestor_aa = 'HZENJOEOLEBRGKQYJBGGIJNLX7RAAVSH'; // testnet
// $attestor_aa = 'NBT6VI5RSJ2BQTPUENB4A6DMY4VW7NJ3'; // testkit tests
$has_attestation = ($address, $github_username) => {
$owner_address = var[$attestor_aa]['u2a_' || $github_username];
$owner_address AND $owner_address == $address
// $attestation = attestation[[attestors=$attestor, address=$address]].github_username;
// typeof($attestation) == 'string' AND $attestation == $github_username
};
$parse_repo_str = ($str) => {
if (!has_only($str, '\w/.-') ) {
bounce('bad symbols in repo');
}
$sp = split($str, '/');
$owner = $sp[0];
$project = $sp[1];
if (length($sp) != 2 OR length($owner) < 1 OR length($project) < 1) {
bounce('Invalid repo: ' || $str);
}
$sp
};
$check_rules_and_count_total = ($rules, $own_repo) => {
if (typeof($rules) != 'object' OR is_array($rules)) {
bounce('rules is not an object');
}
if (length($rules) > 10) {
bounce('Maximum number of recipient repositories is 10');
}
if (length($rules) == 0) {
return 0;
}
$total = reduce($rules, 10, ($acc, $key, $value) => {
$parse_repo_str($key);
if ($key == $own_repo) {
bounce('Invalid repo: ' || $key || ' Don\'t set own repo in rules; You will receive the unshared remainder');
}
if (typeof($value) != 'number') {
bounce('Not a number ' || $value || ' in repo ' || $key);
}
if ($value <= 0 OR $value > 100) {
bounce('percentage must be between 0 and 100');
}
$acc + $value
}, 0);
$total
};
}",
"messages": {
"cases": [
{
"if": "{trigger.data.set_rules AND trigger.data.repo}",
"messages": [
{
"app": "state",
"state": "{
if (trigger.output[[asset=base]] < 4224) {
bounce('Not enough fee to pay rules storage');
}
$sp = $parse_repo_str(trigger.data.repo);
$owner = $sp[0];
$project = $sp[1];
if (!$has_attestation(trigger.address, $owner)) {
bounce('Address ' || trigger.address || ' has no attestation for ' || $owner);
}
$rules = trigger.data.rules OTHERWISE {};
$total = $check_rules_and_count_total($rules, trigger.data.repo);
if ($total > 100) {
bounce('Sum of rules distribution is more than 100');
}
var[trigger.data.repo || '*rules'] = $rules;
response['message'] = 'Rules for ' || trigger.data.repo || ' are set';
response['new_rules'] = json_stringify($rules);
}"
}
]
},
{
"if": "{trigger.data.donate AND trigger.data.repo}",
"messages": [
{
"app": "state",
"state": "{
$parse_repo_str(trigger.data.repo);
if (trigger.output[[asset=base]] < 1000) {
bounce('Not enough fee to pay storage');
}
if (trigger.output[[asset=base]] > 10000) {
$base_amount = trigger.output[[asset=base]] - 1000; // 1000 bytes storage fee
var[trigger.data.repo || '*pool*base'] += $base_amount;
var[trigger.data.repo || '*total_received*base'] += $base_amount;
response['donated_in_base'] = $base_amount;
}
$asset = trigger.output[[asset!=base]].asset;
if ($asset == 'ambiguous') {
bounce('Ambiguous asset');
}
if (trigger.output[[asset=base]] <= 10000 AND $asset == 'none') {
bounce('You paid only the commission');
}
if ($asset != 'none') {
$asset_amount = trigger.output[[asset!=base]];
var[trigger.data.repo || '*pool*' || $asset] += $asset_amount;
var[trigger.data.repo || '*total_received*' || $asset] += $asset_amount;
response['donated_in_' || $asset] = $asset_amount;
}
response['message'] = 'Successful donation to ' || trigger.data.repo;
if (trigger.data.donor AND is_valid_address(trigger.data.donor)) {
response['donor'] = trigger.data.donor;
}
}"
}
]
},
{
"if": "{trigger.data.distribute AND trigger.data.repo}",
"init": "{
$sp = $parse_repo_str(trigger.data.repo);
$owner = $sp[0];
$project = $sp[1];
$rules = var[trigger.data.repo || '*rules'];
$claimer = $has_attestation(trigger.address, $owner) ? trigger.data.to OTHERWISE trigger.address : false;
if (exists($rules)) {
$asset = trigger.data.asset OTHERWISE 'base';
$asset_pool_var = trigger.data.repo || '*pool*' || $asset;
$balance = var[$asset_pool_var];
$unclaimed_var = trigger.data.repo || '*unclaimed*' || $asset;
$unclaimed = var[$unclaimed_var] OTHERWISE 0;
if (!$balance AND !($claimer AND $unclaimed)) {
bounce('Nothing to distribute in repo ' || trigger.data.repo || ' for asset ' || $asset);
}
$to_self = $balance - reduce($rules, 10, ($acc, $repo, $percent) => {
$acc + floor($balance * $percent / 100)
}, 0);
} else {
bounce('Rules for repo ' || trigger.data.repo || ' are not set yet');
}
}",
"messages": [
{
"if": "{$claimer}",
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{$claimer}",
"amount": "{$to_self + $unclaimed}"
}
]
}
},
{
"app": "state",
"state": "{
// distribution from/to some extra long repos might fail here due to too long var names, exclude them from the rules in this case
foreach($rules, 10, ($repo, $percent) => {
$to_share = floor($balance * $percent / 100);
var[$repo || '*pool*' || $asset] += $to_share;
var[trigger.data.repo || '*to*' || $repo || '*' || $asset] += $to_share;
var[$repo || '*total_received*' || $asset] += $to_share;
var[$repo || '*total_received_from_other_repos*' || $asset] += $to_share;
});
if ($claimer) {
var['paid_to*' || trigger.address || '*' || $asset] += $to_self + $unclaimed;
var[$unclaimed_var] = 0;
response['claimer'] = $claimer;
response['claimed'] = $to_self + $unclaimed;
} else {
var[$unclaimed_var] += $to_self;
}
var[$asset_pool_var] = 0;
response['asset'] = $asset;
response['message'] = 'Distribution for repo ' || trigger.data.repo || ' in asset ' || $asset || ' done';
}"
}
]
},
{
"if": "{trigger.data.nickname}",
"messages": [
{
"app": "state",
"state": "{
if (typeof(trigger.data.nickname) != 'string') {
bounce('Nickname is not a string');
}
if (exists(var['nickname_owner*' || trigger.data.nickname])) {
bounce('Nickname ' || trigger.data.nickname || ' is already taken');
} else {
$old_nickname = var['nickname*' || trigger.address];
if ($old_nickname) {
var['nickname_owner*' || $old_nickname] = false;
}
var['nickname*' || trigger.address] = trigger.data.nickname;
var['nickname_owner*' || trigger.data.nickname] = trigger.address;
}
response['message'] = 'Nickname for ' || trigger.address || ' is now ' || trigger.data.nickname;
}"
}
]
}
]
}
}
]