-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeposits.oscript
562 lines (531 loc) · 17.4 KB
/
deposits.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
{
doc_url: "https://ostable.org/bonded-stablecoin-deposits.json",
getters: `{
$_get_param = ($name, $state_params, $initial_params, $default_params) => {
$state_value = $state_params[$name];
if (exists($state_value))
return $state_value;
$initial_value = $initial_params[$name];
if (exists($initial_value))
return $initial_value;
$default_params[$name]
};
$get_curve_aa = () => params.curve_aa;
$get_curve_aa_params = () => definition[params.curve_aa][1].params;
$get_deposit_params = () => {
$default_params = {
min_deposit_term: 2 * 3600,
challenging_period: 12 * 3600,
challenge_immunity_period: 3600,
reporter_share: 0.2, // 20%
};
$curve_aa_params = $get_curve_aa_params();
$deposit_state_params = var[params.curve_aa]['deposit_params'];
{
min_deposit_term: $_get_param('min_deposit_term', $deposit_state_params, $curve_aa_params.deposits, $default_params),
challenging_period: $_get_param('challenging_period', $deposit_state_params, $curve_aa_params.deposits, $default_params),
challenge_immunity_period: $_get_param('challenge_immunity_period', $deposit_state_params, $curve_aa_params.deposits, $default_params),
reporter_share: $_get_param('reporter_share', $deposit_state_params, $curve_aa_params.deposits, $default_params),
}
};
}`,
init: `{
$bank_aa = 'GV5YXIIRH3DH5FTEECW7IS2EQTAYJJ6S';
$curve_aa = params.curve_aa;
// tokens
$asset = var['asset'];
$interest_rate = var[$curve_aa]['interest_rate'];
$term = (timestamp - var[$curve_aa]['rate_update_ts']) / (360 * 24 * 3600); // in years
$growth_factor = var[$curve_aa]['growth_factor'] * (1 + $interest_rate)^$term;
$curve_aa_params = $get_curve_aa_params();
$reserve_asset = $curve_aa_params.reserve_asset;// OTHERWISE 'base';
$interest_asset = var[$curve_aa]['asset2'];
$deposit_params = $get_deposit_params();
$min_deposit_term = $deposit_params.min_deposit_term;
$challenging_period = $deposit_params.challenging_period;
$challenge_immunity_period = $deposit_params.challenge_immunity_period;
$reporter_share = $deposit_params.reporter_share;
$remove_deposit = ($id, $stable_amount) => {
var['deposit_' || $id] = false;
var['supply'] -= $stable_amount;
};
}`,
messages: {
cases: [
// define the stable asset
{
if: `{ trigger.data.define AND !$asset }`,
messages: [
{
app: 'asset',
payload: {
// without cap
is_private: false,
is_transferrable: true,
auto_destroy: false,
fixed_denominations: false,
issued_by_definer_only: true,
cosigned_by_definer: false,
spender_attested: false,
}
},
{ // call back to factory to record our newly defined asset
if: `{trigger.data.factory}`,
app: 'data',
payload: {
write_stable_asset: 1,
curve_aa: `{$curve_aa}`,
}
},
{
if: `{trigger.data.factory}`,
app: 'payment',
payload: {
asset: 'base',
outputs: [
{address: '{trigger.data.factory}'},
]
}
},
{
app: 'state',
state: `{
var['asset'] = response_unit;
response['asset'] = response_unit;
}`
}
]
},
// open a deposit in interest token and take a stable token in exchange
{
if: `{ $asset AND trigger.output[[asset=$interest_asset]] > 0 }`,
init: `{
if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient))
bounce("bad address of interest recipient");
if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to))
bounce("bad to-address");
if (exists(trigger.data.owner) AND !is_valid_address(trigger.data.owner))
bounce("bad owner address");
$to = trigger.data.to OTHERWISE trigger.address;
$owner = trigger.data.owner OTHERWISE $to;
$deposit_amount = trigger.output[[asset=$interest_asset]];
$stable_amount = floor($deposit_amount * $growth_factor);
$protection = max(0, trigger.output[[asset=$reserve_asset]] - ($reserve_asset == 'base' ? 10000 : 0));
$id = trigger.unit;
}`,
messages: [
{
app: 'payment',
payload: {
asset: `{$asset}`,
outputs: [
{address: "{$to}", amount: "{ $stable_amount }"}
]
}
},
{
if: `{trigger.data.to AND is_aa(trigger.data.to)}`,
app: 'data',
payload: {
to: `{trigger.address}`
}
},
{
app: 'state',
state: `{
response['id'] = $id;
$deposit = {
amount: $deposit_amount,
stable_amount: $stable_amount,
owner: $owner,
ts: timestamp,
};
if (trigger.data.interest_recipient)
$deposit.interest_recipient = trigger.data.interest_recipient;
if ($protection)
$deposit.protection = $protection;
var['deposit_' || $id] = $deposit;
var['supply'] += $stable_amount;
}`
}
]
},
// close a deposit and get the interest token back
{
if: `{ $asset AND trigger.output[[asset=$asset]] > 0 AND trigger.data.id }`,
init: `{
if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to))
bounce("bad to-address");
$to = trigger.data.to OTHERWISE trigger.address;
$id = trigger.data.id;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("no such deposit");
if (timestamp < $deposit.ts + $min_deposit_term)
bounce("the deposit is too new to be closed");
$bOwner = ($deposit.owner == trigger.address);
$bForceClose = !$bOwner;
if (var['deposit_' || $id || '_force_close'])
bounce("force-close already requested");
if ($deposit.interest_recipient AND $deposit.interest_recipient != $deposit.owner OR $bForceClose){
$new_stable_amount = floor($deposit.amount * $growth_factor);
$interest = $new_stable_amount - $deposit.stable_amount;
$expected_stable_amount = $new_stable_amount;
}
else
$expected_stable_amount = $deposit.stable_amount;
if ($expected_stable_amount > trigger.output[[asset=$asset]])
bounce("expected " || $expected_stable_amount);
$change = trigger.output[[asset=$asset]] - $expected_stable_amount;
$to_aa = trigger.data.to AND is_aa(trigger.data.to);
}`,
messages: [
{
if: '{$bOwner}',
app: 'payment',
payload: {
asset: `{$interest_asset}`,
outputs: [
{address: "{$to}", amount: "{ $deposit.amount }"}
]
}
},
{
if: `{$to_aa AND $bOwner}`,
app: 'data',
payload: {
to: `{trigger.address}`
}
},
{
if: `{$to_aa AND $bOwner}`,
app: 'payment',
payload: {
asset: 'base',
outputs: [
{address: "{$to}", amount: 2000}
]
}
},
{ // pay the accrued interest to the interest recipient and the change back to the requester
app: 'payment',
payload: {
asset: `{$asset}`,
outputs: [
{if: '{$interest AND $bOwner}', address: "{$deposit.interest_recipient}", amount: "{ $interest }"},
{address: "{trigger.address}", amount: "{$change}"},
]
}
},
{ // pay the protection back to the owner (not to the "to" address which is for the interest token only)
if: '{$deposit.protection AND $bOwner}',
app: 'payment',
payload: {
asset: `{$reserve_asset}`,
outputs: [
{address: "{$deposit.owner}", amount: "{ $deposit.protection }"}
]
}
},
{
app: 'state',
state: `{
if ($bOwner)
$remove_deposit($id, $deposit.stable_amount);
else // force close
var['deposit_' || $id || '_force_close'] = {
ts: timestamp,
closer: trigger.address,
interest: $interest,
protection_ratio: $deposit.protection / $deposit.amount,
};
}`
}
]
},
// commit a force-close
{
if: `{ $asset AND trigger.data.commit_force_close AND trigger.data.id }`,
init: `{
$id = trigger.data.id;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("no such deposit");
$force_close = var['deposit_' || $id || '_force_close'];
if (!$force_close)
bounce("this deposit was not force closed");
if (timestamp < $force_close.ts + $challenging_period)
bounce("challenging period has not expired yet");
$interest_recipient = $deposit.interest_recipient OTHERWISE $deposit.owner;
$payments = [
{ // the closer gets the deposited interest asset
address: $force_close.closer,
asset: $interest_asset,
amount: $deposit.amount
},
{ // pay the accrued interest to the interest recipient
// interest is remembered from the time of the request, it stops accruing during the challenging period
address: $interest_recipient,
asset: $asset,
amount: $force_close.interest
},
{ // pay the protection back to the owner
address: $deposit.owner,
asset: $reserve_asset,
amount: $deposit.protection
},
];
foreach($payments, 3, ($i, $payment) => {
$payments[$i].is_aa = is_aa($payment.address);
});
$res = $bank_aa.$get_payment_messages($payments);
$payment_messages = $res.payment_messages;
$buffer_recipients = $res.buffer_recipients;
}`,
messages: [
`{$payment_messages[0] OTHERWISE ''}`,
`{$payment_messages[1] OTHERWISE ''}`,
`{$payment_messages[2] OTHERWISE ''}`,
`{$payment_messages[3] OTHERWISE ''}`,
{
if: `{length($buffer_recipients)}`,
app: 'data',
payload: {
recipients: `{$buffer_recipients}`
}
},
{
app: 'state',
state: `{
$remove_deposit($id, $deposit.stable_amount);
var['deposit_' || $id || '_force_close'] = false;
var['last_force_closed_protection_ratio'] = $force_close.protection_ratio;
}`
}
]
},
// challenge a force-close
{
if: `{ $asset AND trigger.data.challenge_force_close AND trigger.data.id AND trigger.data.weaker_id }`,
init: `{
$id = trigger.data.id;
$weaker_id = trigger.data.weaker_id;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("deposit not found");
// challenge is accepted even if the request has already expired but has not been committed yet
$force_close = var['deposit_' || $id || '_force_close'];
if (!$force_close)
bounce("this deposit was not force closed");
$weaker_deposit = var['deposit_' || $weaker_id];
if (!$weaker_deposit)
bounce("weaker deposit doesn't exist");
if (var['deposit_' || $weaker_id || '_force_close'])
bounce("the weaker deposit is also challenged");
if ($weaker_deposit.ts + $min_deposit_term + $challenge_immunity_period > $force_close.ts)
bounce("the weaker deposit is too new");
$weaker_protection_withdrawal_ts = $weaker_deposit.protection_withdrawal_ts OTHERWISE 0;
if ($weaker_protection_withdrawal_ts > $force_close.ts - $challenge_immunity_period)
bounce("weaker deposit's protection was decreased recently");
$weaker_protection_ratio = $weaker_deposit.protection / $weaker_deposit.amount;
if ($weaker_protection_ratio >= $force_close.protection_ratio)
bounce("the weaker deposit does not appear to be weaker: " || $weaker_protection_ratio);
$closer_amount = $deposit.stable_amount + $force_close.interest;
$reporter_reward_amount = ceil($closer_amount * $reporter_share);
$closer_refund_amount = $closer_amount - $reporter_reward_amount;
// pay the reward to the reporter and refund the rest to the closer
$payments = [
{
address: $force_close.closer,
asset: $asset,
amount: $closer_refund_amount,
is_aa: is_aa($force_close.closer)
},
{
address: trigger.address,
asset: $asset,
amount: $reporter_reward_amount,
is_aa: false // it might be an AA but the reporter is supposed to know what he's doing
},
];
$res = $bank_aa.$get_payment_messages($payments);
$payment_messages = $res.payment_messages;
$buffer_recipients = $res.buffer_recipients;
}`,
messages: [
`{$payment_messages[0] OTHERWISE ''}`,
`{$payment_messages[1] OTHERWISE ''}`,
{
if: `{length($buffer_recipients)}`,
app: 'data',
payload: {
recipients: `{$buffer_recipients}`
}
},
{
app: 'state',
state: `{
// discard the fraudulent close request
var['deposit_' || $id || '_force_close'] = false;
}`
}
]
},
// add protection
{
if: `{ $asset AND trigger.data.id AND trigger.data.add_protection AND trigger.output[[asset=$reserve_asset]] > 0 }`,
init: `{
$id = trigger.data.id;
if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to))
bounce("bad to-address");
$to = trigger.data.to OTHERWISE trigger.address;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("deposit not found");
if ($deposit.owner != $to)
bounce("you are not the owner");
}`,
messages: [
{
app: 'state',
state: `{
$deposit.protection = $deposit.protection + trigger.output[[asset=$reserve_asset]];
var['deposit_' || $id] = $deposit;
}`
}
]
},
// withdraw protection
{
if: `{ $asset AND trigger.data.id AND trigger.data.withdraw_protection AND trigger.data.amount }`,
init: `{
$id = trigger.data.id;
if (exists(trigger.data.to) AND !is_valid_address(trigger.data.to))
bounce("bad to-address");
$to = trigger.data.to OTHERWISE trigger.address;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("deposit not found");
if ($deposit.owner != trigger.address)
bounce("you are not the owner");
$amount = trigger.data.amount;
if ($amount == 'all')
$withdraw_amount = $deposit.protection;
else {
if (!is_integer($amount) OR $amount <= 0)
bounce("bad amount: " || $amount);
$withdraw_amount = $amount;
}
if ($withdraw_amount > $deposit.protection)
bounce("trying to withdraw more than you have: " || $deposit.protection);
}`,
messages: [
{
app: 'payment',
payload: {
asset: `{$reserve_asset}`,
outputs: [
{address: "{$to}", amount: "{ $withdraw_amount }"}
]
}
},
{
app: 'state',
state: `{
$deposit.protection = $deposit.protection - $withdraw_amount;
$deposit.protection_withdrawal_ts = timestamp;
var['deposit_' || $id] = $deposit;
}`
}
]
},
// change the interest recipient
{
if: `{ $asset AND trigger.data.id AND trigger.data.change_interest_recipient }`,
init: `{
// if trigger.data.interest_recipient is not specified then the owner wants to get interest himself
if (exists(trigger.data.interest_recipient) AND !is_valid_address(trigger.data.interest_recipient))
bounce("bad address of new interest recipient");
$id = trigger.data.id;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("deposit not found");
if ($deposit.owner != trigger.address)
bounce("you are not the owner");
if (var['deposit_' || $id || '_force_close'])
bounce("force-close requested, can't change interest recipient");
$old_recipient = $deposit.interest_recipient OTHERWISE trigger.address;
$new_stable_amount = floor($deposit.amount * $growth_factor);
$interest = $new_stable_amount - $deposit.stable_amount;
if ($interest < 0)
bounce("negative interest?");
}`,
messages: [
{ // pay the accrued interest to the old recipient
if: '{$interest > 0}',
app: 'payment',
payload: {
asset: `{$asset}`,
outputs: [
{address: "{$old_recipient}", amount: "{ $interest }"}
]
}
},
{
app: 'state',
state: `{
$deposit.stable_amount = $new_stable_amount;
if (trigger.data.interest_recipient)
$deposit.interest_recipient = trigger.data.interest_recipient;
else // can be false to redirect interest to self
delete($deposit, 'interest_recipient');
var['deposit_' || $id] = $deposit;
var['supply'] += $interest;
}`
}
]
},
// get interest
{
if: `{ $asset AND trigger.data.id }`,
init: `{
$id = trigger.data.id;
$deposit = var['deposit_' || $id];
if (!$deposit)
bounce("deposit not found");
$recipient = $deposit.interest_recipient OTHERWISE trigger.address;
// if interest recipient is not assigned, only the owner can trigger an interest payment
if (!$deposit.interest_recipient AND $deposit.owner != trigger.address)
bounce("you are not the owner");
if (var['deposit_' || $id || '_force_close'])
bounce("force-close requested, can't pay interest");
$new_stable_amount = floor($deposit.amount * $growth_factor);
$interest = $new_stable_amount - $deposit.stable_amount;
if ($interest < 0)
bounce("negative interest?");
// if ($interest == 0)
// bounce("0 interest accrued");
}`,
messages: [
{
app: 'payment',
payload: {
asset: `{$asset}`,
outputs: [
{address: "{$recipient}", amount: "{ $interest }"}
]
}
},
{
app: 'state',
state: `{
$deposit.stable_amount = $new_stable_amount;
var['deposit_' || $id] = $deposit;
var['supply'] += $interest;
}`
}
]
},
]
}
}