From 0ba4f249c88d1f7cc5d75dd9c8d88414e4c7f490 Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Wed, 24 Jun 2020 18:15:14 +0800 Subject: [PATCH 1/3] Add GetUnclaimedGas() in RpcClient and UT --- src/RpcClient/Models/RpcUnclaimedGas.cs | 31 +++++++++++ src/RpcClient/RpcClient.cs | 31 ++++++----- src/RpcServer/RpcServer.Wallet.cs | 20 +++---- tests/Neo.Network.RPC.Tests/RpcTestCases.json | 53 ++++++++++++------- tests/Neo.Network.RPC.Tests/UT_RpcClient.cs | 30 +++++++---- 5 files changed, 113 insertions(+), 52 deletions(-) create mode 100644 src/RpcClient/Models/RpcUnclaimedGas.cs diff --git a/src/RpcClient/Models/RpcUnclaimedGas.cs b/src/RpcClient/Models/RpcUnclaimedGas.cs new file mode 100644 index 000000000..dd9950b8c --- /dev/null +++ b/src/RpcClient/Models/RpcUnclaimedGas.cs @@ -0,0 +1,31 @@ +using Neo.IO.Json; +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Text; + +namespace Neo.Network.RPC.Models +{ + public class RpcUnclaimedGas + { + public BigInteger Unclaimed { get; set; } + + public string Address { get; set; } + + public JObject ToJson() + { + JObject json = new JObject(); + json["unclaimed"] = Unclaimed.ToString(); + json["address"] = Address; + return json; + } + + public static RpcUnclaimedGas FromJson(JObject json) + { + RpcUnclaimedGas gas = new RpcUnclaimedGas(); + gas.Unclaimed = BigInteger.Parse(json["unclaimed"].AsString()); + gas.Address = json["address"].AsString(); + return gas; + } + } +} diff --git a/src/RpcClient/RpcClient.cs b/src/RpcClient/RpcClient.cs index ea1eebeaf..35f7500f4 100644 --- a/src/RpcClient/RpcClient.cs +++ b/src/RpcClient/RpcClient.cs @@ -340,6 +340,11 @@ public RpcInvokeResult InvokeScript(byte[] script, params UInt160[] scriptHashes return RpcInvokeResult.FromJson(RpcSend("invokescript", parameters.ToArray())); } + public RpcUnclaimedGas GetUnclaimedGas(string address) + { + return RpcUnclaimedGas.FromJson(RpcSend("getunclaimedgas", address)); + } + #endregion SmartContract #region Utilities @@ -381,31 +386,31 @@ public string DumpPrivKey(string address) } /// - /// Returns the balance of the corresponding asset in the wallet, based on the specified asset Id. - /// This method applies to assets that conform to NEP-5 standards. + /// Creates a new account in the wallet opened by RPC. /// - /// new address as string - public BigDecimal GetBalance(string assetId) + public string GetNewAddress() { - byte decimals = new Nep5API(this).Decimals(UInt160.Parse(assetId)); - BigInteger balance = BigInteger.Parse(RpcSend("getbalance", assetId)["balance"].AsString()); - return new BigDecimal(balance, decimals); + return RpcSend("getnewaddress").AsString(); } /// - /// Creates a new account in the wallet opened by RPC. + /// Returns the balance of the corresponding asset in the wallet, based on the specified asset Id. + /// This method applies to assets that conform to NEP-5 standards. /// - public string GetNewAddress() + /// new address as string + public BigDecimal GetWalletBalance(string assetId) { - return RpcSend("getnewaddress").AsString(); + byte decimals = new Nep5API(this).Decimals(UInt160.Parse(assetId)); + BigInteger balance = BigInteger.Parse(RpcSend("getwalletbalance", assetId)["balance"].AsString()); + return new BigDecimal(balance, decimals); } /// /// Gets the amount of unclaimed GAS in the wallet. /// - public BigInteger GetUnclaimedGas() + public BigInteger GetWalletUnclaimedGas() { - return BigInteger.Parse(RpcSend("getunclaimedgas").AsString()); + return BigInteger.Parse(RpcSend("getwalletunclaimedgas").AsString()); } /// @@ -467,7 +472,7 @@ public JObject SendToAddress(string assetId, string address, string amount) return RpcSend("sendtoaddress", assetId, address, amount); } - #endregion Utilities + #endregion Wallet #region Plugins diff --git a/src/RpcServer/RpcServer.Wallet.cs b/src/RpcServer/RpcServer.Wallet.cs index b0b0f4f74..f58c1472e 100644 --- a/src/RpcServer/RpcServer.Wallet.cs +++ b/src/RpcServer/RpcServer.Wallet.cs @@ -47,16 +47,6 @@ private JObject DumpPrivKey(JArray _params) return account.GetKey().Export(); } - [RpcMethod] - private JObject GetWalletBalance(JArray _params) - { - CheckWallet(); - UInt160 asset_id = UInt160.Parse(_params[0].AsString()); - JObject json = new JObject(); - json["balance"] = wallet.GetAvailable(asset_id).Value.ToString(); - return json; - } - [RpcMethod] private JObject GetNewAddress(JArray _params) { @@ -67,6 +57,16 @@ private JObject GetNewAddress(JArray _params) return account.Address; } + [RpcMethod] + private JObject GetWalletBalance(JArray _params) + { + CheckWallet(); + UInt160 asset_id = UInt160.Parse(_params[0].AsString()); + JObject json = new JObject(); + json["balance"] = wallet.GetAvailable(asset_id).Value.ToString(); + return json; + } + [RpcMethod] private JObject GetWalletUnclaimedGas(JArray _params) { diff --git a/tests/Neo.Network.RPC.Tests/RpcTestCases.json b/tests/Neo.Network.RPC.Tests/RpcTestCases.json index 86bf50c7d..1c54c95c6 100644 --- a/tests/Neo.Network.RPC.Tests/RpcTestCases.json +++ b/tests/Neo.Network.RPC.Tests/RpcTestCases.json @@ -760,6 +760,23 @@ } }, + { + "Name": "getunclaimedgas", + "Request": { + "jsonrpc": "2.0", + "method": "getunclaimedgas", + "params": [ "NPvKVTGZapmFWABLsyvfreuqn73jCjJtN1" ], + "id": 1 + }, + "Response": { + "jsonrpc": "2.0", + "id": 1, + "result": { + "unclaimed": "735870007400", + "address": "NPvKVTGZapmFWABLsyvfreuqn73jCjJtN1" + } + } + }, { "Name": "listplugins", @@ -849,22 +866,6 @@ "result": "KyoYyZpoccbR6KZ25eLzhMTUxREwCpJzDsnuodGTKXSG8fDW9t7x" } }, - { - "Name": "getbalance", - "Request": { - "jsonrpc": "2.0", - "method": "getbalance", - "params": [ "0x8c23f196d8a1bfd103a9dcb1f9ccf0c611377d3b" ], - "id": 1 - }, - "Response": { - "jsonrpc": "2.0", - "id": 1, - "result": { - "balance": "3001101329992600" - } - } - }, // mock decimals { "Name": "invokescript", @@ -906,10 +907,26 @@ } }, { - "Name": "getunclaimedgas", + "Name": "getwalletbalance", "Request": { "jsonrpc": "2.0", - "method": "getunclaimedgas", + "method": "getwalletbalance", + "params": [ "0x8c23f196d8a1bfd103a9dcb1f9ccf0c611377d3b" ], + "id": 1 + }, + "Response": { + "jsonrpc": "2.0", + "id": 1, + "result": { + "balance": "3001101329992600" + } + } + }, + { + "Name": "getwalletunclaimedgas", + "Request": { + "jsonrpc": "2.0", + "method": "getwalletunclaimedgas", "params": [], "id": 1 }, diff --git a/tests/Neo.Network.RPC.Tests/UT_RpcClient.cs b/tests/Neo.Network.RPC.Tests/UT_RpcClient.cs index d4f9cca5e..302040e47 100644 --- a/tests/Neo.Network.RPC.Tests/UT_RpcClient.cs +++ b/tests/Neo.Network.RPC.Tests/UT_RpcClient.cs @@ -291,6 +291,14 @@ public void TestInvokeScript() Assert.AreEqual(test.Response.Result.ToString(), result.ToJson().ToString()); } + [TestMethod] + public void TestGetUnclaimedGas() + { + var test = TestUtils.RpcTestCases.Find(p => p.Name == nameof(rpc.GetUnclaimedGas).ToLower()); + var result = rpc.GetUnclaimedGas(test.Request.Params[0].AsString()); + Assert.AreEqual(test.Response.Result["unclaimed"].AsString(), result.Unclaimed.ToString()); + } + #endregion SmartContract #region Utilities @@ -331,14 +339,6 @@ public void TestDumpPrivKey() Assert.AreEqual(test.Response.Result.AsString(), result); } - [TestMethod] - public void TestGetBalance() - { - var test = TestUtils.RpcTestCases.Find(p => p.Name == nameof(rpc.GetBalance).ToLower()); - var result = rpc.GetBalance(test.Request.Params[0].AsString()); - Assert.AreEqual(test.Response.Result["balance"].AsString(), result.Value.ToString()); - } - [TestMethod] public void TestGetNewAddress() { @@ -348,10 +348,18 @@ public void TestGetNewAddress() } [TestMethod] - public void TestGetUnclaimedGas() + public void TestGetWalletBalance() { - var test = TestUtils.RpcTestCases.Find(p => p.Name == nameof(rpc.GetUnclaimedGas).ToLower()); - var result = rpc.GetUnclaimedGas(); + var test = TestUtils.RpcTestCases.Find(p => p.Name == nameof(rpc.GetWalletBalance).ToLower()); + var result = rpc.GetWalletBalance(test.Request.Params[0].AsString()); + Assert.AreEqual(test.Response.Result["balance"].AsString(), result.Value.ToString()); + } + + [TestMethod] + public void TestGetWalletUnclaimedGas() + { + var test = TestUtils.RpcTestCases.Find(p => p.Name == nameof(rpc.GetWalletUnclaimedGas).ToLower()); + var result = rpc.GetWalletUnclaimedGas(); Assert.AreEqual(test.Response.Result.AsString(), result.ToString()); } From 452934386b03591806c3095c1a1e1f6e8b57e7fd Mon Sep 17 00:00:00 2001 From: zhuoqian Date: Wed, 24 Jun 2020 18:17:22 +0800 Subject: [PATCH 2/3] Fix format --- src/RpcClient/Models/RpcUnclaimedGas.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/RpcClient/Models/RpcUnclaimedGas.cs b/src/RpcClient/Models/RpcUnclaimedGas.cs index dd9950b8c..0702e7f97 100644 --- a/src/RpcClient/Models/RpcUnclaimedGas.cs +++ b/src/RpcClient/Models/RpcUnclaimedGas.cs @@ -1,8 +1,5 @@ using Neo.IO.Json; -using System; -using System.Collections.Generic; using System.Numerics; -using System.Text; namespace Neo.Network.RPC.Models { From 8b35381fbeef0cabe2a9c4b88180455df6c28ac2 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Wed, 24 Jun 2020 18:49:36 +0800 Subject: [PATCH 3/3] Update tests/Neo.Network.RPC.Tests/RpcTestCases.json --- tests/Neo.Network.RPC.Tests/RpcTestCases.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Neo.Network.RPC.Tests/RpcTestCases.json b/tests/Neo.Network.RPC.Tests/RpcTestCases.json index 1c54c95c6..c6d0548b7 100644 --- a/tests/Neo.Network.RPC.Tests/RpcTestCases.json +++ b/tests/Neo.Network.RPC.Tests/RpcTestCases.json @@ -866,7 +866,6 @@ "result": "KyoYyZpoccbR6KZ25eLzhMTUxREwCpJzDsnuodGTKXSG8fDW9t7x" } }, - // mock decimals { "Name": "invokescript", "Request": {