| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://city.obyte.org/governance.json", |
| 5 | "init": "{ |
| 6 | |
| 7 | $challenging_period = params.challenging_period OTHERWISE 3*24*3600; |
| 8 | |
| 9 | $city_aa = params.city_aa; |
| 10 | require($city_aa, "no city_aa"); |
| 11 | |
| 12 | $min_majority_for_new_city = 0.75; |
| 13 | |
| 14 | $names = [ |
| 15 | 'matching_probability', |
| 16 | 'plot_price', |
| 17 | 'referral_boost', |
| 18 | 'randomness_aa', |
| 19 | 'randomness_price', |
| 20 | 'p2p_sale_fee', |
| 21 | 'shortcode_sale_fee', |
| 22 | 'rental_surcharge_factor', |
| 23 | 'followup_reward_share', |
| 24 | 'attestors', |
| 25 | 'new_city', |
| 26 | 'mayor', |
| 27 | ]; |
| 28 | $count_names = 12; |
| 29 | require(length($names) == $count_names, "wrong number of governable parameters"); |
| 30 | |
| 31 | $is_allowed_name = $name => { |
| 32 | length(filter($names, $count_names, $n => $n == $name)) == 1 |
| 33 | }; |
| 34 | |
| 35 | $per_city_names = { |
| 36 | matching_probability: true, |
| 37 | plot_price: true, |
| 38 | referral_boost: true, |
| 39 | mayor: true, |
| 40 | }; |
| 41 | |
| 42 | $get_value_key = $value => $value; |
| 43 | |
| 44 | }", |
| 45 | "messages": { |
| 46 | "cases": [ |
| 47 | { |
| 48 | "if": "{ trigger.data.name AND trigger.data.commit }", |
| 49 | "init": "{ |
| 50 | $name = trigger.data.name; |
| 51 | $city = trigger.data.city; |
| 52 | if ($name == 'new_city'){ |
| 53 | $mayor = trigger.data.mayor; |
| 54 | require($city, "city name not provided"); |
| 55 | require(is_valid_address($mayor), "mayor address not valid"); |
| 56 | require(!var[$city_aa]['city_'||$city], "this city already exists"); |
| 57 | $full_name = $name||'|'||$city||'|'||$mayor; |
| 58 | } |
| 59 | else if ($per_city_names[$name] AND $city) |
| 60 | $full_name = $name||'|'||$city; |
| 61 | else |
| 62 | $full_name = $name; |
| 63 | $leader = var['leader_' || $full_name]; |
| 64 | require(exists($leader), "no leader"); |
| 65 | $current_value = var[$full_name]; |
| 66 | if (exists($current_value) AND $leader == $current_value) |
| 67 | bounce("already equal to leader"); |
| 68 | require(var['challenging_period_start_ts_' || $full_name] + $challenging_period < timestamp, "challenging period not expired yet"); |
| 69 | $payload = { |
| 70 | name: $name, |
| 71 | value: $leader, |
| 72 | }; |
| 73 | if ($city) |
| 74 | $payload.city = $city; |
| 75 | if ($name == 'new_city'){ |
| 76 | require($leader == 'yes', "leader must be yes"); |
| 77 | $state = var[$city_aa]['state']; |
| 78 | require(var['support_' || $full_name || '_yes'] / $state.total_land > $min_majority_for_new_city, "this decision requires a "||($min_majority_for_new_city*100)||"% majority"); |
| 79 | $payload.mayor = $mayor; |
| 80 | } |
| 81 | }", |
| 82 | "messages": [ |
| 83 | { |
| 84 | "app": "payment", |
| 85 | "payload": { |
| 86 | "asset": "base", |
| 87 | "outputs": [ |
| 88 | { |
| 89 | "address": "{$city_aa}", |
| 90 | "amount": 5000 |
| 91 | } |
| 92 | ] |
| 93 | } |
| 94 | }, |
| 95 | { |
| 96 | "app": "data", |
| 97 | "payload": "{$payload}" |
| 98 | }, |
| 99 | { |
| 100 | "app": "state", |
| 101 | "state": "{ |
| 102 | var[$full_name] = $leader; |
| 103 | }" |
| 104 | } |
| 105 | ] |
| 106 | }, |
| 107 | { |
| 108 | "if": "{ trigger.data.name }", |
| 109 | "init": "{ |
| 110 | $name = trigger.data.name; |
| 111 | $value = trigger.data.value; |
| 112 | require($is_allowed_name($name), "unknown name: " || $name); |
| 113 | $city = trigger.data.city; |
| 114 | if ($city){ |
| 115 | require(!contains($city, '|'), "| not allowed in city name"); |
| 116 | require($per_city_names[$name] OR $name == 'new_city', "this is not a per-city variable"); |
| 117 | if ($name != 'new_city') |
| 118 | require(var[$city_aa]['city_'||$city], "no such city"); |
| 119 | } |
| 120 | if ($name == 'mayor' AND !$city) bounce("mayor without a city"); |
| 121 | $bCityVote = $per_city_names[$name] AND $city; |
| 122 | |
| 123 | $balance = var[$city_aa]['user_land_' || ($bCityVote ? $city||'_' : '') || trigger.address]; |
| 124 | require($balance, "you have no land and cannot vote"); |
| 125 | |
| 126 | if (exists($value)){ |
| 127 | if ($name == 'randomness_aa' OR $name == 'attestors' OR $name == 'new_city' OR $name == 'mayor'){ |
| 128 | require(typeof($value) == 'string', "must be a string"); |
| 129 | if ($name == 'randomness_aa') |
| 130 | require(is_aa($value), "not an AA"); |
| 131 | else if ($name == 'mayor') |
| 132 | require(is_valid_address($value), "mayor address not valid"); |
| 133 | else if ($name == 'attestors'){ |
| 134 | $arr = split($value, ':'); |
| 135 | require(length($arr) > 0, "no attestors"); |
| 136 | foreach($arr, 5, $addr => { require(is_valid_address($addr), "invalid atestor address"); }); |
| 137 | } |
| 138 | else if ($name == 'new_city'){ |
| 139 | require($value == 'yes' OR $value == 'no', "value must be yes or no"); |
| 140 | $mayor = trigger.data.mayor; |
| 141 | require($city, "city name not provided"); |
| 142 | require(length($city) <= 20, "new city name is too long"); |
| 143 | require(is_valid_address($mayor), "mayor address not valid"); |
| 144 | require(!var[$city_aa]['city_'||$city], "this city already exists"); |
| 145 | } |
| 146 | } |
| 147 | else{ |
| 148 | require(typeof($value) == 'number' AND $value > 0, "must be a positive number"); |
| 149 | if ($name == 'matching_probability') |
| 150 | require($value < 1/4, "must be < 1/4"); |
| 151 | if ($name == 'randomness_price' OR $name == 'p2p_sale_fee' OR $name == 'shortcode_sale_fee' OR $name == 'followup_reward_share') |
| 152 | require($value < 1, "must be <1"); |
| 153 | else if ($name == 'plot_price') |
| 154 | require(is_integer($value), "must be integer"); |
| 155 | else if ($name == 'rental_surcharge_factor') |
| 156 | require($value >= 1, "must be >=1"); |
| 157 | } |
| 158 | $full_name = ($name == 'new_city') ? ($name||'|'||$city||'|'||$mayor) : ($city ? $name||'|'||$city : $name); |
| 159 | } |
| 160 | }", |
| 161 | "messages": [ |
| 162 | { |
| 163 | "app": "state", |
| 164 | "state": "{ |
| 165 | $votes = var['votes_'||trigger.address] OTHERWISE {}; |
| 166 | $prev_choice = $votes[$full_name]; |
| 167 | $leader = var['leader_' || $full_name]; |
| 168 | var['choice_' || trigger.address || '_' || $full_name] = $value; |
| 169 | if (exists($prev_choice)){ |
| 170 | $prev_choice_key = $get_value_key($prev_choice.value); |
| 171 | var['support_' || $full_name || '_' || $prev_choice_key] -= $prev_choice.balance; |
| 172 | delete($votes, $full_name); |
| 173 | } |
| 174 | if (exists($value)){ |
| 175 | $value_key = $get_value_key($value); |
| 176 | var['support_' || $full_name || '_' || $value_key] += $balance; |
| 177 | $votes[$full_name] = { value: $value, balance: $balance }; |
| 178 | if (!exists($leader) OR var['support_' || $full_name || '_' || $value_key] > var['support_' || $full_name || '_' || $get_value_key($leader)]){ |
| 179 | var['leader_' || $full_name] = $value; |
| 180 | var['challenging_period_start_ts_' || $full_name] = timestamp; |
| 181 | } |
| 182 | } |
| 183 | var['votes_'||trigger.address] = $votes; |
| 184 | }" |
| 185 | } |
| 186 | ] |
| 187 | }, |
| 188 | { |
| 189 | "if": "{ trigger.data.update_user_balance AND trigger.data.address AND trigger.data.city }", |
| 190 | "init": "{ |
| 191 | }", |
| 192 | "messages": [ |
| 193 | { |
| 194 | "app": "state", |
| 195 | "state": "{ |
| 196 | $votes = var['votes_'||trigger.data.address]; |
| 197 | if (!$votes) return; |
| 198 | $balance = var[$city_aa]['user_land_' || trigger.data.address]; |
| 199 | $city_balance = var[$city_aa]['user_land_' || trigger.data.city || '_' || trigger.data.address]; |
| 200 | foreach($votes, $count_names, ($name, $vote) => { |
| 201 | $value_key = $get_value_key($vote.value); |
| 202 | $parts = split($name, '|'); |
| 203 | if (length($parts) == 2){ |
| 204 | if ($parts[1] != trigger.data.city) return; |
| 205 | $bInThisCity = true; |
| 206 | } |
| 207 | $new_balance = $bInThisCity ? $city_balance : $balance; |
| 208 | var['support_' || $name || '_' || $value_key] += $new_balance - $vote.balance; |
| 209 | $votes[$name].balance = $new_balance; |
| 210 | }); |
| 211 | var['votes_'||trigger.data.address] = $votes; |
| 212 | }" |
| 213 | } |
| 214 | ] |
| 215 | } |
| 216 | ] |
| 217 | } |
| 218 | } |
| 219 | ] |