-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NEP-5 project, comply with the NEP-5 specification. (#26)
- Loading branch information
Showing
8 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
flam-income
|
||
} | ||
|
||
[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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I don't know whom should be blamed, the author or the compiler.
In fact,
c == null
may fail, Usec.Equal(null)
instead