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

sync to Neo.SmartContract.Framework v3.0.0-rc3 #44

Merged
merged 14 commits into from
Jun 29, 2021
16 changes: 9 additions & 7 deletions csharp/NEP17/AssetStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services;
using System.Numerics;

namespace Neo.SmartContract.Examples
Expand All @@ -7,11 +7,13 @@ public static class AssetStorage
{
public static readonly string mapName = "asset";

public static readonly StorageMap assetMap = new StorageMap(Storage.CurrentContext, mapName);

public static void Increase(UInt160 key, BigInteger value) => Put(key, Get(key) + value);

public static void Enable() => Storage.CurrentContext.CreateMap(mapName).Put("enable", 1);
public static void Enable() => assetMap.Put("enable", 1);

public static void Disable() => Storage.CurrentContext.CreateMap(mapName).Put("enable", 0);
public static void Disable() => assetMap.Put("enable", 0);

public static void Reduce(UInt160 key, BigInteger value)
{
Expand All @@ -22,16 +24,16 @@ public static void Reduce(UInt160 key, BigInteger value)
Put(key, oldValue - value);
}

public static void Put(UInt160 key, BigInteger value) => Storage.CurrentContext.CreateMap(mapName).Put(key, value);
public static void Put(UInt160 key, BigInteger value) => assetMap.Put(key, value);

public static BigInteger Get(UInt160 key)
{
var value = Storage.CurrentContext.CreateMap(mapName).Get(key);
var value = assetMap.Get(key);
return value is null ? 0 : (BigInteger)value;
}

public static bool GetPaymentStatus() => ((BigInteger) Storage.CurrentContext.CreateMap(mapName).Get("enable")).Equals(1);
public static bool GetPaymentStatus() => ((BigInteger)assetMap.Get("enable")).Equals(1);

public static void Remove(UInt160 key) => Storage.CurrentContext.CreateMap(mapName).Delete(key);
public static void Remove(UInt160 key) => assetMap.Delete(key);
}
}
12 changes: 6 additions & 6 deletions csharp/NEP17/NEP17.Crowdsale.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;
using System;
using System.Numerics;

Expand All @@ -9,13 +9,13 @@ partial class NEP17Demo
{
public static void OnNEP17Payment(UInt160 from, BigInteger amount, object data)
{
if (AssetStorage.GetPaymentStatus())
if (IsPayable())
{
if (ExecutionEngine.CallingScriptHash == NEO.Hash)
if (Runtime.CallingScriptHash == NEO.Hash)
{
Mint(amount * TokensPerNEO);
}
else if (ExecutionEngine.CallingScriptHash == GAS.Hash)
else if (Runtime.CallingScriptHash == GAS.Hash)
{
if (from != null) Mint(amount * TokensPerGAS);
}
Expand All @@ -39,7 +39,7 @@ private static void Mint(BigInteger amount)
if (amount <= 0) throw new Exception("Amount must be greater than zero.");
if (amount > avaliable_supply) throw new Exception("Insufficient supply for mint tokens.");

Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
Transaction tx = (Transaction)Runtime.ScriptContainer;
AssetStorage.Increase(tx.Sender, amount);
TotalSupplyStorage.Increase(amount);

Expand Down
12 changes: 9 additions & 3 deletions csharp/NEP17/NEP17.Methods.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;
using System;
using System.Numerics;

namespace Neo.SmartContract.Examples
{
partial class NEP17Demo
{
[Safe]
public static BigInteger TotalSupply() => TotalSupplyStorage.Get();

[Safe]
public static bool IsPayable() => AssetStorage.GetPaymentStatus();

[Safe]
public static BigInteger BalanceOf(UInt160 account) => AssetStorage.Get(account);

public static bool Transfer(UInt160 from, UInt160 to, BigInteger amount, object data)
{
if (amount <= 0) throw new Exception("The parameter amount MUST be greater than 0.");
if (!Runtime.CheckWitness(from) && !from.Equals(ExecutionEngine.CallingScriptHash)) throw new Exception("No authorization.");
if (!Runtime.CheckWitness(from) && !from.Equals(Runtime.CallingScriptHash)) throw new Exception("No authorization.");
if (AssetStorage.Get(from) < amount) throw new Exception("Insufficient balance.");
if (from == to) return true;

Expand Down
3 changes: 2 additions & 1 deletion csharp/NEP17/NEP17.Owner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;
using System;

namespace Neo.SmartContract.Examples
Expand Down
12 changes: 10 additions & 2 deletions csharp/NEP17/NEP17.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace Neo.SmartContract.Examples
public partial class NEP17Demo : Framework.SmartContract
{
#region Token Settings
[InitialValue("NhGobEnuWX5rVdpnuZZAZExPoRs5J6D2Sb", ContractParameterType.Hash160)]
private static readonly UInt160 Owner = default;

static readonly ulong MaxSupply = 10_000_000_000_000_000;
static readonly ulong InitialSupply = 2_000_000_000_000_000;
static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash();
static readonly ulong TokensPerNEO = 1_000_000_000;
static readonly ulong TokensPerGAS = 1;
#endregion
Expand All @@ -30,8 +32,14 @@ public partial class NEP17Demo : Framework.SmartContract
// For example, this method needs to be called when withdrawing token from the contract.
public static bool Verify() => IsOwner();

public static string Symbol() => "TokenSymbol";
[Safe]
public static string Symbol() => "NTT";

[Safe]
public static ulong Decimals() => 8;

[Safe]
public static UInt160 GetOwner() => Owner;

}
}
6 changes: 3 additions & 3 deletions csharp/NEP17/NEP17.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>Neo.SmartContract.Examples</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo.SmartContract.Framework" Version="3.0.0-preview5" />
<PackageReference Include="Neo.SmartContract.Framework" Version="3.0.0-CI00300" />
Copy link
Member

@chenzhitong chenzhitong Apr 16, 2021

Choose a reason for hiding this comment

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

Missing NuGet.Config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="MyGet-neo" value="https://www.myget.org/F/neo/api/v3/index.json" />
    <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Message Text="Start NeoContract converter, Source File: $(TargetPath)" Importance="high">
</Message>
<Exec Command="neon -f &quot;$(TargetPath)&quot; -o" />
<Exec Command="nccs &quot;$(ProjectPath)&quot;" />
</Target>

</Project>
8 changes: 8 additions & 0 deletions csharp/NEP17/NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
superboyiii marked this conversation as resolved.
Show resolved Hide resolved
<configuration>
<packageSources>
<clear />
<add key="MyGet-neo" value="https://www.myget.org/F/neo/api/v3/index.json" />
<add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
8 changes: 5 additions & 3 deletions csharp/NEP17/TotalSupplyStorage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services;
using System.Numerics;

namespace Neo.SmartContract.Examples
Expand All @@ -9,15 +9,17 @@ public static class TotalSupplyStorage

public static readonly string key = "totalSupply";

public static readonly StorageMap contractMap = new StorageMap(Storage.CurrentContext, mapName);

public static void Increase(BigInteger value) => Put(Get() + value);

public static void Reduce(BigInteger value) => Put(Get() - value);

public static void Put(BigInteger value) => Storage.CurrentContext.CreateMap(mapName).Put(key, value);
public static void Put(BigInteger value) => contractMap.Put(key, value);

public static BigInteger Get()
{
var value = Storage.CurrentContext.CreateMap(mapName).Get(key);
var value = contractMap.Get(key);
return value is null ? 0 : (BigInteger)value;
}
}
Expand Down