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