| 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 | $get_total_donated = ($donor_address_or_repo, $recipient_repo, $asset) => |
| 68 | var[$donor_address_or_repo || '*to*' || $recipient_repo || '*' || $asset]; |
| 69 | |
| 70 | $get_total_donated_to_all = ($donor_address_or_repo, $asset) => |
| 71 | var[$donor_address_or_repo || '*total_donated*' || $asset]; |
| 72 | |
| 73 | $get_grand_total = ($donor_address_or_repo, $asset) => |
| 74 | var['total_donated*' || $asset]; |
| 75 | |
| 76 | |
| 77 | $get_donor_weight = ($donor_address_or_repo, $recipient_repo, $exchange_rates) => |
| 78 | reduce( |
| 79 | $exchange_rates, |
| 80 | 20, |
| 81 | ($acc, $asset, $exchange_rate) => |
| 82 | $acc + var[$donor_address_or_repo || '*to*' || $recipient_repo || '*' || $asset] * $exchange_rate, |
| 83 | 0 |
| 84 | ); |
| 85 | |
| 86 | |
| 87 | $get_total_weight = ($recipient_repo, $exchange_rates) => |
| 88 | reduce( |
| 89 | $exchange_rates, |
| 90 | 20, |
| 91 | ($acc, $asset, $exchange_rate) => |
| 92 | $acc + var[$recipient_repo || '*total_received*' || $asset] * $exchange_rate, |
| 93 | 0 |
| 94 | ); |
| 95 | |
| 96 | |
| 97 | $get_total_donated_to_all_in_all_assets = ($donor_address_or_repo, $exchange_rates) => |
| 98 | reduce( |
| 99 | $exchange_rates, |
| 100 | 20, |
| 101 | ($acc, $asset, $exchange_rate) => |
| 102 | $acc + var[$donor_address_or_repo || '*total_donated*' || $asset] * $exchange_rate, |
| 103 | 0 |
| 104 | ); |
| 105 | |
| 106 | |
| 107 | $get_grand_total_in_all_assets = ($exchange_rates) => |
| 108 | reduce( |
| 109 | $exchange_rates, |
| 110 | 20, |
| 111 | ($acc, $asset, $exchange_rate) => |
| 112 | $acc + var['total_donated*' || $asset] * $exchange_rate, |
| 113 | 0 |
| 114 | ); |
| 115 | |
| 116 | }", |
| 117 | "init": "{ |
| 118 | if (trigger.data.repo){ |
| 119 | $sp = $parse_repo_str(trigger.data.repo); |
| 120 | $owner = $sp[0]; |
| 121 | $project = $sp[1]; |
| 122 | } |
| 123 | }", |
| 124 | "messages": { |
| 125 | "cases": [ |
| 126 | { |
| 127 | "if": "{trigger.data.set_rules AND trigger.data.repo}", |
| 128 | "messages": [ |
| 129 | { |
| 130 | "app": "state", |
| 131 | "state": "{ |
| 132 | if (trigger.output[[asset=base]] < 4224) { |
| 133 | bounce('Not enough fee to pay rules storage'); |
| 134 | } |
| 135 | |
| 136 | if (!$has_attestation(trigger.address, $owner)) { |
| 137 | bounce('Address ' || trigger.address || ' has no attestation for ' || $owner); |
| 138 | } |
| 139 | |
| 140 | $rules = trigger.data.rules OTHERWISE {}; |
| 141 | $total = $check_rules_and_count_total($rules, trigger.data.repo); |
| 142 | if ($total > 100) { |
| 143 | bounce('Sum of rules distribution is more than 100'); |
| 144 | } |
| 145 | |
| 146 | var[trigger.data.repo || '*rules'] = $rules; |
| 147 | response['message'] = 'Rules for ' || trigger.data.repo || ' are set'; |
| 148 | response['new_rules'] = json_stringify($rules); |
| 149 | }" |
| 150 | } |
| 151 | ] |
| 152 | }, |
| 153 | { |
| 154 | "if": "{trigger.data.notification_aa AND trigger.data.repo}", |
| 155 | "messages": [ |
| 156 | { |
| 157 | "app": "state", |
| 158 | "state": "{ |
| 159 | if (!$has_attestation(trigger.address, $owner)) { |
| 160 | bounce('Address ' || trigger.address || ' has no attestation for ' || $owner); |
| 161 | } |
| 162 | |
| 163 | require(is_aa(trigger.data.notification_aa), "should be an AA"); |
| 164 | |
| 165 | var[trigger.data.repo || '*notification_aa'] = trigger.data.notification_aa; |
| 166 | response['message'] = 'Set notification AA for ' || trigger.data.repo; |
| 167 | }" |
| 168 | } |
| 169 | ] |
| 170 | }, |
| 171 | { |
| 172 | "if": "{trigger.data.donate AND trigger.data.repo}", |
| 173 | "init": "{ |
| 174 | $notification_aa = var[trigger.data.repo || '*notification_aa']; |
| 175 | $min_fee = 1000; |
| 176 | |
| 177 | if (trigger.output[[asset=base]] < $min_fee) { |
| 178 | bounce('Not enough fee to pay storage'); |
| 179 | } |
| 180 | |
| 181 | if (trigger.data.donor AND is_valid_address(trigger.data.donor)) { |
| 182 | $donor = trigger.data.donor; |
| 183 | response['donor'] = $donor; |
| 184 | } |
| 185 | else { |
| 186 | $donor = trigger.address; |
| 187 | } |
| 188 | |
| 189 | if (trigger.output[[asset=base]] > 10000) { |
| 190 | $base_amount = trigger.output[[asset=base]] - $min_fee; |
| 191 | } |
| 192 | |
| 193 | $non_base_asset = trigger.output[[asset!=base]].asset; |
| 194 | if ($non_base_asset == 'ambiguous') { |
| 195 | bounce('Ambiguous asset'); |
| 196 | } |
| 197 | |
| 198 | if (trigger.output[[asset=base]] <= 10000 AND $non_base_asset == 'none') { |
| 199 | bounce('You paid only the commission'); |
| 200 | } |
| 201 | |
| 202 | if ($non_base_asset != 'none') { |
| 203 | $asset_amount = trigger.output[[asset!=base]]; |
| 204 | require(!$base_amount, "donated both in base and another asset"); |
| 205 | $asset = $non_base_asset; |
| 206 | $amount = $asset_amount; |
| 207 | } |
| 208 | else{ |
| 209 | require($base_amount, "no base amount"); |
| 210 | $asset = 'base'; |
| 211 | $amount = $base_amount; |
| 212 | } |
| 213 | }", |
| 214 | "messages": [ |
| 215 | { |
| 216 | "if": "{$notification_aa}", |
| 217 | "app": "payment", |
| 218 | "payload": { |
| 219 | "asset": "base", |
| 220 | "outputs": [ |
| 221 | { |
| 222 | "address": "{$notification_aa}", |
| 223 | "amount": 100 |
| 224 | } |
| 225 | ] |
| 226 | } |
| 227 | }, |
| 228 | { |
| 229 | "if": "{$notification_aa}", |
| 230 | "app": "data", |
| 231 | "payload": { |
| 232 | "repo": "{trigger.data.repo}", |
| 233 | "donor": "{$donor}", |
| 234 | "amount": "{$amount}", |
| 235 | "asset": "{$asset}" |
| 236 | } |
| 237 | }, |
| 238 | { |
| 239 | "app": "state", |
| 240 | "state": "{ |
| 241 | var[trigger.data.repo || '*pool*' || $asset] += $amount; |
| 242 | |
| 243 | var[trigger.data.repo || '*total_received*' || $asset] += $amount; |
| 244 | |
| 245 | var[$donor || '*to*' || trigger.data.repo || '*' || $asset] += $amount; |
| 246 | var[trigger.data.repo || '*from*' || $donor || '*' || $asset] += $amount; |
| 247 | |
| 248 | var[$donor || '*total_donated*' || $asset] += $amount; |
| 249 | var['total_donated*' || $asset] += $amount; |
| 250 | |
| 251 | response['donated_in_' || $asset] = $amount; |
| 252 | |
| 253 | response['message'] = 'Successful donation to ' || trigger.data.repo; |
| 254 | }" |
| 255 | } |
| 256 | ] |
| 257 | }, |
| 258 | { |
| 259 | "if": "{trigger.data.distribute AND trigger.data.repo}", |
| 260 | "init": "{ |
| 261 | $rules = var[trigger.data.repo || '*rules']; |
| 262 | $claimer = $has_attestation(trigger.address, $owner) ? trigger.data.to OTHERWISE trigger.address : false; |
| 263 | |
| 264 | if (exists($rules)) { |
| 265 | $asset = trigger.data.asset OTHERWISE 'base'; |
| 266 | $asset_pool_var = trigger.data.repo || '*pool*' || $asset; |
| 267 | $balance = var[$asset_pool_var]; |
| 268 | |
| 269 | $unclaimed_var = trigger.data.repo || '*unclaimed*' || $asset; |
| 270 | $unclaimed = var[$unclaimed_var] OTHERWISE 0; |
| 271 | |
| 272 | if (!$balance AND !($claimer AND $unclaimed)) { |
| 273 | bounce('Nothing to distribute in repo ' || trigger.data.repo || ' for asset ' || $asset); |
| 274 | } |
| 275 | |
| 276 | $to_self = $balance - reduce($rules, 10, ($acc, $repo, $percent) => { |
| 277 | $acc + floor($balance * $percent / 100) |
| 278 | }, 0); |
| 279 | } else { |
| 280 | bounce('Rules for repo ' || trigger.data.repo || ' are not set yet'); |
| 281 | } |
| 282 | }", |
| 283 | "messages": [ |
| 284 | { |
| 285 | "if": "{$claimer}", |
| 286 | "app": "payment", |
| 287 | "payload": { |
| 288 | "asset": "{$asset}", |
| 289 | "outputs": [ |
| 290 | { |
| 291 | "address": "{$claimer}", |
| 292 | "amount": "{$to_self + $unclaimed}" |
| 293 | } |
| 294 | ] |
| 295 | } |
| 296 | }, |
| 297 | { |
| 298 | "app": "state", |
| 299 | "state": "{ |
| 300 | |
| 301 | foreach($rules, 10, ($repo, $percent) => { |
| 302 | $to_share = floor($balance * $percent / 100); |
| 303 | var[$repo || '*pool*' || $asset] += $to_share; |
| 304 | |
| 305 | var[trigger.data.repo || '*total_donated*' || $asset] += $to_share; |
| 306 | var[trigger.data.repo || '*to*' || $repo || '*' || $asset] += $to_share; |
| 307 | var[$repo || '*from*' || trigger.data.repo || '*' || $asset] += $to_share; |
| 308 | |
| 309 | var[$repo || '*total_received*' || $asset] += $to_share; |
| 310 | var[$repo || '*total_received_from_other_repos*' || $asset] += $to_share; |
| 311 | }); |
| 312 | |
| 313 | if ($claimer) { |
| 314 | var['paid_to*' || trigger.address || '*' || $asset] += $to_self + $unclaimed; |
| 315 | var[$unclaimed_var] = 0; |
| 316 | |
| 317 | response['claimer'] = $claimer; |
| 318 | response['claimed'] = $to_self + $unclaimed; |
| 319 | } else { |
| 320 | var[$unclaimed_var] += $to_self; |
| 321 | } |
| 322 | |
| 323 | var[$asset_pool_var] = 0; |
| 324 | response['asset'] = $asset; |
| 325 | response['message'] = 'Distribution for repo ' || trigger.data.repo || ' in asset ' || $asset || ' done'; |
| 326 | }" |
| 327 | } |
| 328 | ] |
| 329 | }, |
| 330 | { |
| 331 | "if": "{trigger.data.nickname}", |
| 332 | "messages": [ |
| 333 | { |
| 334 | "app": "state", |
| 335 | "state": "{ |
| 336 | if (typeof(trigger.data.nickname) != 'string') { |
| 337 | bounce('Nickname is not a string'); |
| 338 | } |
| 339 | |
| 340 | if (exists(var['nickname_owner*' || trigger.data.nickname])) { |
| 341 | bounce('Nickname ' || trigger.data.nickname || ' is already taken'); |
| 342 | } else { |
| 343 | $old_nickname = var['nickname*' || trigger.address]; |
| 344 | if ($old_nickname) { |
| 345 | var['nickname_owner*' || $old_nickname] = false; |
| 346 | } |
| 347 | var['nickname*' || trigger.address] = trigger.data.nickname; |
| 348 | var['nickname_owner*' || trigger.data.nickname] = trigger.address; |
| 349 | } |
| 350 | response['message'] = 'Nickname for ' || trigger.address || ' is now ' || trigger.data.nickname; |
| 351 | }" |
| 352 | } |
| 353 | ] |
| 354 | } |
| 355 | ] |
| 356 | } |
| 357 | } |
| 358 | ] |