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 NFT templete and UT #268

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
2 changes: 2 additions & 0 deletions neo-devpack-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Template.NEP5.CSharp", "tem
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Template.NEP5.UnitTests", "tests\Template.NEP5.UnitTests\Template.NEP5.UnitTests.csproj", "{780141EE-D6E9-4591-8470-8F91B12027CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Template.NFT.CSharp", "templates\Template.NFT.CSharp\Template.NFT.CSharp.csproj", "{C3A7FF3C-3B54-49E1-A56A-84AD4BFAE0E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
11 changes: 11 additions & 0 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public static byte ToByte(this int source)
return (byte)(source + 0);
}

public static bool Equals(this byte[] left, byte[] right)
{
if (left == null || right == null) return false;
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (left.Length != right.Length) return false;
for (int i = 0; i < left.Length; i++)
{
if (left[i] != right[i]) return false;
}
return true;
}

[OpCode(OpCode.CAT)]
public extern static byte[] Concat(this byte[] first, byte[] second);

Expand Down
193 changes: 193 additions & 0 deletions templates/Template.NFT.CSharp/NFTContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
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("MintedToken")]
public static event Action<byte[], byte[], byte[]> MintedToken;

[DisplayName("Transferred")]
shargon marked this conversation as resolved.
Show resolved Hide resolved
public static event Action<byte[], byte[], BigInteger, byte[]> Transferred;

private static readonly byte[] superAdmin = Helper.ToScriptHash("Nj9Epc1x2sDmd6yH5qJPYwXRqSRf5X6KHE");
Copy link
Member

Choose a reason for hiding this comment

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

Is there possibility to change superAdmin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's better not to change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

contractOwner?


private static StorageContext Context() => Storage.CurrentContext;
private static byte[] Prefix_TotalSupply => new byte[] { 0x10 };
ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
private static byte[] Prefix_TokenOwner => new byte[] { 0x11 };
private static byte[] Prefix_TokenBalance => new byte[] { 0x12 };
private static byte[] Prefix_Properties => new byte[] { 0x13 };
private static byte[] Prefix_TokensOf => new byte[] { 0x14 };

private const int TOKEN_DECIMALS = 8;
private const int FACTOR = 100_000_000;

public static string Name()
{
return "MyNFT";
ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
}

public static string Symbol()
{
return "MNFT";
}

public static string SupportedStandards()
{
return "NEP-10, NEP-11";
ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
}

public static byte[] CreateStorageKey(byte[] prefix, byte[] key)
{
return prefix.Concat(key);
}

public static BigInteger TotalSupply()
{
return Storage.Get(Context(), Prefix_TotalSupply).ToBigInteger();
}

public static int Decimals()
{
return TOKEN_DECIMALS;
}

public static Enumerator<byte[]> OwnerOf(byte[] tokenid)
{
return Storage.Find(Context(), CreateStorageKey(Prefix_TokenOwner, tokenid)).Values;
}

public static Enumerator<byte[]> TokensOf(byte[] owner)
{
if (owner.Length != 20) throw new FormatException("The parameter 'owner' should be 20-byte address.");
return Storage.Find(Context(), CreateStorageKey(Prefix_TokensOf, owner)).Values;
}

public static string Properties(byte[] tokenid)
{
return Storage.Get(Context(), CreateStorageKey(Prefix_Properties, tokenid)).AsString();
}

ShawnYun marked this conversation as resolved.
Show resolved Hide resolved
public static bool Mint(byte[] tokenId, byte[] owner, byte[] properties)
{
if (!Runtime.CheckWitness(superAdmin)) return false;

if (owner.Length != 20) throw new FormatException("The parameter 'owner' should be 20-byte address.");
if (properties.Length > 2048) throw new FormatException("The length of 'properties' should be less than 2048.");

StorageMap tokenOwnerMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenOwner, tokenId));
if (tokenOwnerMap.Get(owner) != null) return false;

StorageMap tokenOfMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokensOf, owner));
Storage.Put(Context(), CreateStorageKey(Prefix_Properties, tokenId), properties);
tokenOwnerMap.Put(owner, owner);
tokenOfMap.Put(tokenId, tokenId);

var totalSupply = Storage.Get(Context(), Prefix_TotalSupply);
if (totalSupply is null)
Storage.Put(Context(), Prefix_TotalSupply, 1);
else
Storage.Put(Context(), Prefix_TotalSupply, totalSupply.ToBigInteger() + 1);

StorageMap tokenBalanceMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenBalance, owner));
tokenBalanceMap.Put(tokenId, FACTOR);

//notify
MintedToken(owner, tokenId, properties);
return true;
}

public static BigInteger BalanceOf(byte[] owner, byte[] tokenid)
{
if (owner.Length != 20) throw new FormatException("The parameter 'owner' should be 20-byte address.");
if (tokenid is null)
{
var iterator = Storage.Find(Context(), CreateStorageKey(Prefix_TokenBalance, owner));
BigInteger result = 0;
while (iterator.Next())
result += iterator.Value.ToBigInteger();
return result;
}
else
return Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenBalance, owner)).Get(tokenid).ToBigInteger();
}

public static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] tokenId)
{
if (from.Length != 20 || to.Length != 20) throw new FormatException("The parameters 'from' and 'to' should be 20-byte addresses.");
if (amount < 0 || amount > FACTOR) throw new FormatException("The parameters 'amount' is out of range.");
if (!Runtime.CheckWitness(from)) return false;

if (from.Equals(to))
{
Transferred(from, to, amount, tokenId);
return true;
}

StorageMap fromTokenBalanceMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenBalance, from));
StorageMap toTokenBalanceMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenBalance, to));
StorageMap tokenOwnerMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokenOwner, tokenId));
StorageMap fromTokensOfMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokensOf, from));
StorageMap toTokensOfMap = Storage.CurrentContext.CreateMap(CreateStorageKey(Prefix_TokensOf, to));

var fromTokenBalance = fromTokenBalanceMap.Get(tokenId);
if (fromTokenBalance == null || fromTokenBalance.ToBigInteger() < amount) return false;
var fromNewBalance = fromTokenBalance.ToBigInteger() - amount;
if (fromNewBalance == 0)
{
tokenOwnerMap.Delete(from);
fromTokensOfMap.Delete(tokenId);
}
fromTokenBalanceMap.Put(tokenId, fromNewBalance);

var toTokenBalance = toTokenBalanceMap.Get(tokenId);
if (toTokenBalance is null && amount > 0)
{
tokenOwnerMap.Put(to, to);
toTokenBalanceMap.Put(tokenId, amount);
toTokensOfMap.Put(tokenId, tokenId);
}
else
{
toTokenBalanceMap.Put(tokenId, toTokenBalance.ToBigInteger() + amount);
}

//notify
Transferred(from, to, amount, tokenId);
return true;
}

public static bool Migrate(byte[] script, string manifest)
{
if (!Runtime.CheckWitness(superAdmin))
{
return false;
}
if (script.Length == 0 || manifest.Length == 0)
{
return false;
}
Contract.Update(script, manifest);
return true;
}

public static bool Destroy()
{
if (!Runtime.CheckWitness(superAdmin))
{
return false;
}

Contract.Destroy();
return true;
}
}
}
11 changes: 11 additions & 0 deletions templates/Template.NFT.CSharp/Template.NFT.CSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj" />
</ItemGroup>

</Project>
Loading