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