-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathSampleRoyaltyNEP11Token.cs
More file actions
198 lines (157 loc) · 6.97 KB
/
SampleRoyaltyNEP11Token.cs
File metadata and controls
198 lines (157 loc) · 6.97 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
// Copyright (C) 2015-2026 The Neo Project.
//
// SampleRoyaltyNEP11Token.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Attributes;
using Neo.SmartContract.Framework.Interfaces;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;
using System.ComponentModel;
using System.Numerics;
namespace NonDivisibleNEP11
{
/// <inheritdoc />
[DisplayName("SampleRoyaltyNEP11Token")]
[ContractAuthor("core-dev", "dev@neo.org")]
[ContractVersion("0.0.1")]
[ContractDescription("A sample of NEP-11 Royalty Feature")]
[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")]
[ContractPermission(Permission.Any, Method.Any)]
[SupportedStandards(NepStandard.Nep11)]
[SupportedStandards(NepStandard.Nep24)]
public class SampleRoyaltyNEP11Token : Nep11Token<Nep11TokenState>, INep24
{
#region Owner
private const byte PrefixOwner = 0xff;
private static readonly UInt160 InitialOwner = Neo.SmartContract.Framework.UInt160.Parse("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP");
[Safe]
public static UInt160 GetOwner()
{
var currentOwner = Storage.Get(new[] { PrefixOwner });
if (currentOwner == null)
return InitialOwner;
return (UInt160)currentOwner;
}
private static bool IsOwner() => Runtime.CheckWitness(GetOwner());
public delegate void OnSetOwnerDelegate(UInt160 newOwner);
[DisplayName("SetOwner")]
public static event OnSetOwnerDelegate OnSetOwner;
public static void SetOwner(UInt160? newOwner)
{
ExecutionEngine.Assert(IsOwner(), "No Authorization!");
ExecutionEngine.Assert(newOwner.IsValid && !newOwner.IsZero, "Wrong newOwner");
Storage.Put(new[] { PrefixOwner }, newOwner);
OnSetOwner(newOwner);
}
#endregion
#region Minter
private const byte PrefixMinter = 0xfd;
private const byte PrefixCounter = 0xee;
private static readonly UInt160 InitialMinter = Neo.SmartContract.Framework.UInt160.Parse("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP");
[Safe]
public static UInt160 GetMinter()
{
var currentMinter = Storage.Get(new[] { PrefixMinter });
if (currentMinter == null)
return InitialMinter;
return (UInt160)currentMinter;
}
private static bool IsMinter() => Runtime.CheckWitness(GetMinter());
public delegate void OnSetMinterDelegate(UInt160 newMinter);
[DisplayName("SetMinter")]
public static event OnSetMinterDelegate OnSetMinter;
public static void SetMinter(UInt160? newMinter)
{
ExecutionEngine.Assert(IsOwner(), "No Authorization!");
ExecutionEngine.Assert(newMinter.IsValid && !newMinter.IsZero, "Wrong newMinter");
Storage.Put(new[] { PrefixMinter }, newMinter);
OnSetMinter(newMinter);
}
public static void Mint(UInt160 to)
{
ExecutionEngine.Assert(IsOwner() || IsMinter(), "No Authorization!");
IncreaseCount();
BigInteger tokenId = CurrentCount();
Nep11TokenState nep11TokenState = new Nep11TokenState()
{
Name = "SampleRoyaltyNep11Token",
Owner = to
};
Mint((ByteString)tokenId, nep11TokenState);
}
private static void SetCount(BigInteger count)
{
Storage.Put(new[] { PrefixCounter }, count);
}
[Safe]
public static BigInteger CurrentCount()
{
return (BigInteger)Storage.Get(new[] { PrefixCounter });
}
private static void IncreaseCount()
{
SetCount(CurrentCount() + 1);
}
#endregion
#region Example.SmartContract.NEP11
public override string Symbol { [Safe] get => "SampleRoyalty"; }
#endregion
#region Royalty
private const byte PrefixRoyalty = 0xfb;
private static readonly UInt160 InitialRecipient = Neo.SmartContract.Framework.UInt160.Parse("NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP");
private static readonly BigInteger InitialFactor = 700;
public static void SetRoyaltyInfo(ByteString tokenId, Map<string, object>[] royaltyInfos)
{
ExecutionEngine.Assert(IsOwner(), "No Authorization!");
ExecutionEngine.Assert(tokenId.Length <= 64, "The argument \"tokenId\" should be 64 or less bytes long.");
for (uint i = 0; i < royaltyInfos.Length; i++)
{
ExecutionEngine.Assert(((UInt160)royaltyInfos[i]["royaltyRecipient"]).IsValid == true && (BigInteger)royaltyInfos[i]["royaltyRecipient"] >= 0 && (BigInteger)royaltyInfos[i]["royaltyRecipient"] <= 10000, "Parameter error");
}
Storage.Put(PrefixRoyalty + tokenId, StdLib.Serialize(royaltyInfos));
}
/// <summary>
/// This implements Royalty Standard: https://github.com/neo-project/proposals/pull/155/
/// This method returns a map of NeoVM Array stack item with single or multi array, each array includes royaltyRecipient and royaltyAmount
/// </summary>
/// <param name="tokenId">tokenId</param>
/// <param name="royaltyToken">royaltyToken hash for payment</param>
/// <param name="salePrice">royaltyToken amount for payment</param>
/// <returns>royaltyInfo</returns>
[Safe]
public static Map<string, object>[] RoyaltyInfo(ByteString tokenId, UInt160 royaltyToken, BigInteger salePrice)
{
ExecutionEngine.Assert(OwnerOf(tokenId) != null, "This TokenId doesn't exist!");
byte[] data = (byte[])Storage.Get(PrefixRoyalty + tokenId);
if (data == null)
{
var royaltyInfo = new Map<string, object>();
royaltyInfo["royaltyRecipient"] = InitialRecipient;
royaltyInfo["royaltyAmount"] = salePrice / InitialFactor;
return new[] { royaltyInfo };
}
else
{
return (Map<string, object>[])StdLib.Deserialize((ByteString)data);
}
}
#endregion
#region Basic
[Safe]
public static bool Verify() => IsOwner();
public static bool Update(ByteString nefFile, ByteString manifest, object data)
{
ExecutionEngine.Assert(IsOwner(), "No Authorization!");
ContractManagement.Update(nefFile, manifest, data);
return true;
}
#endregion
}
}