Skip to content

Commit

Permalink
Add NEP-5 project, comply with the NEP-5 specification. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
陈志同 authored and erikzhang committed Nov 20, 2018
1 parent 8864d18 commit 5dee924
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 0 deletions.
113 changes: 113 additions & 0 deletions NEP5/NEP5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using System;
using System.ComponentModel;
using System.Numerics;

namespace NEP5
{
public class NEP5 : SmartContract
{
[DisplayName("transfer")]
public static event Action<byte[], byte[], BigInteger> Transferred;

private static readonly byte[] Owner = "Ae2d6qj91YL3LVUMkza7WQsaTYjzjHm4z1".ToScriptHash(); //Owner Address

public static object Main(string method, object[] args)
{
if (Runtime.Trigger == TriggerType.Verification)
{
return Runtime.CheckWitness(Owner);
}
else if (Runtime.Trigger == TriggerType.Application)
{
var callscript = ExecutionEngine.CallingScriptHash;

if (method == "balanceOf") return BalanceOf((byte[])args[0]);

if (method == "decimals") return Decimals();

if (method == "name") return Name();

if (method == "symbol") return Symbol();

if (method == "supportedStandards") return SupportedStandards();

if (method == "totalSupply") return TotalSupply();

if (method == "transfer") return Transfer((byte[])args[0], (byte[])args[1], (BigInteger)args[2], callscript);
}
return false;
}

[DisplayName("balanceOf")]
public static BigInteger BalanceOf(byte[] account)
{
if (account.Length != 20)
throw new InvalidOperationException("The parameter account SHOULD be 20-byte addresses.");
StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));
return asset.Get(account).AsBigInteger();
}
[DisplayName("decimals")]
public static byte Decimals() => 8;

private static bool IsPayable(byte[] to)
{
var c = Blockchain.GetContract(to);
return c == null || c.IsPayable;

This comment has been minimized.

Copy link
@flam-income

flam-income Sep 3, 2020

I don't know whom should be blamed, the author or the compiler.

In fact, c == null may fail, Use c.Equal(null) instead

This comment has been minimized.

Copy link
@erikzhang

erikzhang Sep 3, 2020

Member

c is null is better.

This comment has been minimized.

Copy link
@flam-income

flam-income Sep 3, 2020

Oh man, it is not C sharp.

Think it in NEO-VM way instead of C sharp way.

== and is result in the same when compiling.

}

[DisplayName("name")]
public static string Name() => "MyToken"; //name of the token

[DisplayName("symbol")]
public static string Symbol() => "MYT"; //symbol of the token

[DisplayName("supportedStandards")]
public static string[] SupportedStandards() => new string[] { "NEP-5", "NEP-7", "NEP-10" };

[DisplayName("totalSupply")]
public static BigInteger TotalSupply()
{
StorageMap contract = Storage.CurrentContext.CreateMap(nameof(contract));
return contract.Get("totalSupply").AsBigInteger();
}
#if DEBUG
[DisplayName("transfer")] //Only for ABI file
public static bool Transfer(byte[] from, byte[] to, BigInteger amount) => true;
#endif
//Methods of actual execution
private static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] callscript)
{
//Check parameters
if (from.Length != 20 || to.Length != 20)
throw new InvalidOperationException("The parameters from and to SHOULD be 20-byte addresses.");
if (amount <= 0)
throw new InvalidOperationException("The parameter amount MUST be greater than 0.");
if (!IsPayable(to))
return false;
if (!Runtime.CheckWitness(from) && from.AsBigInteger() != callscript.AsBigInteger())
return false;
StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));
var fromAmount = asset.Get(from).AsBigInteger();
if (fromAmount < amount)
return false;
if (from == to)
return true;

//Reduce payer balances
if (fromAmount == amount)
asset.Delete(from);
else
asset.Put(from, fromAmount - amount);

//Increase the payee balance
var toAmount = asset.Get(to).AsBigInteger();
asset.Put(to, toAmount + amount);

Transferred(from, to, amount);
return true;
}
}
}
67 changes: 67 additions & 0 deletions NEP5/NEP5.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2712642F-46ED-47B6-95D9-FB2567BFA2F5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NEP5</RootNamespace>
<AssemblyName>NEP5</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Neo.SmartContract.Framework, Version=2.7.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\Neo.SmartContract.Framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NEP5.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="build.tasks" />
</ItemGroup>
<ItemGroup>
<Content Include="Neo.ConvertTask.dll" />
<Content Include="Neo.SmartContract.Framework.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="Build.tasks" />
<Target Name="AfterBuild">
<Message Text="Start NeoContract converter, Source File: $(TargetPath)" Importance="high">
</Message>
<ConvertTask DataSource="$(TargetPath)" />
</Target>
</Project>
Binary file added NEP5/Neo.ConvertTask.dll
Binary file not shown.
Binary file added NEP5/Neo.SmartContract.Framework.dll
Binary file not shown.
36 changes: 36 additions & 0 deletions NEP5/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NEP5")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NEP5")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2712642f-46ed-47b6-95d9-fb2567bfa2f5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
4 changes: 4 additions & 0 deletions NEP5/build.tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask AssemblyFile="Neo.ConvertTask.dll" TaskName="Neo.ConvertTask"/>
</Project>
4 changes: 4 additions & 0 deletions NEP5/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Neo.SmartContract.Framework" version="2.7.3" targetFramework="net40" />
</packages>
6 changes: 6 additions & 0 deletions examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapExample", "MapExample\Ma
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventExample", "EventExample\EventExample.csproj", "{63CECEA1-2506-4FDE-BFE2-EA89F75FBF7A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEP5", "NEP5\NEP5.csproj", "{2712642F-46ED-47B6-95D9-FB2567BFA2F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{63CECEA1-2506-4FDE-BFE2-EA89F75FBF7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63CECEA1-2506-4FDE-BFE2-EA89F75FBF7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63CECEA1-2506-4FDE-BFE2-EA89F75FBF7A}.Release|Any CPU.Build.0 = Release|Any CPU
{2712642F-46ED-47B6-95D9-FB2567BFA2F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2712642F-46ED-47B6-95D9-FB2567BFA2F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2712642F-46ED-47B6-95D9-FB2567BFA2F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2712642F-46ED-47B6-95D9-FB2567BFA2F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 5dee924

Please sign in to comment.