| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://friends.obyte.org/governance.json", |
| 5 | "init": "{ |
| 6 | |
| 7 | $year = 31536000; |
| 8 | $challenging_period = params.challenging_period OTHERWISE 3*24*3600; |
| 9 | |
| 10 | $friend_aa = params.friend_aa; |
| 11 | require($friend_aa, "no friend_aa"); |
| 12 | |
| 13 | $constants = var[$friend_aa]['constants']; |
| 14 | $asset = $constants.asset; |
| 15 | |
| 16 | $names = [ |
| 17 | 'rewards_aa', |
| 18 | 'messaging_attestors', |
| 19 | 'real_name_attestors', |
| 20 | 'referrer_frd_deposit_reward_share', |
| 21 | 'referrer_bytes_deposit_reward_share', |
| 22 | 'referrer_deposit_asset_deposit_reward_share', |
| 23 | 'followup_reward_share', |
| 24 | 'min_balance_instead_of_real_name', |
| 25 | 'deposit_asset', |
| 26 | ]; |
| 27 | $count_names = 9; |
| 28 | require(length($names) == $count_names, "wrong number of governable parameters"); |
| 29 | |
| 30 | $is_allowed_name = $name => { |
| 31 | length(filter($names, $count_names, $n => $n == $name)) == 1 |
| 32 | }; |
| 33 | |
| 34 | $get_sqrt_balance = ($address, $bThrow) => { |
| 35 | $user = var[$friend_aa]['user_'||$address]; |
| 36 | require($user, "you are not a user here"); |
| 37 | |
| 38 | $min_unlock_date = timestamp_to_string(timestamp + $year, 'date'); |
| 39 | if ($user.unlock_date < $min_unlock_date){ |
| 40 | if ($bThrow) |
| 41 | bounce("your unlock date must be "||$min_unlock_date||" or later"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | $balance = $user.balances.frd + $user.balances.base / $friend_aa#2.$get_ceiling_price(); |
| 47 | if (!$balance){ |
| 48 | if ($bThrow) |
| 49 | bounce("you have no locked balance and cannot vote"); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | sqrt($balance) |
| 54 | }; |
| 55 | |
| 56 | $get_value_key = $value => { |
| 57 | $key_len = length('support_messaging_attestors_' || $value); |
| 58 | ($key_len > 128) ? 'hash_'||sha256($value) : $value |
| 59 | }; |
| 60 | |
| 61 | }", |
| 62 | "messages": { |
| 63 | "cases": [ |
| 64 | { |
| 65 | "if": "{ trigger.data.name AND trigger.data.commit }", |
| 66 | "init": "{ |
| 67 | $name = trigger.data.name; |
| 68 | if ($name == 'deposit_asset'){ |
| 69 | $deposit_asset = trigger.data.deposit_asset; |
| 70 | require(typeof($deposit_asset) == 'string' AND length($deposit_asset) == 44 AND $deposit_asset != $asset, "invalid deposit asset"); |
| 71 | $full_name = $name||'_'||$deposit_asset; |
| 72 | } |
| 73 | else |
| 74 | $full_name = $name; |
| 75 | |
| 76 | $leader = var['leader_' || $full_name]; |
| 77 | require(exists($leader), "no leader"); |
| 78 | $current_value = var[$full_name]; |
| 79 | if (exists($current_value) AND $leader == $current_value) |
| 80 | bounce("already equal to leader"); |
| 81 | require(var['challenging_period_start_ts_' || $full_name] + $challenging_period < timestamp, "challenging period not expired yet"); |
| 82 | $payload = { |
| 83 | name: $name, |
| 84 | value: $leader, |
| 85 | }; |
| 86 | if ($name == 'deposit_asset'){ |
| 87 | require($leader != 'no', "only oswap AA can be committed"); |
| 88 | $payload.deposit_asset = $deposit_asset; |
| 89 | } |
| 90 | }", |
| 91 | "messages": [ |
| 92 | { |
| 93 | "app": "payment", |
| 94 | "payload": { |
| 95 | "asset": "base", |
| 96 | "outputs": [ |
| 97 | { |
| 98 | "address": "{$friend_aa}", |
| 99 | "amount": 5000 |
| 100 | } |
| 101 | ] |
| 102 | } |
| 103 | }, |
| 104 | { |
| 105 | "app": "data", |
| 106 | "payload": "{$payload}" |
| 107 | }, |
| 108 | { |
| 109 | "app": "state", |
| 110 | "state": "{ |
| 111 | var[$full_name] = $leader; |
| 112 | }" |
| 113 | } |
| 114 | ] |
| 115 | }, |
| 116 | { |
| 117 | "if": "{ trigger.data.name }", |
| 118 | "init": "{ |
| 119 | $name = trigger.data.name; |
| 120 | $value = trigger.data.value; |
| 121 | require($is_allowed_name($name), "unknown name: " || $name); |
| 122 | if ($name == 'deposit_asset'){ |
| 123 | $deposit_asset = trigger.data.deposit_asset; |
| 124 | require(typeof($deposit_asset) == 'string' AND length($deposit_asset) == 44 AND $deposit_asset != $asset, "invalid deposit asset"); |
| 125 | $full_name = $name||'_'||$deposit_asset; |
| 126 | } |
| 127 | else |
| 128 | $full_name = $name; |
| 129 | |
| 130 | $sqrt_balance = $get_sqrt_balance(trigger.address, true); |
| 131 | |
| 132 | if (exists($value)){ |
| 133 | if ($name == 'rewards_aa' OR $name == 'messaging_attestors' OR $name == 'real_name_attestors' OR $name == 'deposit_asset'){ |
| 134 | require(typeof($value) == 'string', "must be a string"); |
| 135 | if ($name == 'rewards_aa') |
| 136 | require(is_aa($value), "not an AA"); |
| 137 | else if ($name == 'deposit_asset'){ |
| 138 | if ($value == 'no') |
| 139 | require(!var[$full_name], "this asset is already added, you can't vote 'no' anymore"); |
| 140 | else{ |
| 141 | require(is_aa($value), "not an AA"); |
| 142 | |
| 143 | $friend_aa#2.$get_deposit_asset_exchange_rates_on_aa($deposit_asset, $value); |
| 144 | } |
| 145 | } |
| 146 | else{ |
| 147 | $arr = split($value, ':'); |
| 148 | require(length($arr) > 0, "no attestors"); |
| 149 | foreach($arr, 5, $addr => { require(is_valid_address($addr), "invalid attestor address"); }); |
| 150 | } |
| 151 | } |
| 152 | else{ |
| 153 | require(typeof($value) == 'number' AND $value >= 0, "must be a nonnegative number"); |
| 154 | if ($name == 'min_balance_instead_of_real_name') |
| 155 | require(is_integer($value), "value must be integer"); |
| 156 | else |
| 157 | require($value < 1, "value must be < 1"); |
| 158 | } |
| 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.sqrt_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] += $sqrt_balance; |
| 177 | $votes[$full_name] = { value: $value, sqrt_balance: $sqrt_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 }", |
| 190 | "messages": [ |
| 191 | { |
| 192 | "app": "state", |
| 193 | "state": "{ |
| 194 | $votes = var['votes_'||trigger.data.address]; |
| 195 | if (!$votes) return; |
| 196 | $sqrt_balance = $get_sqrt_balance(trigger.data.address, false); |
| 197 | foreach($votes, $count_names, ($name, $vote) => { |
| 198 | $value_key = $get_value_key($vote.value); |
| 199 | var['support_' || $name || '_' || $value_key] += $sqrt_balance - $vote.sqrt_balance; |
| 200 | $votes[$name].sqrt_balance = $sqrt_balance; |
| 201 | }); |
| 202 | var['votes_'||trigger.data.address] = $votes; |
| 203 | }" |
| 204 | } |
| 205 | ] |
| 206 | } |
| 207 | ] |
| 208 | } |
| 209 | } |
| 210 | ] |