Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example nep11 #1009

Merged
merged 27 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2353d01
add example nep11
vikkkko Mar 29, 2024
e630e04
Merge branch 'master' into master
vikkkko Mar 29, 2024
7d027ab
Update examples/Example.SmartContract.NEP11/Example.SmartContract.NEP…
Jim8y Mar 31, 2024
f990047
change name and change storage usage
vikkkko Apr 1, 2024
fd3ead6
multi-fix
superboyiii Apr 3, 2024
75f90a6
more fix
superboyiii Apr 3, 2024
eb45055
owner revert
superboyiii Apr 3, 2024
f91407b
Merge pull request #1 from superboyiii/royalty-example
vikkkko Apr 3, 2024
142e931
Update examples/Example.SmartContract.NonDivisibleNEP11/SampleNonDivi…
Jim8y Apr 3, 2024
4ba9fc6
Update examples/Example.SmartContract.NonDivisibleNEP11/SampleNonDivi…
Jim8y Apr 3, 2024
ff66e79
Update examples/Example.SmartContract.NonDivisibleNEP11/SampleNonDivi…
Jim8y Apr 3, 2024
f6b0b92
Update examples/Example.SmartContract.NonDivisibleNEP11/SampleNonDivi…
Jim8y Apr 3, 2024
f363da5
Update examples/Example.SmartContract.NonDivisibleNEP11/SampleNonDivi…
Jim8y Apr 3, 2024
bda7481
Merge branch 'master' into master
Jim8y Apr 4, 2024
5716d0c
fix name and comment
superboyiii Apr 11, 2024
81c2515
fix
superboyiii Apr 11, 2024
6a89ce4
fix
superboyiii Apr 11, 2024
653226e
fix update
superboyiii Apr 11, 2024
7d97d18
Merge pull request #3 from superboyiii/royalty-example
vikkkko Apr 11, 2024
e73558f
Merge branch 'master' into master
vikkkko Apr 11, 2024
3aba116
fix solution
superboyiii Apr 11, 2024
ca1f14f
Merge pull request #4 from superboyiii/royalty-example
vikkkko Apr 11, 2024
d6ef385
Update SampleRoyaltyNEP11Token.cs
Jim8y Apr 11, 2024
dbbf66c
Apply suggestions from code review
cschuchardt88 Apr 15, 2024
eac9d27
Merge branch 'master' into master
Jim8y Apr 16, 2024
ef7c040
fix Assert
superboyiii Apr 16, 2024
a330c74
Merge pull request #5 from superboyiii/royalty-example
vikkkko Apr 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Nep11Token.cs file belongs to the neo project and is free
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
// 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.Native;
using Neo.SmartContract.Framework.Services;
using System;
using System.ComponentModel;
using System.Numerics;

