[
"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
};
$get_total_donated = ($donor_address_or_repo, $recipient_repo, $asset) =>
var[$donor_address_or_repo || '*to*' || $recipient_repo || '*' || $asset];
$get_total_donated_to_all = ($donor_address_or_repo, $asset) =>
var[$donor_address_or_repo || '*total_donated*' || $asset];
$get_grand_total = ($donor_address_or_repo, $asset) =>
var['total_donated*' || $asset];
// voting weight of the donor: sum of their donations with all amounts converted to a single asset using $exchange_rates
$get_donor_weight = ($donor_address_or_repo, $recipient_repo, $exchange_rates) =>
reduce(
$exchange_rates,
20,
($acc, $asset, $exchange_rate) =>
$acc + var[$donor_address_or_repo || '*to*' || $recipient_repo || '*' || $asset] * $exchange_rate,
0
);
// all donations to a repo with all amounts converted to a single asset using $exchange_rates
$get_total_weight = ($recipient_repo, $exchange_rates) =>
reduce(
$exchange_rates,
20,
($acc, $asset, $exchange_rate) =>
$acc + var[$recipient_repo || '*total_received*' || $asset] * $exchange_rate,
0
);
// all donations by a donor with all amounts converted to a single asset using $exchange_rates
$get_total_donated_to_all_in_all_assets = ($donor_address_or_repo, $exchange_rates) =>
reduce(
$exchange_rates,
20,
($acc, $asset, $exchange_rate) =>
$acc + var[$donor_address_or_repo || '*total_donated*' || $asset] * $exchange_rate,
0
);
// all donations on kivach with all amounts converted to a single asset using $exchange_rates
$get_grand_total_in_all_assets = ($exchange_rates) =>
reduce(
$exchange_rates,
20,
($acc, $asset, $exchange_rate) =>
$acc + var['total_donated*' || $asset] * $exchange_rate,
0
);
}",
"init": "{
if (trigger.data.repo){
$sp = $parse_repo_str(trigger.data.repo);
$owner = $sp[0];
$project = $sp[1];
}
}",
"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');
}
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.notification_aa AND trigger.data.repo}",
"messages": [
{
"app": "state",
"state": "{
if (!$has_attestation(trigger.address, $owner)) {
bounce('Address ' || trigger.address || ' has no attestation for ' || $owner);
}
require(is_aa(trigger.data.notification_aa), "should be an AA");
var[trigger.data.repo || '*notification_aa'] = trigger.data.notification_aa;
response['message'] = 'Set notification AA for ' || trigger.data.repo;
}"
}
]
},
{
"if": "{trigger.data.donate AND trigger.data.repo}",
"init": "{
$notification_aa = var[trigger.data.repo || '*notification_aa'];
$min_fee = /*$notification_aa ? 3000 :*/ 1000;
if (trigger.output[[asset=base]] < $min_fee) {
bounce('Not enough fee to pay storage');
}
if (trigger.data.donor AND is_valid_address(trigger.data.donor)) {
$donor = trigger.data.donor;
response['donor'] = $donor;
}
else {
$donor = trigger.address;
}
if (trigger.output[[asset=base]] > 10000) {
$base_amount = trigger.output[[asset=base]] - $min_fee; // 1000 bytes storage fee
}
$non_base_asset = trigger.output[[asset!=base]].asset;
if ($non_base_asset == 'ambiguous') {
bounce('Ambiguous asset');
}
if (trigger.output[[asset=base]] <= 10000 AND $non_base_asset == 'none') {
bounce('You paid only the commission');
}
if ($non_base_asset != 'none') {
$asset_amount = trigger.output[[asset!=base]];
require(!$base_amount, "donated both in base and another asset");
$asset = $non_base_asset;
$amount = $asset_amount;
}
else{
require($base_amount, "no base amount");
$asset = 'base';
$amount = $base_amount;
}
}",
"messages": [
{
"if": "{$notification_aa}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$notification_aa}",
"amount": 100
}
]
}
},
{
"if": "{$notification_aa}",
"app": "data",
"payload": {
"repo": "{trigger.data.repo}",
"donor": "{$donor}",
"amount": "{$amount}",
"asset": "{$asset}"
}
},
{
"app": "state",
"state": "{
var[trigger.data.repo || '*pool*' || $asset] += $amount;
var[trigger.data.repo || '*total_received*' || $asset] += $amount;
var[$donor || '*to*' || trigger.data.repo || '*' || $asset] += $amount;
var[trigger.data.repo || '*from*' || $donor || '*' || $asset] += $amount;
var[$donor || '*total_donated*' || $asset] += $amount;
var['total_donated*' || $asset] += $amount;
response['donated_in_' || $asset] = $amount;
response['message'] = 'Successful donation to ' || trigger.data.repo;
}"
}
]
},
{
"if": "{trigger.data.distribute AND trigger.data.repo}",
"init": "{
$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 || '*total_donated*' || $asset] += $to_share;
var[trigger.data.repo || '*to*' || $repo || '*' || $asset] += $to_share;
var[$repo || '*from*' || trigger.data.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;
}"
}
]
}
]
}
}
]