forked from mwherman2000/csharp-smart-contract-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6_NEP5pattern.cs
208 lines (178 loc) · 6.81 KB
/
lab6_NEP5pattern.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
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
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;
// NEP5 Token Proposal: https://github.com/neo-project/proposals/blob/master/nep-5.mediawiki
// Testing:
// operation, args
// ---------, -------------------------------------------------------------------------------
// "deploy"
//
// "totalSupply"
// "name"
// "symbol"
// "decimals"
//
// "balanceOf", ["ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr"] // Owner Account
// "balanceOf", ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"] // Account #2
// "balanceOf", ["AZ9Bmz6qmboZ4ry1z8p2KF3ftyA2ckJAym"] // Account #3
// "transfer", ["ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr", "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", 100]
// "transfer", ["ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr", "AZ9Bmz6qmboZ4ry1z8p2KF3ftyA2ckJAym", 100]
// "transfer", ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", "AZ9Bmz6qmboZ4ry1z8p2KF3ftyA2ckJAym", 50]
// "transfer", ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", "AZ9Bmz6qmboZ4ry1z8p2KF3ftyA2ckJAym", 500]
// "transfer", ["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", 50]
namespace lab6_NEP5pattern
{
public class NEP5Base
{
public BigInteger TotalSupply;
public string Name;
public string Symbol;
public byte Decimals;
public byte[] OwnerAccountScriptHash;
}
public class lab6_NEP5pattern : SmartContract
{
static readonly byte[] _OwnerAccountScriptHash = "ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr".AsByteArray(); // .ToScriptHash() // Demo
public static event Action<byte[], byte[], BigInteger> transfer;
public static object Main(string operation, params object[] args)
{
object result = false; // = 0 (zero)
NEP5Base TOKENBASE = new NEP5Base { TotalSupply = 10000,
Name = "My Test Token",
Symbol = "MTT",
Decimals = 8,
OwnerAccountScriptHash = _OwnerAccountScriptHash };
if (operation == "totalSupply")
{
Runtime.Notify("totalSupply");
result = TotalSupply(TOKENBASE);
}
else if (operation == "name")
{
Runtime.Notify("name");
result = Name(TOKENBASE);
}
else if (operation == "symbol")
{
Runtime.Notify("symbol");
result = Symbol(TOKENBASE);
}
else if (operation == "decimals")
{
Runtime.Notify("decimals");
result = Decimals(TOKENBASE);
}
else if (operation == "balanceOf")
{
if (args.Length < 1)
{
result = false;
}
else
{
byte[] account = (byte[])args[0];
Runtime.Notify("balanceOf");
result = BalanceOf(TOKENBASE, account);
}
}
else if (operation == "transfer")
{
if (args.Length < 3)
{
result = false;
}
else
{
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
BigInteger amount = (BigInteger)args[2];
Runtime.Notify("transfer", args[0], args[1], args[2]);
result = Transfer(TOKENBASE, from, to, amount);
}
}
else if (operation == "deploy")
{
Runtime.Notify("deploy");
result = Deploy(TOKENBASE);
}
else
{
result = false;
}
return result;
}
private static BigInteger TotalSupply(NEP5Base TOKENBASE)
{
return TOKENBASE.TotalSupply;
}
private static string Name(NEP5Base TOKENBASE)
{
return TOKENBASE.Name;
}
private static string Symbol(NEP5Base TOKENBASE)
{
return TOKENBASE.Symbol;
}
private static byte Decimals(NEP5Base TOKENBASE)
{
return TOKENBASE.Decimals;
}
private static BigInteger BalanceOf(NEP5Base TOKENBASE, byte[] account)
{
BigInteger result = 0;
StorageContext ctx = Storage.CurrentContext;
BigInteger currentBalance = Storage.Get(ctx, account).AsBigInteger();
result = currentBalance;
return result;
}
private static bool Deploy(NEP5Base TOKENBASE)
{
bool result = false;
if (true) // Runtime.CheckWitness(TOKENBASE.OwnerAccountScriptHash))
{
StorageContext ctx = Storage.CurrentContext;
// Create on-chain ledger entry for the owner of this token. Check to see if the ledger already exists
byte[] currentBalance = Storage.Get(ctx, TOKENBASE.OwnerAccountScriptHash);
if (currentBalance.Length == 0)
{
Storage.Put(ctx, TOKENBASE.OwnerAccountScriptHash, TOKENBASE.TotalSupply);
result = true;
}
}
return result;
}
private static bool Transfer(NEP5Base TOKENBASE, byte[] from, byte[] to, BigInteger amount)
{
bool result = false;
if (amount > 0)
{
if (true) // Runtime.CheckWitness(from)) // is the account invoking this contract == "from" account
{
StorageContext ctx = Storage.CurrentContext;
// Get balance from the "from" ledger
BigInteger fromBalance = Storage.Get(ctx, from).AsBigInteger();
if (fromBalance >= amount)
{
if (from == to)
{
result = true;
}
else // from != to
{
// Update "from" ledger
Storage.Put(ctx, from, fromBalance - amount);
// Update "to" ledger
BigInteger toBalance = Storage.Get(ctx, to).AsBigInteger();
Storage.Put(ctx, to, toBalance + amount);
// Log a "transfer" event
transfer(from, to, amount);
result = true;
}
}
}
}
return result;
}
}
}