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