-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpdexVault.cs
More file actions
510 lines (408 loc) · 18.6 KB
/
OpdexVault.cs
File metadata and controls
510 lines (408 loc) · 18.6 KB
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
using Stratis.SmartContracts;
/// <summary>
/// A smart contract that locks tokens only released through successful proposals and after a vesting period.
/// </summary>
public class OpdexVault : SmartContract, IOpdexVault
{
private const ulong OneDay = 60 * 60 * 24 / 16;
private const ulong VoteDuration = OneDay * 3;
private const ulong PledgeDuration = OneDay * 7;
private const ulong ProposalDeposit = 50000000000ul; // 500 CRS
/// <summary>
/// Constructor initializing an empty vault smart contract.
/// </summary>
/// <param name="state">Smart contract state.</param>
/// <param name="token">The locked SRC token.</param>
/// <param name="vestingDuration">The length in blocks of the vesting period.</param>
/// <param name="totalPledgeMinimum">The minimum total number of tokens pledged to a proposal to move to a vote.</param>
/// <param name="totalVoteMinimum">The minimum total number of tokens voted on a proposal to have a chance to be approved.</param>
public OpdexVault(ISmartContractState state, Address token, ulong vestingDuration, ulong totalPledgeMinimum, ulong totalVoteMinimum) : base(state)
{
Token = token;
VestingDuration = vestingDuration;
TotalPledgeMinimum = totalPledgeMinimum;
TotalVoteMinimum = totalVoteMinimum;
NextProposalId = 1;
}
/// <inheritdoc />
public ulong VestingDuration
{
get => State.GetUInt64(VaultStateKeys.VestingDuration);
private set => State.SetUInt64(VaultStateKeys.VestingDuration, value);
}
/// <inheritdoc />
public Address Token
{
get => State.GetAddress(VaultStateKeys.Token);
private set => State.SetAddress(VaultStateKeys.Token, value);
}
/// <inheritdoc />
public UInt256 TotalSupply
{
get => State.GetUInt256(VaultStateKeys.TotalSupply);
private set => State.SetUInt256(VaultStateKeys.TotalSupply, value);
}
/// <inheritdoc />
public ulong NextProposalId
{
get => State.GetUInt64(VaultStateKeys.NextProposalId);
private set => State.SetUInt64(VaultStateKeys.NextProposalId, value);
}
/// <inheritdoc />
public UInt256 TotalProposedAmount
{
get => State.GetUInt256(VaultStateKeys.TotalProposedAmount);
private set => State.SetUInt256(VaultStateKeys.TotalProposedAmount, value);
}
/// <inheritdoc />
public ulong TotalPledgeMinimum
{
get => State.GetUInt64(VaultStateKeys.TotalPledgeMinimum);
private set => State.SetUInt64(VaultStateKeys.TotalPledgeMinimum, value);
}
/// <inheritdoc />
public ulong TotalVoteMinimum
{
get => State.GetUInt64(VaultStateKeys.TotalVoteMinimum);
private set => State.SetUInt64(VaultStateKeys.TotalVoteMinimum, value);
}
/// <inheritdoc />
public Certificate GetCertificate(Address wallet)
{
return State.GetStruct<Certificate>($"{VaultStateKeys.Certificate}:{wallet}");
}
private void SetCertificate(Address wallet, Certificate certificate)
{
State.SetStruct($"{VaultStateKeys.Certificate}:{wallet}", certificate);
}
/// <inheritdoc />
public ProposalDetails GetProposal(ulong proposalId)
{
return State.GetStruct<ProposalDetails>($"{VaultStateKeys.Proposal}:{proposalId}");
}
private void SetProposal(ulong proposalId, ProposalDetails proposal)
{
State.SetStruct($"{VaultStateKeys.Proposal}:{proposalId}", proposal);
}
/// <inheritdoc />
public ProposalVote GetProposalVote(ulong proposalId, Address voter)
{
return State.GetStruct<ProposalVote>($"{VaultStateKeys.ProposalVote}:{proposalId}:{voter}");
}
private void SetProposalVote(ulong proposalId, Address voter, ProposalVote vote)
{
State.SetStruct($"{VaultStateKeys.ProposalVote}:{proposalId}:{voter}", vote);
}
/// <inheritdoc />
public ulong GetProposalPledge(ulong proposalId, Address pledger)
{
return State.GetUInt64($"{VaultStateKeys.ProposalPledge}:{proposalId}:{pledger}");
}
private void SetProposalPledge(ulong proposalId, Address pledger, ulong amount)
{
State.SetUInt64($"{VaultStateKeys.ProposalPledge}:{proposalId}:{pledger}", amount);
}
/// <inheritdoc />
public ulong GetCertificateProposalIdByRecipient(Address recipient)
{
return State.GetUInt64($"{VaultStateKeys.ProposalIdByRecipient}:{recipient}");
}
private void SetCertificateProposalIdByRecipient(Address recipient, ulong proposalId)
{
State.SetUInt64($"{VaultStateKeys.ProposalIdByRecipient}:{recipient}", proposalId);
}
/// <inheritdoc />
public void NotifyDistribution(UInt256 amount)
{
Assert(Message.Sender == Token, "OPDEX: UNAUTHORIZED");
TotalSupply += amount;
}
/// <inheritdoc />
public ulong CreateNewCertificateProposal(UInt256 amount, Address recipient, string description)
{
ValidateProposal(amount, description);
var certificate = GetCertificate(recipient);
Assert(certificate.VestedBlock == 0ul, "OPDEX: CERTIFICATE_EXISTS");
var totalProposedAmount = TotalProposedAmount;
Assert(TotalSupply - totalProposedAmount >= amount, "OPDEX: INSUFFICIENT_VAULT_SUPPLY");
TotalProposedAmount = totalProposedAmount + amount;
return CreateCertificateTypeProposalExecute(recipient, amount, description, (byte)ProposalType.Create);
}
/// <inheritdoc />
public ulong CreateRevokeCertificateProposal(Address recipient, string description)
{
var certificate = GetCertificate(recipient);
var maxExpiration = Block.Number + VoteDuration + PledgeDuration;
Assert(!certificate.Revoked && certificate.VestedBlock > maxExpiration, "OPDEX: INVALID_CERTIFICATE");
ValidateProposal(certificate.Amount, description);
return CreateCertificateTypeProposalExecute(recipient, certificate.Amount, description, (byte)ProposalType.Revoke);
}
/// <inheritdoc />
public ulong CreateTotalPledgeMinimumProposal(UInt256 amount, string description)
{
ValidateProposal(amount, description);
return CreateMinimumAmountChangeProposalExecute(amount, description, (byte)ProposalType.TotalPledgeMinimum);
}
/// <inheritdoc />
public ulong CreateTotalVoteMinimumProposal(UInt256 amount, string description)
{
ValidateProposal(amount, description);
return CreateMinimumAmountChangeProposalExecute(amount, description, (byte)ProposalType.TotalVoteMinimum);
}
/// <inheritdoc />
public void Pledge(ulong proposalId)
{
Assert(Message.Value > 0, "OPDEX: INSUFFICIENT_PLEDGE_AMOUNT");
var proposal = GetProposal(proposalId);
var proposalStatus = (ProposalStatus)proposal.Status;
Assert(proposalStatus == ProposalStatus.Pledge, "OPDEX: INVALID_STATUS");
Assert(proposal.Expiration >= Block.Number, "OPDEX: PROPOSAL_EXPIRED");
proposal.PledgeAmount += Message.Value;
var pledgeMinimumMet = proposal.PledgeAmount >= TotalPledgeMinimum;
if (pledgeMinimumMet)
{
proposal.Status = (byte)ProposalStatus.Vote;
proposal.Expiration = Block.Number + VoteDuration;
}
var totalWalletPledgedAmount = GetProposalPledge(proposalId, Message.Sender) + Message.Value;
SetProposalPledge(proposalId, Message.Sender, totalWalletPledgedAmount);
SetProposal(proposalId, proposal);
Log(new VaultProposalPledgeLog
{
ProposalId = proposalId,
Pledger = Message.Sender,
PledgeAmount = Message.Value,
PledgerAmount = totalWalletPledgedAmount,
ProposalPledgeAmount = proposal.PledgeAmount,
TotalPledgeMinimumMet = pledgeMinimumMet
});
}
/// <inheritdoc />
public void Vote(ulong proposalId, bool inFavor)
{
Assert(Message.Value > 0, "OPDEX: INSUFFICIENT_VOTE_AMOUNT");
var proposal = GetProposal(proposalId);
Assert((ProposalStatus)proposal.Status == ProposalStatus.Vote, "OPDEX: INVALID_STATUS");
Assert(proposal.Expiration >= Block.Number, "OPDEX: PROPOSAL_EXPIRED");
var proposalVote = GetProposalVote(proposalId, Message.Sender);
var previouslyVoted = proposalVote.Amount > UInt256.Zero;
if (inFavor)
{
if (previouslyVoted) Assert(proposalVote.InFavor, "OPDEX: ALREADY_VOTED_NOT_IN_FAVOR");
proposal.YesAmount += Message.Value;
}
else
{
if (previouslyVoted) Assert(!proposalVote.InFavor, "OPDEX: ALREADY_VOTED_IN_FAVOR");
proposal.NoAmount += Message.Value;
}
proposalVote.Amount += Message.Value;
proposalVote.InFavor = inFavor;
SetProposalVote(proposalId, Message.Sender, proposalVote);
SetProposal(proposalId, proposal);
Log(new VaultProposalVoteLog
{
ProposalId = proposalId,
Voter = Message.Sender,
InFavor = inFavor,
VoteAmount = Message.Value,
VoterAmount = proposalVote.Amount,
ProposalYesAmount = proposal.YesAmount,
ProposalNoAmount = proposal.NoAmount
});
}
/// <inheritdoc />
public void WithdrawVote(ulong proposalId, ulong withdrawAmount)
{
var proposalVote = GetProposalVote(proposalId, Message.Sender);
var proposal = GetProposal(proposalId);
var proposalStatus = (ProposalStatus)proposal.Status;
var proposalIsActive = proposal.Expiration >= Block.Number;
var voteWithdrawn = proposalIsActive && proposalStatus == ProposalStatus.Vote;
var remainingVoteAmount = proposalVote.Amount - withdrawAmount;
EnsureWithdrawalValidity(withdrawAmount, proposalVote.Amount);
proposalVote.Amount = remainingVoteAmount;
SetProposalVote(proposalId, Message.Sender, proposalVote);
SafeTransfer(Message.Sender, withdrawAmount);
if (voteWithdrawn)
{
if (proposalVote.InFavor) proposal.YesAmount -= withdrawAmount;
else proposal.NoAmount -= withdrawAmount;
SetProposal(proposalId, proposal);
}
Log(new VaultProposalWithdrawVoteLog
{
ProposalId = proposalId,
Voter = Message.Sender,
WithdrawAmount = withdrawAmount,
VoterAmount = remainingVoteAmount,
ProposalYesAmount = proposal.YesAmount,
ProposalNoAmount = proposal.NoAmount,
VoteWithdrawn = voteWithdrawn
});
if (!proposalIsActive && proposalStatus != ProposalStatus.Complete) CompleteProposalExecute(proposalId, proposal);
}
/// <inheritdoc />
public void WithdrawPledge(ulong proposalId, ulong withdrawAmount)
{
var pledge = GetProposalPledge(proposalId, Message.Sender);
var proposal = GetProposal(proposalId);
var proposalIsActive = proposal.Expiration >= Block.Number;
var voteWithdrawn = proposalIsActive && (ProposalStatus)proposal.Status == ProposalStatus.Pledge;
var remainingPledgeAmount = pledge - withdrawAmount;
EnsureWithdrawalValidity(withdrawAmount, pledge);
SetProposalPledge(proposalId, Message.Sender, remainingPledgeAmount);
SafeTransfer(Message.Sender, withdrawAmount);
if (voteWithdrawn)
{
proposal.PledgeAmount -= withdrawAmount;
SetProposal(proposalId, proposal);
}
Log(new VaultProposalWithdrawPledgeLog
{
ProposalId = proposalId,
Pledger = Message.Sender,
WithdrawAmount = withdrawAmount,
PledgerAmount = remainingPledgeAmount,
ProposalPledgeAmount = proposal.PledgeAmount,
PledgeWithdrawn = voteWithdrawn
});
if (!proposalIsActive && (ProposalStatus)proposal.Status != ProposalStatus.Complete) CompleteProposalExecute(proposalId, proposal);
}
/// <inheritdoc />
public void CompleteProposal(ulong proposalId)
{
EnsureNotPayable();
var proposal = GetProposal(proposalId);
var proposalStatus = (ProposalStatus)proposal.Status;
Assert(proposal.Expiration > 0, "OPDEX: INVALID_PROPOSAL");
Assert(proposalStatus == ProposalStatus.Vote || proposalStatus == ProposalStatus.Pledge, "OPDEX: ALREADY_COMPLETE");
Assert(Block.Number > proposal.Expiration, "OPDEX: PROPOSAL_IN_PROGRESS");
CompleteProposalExecute(proposalId, proposal);
}
/// <inheritdoc />
public void RedeemCertificate()
{
EnsureNotPayable();
var certificate = GetCertificate(Message.Sender);
Assert(certificate.VestedBlock > 0, "OPDEX: CERTIFICATE_NOT_FOUND");
Assert(certificate.VestedBlock < Block.Number, "OPDEX: CERTIFICATE_VESTING");
SetCertificate(Message.Sender, default(Certificate));
SafeTransferTo(Token, Message.Sender, certificate.Amount);
Log(new RedeemVaultCertificateLog {Owner = Message.Sender, Amount = certificate.Amount, VestedBlock = certificate.VestedBlock});
}
private void CreateCertificate(Address to, UInt256 amount)
{
var vestedBlock = Block.Number + VestingDuration;
var certificate = new Certificate { Amount = amount, VestedBlock = vestedBlock, Revoked = false };
SetCertificate(to, certificate);
TotalSupply -= amount;
Log(new CreateVaultCertificateLog { Owner = to, Amount = amount, VestedBlock = vestedBlock });
}
private void RevokeCertificate(Address wallet)
{
var certificate = GetCertificate(wallet);
// Manual triggers cause the possibility of revocation attempts on vested certificates
if (certificate.VestedBlock < Block.Number) return;
var vestingDuration = VestingDuration;
var vestingAmount = certificate.Amount;
var vestingBlock = certificate.VestedBlock - vestingDuration;
var vestedBlocks = Block.Number - vestingBlock;
UInt256 percentageOffset = 100;
var divisor = vestingDuration * percentageOffset / vestedBlocks;
var newAmount = vestingAmount * percentageOffset / divisor;
certificate.Amount = newAmount;
certificate.Revoked = true;
SetCertificate(wallet, certificate);
TotalSupply += (vestingAmount - newAmount);
Log(new RevokeVaultCertificateLog {Owner = wallet, OldAmount = vestingAmount, NewAmount = newAmount, VestedBlock = certificate.VestedBlock});
}
private ulong CreateCertificateTypeProposalExecute(Address recipient, UInt256 amount, string description, byte type)
{
var existingProposalId = GetCertificateProposalIdByRecipient(recipient);
Assert(existingProposalId == 0ul, "OPDEX: RECIPIENT_PROPOSAL_IN_PROGRESS");
var proposalId = CreateProposalExecute(recipient, amount, description, type);
SetCertificateProposalIdByRecipient(recipient, proposalId);
return proposalId;
}
private ulong CreateMinimumAmountChangeProposalExecute(UInt256 amount, string description, byte type)
{
Assert(amount <= ulong.MaxValue, "OPDEX: EXCESSIVE_AMOUNT");
return CreateProposalExecute(Message.Sender, amount, description, type);
}
private ulong CreateProposalExecute(Address wallet, UInt256 amount, string description, byte type)
{
const byte status = (byte)ProposalStatus.Pledge;
var proposalId = NextProposalId;
var expiration = Block.Number + PledgeDuration;
NextProposalId += 1;
SetProposal(proposalId, new ProposalDetails
{
Creator = Message.Sender,
Wallet = wallet,
Amount = amount,
Type = type,
Status = status,
Expiration = expiration
});
Log(new CreateVaultProposalLog
{
ProposalId = proposalId,
Wallet = wallet,
Amount = amount,
Description = description,
Type = type,
Status = status,
Expiration = expiration
});
SafeTransfer(Message.Sender, Message.Value - ProposalDeposit);
return proposalId;
}
private void CompleteProposalExecute(ulong proposalId, ProposalDetails proposal)
{
var proposalType = (ProposalType)proposal.Type;
var totalVoteAmount = proposal.YesAmount + proposal.NoAmount;
var approved = (ProposalStatus)proposal.Status == ProposalStatus.Vote && proposal.YesAmount > proposal.NoAmount && totalVoteAmount >= TotalVoteMinimum;
if (approved)
{
if (proposalType == ProposalType.Create) CreateCertificate(proposal.Wallet, proposal.Amount);
else if (proposalType == ProposalType.Revoke) RevokeCertificate(proposal.Wallet);
else if (proposalType == ProposalType.TotalPledgeMinimum) TotalPledgeMinimum = (ulong)proposal.Amount;
else TotalVoteMinimum = (ulong)proposal.Amount;
}
proposal.Status = (byte)ProposalStatus.Complete;
SetProposal(proposalId, proposal);
if (proposalType == ProposalType.Create || proposalType == ProposalType.Revoke)
{
SetCertificateProposalIdByRecipient(proposal.Wallet, 0ul);
if (proposalType == ProposalType.Create) TotalProposedAmount -= proposal.Amount;
}
SafeTransfer(proposal.Creator, ProposalDeposit);
Log(new CompleteVaultProposalLog { ProposalId = proposalId, Approved = approved });
}
private void ValidateProposal(UInt256 amount, string description)
{
Assert(!State.IsContract(Message.Sender), "OPDEX: INVALID_CREATOR");
Assert(Message.Value >= ProposalDeposit, "OPDEX: INSUFFICIENT_DEPOSIT");
Assert(!string.IsNullOrWhiteSpace(description) && description.Length <= 200, "OPDEX: INVALID_DESCRIPTION");
Assert(amount > 0, "OPDEX: INVALID_AMOUNT");
}
private void SafeTransferTo(Address token, Address to, UInt256 amount)
{
if (amount == 0) return;
var result = Call(token, 0, nameof(IOpdexMinedToken.TransferTo), new object[] {to, amount});
Assert(result.Success && (bool)result.ReturnValue, "OPDEX: INVALID_TRANSFER_TO");
}
private void SafeTransfer(Address to, ulong amount)
{
if (amount == 0) return;
Assert(Transfer(to, amount).Success, "OPDEX: INVALID_TRANSFER");
}
private void EnsureNotPayable() => Assert(Message.Value == 0ul, "OPDEX: NOT_PAYABLE");
private void EnsureWithdrawalValidity(ulong requestedWithdrawAmount, ulong balance)
{
EnsureNotPayable();
Assert(requestedWithdrawAmount > 0ul, "OPDEX: INSUFFICIENT_WITHDRAW_AMOUNT");
Assert(requestedWithdrawAmount <= balance, "OPDEX: INSUFFICIENT_FUNDS");
}
}