diff --git a/csharp/NEP17/AssetStorage.cs b/csharp/NEP17/AssetStorage.cs deleted file mode 100644 index 4f0b1bb..0000000 --- a/csharp/NEP17/AssetStorage.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Neo.SmartContract.Framework.Services.Neo; -using System.Numerics; - -namespace Neo.SmartContract.Examples -{ - public static class AssetStorage - { - public static readonly string mapName = "asset"; - - 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 Disable() => Storage.CurrentContext.CreateMap(mapName).Put("enable", 0); - - public static void Reduce(UInt160 key, BigInteger value) - { - var oldValue = Get(key); - if (oldValue == value) - Remove(key); - else - Put(key, oldValue - value); - } - - public static void Put(UInt160 key, BigInteger value) => Storage.CurrentContext.CreateMap(mapName).Put(key, value); - - public static BigInteger Get(UInt160 key) - { - var value = Storage.CurrentContext.CreateMap(mapName).Get(key); - return value is null ? 0 : (BigInteger)value; - } - - public static bool GetPaymentStatus() => ((BigInteger) Storage.CurrentContext.CreateMap(mapName).Get("enable")).Equals(1); - - public static void Remove(UInt160 key) => Storage.CurrentContext.CreateMap(mapName).Delete(key); - } -} diff --git a/csharp/NEP17/NEP17.Crowdsale.cs b/csharp/NEP17/NEP17.Crowdsale.cs deleted file mode 100644 index fe390ff..0000000 --- a/csharp/NEP17/NEP17.Crowdsale.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Neo.SmartContract.Framework.Services.Neo; -using Neo.SmartContract.Framework.Services.System; -using System; -using System.Numerics; - -namespace Neo.SmartContract.Examples -{ - partial class NEP17Demo - { - public static void OnNEP17Payment(UInt160 from, BigInteger amount, object data) - { - if (AssetStorage.GetPaymentStatus()) - { - if (ExecutionEngine.CallingScriptHash == NEO.Hash) - { - Mint(amount * TokensPerNEO); - } - else if (ExecutionEngine.CallingScriptHash == GAS.Hash) - { - if (from != null) Mint(amount * TokensPerGAS); - } - else - { - throw new Exception("Wrong calling script hash"); - } - } - else - { - throw new Exception("Payment is disable on this contract!"); - } - } - - private static void Mint(BigInteger amount) - { - var totalSupply = TotalSupplyStorage.Get(); - - var avaliable_supply = MaxSupply - totalSupply; - - 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; - AssetStorage.Increase(tx.Sender, amount); - TotalSupplyStorage.Increase(amount); - - OnTransfer(null, tx.Sender, amount); - } - } -} diff --git a/csharp/NEP17/NEP17.Methods.cs b/csharp/NEP17/NEP17.Methods.cs deleted file mode 100644 index ddf5fc7..0000000 --- a/csharp/NEP17/NEP17.Methods.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Neo.SmartContract.Framework.Services.Neo; -using Neo.SmartContract.Framework.Services.System; -using System; -using System.Numerics; - -namespace Neo.SmartContract.Examples -{ - partial class NEP17Demo - { - public static BigInteger TotalSupply() => TotalSupplyStorage.Get(); - - 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 (AssetStorage.Get(from) < amount) throw new Exception("Insufficient balance."); - if (from == to) return true; - - AssetStorage.Reduce(from, amount); - AssetStorage.Increase(to, amount); - - OnTransfer(from, to, amount); - - // Validate payable - if (ContractManagement.GetContract(to) != null) - Contract.Call(to, "onNEP17Payment", CallFlags.All, new object[] { from, amount, data }); - return true; - } - } -} diff --git a/csharp/NEP17/NEP17.Owner.cs b/csharp/NEP17/NEP17.Owner.cs deleted file mode 100644 index 3cf2a01..0000000 --- a/csharp/NEP17/NEP17.Owner.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Neo.SmartContract.Framework; -using Neo.SmartContract.Framework.Services.Neo; -using System; - -namespace Neo.SmartContract.Examples -{ - partial class NEP17Demo - { - public static void _deploy(object data, bool update) - { - if (update) return; - if (TotalSupplyStorage.Get() > 0) throw new Exception("Contract has been deployed."); - - TotalSupplyStorage.Increase(InitialSupply); - AssetStorage.Increase(Owner, InitialSupply); - - OnTransfer(null, Owner, InitialSupply); - } - - public static void Update(ByteString nefFile, string manifest, object data) - { - if (!IsOwner()) throw new Exception("No authorization."); - ContractManagement.Update(nefFile, manifest, data); - } - - public static void Destroy() - { - if (!IsOwner()) throw new Exception("No authorization."); - ContractManagement.Destroy(); - } - - public static void EnablePayment() - { - if (!IsOwner()) throw new Exception("No authorization."); - AssetStorage.Enable(); - } - - public static void DisablePayment() - { - if (!IsOwner()) throw new Exception("No authorization."); - AssetStorage.Disable(); - } - - private static bool IsOwner() => Runtime.CheckWitness(Owner); - } -} diff --git a/csharp/NEP17/NEP17.cs b/csharp/NEP17/NEP17.cs index d5e02c9..9a99286 100644 --- a/csharp/NEP17/NEP17.cs +++ b/csharp/NEP17/NEP17.cs @@ -1,6 +1,7 @@ using Neo.SmartContract.Framework; +using Neo.SmartContract.Framework.Native; +using Neo.SmartContract.Framework.Services; using System; -using System.ComponentModel; using System.Numerics; namespace Neo.SmartContract.Examples @@ -10,28 +11,53 @@ namespace Neo.SmartContract.Examples [ManifestExtra("Description", "This is a NEP17 example")] [SupportedStandards("NEP-17")] [ContractPermission("*", "onNEP17Payment")] - public partial class NEP17Demo : Framework.SmartContract + public partial class NEP17Demo : Nep17Token { - #region Token Settings - 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 + [InitialValue("NhGobEnuWX5rVdpnuZZAZExPoRs5J6D2Sb", ContractParameterType.Hash160)] + private static readonly UInt160 owner = default; + // Prefix_TotalSupply = 0x00; Prefix_Balance = 0x01; + private const byte Prefix_Contract = 0x02; + public static readonly StorageMap ContractMap = new StorageMap(Storage.CurrentContext, Prefix_Contract); + private static readonly byte[] ownerKey = "owner".ToByteArray(); + private static bool IsOwner() => Runtime.CheckWitness(GetOwner()); + public override byte Decimals() => 8; + public override string Symbol() => "NEP17"; - #region Notifications - [DisplayName("Transfer")] - public static event Action OnTransfer; - #endregion + public static void _deploy(object data, bool update) + { + if (update) return; + ContractMap.Put(ownerKey, owner); + } - // When this contract address is included in the transaction signature, - // this method will be triggered as a VerificationTrigger to verify that the signature is correct. - // For example, this method needs to be called when withdrawing token from the contract. - public static bool Verify() => IsOwner(); + public static UInt160 GetOwner() + { + return (UInt160)ContractMap.Get(ownerKey); + } - public static string Symbol() => "TokenSymbol"; + public static new void Mint(UInt160 account, BigInteger amount) + { + if (!IsOwner()) throw new InvalidOperationException("No Authorization!"); + Nep17Token.Mint(account, amount); + } - public static ulong Decimals() => 8; + public static new void Burn(UInt160 account, BigInteger amount) + { + if (!IsOwner()) throw new InvalidOperationException("No Authorization!"); + Nep17Token.Burn(account, amount); + } + + public static bool Update(ByteString nefFile, string manifest) + { + if (!IsOwner()) throw new InvalidOperationException("No Authorization!"); + ContractManagement.Update(nefFile, manifest, null); + return true; + } + + public static bool Destroy() + { + if (!IsOwner()) throw new InvalidOperationException("No Authorization!"); + ContractManagement.Destroy(); + return true; + } } } diff --git a/csharp/NEP17/NEP17.csproj b/csharp/NEP17/NEP17.csproj index f2703bf..b04b86d 100644 --- a/csharp/NEP17/NEP17.csproj +++ b/csharp/NEP17/NEP17.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -6,13 +6,13 @@ - + - + diff --git a/csharp/NEP17/TotalSupplyStorage.cs b/csharp/NEP17/TotalSupplyStorage.cs deleted file mode 100644 index f31264f..0000000 --- a/csharp/NEP17/TotalSupplyStorage.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Neo.SmartContract.Framework.Services.Neo; -using System.Numerics; - -namespace Neo.SmartContract.Examples -{ - public static class TotalSupplyStorage - { - public static readonly string mapName = "contract"; - - public static readonly string key = "totalSupply"; - - 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 BigInteger Get() - { - var value = Storage.CurrentContext.CreateMap(mapName).Get(key); - return value is null ? 0 : (BigInteger)value; - } - } -}