namespace NonDivisibleNEP11
{
/// <inheritdoc />
[DisplayName("SampleNonDivisibleNEP11Token")]
[ContractAuthor("core-dev", "dev@neo.org")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The author and email can be yours.

[ContractVersion("0.0.1")]
[ContractDescription("A sample NonDivisibleNEP11 token")]
[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")]
[ContractPermission(Permission.WildCard, Method.WildCard)]
[SupportedStandards(NepStandard.Nep11)]
public class SampleNonDivisibleNEP11Token : Nep11Token<Nep11TokenState>
{
#region Owner

private const byte PrefixOwner = 0xff;

private static readonly UInt160 InitialOwner = "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)
{
if (IsOwner() == false)
throw new InvalidOperationException("No Authorization!");
if (newOwner != null && newOwner.IsValid && !newOwner.IsZero)
{
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 = "NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP";
lock9 marked this conversation as resolved.
Show resolved Hide resolved

[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)
{
if (IsOwner() == false)
throw new InvalidOperationException("No Authorization!");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
if (newMinter != null && newMinter.IsValid)
{
Storage.Put(new[] { PrefixMinter }, newMinter);
OnSetMinter(newMinter);
}
}

public static void Mint(UInt160 to)
{
if (IsOwner() == false && IsMinter() == false)
throw new InvalidOperationException("No Authorization!");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
IncreaseCount();
BigInteger tokenId = CurrentCount();
Nep11TokenState nep11TokenState = new Nep11TokenState()
{
Name = "SampleNep11Token",
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
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 => "SampleNonDivisibleNEP11Token"; }

#endregion
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved

#region Royalty
private const byte PrefixRoyalty = 0xfb;

private static readonly UInt160 InitialRecipient = "NUuJw4C4XJFzxAvSZnFTfsNoWZytmQKXQP";
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
private static readonly BigInteger InitialFactor = 700;

public static void SetRoyaltyInfo(ByteString tokenId, Map<string, object>[] royaltyInfos)
{
if (tokenId.Length > 64) throw new Exception("The argument \"tokenId\" should be 64 or less bytes long.");
if (IsOwner() == false)
throw new InvalidOperationException("No Authorization!");
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
for (uint i = 0; i < royaltyInfos.Length; i++)
{
if (((UInt160)royaltyInfos[i]["royaltyRecipient"]).IsValid == false ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check existence?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check existence?

How? Maybe just a new address.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Royalties are not part of the NEP-11 standard. Isn't this going to confuse people?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should either add a comment to the contract code that it (additionally) implements neo-project/proposals#155 or we should create one more separate example that includes Royalty standard implementation (and remove this royalty-related code from this contract).

(BigInteger)royaltyInfos[i]["royaltyRecipient"] < 0 ||
(BigInteger)royaltyInfos[i]["royaltyRecipient"] > 10000)
throw new InvalidOperationException("Parameter error");
}
Storage.Put(PrefixRoyalty + tokenId, StdLib.Serialize(royaltyInfos));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check tokenId format

}

[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"] = 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, string manifest, object data)
{
ExecutionEngine.Assert(IsOwner() == false, "No Authorization!");
ContractManagement.Update(nefFile, manifest, data);
return true;
}
#endregion
}
}
17 changes: 12 additions & 5 deletions neo-devpack-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.Trans
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.ZKP", "examples\Example.SmartContract.ZKP\Example.SmartContract.ZKP.csproj", "{141AD5BA-1735-4583-93B6-145CF72721E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.SmartContract.Analyzer.UnitTests", "tests\Neo.SmartContract.Analyzer.UnitTests\Neo.SmartContract.Analyzer.UnitTests.csproj", "{F30E2375-012A-4A38-985B-31CB7DBA4D28}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Analyzer.UnitTests", "tests\Neo.SmartContract.Analyzer.UnitTests\Neo.SmartContract.Analyzer.UnitTests.csproj", "{F30E2375-012A-4A38-985B-31CB7DBA4D28}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SmartContract.NonDivisibleNEP11", "examples\Example.SmartContract.NonDivisibleNEP11\Example.SmartContract.NonDivisibleNEP11.csproj", "{4816CE4A-7C82-499A-93EC-FC222F850951}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -202,6 +204,10 @@ Global
{F30E2375-012A-4A38-985B-31CB7DBA4D28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F30E2375-012A-4A38-985B-31CB7DBA4D28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F30E2375-012A-4A38-985B-31CB7DBA4D28}.Release|Any CPU.Build.0 = Release|Any CPU
{4816CE4A-7C82-499A-93EC-FC222F850951}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4816CE4A-7C82-499A-93EC-FC222F850951}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4816CE4A-7C82-499A-93EC-FC222F850951}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4816CE4A-7C82-499A-93EC-FC222F850951}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -216,6 +222,10 @@ Global
{8D67DD5A-D683-481F-915E-98683EA38791} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{A372F1D6-51FF-472C-9508-FDAF7E6FEB13} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{D0153204-6AEF-4D94-B0E1-8124C38C91D4} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{73223FBD-C562-4FA0-9722-C7F1C382A9DE} = {49D5873D-7B38-48A5-B853-85146F032091}
{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E} = {49D5873D-7B38-48A5-B853-85146F032091}
{35A34EBD-F2BF-4D83-A096-D5F007B12732} = {49D5873D-7B38-48A5-B853-85146F032091}
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
{648DCE6F-A0BA-4032-951B-20CF5BBFD998} = {79389FC0-C621-4CEA-AD2B-6074C32E7BCA}
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{17F45E0B-AB1C-4796-8C99-E5212A5592F8} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
Expand All @@ -234,10 +244,7 @@ Global
{5319BE3E-A5E9-4AFF-94D6-A669DA214F24} = {C10F9957-077F-42C8-B350-8607D4899B7E}
{141AD5BA-1735-4583-93B6-145CF72721E5} = {C10F9957-077F-42C8-B350-8607D4899B7E}
{F30E2375-012A-4A38-985B-31CB7DBA4D28} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{35A34EBD-F2BF-4D83-A096-D5F007B12732} = {49D5873D-7B38-48A5-B853-85146F032091}
{73223FBD-C562-4FA0-9722-C7F1C382A9DE} = {49D5873D-7B38-48A5-B853-85146F032091}
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E} = {49D5873D-7B38-48A5-B853-85146F032091}
{4816CE4A-7C82-499A-93EC-FC222F850951} = {C10F9957-077F-42C8-B350-8607D4899B7E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DA935E1-C674-4364-B087-F1B511B79215}
Expand Down
Loading