-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCX.cs
172 lines (143 loc) · 6.01 KB
/
DCX.cs
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
using System;
using BChain.Core;
using BChain.Core.Models;
using BChain.Core.Models.SimpleAssets;
using BChain.SmartContract.Runtime;
using BChain.SmartContract.Runtime.Attributes;
using BChain.SmartContract.Runtime.Exceptions;
namespace DigiCred.Contracts
{
[Contract(Version = 1000, Description = "DigiCred Cryptographically Secure Currency")]
public sealed class DCX : Smart
{
public DCX() { }
// we're creating a coin, so currency (NOT TOKEN!)
public override ContractType GetContractType() => ContractType.Currency;
// name of our coin
public string Name => (string)@in.assetName;
// symbol for our coin
public string Symbol => (string)@in.assetSymbol;
// active from 2018/1/1
public override DateTime ActiveFromDateTime => DateTime.MinValue;
// active till end of time really.
public override DateTime ActiveToDateTime => DateTime.MaxValue;
protected override bool When()
{
if (@in.addressFrom == ContractAddress)
{
return true;
}
return false;
}
protected override bool Do()
{
Mint(@in.Output);
return true;
}
#region Directives
[ContractDirective(Version = 2, Description = "Mints a new DCX coin to the contract.")]
public override void Mint(ContractParameters parameters)
{
var masterFingerprint = (string)@in.fingerPrint;
var userFingerprint = (string)parameters[ContractParameterTypes.__fingerPrint];
if (masterFingerprint != userFingerprint)
{
throw new DirectiveCallFailed("Fingerprints do not match. Authentication failed.");
}
var fromAddress = (Address)parameters[ContractParameterTypes.__addressFrom];
if (fromAddress != ContractAddress)
{
throw new DirectiveCallFailed("Caller address is not allowed.");
}
var supplyLimit = (long)@in.supplyLimit;
var initialSupply = (long)@in.initialSupply;
var currentSupply = (long)@in.currentSupply;
var amount = (long)@in.amount;
if (initialSupply > (supplyLimit - currentSupply))
{
throw new DirectiveCallFailed($"The supply limit of {string.Format("{0:N0}", supplyLimit)} has been reached. No further minting is possible.");
}
// On Success of transaction, update current supply
var _ = new Action<bool>((success) =>
{
if (success)
{
@in.currentSupply = currentSupply + amount;
}
});
bmx
.Asset()
.Issue()
.Params((coin) =>
{
coin.Symbol = Symbol;
coin.ContractAddress = fromAddress;
coin.To = ContractAddress;
coin.Fingerprint = masterFingerprint;
coin.FromFingerprint = masterFingerprint;
coin.ToFingerprint = masterFingerprint;
coin.Amount = initialSupply;
coin.Name = Name;
})
.From(fromAddress, masterFingerprint)
.To(ContractAddress, masterFingerprint)
.Encrypt()
.Execute()
.Go(_);
}
[ContractDirective(Version = 2, Description = "Issues a DCX coin to individual from the contract.")]
public override void Issue(ContractParameters parameters)
{
var fromAddress = (Address)parameters[ContractParameterTypes.__addressFrom];
if (fromAddress != (string)OwnerInitiationAddress)
{
throw new DirectiveCallFailed($"Not allowed to issue from here.");
}
// from directive input parameters
var toAddress = (Address)parameters[ContractParameterTypes.__addressTo];
var amount = (long)parameters[ContractParameterTypes.__amount];
// from contract input parameters
var supplyLimit = (long)@in.supplyLimit;
var currentSupply = (long)@in.currentSupply;
if (amount > (supplyLimit-currentSupply))
{
throw new DirectiveCallFailed("Amount exceeds supply.");
}
if (currentSupply < supplyLimit)
{
// On Success of transaction, update current supply
var _ = new Action<bool>((success) =>
{
if (success)
{
@out.currentSupply = (currentSupply + amount);
}
});
var fingerprint = (string)@in.fingerPrint;
var masterFingerprint = (string)parameters["FromFingerprint"];
var userFingerprint = (string)parameters["ToFingerprint"];
bmx
.Asset()
.Transfer()
.Params((coin) =>
{
coin.Symbol = Symbol;
coin.Amount = amount;
coin.ContractAddress = ContractAddress;
coin.From = fromAddress;
coin.To = toAddress;
coin.FromFingerprint = masterFingerprint;
coin.ToFingerprint = userFingerprint;
coin.Fingerprint = fingerprint;
coin.Name = Name;
})
.From(fromAddress, masterFingerprint)
.To(toAddress, userFingerprint)
.Encrypt()
.Execute()
.Go(_);
}
}
#endregion
}
}