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

[WIP] NEP5 Template for NEO3 #90

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions neo-devpack-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "templates", "templates", "{
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D5266066-0AFD-44D5-A83E-2F73668A63C8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Template.NEP5.CSharp", "templates\Template.NEP5.CSharp\Template.NEP5.CSharp.csproj", "{ED47C5E5-1D83-4342-9905-E5D5EC42947A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{93BEC5CC-BAFF-4389-89E7-84AAFF5D495D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93BEC5CC-BAFF-4389-89E7-84AAFF5D495D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93BEC5CC-BAFF-4389-89E7-84AAFF5D495D}.Release|Any CPU.Build.0 = Release|Any CPU
{ED47C5E5-1D83-4342-9905-E5D5EC42947A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED47C5E5-1D83-4342-9905-E5D5EC42947A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED47C5E5-1D83-4342-9905-E5D5EC42947A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED47C5E5-1D83-4342-9905-E5D5EC42947A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -69,6 +75,7 @@ Global
{42C0FF0F-0A7C-4166-A773-1F944642C209} = {79389FC0-C621-4CEA-AD2B-6074C32E7BCA}
{DE7C57CD-E4F8-4745-AB71-BABE13A0AE14} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{93BEC5CC-BAFF-4389-89E7-84AAFF5D495D} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{ED47C5E5-1D83-4342-9905-E5D5EC42947A} = {7042EF23-826B-45A1-A905-59AD678C31B7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DA935E1-C674-4364-B087-F1B511B79215}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class Blockchain
public static extern Block GetBlock(byte[] hash);

[Syscall("Neo.Blockchain.GetTransaction")]
public static extern Transaction GetTransaction(byte[] hash);
public static extern Transaction GetTransaction(byte[] hash = null);

[Syscall("Neo.Blockchain.GetContract")]
public static extern Contract GetContract(byte[] script_hash);
Expand Down
12 changes: 12 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public extern byte[] Script
get;
}

public extern byte[] Sender
{
[Syscall("Neo.Transaction.GetSender")]
get;
}

public extern uint Nonce
{
[Syscall("Neo.Transaction.GetNonce")]
get;
}

[Syscall("Neo.Transaction.GetAttributes")]
public extern TransactionAttribute[] GetAttributes();
}
Expand Down
50 changes: 50 additions & 0 deletions templates/Template.NEP5.CSharp/NEP5.Admin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;

namespace Template.NEP5.CSharp
{
public partial class NEP5 : SmartContract
{
private static bool Deploy()
{
if (!Runtime.CheckWitness(Owner()))
{
return false;
}

StorageMap contract = Storage.CurrentContext.CreateMap(GetStoragePrefixContract());
if (contract.Get("totalSupply").Length != 0)
throw new Exception("Contract already deployed");

StorageMap balances = Storage.CurrentContext.CreateMap(GetStoragePrefixBalance());
balances.Put(Owner(), InitialSupply());
contract.Put("totalSupply", InitialSupply());

OnTransfer(null, Owner(), InitialSupply());
return true;
}

public static bool Migrate(byte[] script, ContractPropertyState manifest)
{
if (!Runtime.CheckWitness(Owner()))
{
return false;
}
// TODO Fix Contract.Migrate
Contract.Migrate(script, manifest);
return true;
}

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

Contract.Destroy();
return true;
}
}
}
70 changes: 70 additions & 0 deletions templates/Template.NEP5.CSharp/NEP5.Crowdsale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using System;
using System.Numerics;

namespace Template.NEP5.CSharp
{
public partial class NEP5 : SmartContract
{
private static BigInteger GetTransactionAmount(object state)
{
var notification = (object[])state;
// Ensure the notification expected format
if (notification.Length != 4) return 0;
// Only allow Transfer notifications
if ((string)notification[0] != "Transfer") return 0;
// Check dest
if ((byte[])notification[2] != ExecutionEngine.ExecutingScriptHash) return 0;
// Amount
return (BigInteger)notification[3];
}

private static bool Mint()
{
if (Runtime.InvocationCounter != 1)
throw new Exception();

var notifications = Runtime.GetNotifications();
if (notifications.Length == 0)
throw new Exception();

BigInteger neo = 0;
BigInteger gas = 0;

for (int i = 0; i < notifications.Length; i++)
{
var notification = notifications[i];

if (notification.ScriptHash == NeoToken())
{
neo += GetTransactionAmount(notification.State);
}
else if (notification.ScriptHash == GasToken())
{
gas += GetTransactionAmount(notification.State);
}
}

StorageMap contract = Storage.CurrentContext.CreateMap(GetStoragePrefixContract());
var current_supply = contract.Get("totalSupply").AsBigInteger();
var avaliable_supply = MaxSupply() - current_supply;

var contribution = (neo * TokensPerNEO()) + (gas * TokensPerGAS());
if (contribution <= 0)
throw new Exception();
if (contribution > avaliable_supply)
throw new Exception();

StorageMap balances = Storage.CurrentContext.CreateMap(GetStoragePrefixBalance());
Transaction tx = Blockchain.GetTransaction();
var balance = balances.Get(tx.Sender).AsBigInteger();
balances.Put(tx.Sender, balance + contribution);
contract.Put("totalSupply", current_supply + contribution);

OnTransfer(null, tx.Sender, balance + contribution);
return true;
}
}
}
23 changes: 23 additions & 0 deletions templates/Template.NEP5.CSharp/NEP5.Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;

