| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "doc_url": "https://counterstake.org/bridge-import-governance.json", |
| 5 | "getters": "{ |
| 6 | |
| 7 | $get_oracles = $oracles_string => { |
| 8 | $pairs = split($oracles_string, " "); |
| 9 | if (length($pairs) > 3) |
| 10 | bounce("too many oracles"); |
| 11 | $oracles_data = map($pairs, 3, $pair => { |
| 12 | $oracle = substring($pair, 0, 32); |
| 13 | if (!is_valid_address($oracle)) |
| 14 | bounce("invalid oracle address: " || $oracle); |
| 15 | $op = substring($pair, 32, 1); |
| 16 | if ($op != '*' AND $op != '/') |
| 17 | bounce("invalid format of oracles, should be oracle*feed_name or oracle/feed_name"); |
| 18 | $feed_name = substring($pair, 33); |
| 19 | { |
| 20 | oracle: $oracle, |
| 21 | feed_name: $feed_name, |
| 22 | op: $op, |
| 23 | } |
| 24 | }); |
| 25 | $oracles_data |
| 26 | }; |
| 27 | |
| 28 | $get_challenging_periods = $periods_string => { |
| 29 | $periods = split($periods_string, " "); |
| 30 | if (length($periods) > 20) |
| 31 | bounce("too many periods"); |
| 32 | $prev = {period: 0}; |
| 33 | map($periods, 20, $period => { |
| 34 | $nPeriod = +$period; |
| 35 | if ($nPeriod <= 0 OR $nPeriod > 3*365*24) |
| 36 | bounce("bad challenging period: " || $period); |
| 37 | if ($nPeriod < $prev.period) |
| 38 | bounce("subsequent periods cannot get shorter"); |
| 39 | $prev.period = $nPeriod; |
| 40 | $nPeriod |
| 41 | }) |
| 42 | }; |
| 43 | |
| 44 | $get_parsed_value = ($name, $value) => { |
| 45 | if ($name == 'oracles') |
| 46 | return $get_oracles($value); |
| 47 | if ($name == 'challenging_periods' OR $name == 'large_challenging_periods') |
| 48 | return $get_challenging_periods($value); |
| 49 | $value |
| 50 | }; |
| 51 | |
| 52 | }", |
| 53 | "init": "{ |
| 54 | |
| 55 | $challenging_period = params.challenging_period OTHERWISE 3*24*3600; |
| 56 | |
| 57 | |
| 58 | $freeze_period = params.freeze_period OTHERWISE 30*24*3600; |
| 59 | |
| 60 | $import_aa = params.import_aa; |
| 61 | if (!$import_aa) |
| 62 | bounce("no import_aa"); |
| 63 | $asset = var[$import_aa]['asset']; |
| 64 | |
| 65 | $names = ['ratio', 'counterstake_coef', 'min_tx_age', 'min_stake', 'large_threshold', 'challenging_periods', 'large_challenging_periods', 'min_price', 'oracles']; |
| 66 | |
| 67 | $is_allowed_name = $name => { |
| 68 | length(filter($names, 20, $n => $n == $name)) == 1 |
| 69 | }; |
| 70 | |
| 71 | $get_value_key = $value => { |
| 72 | $key_len = length('support_' || 'oracles' || '_' || $value || '_') + 32; |
| 73 | ($key_len > 128) ? sha256($value) : $value |
| 74 | }; |
| 75 | |
| 76 | }", |
| 77 | "messages": { |
| 78 | "cases": [ |
| 79 | { |
| 80 | "if": "{ trigger.data.name AND trigger.data.commit }", |
| 81 | "init": "{ |
| 82 | $name = trigger.data.name; |
| 83 | $leader = var['leader_' || $name]; |
| 84 | $current_value = var[$name]; |
| 85 | if (!exists($leader)) |
| 86 | bounce("no leader"); |
| 87 | if (exists($current_value) AND $leader == $current_value) |
| 88 | bounce("already equal to leader"); |
| 89 | if (var['challenging_period_start_ts_' || $name] + $challenging_period > timestamp) |
| 90 | bounce("challenging period not expired yet"); |
| 91 | }", |
| 92 | "messages": [ |
| 93 | { |
| 94 | "app": "payment", |
| 95 | "payload": { |
| 96 | "asset": "base", |
| 97 | "outputs": [ |
| 98 | { |
| 99 | "address": "{$import_aa}", |
| 100 | "amount": 5000 |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | }, |
| 105 | { |
| 106 | "app": "data", |
| 107 | "payload": { |
| 108 | "name": "{$name}", |
| 109 | "value": "{$get_parsed_value($name, $leader)}" |
| 110 | } |
| 111 | }, |
| 112 | { |
| 113 | "app": "state", |
| 114 | "state": "{ |
| 115 | var[$name] = $leader; |
| 116 | }" |
| 117 | } |
| 118 | ] |
| 119 | }, |
| 120 | { |
| 121 | "if": "{ trigger.data.name }", |
| 122 | "init": "{ |
| 123 | $balance = var['balance_' || trigger.address] + trigger.output[[asset=$asset]]; |
| 124 | if (!$balance) |
| 125 | bounce("you have no deposited balance and cannot vote"); |
| 126 | $name = trigger.data.name; |
| 127 | $value = trigger.data.value; |
| 128 | if (!$is_allowed_name($name)) |
| 129 | bounce("unknown name: " || $name); |
| 130 | if (exists($value)){ |
| 131 | if ($name == 'ratio' AND !(typeof($value) == 'number' AND $value > 0)) |
| 132 | bounce("invalid value"); |
| 133 | if ($name == 'counterstake_coef' AND !(typeof($value) == 'number' AND $value > 1)) |
| 134 | bounce("invalid value"); |
| 135 | if ($name == 'min_stake' AND !(is_integer($value) AND $value >= 0)) |
| 136 | bounce("invalid value"); |
| 137 | if ($name == 'min_tx_age' AND !(is_integer($value) AND $value >= 0)) |
| 138 | bounce("invalid value"); |
| 139 | if ($name == 'large_threshold' AND !(is_integer($value) AND $value >= 0)) |
| 140 | bounce("invalid value"); |
| 141 | if ($name == 'min_price' AND !(typeof($value) == 'number' AND $value >= 0)) |
| 142 | bounce("invalid value"); |
| 143 | if ($name == 'oracles' OR $name == 'challenging_periods' OR $name == 'large_challenging_periods') |
| 144 | $data = $get_parsed_value($name, $value); |
| 145 | } |
| 146 | }", |
| 147 | "messages": [ |
| 148 | { |
| 149 | "app": "state", |
| 150 | "state": "{ |
| 151 | if (trigger.output[[asset=$asset]]) |
| 152 | var['balance_' || trigger.address] += trigger.output[[asset=$asset]]; |
| 153 | $prev_choice = var['choice_' || trigger.address || '_' || $name]; |
| 154 | $leader = var['leader_' || $name]; |
| 155 | if (exists($leader) AND exists($prev_choice) AND $prev_choice == $leader AND var['challenging_period_start_ts_' || $name] + $challenging_period + $freeze_period > timestamp) |
| 156 | bounce("you cannot change your vote yet"); |
| 157 | var['choice_' || trigger.address || '_' || $name] = $value; |
| 158 | if (exists($prev_choice)){ |
| 159 | $prev_choice_key = $get_value_key($prev_choice); |
| 160 | var['support_' || $name || '_' || $prev_choice_key] -= var['support_' || $name || '_' || $prev_choice_key || '_' || trigger.address]; |
| 161 | var['support_' || $name || '_' || $prev_choice_key || '_' || trigger.address] = false; |
| 162 | } |
| 163 | if (exists($value)){ |
| 164 | $value_key = $get_value_key($value); |
| 165 | var['support_' || $name || '_' || $value_key] += $balance; |
| 166 | var['support_' || $name || '_' || $value_key || '_' || trigger.address] = $balance; |
| 167 | if (!exists($leader) OR var['support_' || $name || '_' || $value_key] > var['support_' || $name || '_' || $get_value_key($leader)]){ |
| 168 | var['leader_' || $name] = $value; |
| 169 | var['challenging_period_start_ts_' || $name] = timestamp; |
| 170 | } |
| 171 | } |
| 172 | }" |
| 173 | } |
| 174 | ] |
| 175 | }, |
| 176 | { |
| 177 | "if": "{ trigger.data.withdraw }", |
| 178 | "init": "{ |
| 179 | $balance = var['balance_' || trigger.address] + trigger.output[[asset=$asset]]; |
| 180 | if (!$balance) |
| 181 | bounce("you have no deposited balance and cannot withdraw"); |
| 182 | $amount = trigger.data.amount OTHERWISE $balance; |
| 183 | if ($amount > $balance) |
| 184 | bounce("your balance is only " || $balance); |
| 185 | foreach($names, 12, $name => { |
| 186 | if (var['choice_' || trigger.address || '_' || $name]) |
| 187 | bounce("support for " || $name || " not removed yet"); |
| 188 | }); |
| 189 | }", |
| 190 | "messages": [ |
| 191 | { |
| 192 | "app": "payment", |
| 193 | "payload": { |
| 194 | "asset": "{$asset}", |
| 195 | "outputs": [ |
| 196 | { |
| 197 | "address": "{trigger.address}", |
| 198 | "amount": "{ $amount }" |
| 199 | } |
| 200 | ] |
| 201 | } |
| 202 | }, |
| 203 | { |
| 204 | "app": "state", |
| 205 | "state": "{ |
| 206 | var['balance_' || trigger.address] -= $amount; |
| 207 | }" |
| 208 | } |
| 209 | ] |
| 210 | } |
| 211 | ] |
| 212 | } |
| 213 | } |
| 214 | ] |