-
Notifications
You must be signed in to change notification settings - Fork 11
/
vesting.ts
593 lines (553 loc) · 17.7 KB
/
vesting.ts
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* <h4>Description</h4>
* <p>
* This module offers capabalities for running a Vesting contract :
* 1. There are `N` vesting periods.
* 2. Each vesting period involves `P` tokens.
* 3. The Provider initially deposits `N * P` tokens into the contract.
* 4. At the end of each vesting period, `P` tokens are transferred from the
* Provider account to the Claimer account.
* 5. During each vesting period, the Provider may cancel the contract, receiving
* back the *unvested* funds from their account and distributing the *vested* funds
* to the claimer.
*
* 6. Also during each vesting period, the Claimer may withdraw once any of the funds in
* their account. The Provider can still cancel the contract during the vesting period
* after the claimer has withdrawn funds during that vesting period.
*
* 7. When the contract's ultimate timeout is reached, vested and unvested funds are
* distributed to the Claimer and Provider, respectively.
*
* 8. The Provider may cancel the contract during the first vesting period.
* 9. The Provider may not cancel the contract after all funds have been vested.
* </p>
*
* <h4>Limitations</h4>
* <p>
*
* Without Merkleization, this contract can't be deployed above 3 periods of time without reaching the Plutus constraints
* when running on chain our Marlowe/Plutus Validators.</p>
*
* @packageDocumentation
*/
import {
Contract,
Case,
datetoTimeout,
timeoutToDate,
close,
TokenValue,
Timeout,
Party,
MarloweState,
Environment,
mkEnvironment,
Input,
IChoice,
} from "@marlowe.io/language-core-v1";
import { Choice, Deposit, Next, emptyApplicables } from "@marlowe.io/language-core-v1/next";
import * as O from "fp-ts/lib/Option.js";
import { pipe } from "fp-ts/lib/function.js";
import * as G from "@marlowe.io/language-core-v1/guards";
/**
* Create The Vesting Marlowe Contract
* @param request Request For Creating a Vesting Marlowe Contract
* @category Vesting Contract DSL Generation
*/
export const mkContract = function (request: VestingRequest): Contract {
const {
scheme: { numberOfPeriods },
} = request;
if (numberOfPeriods < 1) throw "The number of periods needs to be greater or equal to 1";
return initialProviderDeposit(request, claimerDepositDistribution(request));
};
/**
* Request For Creating a Vesting Marlowe Contract
* @category Vesting Request
*/
export type VestingRequest = {
/**
* The party definition of the Token Provider (Role token or a Cardano Address)
*/
provider: Party;
/**
* The party definition of the Token Claimer (Role token or a Cardano Address)
*/
claimer: Party;
/**
* The vesting scheme definition between Token Claimer & Provider
*/
scheme: VestingScheme;
};
/**
* Frequency at which chunks of tokens will be released
* @category Vesting Request
*/
export type Frequency =
| "annually"
| "half-yearly"
| "quarterly"
| "monthly"
| "weekly"
| "daily"
| "hourly"
| "by-10-minutes";
/**
* Vesting Scheme Definition
* @category Vesting Request
*/
export type VestingScheme = {
/**
* Start of the vesting schedule
*/
start: Date;
/**
* Frequency at which chunks of tokens will be released
*/
frequency: Frequency;
/**
* Number of Periods the Provider wants, to release the totality of the tokens to the claimer.
*/
numberOfPeriods: bigint;
/**
* The token and its amount to be vested by the provider
*/
expectedInitialDeposit: TokenValue;
};
/**
* Vesting Contract State
* @category Vesting State
*/
export type VestingState =
| WaitingDepositByProvider
| NoDepositBeforeDeadline
| WithinVestingPeriod
| VestingEnded
| Closed
| UnknownState;
/**
* {@link VestingState:type | Vesting State} where The contract has been created. But no inputs has been applied yet.
* Inputs are predefined, as a user of this contract, you don't need to create these inputs yourself.
* You can provide this input directly to `applyInputs` on the `ContractLifeCycleAPI` :
* 1. `depositInput` is availaible if the connected wallet is the Provider.
* @category Vesting State
*/
export type WaitingDepositByProvider = {
name: "WaitingDepositByProvider";
scheme: VestingScheme;
initialDepositDeadline: Date;
depositInput?: Input[];
};
/**
* {@link VestingState:type | Vesting State} where the Initial Deposit deadline has passed.
* It supposed to be given by the Provider and is a prerequesite to
* continue the contract logic.
* @category Vesting State
*/
export type NoDepositBeforeDeadline = {
name: "NoDepositBeforeDeadline";
scheme: VestingScheme;
initialDepositDeadline: Date;
payMinUtxoBackInput: Input[];
};
/**
* {@link VestingState:type | Vesting State} where the contract in within a vesting period `currentPeriod`
* Inputs are predefined, as a user of this contract, you don't need to create these inputs yourself.
* You can provide this input directly to `applyInputs` on the `ContractLifeCycleAPI` :
* 1. `cancelInput` is availaible if the connected wallet is the Provider.
* 2. `withdrawInput` is availaible if the connected wallet is the Claimer.
* @category Vesting State
*/
export type WithinVestingPeriod = {
name: "WithinVestingPeriod";
scheme: VestingScheme;
currentPeriod: bigint;
periodInterval: [Date, Date];
quantities: Quantities;
cancelInput?: Input[];
withdrawInput?: Input[];
};
/**
* {@link VestingState:type | Vesting State} where the contract has passed all its vesting periods
* Inputs are predefined, as a user of this contract, you don't need to create these inputs yourself.
* You can provide this input directly to `applyInputs` on the `ContractLifeCycleAPI` :
* - `withdrawInput` is availaible if the connected wallet is the Claimer.
* @category Vesting State
*/
export type VestingEnded = {
name: "VestingEnded";
scheme: VestingScheme;
quantities: Quantities;
withdrawInput?: Input[];
};
/**
* {@link VestingState:type | Vesting State } where the contract is closed
* @category Vesting State
*/
export type Closed = {
name: "Closed";
scheme: VestingScheme;
closeCondition: CloseCondition;
};
export type CloseCondition =
| DepositDeadlineCloseCondition
| CancelledCloseCondition
| FullyClaimedCloseCondition
| UnknownCloseCondition;
/**
* {@link VestingState:type | Vesting Closed State} the contract is closed because the provider didn't provide the deposit
* before the deadline.
* @category Vesting State
*/
export type DepositDeadlineCloseCondition = {
name: "DepositDeadlineCloseCondition";
};
/**
* {@link VestingState:type | Vesting Closed State} the contract is closed because the provider has cancelled the token plan
* before the end.
* @category Vesting State
*/
export type CancelledCloseCondition = {
name: "CancelledCloseCondition";
percentageClaimed: bigint;
};
/**
* {@link VestingState:type | Vesting Closed State} the contract is closed because the claimer has fully withdrawn the tokens from the
* plan (happy path).
* @category Vesting State
*/
export type FullyClaimedCloseCondition = { name: "FullyClaimedCloseCondition" };
/**
* {@link VestingState:type | Vesting Closed State} the contract is closed but in an unexpected manner
* @category Vesting State
*/
export type UnknownCloseCondition = {
name: "UnknownCloseCondition";
inputHistory: Input[];
};
/**
* The contract is in an unexpected state
* @category Vesting State
*/
export type UnknownState = {
name: "UnknownState";
scheme: VestingScheme;
state: MarloweState;
next: Next;
};
export type Quantities = {
total: bigint;
vested: bigint;
claimed: bigint;
withdrawable: bigint;
};
/**
* Provide the State in which a vesting contract is.
* @category Vesting State
*/
export const getVestingState = async (
scheme: VestingScheme,
stateOpt: O.Option<MarloweState>,
inputHistory: Input[],
getNext: (environement: Environment) => Promise<Next>
): Promise<VestingState> => {
const state = pipe(
stateOpt,
O.match(
() => null,
(a) => a
)
);
if (state === null) {
if (inputHistory.length === 0) {
// Deadline has passed and there is one reduced applied to close the contract
return {
name: "Closed",
scheme: scheme,
closeCondition: { name: "DepositDeadlineCloseCondition" },
};
}
if (inputHistory.length === 1) {
return {
name: "Closed",
scheme: scheme,
closeCondition: { name: "FullyClaimedCloseCondition" },
};
}
const isCancelled =
1 ===
inputHistory
.filter((input) => G.IChoice.is(input))
.map((input) => input as IChoice)
.filter((choice) => choice.for_choice_id.choice_name === "cancel").length;
const amountClaimed = inputHistory
.filter((input) => G.IChoice.is(input))
.map((input) => input as IChoice)
.filter((choice) => choice.for_choice_id.choice_name === "withdraw")
.map((choice) => choice.input_that_chooses_num)
.reduce((a, b) => a + b, 0n);
if (isCancelled) {
return {
name: "Closed",
scheme: scheme,
closeCondition: {
name: "CancelledCloseCondition",
percentageClaimed: (amountClaimed * 100n) / scheme.expectedInitialDeposit.amount,
},
};
}
if (amountClaimed === scheme.expectedInitialDeposit.amount) {
return {
name: "Closed",
scheme: scheme,
closeCondition: { name: "FullyClaimedCloseCondition" },
};
}
// Closed condition Fallback
return {
name: "Closed",
scheme: scheme,
closeCondition: {
name: "UnknownCloseCondition",
inputHistory: inputHistory,
},
};
}
const startTimeout: Timeout = datetoTimeout(new Date(scheme.start));
const periodInMilliseconds: bigint = getPeriodInMilliseconds(scheme.frequency);
// Provider needs to deposit before the first vesting period
const initialDepositDeadline: Timeout = startTimeout + periodInMilliseconds;
const now = datetoTimeout(new Date());
const currentPeriod: bigint = (now - startTimeout) / periodInMilliseconds;
const periodInterval: [Date, Date] = [
timeoutToDate(startTimeout + periodInMilliseconds * currentPeriod + 1n),
timeoutToDate(startTimeout + periodInMilliseconds * (currentPeriod + 1n) - 1n),
];
const vestingAmountPerPeriod = scheme.expectedInitialDeposit.amount / BigInt(scheme.numberOfPeriods);
const vested = currentPeriod * vestingAmountPerPeriod;
const claimed = state.choices
.filter((a) => a[0].choice_name === "withdraw")
.map((a) => a[1])
.reduce((sum, current) => sum + current, 0n);
const quantities: Quantities = {
total: scheme.expectedInitialDeposit.amount,
vested: vested,
claimed: claimed,
withdrawable: vested - claimed,
};
const environment = mkEnvironment(periodInterval[0])(periodInterval[1]);
const next = await getNext(environment);
if (
// Passed the deadline, can reduce , deposit == min utxo
now > initialDepositDeadline &&
next.can_reduce &&
emptyApplicables(next) &&
state?.accounts.length === 1 &&
state?.accounts[0][1] <= 3_000_000 // min utxo
)
return {
name: "NoDepositBeforeDeadline",
scheme: scheme,
initialDepositDeadline: timeoutToDate(initialDepositDeadline),
payMinUtxoBackInput: [],
};
if (
// can reduce, periods have passed.
next.can_reduce &&
emptyApplicables(next) &&
currentPeriod >= scheme.numberOfPeriods
)
return {
name: "VestingEnded",
quantities: {
total: scheme.expectedInitialDeposit.amount,
vested: scheme.expectedInitialDeposit.amount,
claimed: claimed,
withdrawable: scheme.expectedInitialDeposit.amount - claimed,
},
scheme: scheme,
withdrawInput: [],
};
// Initial Deposit Phase
if (
// before deposit deadline or 1 period and deposit < initial deposit
state?.accounts.length == 1 &&
now < initialDepositDeadline &&
state?.accounts[0][1] <= scheme.expectedInitialDeposit.amount
) {
const depositInput =
next.applicable_inputs.deposits.length == 1 ? [Deposit.toInput(next.applicable_inputs.deposits[0])] : undefined;
return {
name: "WaitingDepositByProvider",
scheme: scheme,
initialDepositDeadline: timeoutToDate(initialDepositDeadline),
depositInput: depositInput,
};
}
if (
next.applicable_inputs.choices.length == 1 &&
next.applicable_inputs.choices[0].for_choice.choice_name == "cancel"
)
return {
name: "WithinVestingPeriod",
scheme: scheme,
currentPeriod: currentPeriod,
periodInterval: periodInterval,
quantities: quantities,
cancelInput: [Choice.toInput(next.applicable_inputs.choices[0])(1n)],
};
if (
next.applicable_inputs.choices.length == 1 &&
next.applicable_inputs.choices[0].for_choice.choice_name == "withdraw"
)
return {
name: "WithinVestingPeriod",
scheme: scheme,
currentPeriod: currentPeriod,
periodInterval: periodInterval,
quantities,
withdrawInput: [Choice.toInput(next.applicable_inputs.choices[0])(quantities.withdrawable)],
};
if (next.applicable_inputs.choices.length == 2)
return {
name: "WithinVestingPeriod",
scheme: scheme,
currentPeriod: currentPeriod,
periodInterval: periodInterval,
quantities: quantities,
cancelInput: [Choice.toInput(next.applicable_inputs.choices[1])(1n)],
withdrawInput: [Choice.toInput(next.applicable_inputs.choices[0])(quantities.withdrawable)],
};
return { name: "UnknownState", scheme: scheme, state: state, next: next };
};
const getPeriodInMilliseconds = function (frequency: Frequency): bigint {
switch (frequency) {
case "annually":
return 2n * getPeriodInMilliseconds("half-yearly");
case "half-yearly":
return 2n * getPeriodInMilliseconds("quarterly");
case "quarterly":
return 3n * getPeriodInMilliseconds("monthly");
case "monthly":
return 30n * getPeriodInMilliseconds("daily");
case "weekly":
return 7n * getPeriodInMilliseconds("daily");
case "daily":
return 24n * getPeriodInMilliseconds("hourly");
case "hourly":
return 1n * 60n * 60n * 1000n;
case "by-10-minutes":
return 10n * 60n * 1000n;
}
};
const initialProviderDeposit = function (request: VestingRequest, continuation: Contract): Contract {
const {
provider,
scheme: { start, frequency, numberOfPeriods, expectedInitialDeposit },
} = request;
if (numberOfPeriods < 1) throw "The number of periods needs to be greater or equal to 1";
const startTimeout: Timeout = datetoTimeout(start);
const periodInMilliseconds: bigint = getPeriodInMilliseconds(frequency);
// Provider needs to deposit before the first vesting period
const initialDepositDeadline: Timeout = startTimeout + periodInMilliseconds;
return {
when: [
{
case: {
party: provider,
deposits: expectedInitialDeposit.amount,
of_token: expectedInitialDeposit.token,
into_account: provider,
},
then: continuation,
},
],
timeout: initialDepositDeadline,
timeout_continuation: close,
};
};
const claimerDepositDistribution = function (request: VestingRequest): Contract {
return recursiveClaimerDepositDistribution(request, 1n);
};
/** NOTE: Currently this logic presents the withdrawal and cancel for the last period, even though it doesn't make sense
* because there is nothing to cancel, and even if the claimer does a partial withdrawal, they receive the balance in their account.
*/
export const recursiveClaimerDepositDistribution = function (request: VestingRequest, periodIndex: bigint): Contract {
const {
claimer,
provider,
scheme: { start, frequency, numberOfPeriods, expectedInitialDeposit },
} = request;
const vestingAmountPerPeriod = expectedInitialDeposit.amount / BigInt(numberOfPeriods);
const startTimeout: Timeout = datetoTimeout(start);
// Provider needs to deposit before the first vesting period
const periodInMilliseconds = getPeriodInMilliseconds(frequency);
const continuation: Contract =
periodIndex === numberOfPeriods ? close : recursiveClaimerDepositDistribution(request, periodIndex + 1n);
const vestingDate = startTimeout + periodIndex * periodInMilliseconds;
const nextVestingDate = vestingDate + periodInMilliseconds;
// On every period, we allow a claimer to do a withdrawal.
const claimerWithdrawCase: Case = {
case: {
choose_between: [
{
from: 1n,
to: periodIndex * vestingAmountPerPeriod,
},
],
for_choice: {
choice_name: "withdraw",
choice_owner: claimer,
},
},
then: {
pay: {
value_of_choice: {
choice_name: "withdraw",
choice_owner: claimer,
},
},
token: expectedInitialDeposit.token,
from_account: claimer,
to: {
party: claimer,
},
then: continuation,
},
};
const providerCancelCase: Case = {
case: {
choose_between: [
{
from: 1n,
to: 1n,
},
],
for_choice: {
choice_name: "cancel",
choice_owner: provider,
},
},
then: close,
};
// 1) Wait for the vesting period.
// 2) Release vested funds
// 3) Allow the provider to withdraw or to cancel future vesting periods
return {
when: [providerCancelCase],
timeout: vestingDate,
timeout_continuation: {
pay: vestingAmountPerPeriod,
token: expectedInitialDeposit.token,
from_account: provider,
to: {
account: claimer,
},
then: {
when: periodIndex === numberOfPeriods ? [claimerWithdrawCase] : [claimerWithdrawCase, providerCancelCase],
timeout: nextVestingDate,
timeout_continuation: continuation,
},
},
};
};