namespace Template.NEP5.CSharp
{
public partial class NEP5 : SmartContract
{
private static bool ValidateAddress(byte[] address)
{
if (address.Length != 20)
return false;
if (address.AsBigInteger() == 0)
return false;
return true;
}

private static bool IsPayable(byte[] address)
{
var c = Blockchain.GetContract(address);
return c == null || c.IsPayable;
}
}
}
54 changes: 54 additions & 0 deletions templates/Template.NEP5.CSharp/NEP5.Methods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;

namespace Template.NEP5.CSharp
{
public partial class NEP5 : SmartContract
{
private static BigInteger TotalSupply()
{
StorageMap contract = Storage.CurrentContext.CreateMap(GetStoragePrefixContract());
return contract.Get("totalSupply").AsBigInteger();
}

private static BigInteger BalanceOf(byte[] account)
{
if (!ValidateAddress(account)) throw new FormatException("The parameter 'account' SHOULD be 20-byte addresses.");

StorageMap balances = Storage.CurrentContext.CreateMap(GetStoragePrefixBalance());
return balances.Get(account).AsBigInteger();
}

private static bool Transfer(byte[] from, byte[] to, BigInteger amount)
{
if (!ValidateAddress(from)) throw new FormatException("The parameter 'from' SHOULD be 20-byte addresses.");
if (!ValidateAddress(to)) throw new FormatException("The parameters 'to' SHOULD be 20-byte addresses.");
if (!IsPayable(to)) return false;
if (amount <= 0) throw new InvalidOperationException("The parameter amount MUST be greater than 0.");
if (!Runtime.CheckWitness(from)) return false;

StorageMap balances = Storage.CurrentContext.CreateMap(GetStoragePrefixBalance());
BigInteger fromAmount = balances.Get(from).AsBigInteger();

if (fromAmount < amount) return false;
if (amount == 0 || from == to) return true;

if (fromAmount == amount)
{
balances.Delete(from);
}
else
{
balances.Put(from, fromAmount - amount);
}

BigInteger toAmount = balances.Get(to).AsBigInteger();
balances.Put(to, toAmount + amount);

OnTransfer(from, to, amount);
return true;
}
}
}
84 changes: 84 additions & 0 deletions templates/Template.NEP5.CSharp/NEP5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.ComponentModel;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Template.NEP5.CSharp
{
public partial class NEP5 : SmartContract
{
#region Token Settings
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Name() => "Token Name";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Symbol() => "TokenSymbol";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte Decimals() => 8;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MaxSupply() => 1_000_000_000;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong InitialSupply() => 20_000_000;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string[] SupportedStandards() => new string[] { "NEP-5", "NEP-10" };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte[] Owner() => "ARx15Bf5VKzmUHYPC48qcrcqiHssbqSvJK".ToScriptHash();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong TokensPerNEO() => 1_000_000_000;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong TokensPerGAS() => 1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] NeoToken() => "AHm6aEDPgu7WnmsEFDmCQTJwgZiAcXWNtu".ToScriptHash();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GasToken() => "AKCDN4viZXkCJPTg2jarHA37i1UhtBH2EC".ToScriptHash();
#endregion

#region Notifications
[DisplayName("Transfer")]
public static event Action<byte[], byte[], BigInteger> OnTransfer;
#endregion

#region Storage key prefixes
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte[] GetStoragePrefixBalance() => new byte[] { 0x01, 0x01 };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte[] GetStoragePrefixContract() => new byte[] { 0x02, 0x02 };
#endregion

public static object Main(string operation, object[] args)
{
if (Runtime.Trigger == TriggerType.Verification)
{
return Runtime.CheckWitness(Owner());
}

else if (Runtime.Trigger == TriggerType.Application)
{
#region NEP5 METHODS
if (operation == "name") return Name();
if (operation == "symbol") return Symbol();
if (operation == "decimals") return Decimals();
if (operation == "totalSupply") return TotalSupply();
if (operation == "balanceOf") return BalanceOf((byte[])args[0]);
if (operation == "transfer") return Transfer((byte[])args[0], (byte[])args[1], (BigInteger)args[2]);
#endregion

#region NEP10 METHODS
if (operation == "supportedStandards") return SupportedStandards();
#endregion

#region CROWDSALE METHODS
if (operation == "mint") return Mint();
#endregion

#region ADMIN METHODS
if (operation == "deploy") return Deploy();
if (operation == "migrate") return Migrate((byte[])args[1], (ContractPropertyState)args[2]);
if (operation == "destroy") return Destroy();
#endregion
}
return false;
}
}
}
16 changes: 16 additions & 0 deletions templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo.SmartContract.Framework" Version="2.9.3" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Message Text="Start NeoContract converter, Source File: $(TargetPath)" Importance="high">
</Message>
<Exec Command="dotnet neon.dll &quot;$(TargetPath)&quot;" />
</Target>
</Project>
25 changes: 25 additions & 0 deletions templates/Template.NEP5.CSharp/Template.NEP5.CSharp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29025.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Template.NEP5.CSharp", "Template.NEP5.CSharp.csproj", "{A97D1F8D-1181-489D-893C-F25C019D75FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A97D1F8D-1181-489D-893C-F25C019D75FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A97D1F8D-1181-489D-893C-F25C019D75FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A97D1F8D-1181-489D-893C-F25C019D75FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A97D1F8D-1181-489D-893C-F25C019D75FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {517EB11D-73F9-4594-88B0-0D7C0E9290C1}
EndGlobalSection
EndGlobal
Loading