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