From d293d6554befd1737863053c534a3fb1be4a2a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Tue, 11 Sep 2018 18:26:52 +0800 Subject: [PATCH] Merging transfer and transferAPP --- NeoContract/CGAS.cs | 41 +++-------------------------------------- Tools/Program.cs | 4 ++-- UnitTests/CGASTest.cs | 4 ++-- 3 files changed, 7 insertions(+), 42 deletions(-) diff --git a/NeoContract/CGAS.cs b/NeoContract/CGAS.cs index e0fcc9f..be7cef9 100644 --- a/NeoContract/CGAS.cs +++ b/NeoContract/CGAS.cs @@ -94,8 +94,6 @@ public static object Main(string method, object[] args) if (method == "totalSupply") return TotalSupply(); if (method == "transfer") return Transfer((byte[])args[0], (byte[])args[1], (BigInteger)args[2], callscript); - - if (method == "transferAPP") return TransferAPP((byte[])args[0], (byte[])args[1], (BigInteger)args[2], callscript); } else if (Runtime.Trigger == TriggerType.VerificationR) //Backward compatibility, refusing to accept other assets { @@ -163,7 +161,7 @@ public static bool MintTokens() { if (input.AssetId.AsBigInteger() == AssetId.AsBigInteger()) sender = sender ?? input.ScriptHash; - //SGAS address as inputs is not allowed + //CGAS address as inputs is not allowed if (input.ScriptHash.AsBigInteger() == ExecutionEngine.ExecutingScriptHash.AsBigInteger()) return false; } @@ -288,42 +286,9 @@ private static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] c throw new InvalidOperationException("The parameter amount MUST be greater than 0."); if (ExecutionEngine.EntryScriptHash.AsBigInteger() != callscript.AsBigInteger()) return false; - if (!IsPayable(to) || !Runtime.CheckWitness(from)/*0.2*/) - return false; - StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset)); - var fromAmount = asset.Get(from).AsBigInteger(); //0.1 - if (fromAmount < amount) + if (!IsPayable(to)) return false; - if (from == to) - return true; - - //Reduce payer balances - if (fromAmount == amount) - asset.Delete(from); //0.1 - else - asset.Put(from, fromAmount - amount); //1 - - //Increase the payee balance - var toAmount = asset.Get(to).AsBigInteger(); //0.1 - asset.Put(to, toAmount + amount); //1 - - SetTxInfo(from, to, amount); - Transferred(from, to, amount); - return true; - } -#if DEBUG - [DisplayName("transferAPP")] //Only for ABI file - public static bool TransferAPP(byte[] from, byte[] to, BigInteger amount) => true; -#endif - //Methods of actual execution - private static bool TransferAPP(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) || from.AsBigInteger() != callscript.AsBigInteger()) + if (!Runtime.CheckWitness(from) && from.AsBigInteger() != callscript.AsBigInteger()) /*0.2*/ return false; StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset)); var fromAmount = asset.Get(from).AsBigInteger(); //0.1 diff --git a/Tools/Program.cs b/Tools/Program.cs index 2b22ca6..2229348 100644 --- a/Tools/Program.cs +++ b/Tools/Program.cs @@ -13,9 +13,9 @@ static class Program static void Main(string[] args) { //address 2 script hash - Console.WriteLine(Neo.Wallets.Wallet.ToScriptHash("AK4LdT5ZXR9DQZjfk5X6Xy79mE8ad8jKAW")); + Console.WriteLine(Neo.Wallets.Wallet.ToScriptHash("Ae8AD6Rc3cvQapqttJcUTj9ULfLi2tLHmc")); //script hash 2 address - Console.WriteLine(Neo.Wallets.Wallet.ToAddress(new UInt160("0x9121e89e8a0849857262d67c8408601b5e8e0524".Remove(0, 2).HexToBytes().Reverse().ToArray()))); + Console.WriteLine(Neo.Wallets.Wallet.ToAddress(new UInt160("0x505663a29d83663a838eee091249abd167e928f5".Remove(0, 2).HexToBytes().Reverse().ToArray()))); //hex string 2 string Console.WriteLine("7472616e73666572".HexToString()); diff --git a/UnitTests/CGASTest.cs b/UnitTests/CGASTest.cs index 69855f8..c6b8235 100644 --- a/UnitTests/CGASTest.cs +++ b/UnitTests/CGASTest.cs @@ -15,8 +15,8 @@ namespace UnitTests { public static class CGASTest { - static readonly UInt160 SgasAddress = Wallet.ToScriptHash("AK4LdT5ZXR9DQZjfk5X6Xy79mE8ad8jKAW"); - static readonly UInt160 ScriptHash = new UInt160("0x9121e89e8a0849857262d67c8408601b5e8e0524".Remove(0, 2).HexToBytes().Reverse().ToArray()); + static readonly UInt160 SgasAddress = Wallet.ToScriptHash("Ae8AD6Rc3cvQapqttJcUTj9ULfLi2tLHmc"); + static readonly UInt160 ScriptHash = new UInt160("0x505663a29d83663a838eee091249abd167e928f5".Remove(0, 2).HexToBytes().Reverse().ToArray()); static readonly UInt160 User = Wallet.ToScriptHash("AJd31a8rYPEBkY1QSxpsGy8mdU4vTYTD4U"); static readonly byte[] UserScript = "2103ad1d70f140d84a90ad4491cdf175fa64bfa9287a006e8cbd8f8db8500b5205baac".HexToBytes();