| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://ostable.org/bonded-stablecoin-governance.json", |
| 5 | "init": "{ |
| 6 | $regular_challenging_period = params.regular_challenging_period OTHERWISE 3*24*3600; |
| 7 | $important_challenging_period = params.important_challenging_period OTHERWISE 30*24*3600; |
| 8 | |
| 9 | |
| 10 | $freeze_period = params.freeze_period OTHERWISE 30*24*3600; |
| 11 | |
| 12 | |
| 13 | $proposal_min_support = params.proposal_min_support OTHERWISE 0.5; |
| 14 | |
| 15 | $curve_aa = params.curve_aa; |
| 16 | if (!$curve_aa) |
| 17 | bounce("no curve_aa"); |
| 18 | $fund_aa = var[$curve_aa]['fund_aa']; |
| 19 | $asset = var[$fund_aa]['shares_asset']; |
| 20 | |
| 21 | $names = ['fee_multiplier', 'moved_capacity_share', 'threshold_distance', 'move_capacity_timeout', 'slow_capacity_share', 'sf_capacity_share', 'interest_rate', 'oracles', 'proposal', 'deposits.min_deposit_term', 'deposits.challenging_period', 'deposits.challenge_immunity_period', 'deposits.reporter_share', 'decision_engine_aa']; |
| 22 | |
| 23 | $is_allowed_name = $name => { |
| 24 | length(filter($names, 20, $n => $n == $name)) == 1 |
| 25 | }; |
| 26 | |
| 27 | $get_challenging_period = ($name) => ($name == 'oracles' OR $name == 'decision_engine_aa') ? $important_challenging_period : $regular_challenging_period; |
| 28 | |
| 29 | |
| 30 | $get_oracles = $oracles_string => { |
| 31 | $pairs = split($oracles_string, " "); |
| 32 | if (length($pairs) > 3) |
| 33 | bounce("too many oracles"); |
| 34 | $oracles_data = map($pairs, 3, $pair => { |
| 35 | $oracle = substring($pair, 0, 32); |
| 36 | if (!is_valid_address($oracle)) |
| 37 | bounce("invalid oracle address: " || $oracle); |
| 38 | $op = substring($pair, 32, 1); |
| 39 | if ($op != '*' AND $op != '/') |
| 40 | bounce("invalid format of oracles, should be oracle*feed_name or oracle/feed_name"); |
| 41 | $feed_name = substring($pair, 33); |
| 42 | { |
| 43 | oracle: $oracle, |
| 44 | feed_name: $feed_name, |
| 45 | op: $op, |
| 46 | } |
| 47 | }); |
| 48 | $oracles_data |
| 49 | }; |
| 50 | |
| 51 | $get_value_key = $value => { |
| 52 | $key_len = length('support_' || 'oracles' || '_' || $value || '_') + 32; |
| 53 | ($key_len > 128) ? sha256($value) : $value |
| 54 | }; |
| 55 | |
| 56 | }", |
| 57 | "messages": { |
| 58 | "cases": [ |
| 59 | { |
| 60 | "if": "{ trigger.data.name AND trigger.data.commit }", |
| 61 | "init": "{ |
| 62 | $name = trigger.data.name; |
| 63 | $leader = var['leader_' || $name]; |
| 64 | $current_value = var[$name]; |
| 65 | if (!exists($leader)) |
| 66 | bounce("no leader"); |
| 67 | if (exists($current_value) AND $leader == $current_value) |
| 68 | bounce("already equal to leader"); |
| 69 | if (var['challenging_period_start_ts_' || $name] + $get_challenging_period($name) > timestamp) |
| 70 | bounce("challenging period not expired yet"); |
| 71 | if ($name == 'proposal'){ |
| 72 | $expiry = var['proposal_' || $leader || '_expiry']; |
| 73 | if (parse_date($expiry) < timestamp) |
| 74 | bounce("the proposal has expired"); |
| 75 | if (var['proposal_' || $leader || '_approved']) |
| 76 | bounce("the proposal has already been approved"); |
| 77 | $support = var['support_' || $name || '_' || $leader]; |
| 78 | if ($support < var[$fund_aa]['shares_supply'] * $proposal_min_support) |
| 79 | bounce("not enough support for the proposal"); |
| 80 | $proposal_amount = var['proposal_' || $leader || '_amount']; |
| 81 | if ($proposal_amount == 0) |
| 82 | bounce("the proposal is 0 amount"); |
| 83 | } |
| 84 | }", |
| 85 | "messages": [ |
| 86 | { |
| 87 | "app": "payment", |
| 88 | "payload": { |
| 89 | "asset": "base", |
| 90 | "outputs": [ |
| 91 | { |
| 92 | "address": "{$name == 'proposal' ? var[$curve_aa]['decision_engine_aa'] : $curve_aa}", |
| 93 | "amount": 5000 |
| 94 | } |
| 95 | ] |
| 96 | } |
| 97 | }, |
| 98 | { |
| 99 | "if": "{$name != 'proposal'}", |
| 100 | "app": "data", |
| 101 | "payload": { |
| 102 | "name": "{$name}", |
| 103 | "value": "{($name == 'oracles') ? $get_oracles($leader) : $leader}" |
| 104 | } |
| 105 | }, |
| 106 | { |
| 107 | "if": "{$name == 'proposal'}", |
| 108 | "app": "data", |
| 109 | "payload": { |
| 110 | "grant": 1, |
| 111 | "recipient": "{var['proposal_' || $leader || '_recipient']}", |
| 112 | "amount": "{var['proposal_' || $leader || '_amount']}" |
| 113 | } |
| 114 | }, |
| 115 | { |
| 116 | "app": "state", |
| 117 | "state": "{ |
| 118 | if ($name != 'proposal') |
| 119 | var[$name] = $leader; |
| 120 | else |
| 121 | var['proposal_' || $leader || '_approved'] = 1; |
| 122 | }" |
| 123 | } |
| 124 | ] |
| 125 | }, |
| 126 | { |
| 127 | "if": "{ trigger.data.name }", |
| 128 | "init": "{ |
| 129 | $balance = var['balance_' || trigger.address] + trigger.output[[asset=$asset]]; |
| 130 | if (!$balance) |
| 131 | bounce("you have no deposited balance and cannot vote"); |
| 132 | $name = trigger.data.name; |
| 133 | $value = trigger.data.value; |
| 134 | if (!$is_allowed_name($name)) |
| 135 | bounce("unknown name: " || $name); |
| 136 | if (exists($value)){ |
| 137 | if ($name == 'fee_multiplier' AND !(typeof($value) == 'number' AND $value >= 0)) |
| 138 | bounce("invalid value"); |
| 139 | if ($name == 'moved_capacity_share' AND !(typeof($value) == 'number' AND $value > 0 AND $value <= 1)) |
| 140 | bounce("invalid value"); |
| 141 | if ($name == 'threshold_distance' AND !(typeof($value) == 'number' AND $value > 0 AND $value <= 0.2)) |
| 142 | bounce("invalid value"); |
| 143 | if ($name == 'move_capacity_timeout' AND !(is_integer($value) AND $value > 0)) |
| 144 | bounce("invalid value"); |
| 145 | if ($name == 'slow_capacity_share' AND !(typeof($value) == 'number' AND $value >= 0 AND $value <= 1)) |
| 146 | bounce("invalid value"); |
| 147 | if ($name == 'sf_capacity_share' AND !(typeof($value) == 'number' AND $value >= 0 AND $value <= 1)) |
| 148 | bounce("invalid value"); |
| 149 | if ($name == 'interest_rate' AND !(typeof($value) == 'number' AND $value >= 0)) |
| 150 | bounce("invalid value"); |
| 151 | if ($name == 'oracles') |
| 152 | $oracles_data = $get_oracles($value); |
| 153 | if ($name == 'deposits.min_deposit_term' AND !(is_integer($value) AND $value >= 0)) |
| 154 | bounce("invalid value"); |
| 155 | if ($name == 'deposits.challenging_period' AND !(is_integer($value) AND $value >= 0)) |
| 156 | bounce("invalid value"); |
| 157 | if ($name == 'deposits.challenge_immunity_period' AND !(is_integer($value) AND $value >= 0)) |
| 158 | bounce("invalid value"); |
| 159 | if ($name == 'deposits.reporter_share' AND !(typeof($value) == 'number' AND $value >= 0 AND $value <= 1)) |
| 160 | bounce("invalid value"); |
| 161 | if ($name == 'decision_engine_aa' AND !is_aa($value)) |
| 162 | bounce("invalid value"); |
| 163 | if ($name == 'proposal'){ |
| 164 | if (!(is_integer($value) AND $value > 0)) |
| 165 | bounce("invalid value"); |
| 166 | $expiry = var['proposal_' || $value || '_expiry']; |
| 167 | if (!$expiry) |
| 168 | bounce("no such proposal"); |
| 169 | if (parse_date($expiry) < timestamp) |
| 170 | bounce("the proposal has expired"); |
| 171 | if (var['proposal_' || $value || '_approved']) |
| 172 | bounce("the proposal has already been approved"); |
| 173 | } |
| 174 | } |
| 175 | }", |
| 176 | "messages": [ |
| 177 | { |
| 178 | "app": "state", |
| 179 | "state": "{ |
| 180 | if (trigger.output[[asset=$asset]]) |
| 181 | var['balance_' || trigger.address] += trigger.output[[asset=$asset]]; |
| 182 | $prev_choice = var['choice_' || trigger.address || '_' || $name]; |
| 183 | $leader = var['leader_' || $name]; |
| 184 | if (exists($leader) AND exists($prev_choice) AND $prev_choice == $leader AND var['challenging_period_start_ts_' || $name] + $get_challenging_period($name) + $freeze_period > timestamp) |
| 185 | bounce("you cannot change your vote yet"); |
| 186 | var['choice_' || trigger.address || '_' || $name] = $value; |
| 187 | if (exists($prev_choice)){ |
| 188 | $prev_choice_key = $get_value_key($prev_choice); |
| 189 | var['support_' || $name || '_' || $prev_choice_key] -= var['support_' || $name || '_' || $prev_choice_key || '_' || trigger.address]; |
| 190 | var['support_' || $name || '_' || $prev_choice_key || '_' || trigger.address] = false; |
| 191 | } |
| 192 | if (exists($value)){ |
| 193 | $value_key = $get_value_key($value); |
| 194 | var['support_' || $name || '_' || $value_key] += $balance; |
| 195 | var['support_' || $name || '_' || $value_key || '_' || trigger.address] = $balance; |
| 196 | if (!exists($leader) OR var['support_' || $name || '_' || $value_key] > var['support_' || $name || '_' || $get_value_key($leader)]){ |
| 197 | var['leader_' || $name] = $value; |
| 198 | var['challenging_period_start_ts_' || $name] = timestamp; |
| 199 | } |
| 200 | } |
| 201 | }" |
| 202 | } |
| 203 | ] |
| 204 | }, |
| 205 | { |
| 206 | "if": "{ trigger.data.withdraw }", |
| 207 | "init": "{ |
| 208 | $balance = var['balance_' || trigger.address] + trigger.output[[asset=$asset]]; |
| 209 | if (!$balance) |
| 210 | bounce("you have no deposited balance and cannot withdraw"); |
| 211 | $amount = trigger.data.amount OTHERWISE $balance; |
| 212 | if ($amount > $balance) |
| 213 | bounce("your balance is only " || $balance); |
| 214 | foreach($names, 14, $name => { |
| 215 | if (var['choice_' || trigger.address || '_' || $name]) |
| 216 | bounce("support for " || $name || " not removed yet"); |
| 217 | }); |
| 218 | }", |
| 219 | "messages": [ |
| 220 | { |
| 221 | "app": "payment", |
| 222 | "payload": { |
| 223 | "asset": "{$asset}", |
| 224 | "outputs": [ |
| 225 | { |
| 226 | "address": "{trigger.address}", |
| 227 | "amount": "{ $amount }" |
| 228 | } |
| 229 | ] |
| 230 | } |
| 231 | }, |
| 232 | { |
| 233 | "app": "state", |
| 234 | "state": "{ |
| 235 | var['balance_' || trigger.address] -= $amount; |
| 236 | }" |
| 237 | } |
| 238 | ] |
| 239 | }, |
| 240 | { |
| 241 | "if": "{ trigger.data.add_proposal AND trigger.data.type AND definition[$curve_aa][1].params.allow_grants }", |
| 242 | "init": "{ |
| 243 | if (trigger.data.type != 'grant') |
| 244 | bounce("unrecognized proposal type"); |
| 245 | if (!is_valid_address(trigger.data.recipient)) |
| 246 | bounce("invalid grant recipient address"); |
| 247 | if (!is_integer(trigger.data.amount) OR trigger.data.amount < 0) |
| 248 | bounce("invalid grant amount"); |
| 249 | if (!unit[trigger.data.unit]) |
| 250 | bounce("proposal unit not found"); |
| 251 | if (!trigger.data.expiry) |
| 252 | bounce("proposal expiry date not set"); |
| 253 | $expiry_ts = parse_date(trigger.data.expiry); |
| 254 | if (!$expiry_ts) |
| 255 | bounce("invalid expiry date"); |
| 256 | }", |
| 257 | "messages": [ |
| 258 | { |
| 259 | "app": "state", |
| 260 | "state": "{ |
| 261 | var['count_proposals'] += 1; |
| 262 | $num = var['count_proposals']; |
| 263 | var['proposal_' || $num || '_recipient'] = trigger.data.recipient; |
| 264 | var['proposal_' || $num || '_amount'] = trigger.data.amount; |
| 265 | var['proposal_' || $num || '_unit'] = trigger.data.unit; |
| 266 | var['proposal_' || $num || '_expiry'] = trigger.data.expiry; |
| 267 | }" |
| 268 | } |
| 269 | ] |
| 270 | } |
| 271 | ] |
| 272 | } |
| 273 | } |
| 274 | ] |