| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://kivach.org/cascading-donations.json", |
| 5 | "getters": "{ |
| 6 | $attestor = 'OYW2XTDKSNKGSEZ27LMGNOPJSYIXHBHC'; |
| 7 | |
| 8 | |
| 9 | |
| 10 | $attestor_aa = 'NPTNZFOTBQ7DVTR4OVV7SEMBNYFP2ZZS'; |
| 11 | |
| 12 | |
| 13 | |
| 14 | $has_attestation = ($address, $github_username) => { |
| 15 | $owner_address = var[$attestor_aa]['u2a_' || $github_username]; |
| 16 | $owner_address AND $owner_address == $address |
| 17 | |
| 18 | |
| 19 | }; |
| 20 | |
| 21 | $parse_repo_str = ($str) => { |
| 22 | if (!has_only($str, '\w/.-') ) { |
| 23 | bounce('bad symbols in repo'); |
| 24 | } |
| 25 | $sp = split($str, '/'); |
| 26 | $owner = $sp[0]; |
| 27 | $project = $sp[1]; |
| 28 | if (length($sp) != 2 OR length($owner) < 1 OR length($project) < 1) { |
| 29 | bounce('Invalid repo: ' || $str); |
| 30 | } |
| 31 | $sp |
| 32 | }; |
| 33 | |
| 34 | $check_rules_and_count_total = ($rules, $own_repo) => { |
| 35 | if (typeof($rules) != 'object' OR is_array($rules)) { |
| 36 | bounce('rules is not an object'); |
| 37 | } |
| 38 | |
| 39 | if (length($rules) > 10) { |
| 40 | bounce('Maximum number of recipient repositories is 10'); |
| 41 | } |
| 42 | |
| 43 | if (length($rules) == 0) { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | $total = reduce($rules, 10, ($acc, $key, $value) => { |
| 48 | $parse_repo_str($key); |
| 49 | if ($key == $own_repo) { |
| 50 | bounce('Invalid repo: ' || $key || ' Don\'t set own repo in rules; You will receive the unshared remainder'); |
| 51 | } |
| 52 | |
| 53 | if (typeof($value) != 'number') { |
| 54 | bounce('Not a number ' || $value || ' in repo ' || $key); |
| 55 | } |
| 56 | |
| 57 | if ($value <= 0 OR $value > 100) { |
| 58 | bounce('percentage must be between 0 and 100'); |
| 59 | } |
| 60 | |
| 61 | $acc + $value |
| 62 | }, 0); |
| 63 | |
| 64 | $total |
| 65 | }; |
| 66 | }", |
| 67 | "messages": { |
| 68 | "cases": [ |
| 69 | { |
| 70 | "if": "{trigger.data.set_rules AND trigger.data.repo}", |
| 71 | "messages": [ |
| 72 | { |
| 73 | "app": "state", |
| 74 | "state": "{ |
| 75 | if (trigger.output[[asset=base]] < 4224) { |
| 76 | bounce('Not enough fee to pay rules storage'); |
| 77 | } |
| 78 | |
| 79 | $sp = $parse_repo_str(trigger.data.repo); |
| 80 | $owner = $sp[0]; |
| 81 | $project = $sp[1]; |
| 82 | |
| 83 | if (!$has_attestation(trigger.address, $owner)) { |
| 84 | bounce('Address ' || trigger.address || ' has no attestation for ' || $owner); |
| 85 | } |
| 86 | |
| 87 | $rules = trigger.data.rules OTHERWISE {}; |
| 88 | $total = $check_rules_and_count_total($rules, trigger.data.repo); |
| 89 | if ($total > 100) { |
| 90 | bounce('Sum of rules distribution is more than 100'); |
| 91 | } |
| 92 | |
| 93 | var[trigger.data.repo || '*rules'] = $rules; |
| 94 | response['message'] = 'Rules for ' || trigger.data.repo || ' are set'; |
| 95 | response['new_rules'] = json_stringify($rules); |
| 96 | }" |
| 97 | } |
| 98 | ] |
| 99 | }, |
| 100 | { |
| 101 | "if": "{trigger.data.donate AND trigger.data.repo}", |
| 102 | "messages": [ |
| 103 | { |
| 104 | "app": "state", |
| 105 | "state": "{ |
| 106 | $parse_repo_str(trigger.data.repo); |
| 107 | |
| 108 | if (trigger.output[[asset=base]] < 1000) { |
| 109 | bounce('Not enough fee to pay storage'); |
| 110 | } |
| 111 | |
| 112 | if (trigger.output[[asset=base]] > 10000) { |
| 113 | $base_amount = trigger.output[[asset=base]] - 1000; |
| 114 | var[trigger.data.repo || '*pool*base'] += $base_amount; |
| 115 | |
| 116 | var[trigger.data.repo || '*total_received*base'] += $base_amount; |
| 117 | response['donated_in_base'] = $base_amount; |
| 118 | } |
| 119 | |
| 120 | $asset = trigger.output[[asset!=base]].asset; |
| 121 | if ($asset == 'ambiguous') { |
| 122 | bounce('Ambiguous asset'); |
| 123 | } |
| 124 | |
| 125 | if (trigger.output[[asset=base]] <= 10000 AND $asset == 'none') { |
| 126 | bounce('You paid only the commission'); |
| 127 | } |
| 128 | |
| 129 | if ($asset != 'none') { |
| 130 | $asset_amount = trigger.output[[asset!=base]]; |
| 131 | var[trigger.data.repo || '*pool*' || $asset] += $asset_amount; |
| 132 | |
| 133 | var[trigger.data.repo || '*total_received*' || $asset] += $asset_amount; |
| 134 | response['donated_in_' || $asset] = $asset_amount; |
| 135 | } |
| 136 | |
| 137 | response['message'] = 'Successful donation to ' || trigger.data.repo; |
| 138 | |
| 139 | if (trigger.data.donor AND is_valid_address(trigger.data.donor)) { |
| 140 | response['donor'] = trigger.data.donor; |
| 141 | } |
| 142 | }" |
| 143 | } |
| 144 | ] |
| 145 | }, |
| 146 | { |
| 147 | "if": "{trigger.data.distribute AND trigger.data.repo}", |
| 148 | "init": "{ |
| 149 | $sp = $parse_repo_str(trigger.data.repo); |
| 150 | $owner = $sp[0]; |
| 151 | $project = $sp[1]; |
| 152 | $rules = var[trigger.data.repo || '*rules']; |
| 153 | $claimer = $has_attestation(trigger.address, $owner) ? trigger.data.to OTHERWISE trigger.address : false; |
| 154 | |
| 155 | if (exists($rules)) { |
| 156 | $asset = trigger.data.asset OTHERWISE 'base'; |
| 157 | $asset_pool_var = trigger.data.repo || '*pool*' || $asset; |
| 158 | $balance = var[$asset_pool_var]; |
| 159 | |
| 160 | $unclaimed_var = trigger.data.repo || '*unclaimed*' || $asset; |
| 161 | $unclaimed = var[$unclaimed_var] OTHERWISE 0; |
| 162 | |
| 163 | if (!$balance AND !($claimer AND $unclaimed)) { |
| 164 | bounce('Nothing to distribute in repo ' || trigger.data.repo || ' for asset ' || $asset); |
| 165 | } |
| 166 | |
| 167 | $to_self = $balance - reduce($rules, 10, ($acc, $repo, $percent) => { |
| 168 | $acc + floor($balance * $percent / 100) |
| 169 | }, 0); |
| 170 | } else { |
| 171 | bounce('Rules for repo ' || trigger.data.repo || ' are not set yet'); |
| 172 | } |
| 173 | }", |
| 174 | "messages": [ |
| 175 | { |
| 176 | "if": "{$claimer}", |
| 177 | "app": "payment", |
| 178 | "payload": { |
| 179 | "asset": "{$asset}", |
| 180 | "outputs": [ |
| 181 | { |
| 182 | "address": "{$claimer}", |
| 183 | "amount": "{$to_self + $unclaimed}" |
| 184 | } |
| 185 | ] |
| 186 | } |
| 187 | }, |
| 188 | { |
| 189 | "app": "state", |
| 190 | "state": "{ |
| 191 | |
| 192 | foreach($rules, 10, ($repo, $percent) => { |
| 193 | $to_share = floor($balance * $percent / 100); |
| 194 | var[$repo || '*pool*' || $asset] += $to_share; |
| 195 | |
| 196 | var[trigger.data.repo || '*to*' || $repo || '*' || $asset] += $to_share; |
| 197 | |
| 198 | var[$repo || '*total_received*' || $asset] += $to_share; |
| 199 | var[$repo || '*total_received_from_other_repos*' || $asset] += $to_share; |
| 200 | }); |
| 201 | |
| 202 | if ($claimer) { |
| 203 | var['paid_to*' || trigger.address || '*' || $asset] += $to_self + $unclaimed; |
| 204 | var[$unclaimed_var] = 0; |
| 205 | |
| 206 | response['claimer'] = $claimer; |
| 207 | response['claimed'] = $to_self + $unclaimed; |
| 208 | } else { |
| 209 | var[$unclaimed_var] += $to_self; |
| 210 | } |
| 211 | |
| 212 | var[$asset_pool_var] = 0; |
| 213 | response['asset'] = $asset; |
| 214 | response['message'] = 'Distribution for repo ' || trigger.data.repo || ' in asset ' || $asset || ' done'; |
| 215 | }" |
| 216 | } |
| 217 | ] |
| 218 | }, |
| 219 | { |
| 220 | "if": "{trigger.data.nickname}", |
| 221 | "messages": [ |
| 222 | { |
| 223 | "app": "state", |
| 224 | "state": "{ |
| 225 | if (typeof(trigger.data.nickname) != 'string') { |
| 226 | bounce('Nickname is not a string'); |
| 227 | } |
| 228 | |
| 229 | if (exists(var['nickname_owner*' || trigger.data.nickname])) { |
| 230 | bounce('Nickname ' || trigger.data.nickname || ' is already taken'); |
| 231 | } else { |
| 232 | $old_nickname = var['nickname*' || trigger.address]; |
| 233 | if ($old_nickname) { |
| 234 | var['nickname_owner*' || $old_nickname] = false; |
| 235 | } |
| 236 | var['nickname*' || trigger.address] = trigger.data.nickname; |
| 237 | var['nickname_owner*' || trigger.data.nickname] = trigger.address; |
| 238 | } |
| 239 | response['message'] = 'Nickname for ' || trigger.address || ' is now ' || trigger.data.nickname; |
| 240 | }" |
| 241 | } |
| 242 | ] |
| 243 | } |
| 244 | ] |
| 245 | } |
| 246 | } |
| 247 | ] |