-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNFTContract.cs
264 lines (207 loc) · 10.4 KB
/
NFTContract.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
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
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using System;
using System.ComponentModel;
using System.Numerics;
using Helper = Neo.SmartContract.Framework.Helper;
namespace NFTContract
{
/// <summary>
/// Non-Fungible Token Smart Contract Template
/// </summary>
public class NFTContract : SmartContract
{
[DisplayName("approve")]
public static event Action<byte[], byte[], byte[]> Approved;//(byte[] owner, byte[] to, byte[] TokenId);
[DisplayName("mintToken")]
public static event Action<byte[], byte[], byte[]> MintedToken;//(byte[] to, byte[] tokenId, byte[] properties);
[DisplayName("transfer")]
public static event Action<byte[], byte[], byte[]> Transferred; //(byte[] from , byte[] to, byte[] TokenId)
[DisplayName("modifyProperties")]
public static event Action<byte[], byte[]> ModifyProperties; //(byte[] tokenId, byte[] newProperties)
//super admin address
private static readonly byte[] superAdmin = Helper.ToScriptHash("AcQLYjGbQU2bEQ8RKFXUcf8XvromfUQodq");
/// <summary>
/// NFT Contract Template
/// Parameter List: 0710
/// Return List: 05
/// </summary>
public static object Main(string method, object[] args)
{
var magicstr = "NFT Contract Template v0.3";
if (Runtime.Trigger == TriggerType.Application)
{
var entryscript = ExecutionEngine.EntryScriptHash;
var callscript = ExecutionEngine.CallingScriptHash;
//invoke
if (method == "name") return "NFT-Test";
if (method == "symbol") return "NTT";
if (method == "supportedStandards") return "{\"NEP-10\"}";
if (method == "totalSupply") return Storage.Get(Context(), TotalSupplyKey()).AsBigInteger();
if (method == "getTxInfo") return GetTxInfo((byte[])args[0]);
if (method == "allowance") return Storage.Get(Context(), AllowanceKey((byte[])args[0]));
if (method == "ownerOf") return Storage.Get(Context(), TokenOwnerKey((byte[])args[0]));
if (method == "balanceOf") return Storage.Get(Context(), BalanceKey((byte[])args[0]));
if (method == "properties") return Storage.Get(Context(), PropertiesKey((byte[])args[0]));
if (method == "mintToken") //(address, properties)
{
if (args.Length != 2) return false;
var owner = (byte[])args[0];
var properties = (byte[])args[1];
if (!Runtime.CheckWitness(superAdmin)) return false;
if (owner.Length != 20) return false;
if (properties.Length > 2048) return false;
return MintNFT(owner, properties);
}
if (method == "modifyProperties")
{
if (!Runtime.CheckWitness(superAdmin)) return false;
if (args.Length != 2) return false;
var tokenId = (byte[])args[0];
var properties = (byte[])args[1];
if (properties.Length > 2048) return false;
var addr = Storage.Get(Context(), TokenOwnerKey(tokenId));
if (addr.Length != 20) return false;
Storage.Put(Context(), PropertiesKey(tokenId), properties);
ModifyProperties(tokenId, properties);
return true;
}
if (method == "transfer")
{
if (args.Length != 3) return false;
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
byte[] tokenId = (byte[])args[2];
if (from.Length != 20 || to.Length != 20) return false;
if (!Runtime.CheckWitness(from)) return false;
if (entryscript != callscript) return false;
return Transfer(from, to, tokenId);
}
if (method == "approve")
{
if (args.Length != 3) return false;
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
byte[] tokenId = (byte[])args[2];
if (from.Length != 20 || to.Length != 20) return false;
if (!Runtime.CheckWitness(from)) return false;
if (entryscript != callscript) return false;
return Approve(from, to, tokenId);
}
if (method == "transferFrom")
{
if (args.Length != 3) return false;
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
byte[] tokenId = (byte[])args[2];
if (from.Length != 20 || to.Length != 20) return false;
return TransferFrom(from, to, tokenId);
}
if (method == "transferApp")
{
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
byte[] tokenId = (byte[])args[2];
if (from.Length != 20 || to.Length != 20) return false;
if (from != callscript) return false;
return Transfer(from, to, tokenId);
}
}
return false;
}
private static bool Transfer(byte[] from, byte[] to, byte[] tokenId)
{
if (from == to) return true;
var owner = Storage.Get(Context(), TokenOwnerKey(tokenId));
if (owner != from) return false;
Storage.Put(Context(), TokenOwnerKey(tokenId), to);
var formBalance = Storage.Get(Context(), BalanceKey(from)).AsBigInteger();
Storage.Put(Context(), BalanceKey(from), formBalance - 1);
var toBalance = Storage.Get(Context(), BalanceKey(to)).AsBigInteger();
Storage.Put(Context(), BalanceKey(to), toBalance + 1);
var allowanceKey = AllowanceKey(tokenId);
Storage.Delete(Context(), allowanceKey);
SetTxInfo(from, to, tokenId);
//notify
Transferred(from, to, tokenId);
return true;
}
private static bool TransferFrom(byte[] from, byte[] to, byte[] tokenId)
{
if (from == to) return true;
var owner = Storage.Get(Context(), TokenOwnerKey(tokenId));
if (owner.AsBigInteger() != from.AsBigInteger()) return false;
var allowanceKey = AllowanceKey(tokenId);
var allowanceSpend = Storage.Get(Context(), allowanceKey);
if (allowanceSpend != from.Concat(to)) return false;
Storage.Put(Context(), TokenOwnerKey(tokenId), to);
Storage.Delete(Context(), allowanceKey);
var formBalance = Storage.Get(Context(), BalanceKey(from)).AsBigInteger();
Storage.Put(Context(), BalanceKey(from), formBalance - 1);
var toBalance = Storage.Get(Context(), BalanceKey(to)).AsBigInteger();
Storage.Put(Context(), BalanceKey(to), toBalance + 1);
SetTxInfo(from, to, tokenId);
//notify
Transferred(from, to, tokenId);
return true;
}
private static bool Approve(byte[] from, byte[] to, byte[] tokenId)
{
if (from == to) return true;
var owner = Storage.Get(Context(), TokenOwnerKey(tokenId));
if (owner.AsBigInteger() != from.AsBigInteger()) return false;
Storage.Put(Context(), AllowanceKey(tokenId), from.Concat(to));
//notify
Approved(from, to, tokenId);
return true;
}
private static bool MintNFT(byte[] owner, byte[] properties)
{
var tokenId = Hash256((ExecutionEngine.ScriptContainer as Transaction).Hash.Concat(properties));
var addr = Storage.Get(Context(), TokenOwnerKey(tokenId));
if (addr.Length > 0) return false;
Storage.Put(Context(), PropertiesKey(tokenId), properties);
Storage.Put(Context(), TokenOwnerKey(tokenId), owner);
var keyTotalSupply = TotalSupplyKey();
BigInteger totalSupply = Storage.Get(Context(), keyTotalSupply).AsBigInteger();
Storage.Put(Context(), keyTotalSupply, totalSupply + 1);
var keyBalance = BalanceKey(owner);
BigInteger balance = Storage.Get(Context(), keyBalance).AsBigInteger();
Storage.Put(Context(), keyBalance, balance + 1);
//notify
MintedToken(owner, tokenId, properties);
return true;
}
private static void SetTxInfo(byte[] from, byte[] to, byte[] tokenId)
{
TransferInfo info = new TransferInfo();
info.tokenId = tokenId;
info.from = from;
info.to = to;
byte[] txinfo = Helper.Serialize(info);
var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
Storage.Put(Context(), TxidKey(txid), txinfo);
}
private static TransferInfo GetTxInfo(byte[] txid)
{
byte[] keyTxid = TxidKey(txid);
byte[] v = Storage.Get(Context(), keyTxid);
if (v.Length == 0) return null;
return Helper.Deserialize(v) as TransferInfo;
}
private static StorageContext Context() => Storage.CurrentContext;
private static byte[] TokenOwnerKey(byte[] tokenId) => new byte[] { 0x10 }.Concat(tokenId);
private static byte[] BalanceKey(byte[] owner) => new byte[] { 0x11 }.Concat(owner);
private static byte[] TxidKey(byte[] txid) => new byte[] { 0x15 }.Concat(txid);
private static byte[] AllowanceKey(byte[] tokenId) => new byte[] { 0x16 }.Concat(tokenId);
private static byte[] PropertiesKey(byte[] tokenId) => new byte[] { 0x17 }.Concat(tokenId);
private static string TotalSupplyKey() => "totalSupply";
}
public class TransferInfo
{
public byte[] from;
public byte[] to;
public byte[] tokenId;
}
}