Skip to content

Commit

Permalink
Add a generic GetUnclaimedGas() in RpcClient (#273)
Browse files Browse the repository at this point in the history
* Add GetUnclaimedGas() in RpcClient and UT

* Fix format

* Update tests/Neo.Network.RPC.Tests/RpcTestCases.json

Co-authored-by: Luchuan <[email protected]>
Co-authored-by: Shargon <[email protected]>
  • Loading branch information
3 people authored Jul 7, 2020
1 parent 04df890 commit 50e245b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 53 deletions.
28 changes: 28 additions & 0 deletions src/RpcClient/Models/RpcUnclaimedGas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Neo.IO.Json;
using System.Numerics;

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;
}
}
}
31 changes: 18 additions & 13 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -381,31 +386,31 @@ public string DumpPrivKey(string address)
}

/// <summary>
/// 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.
/// </summary>
/// <returns>new address as string</returns>
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();
}

/// <summary>
/// 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.
/// </summary>
public string GetNewAddress()
/// <returns>new address as string</returns>
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);
}

/// <summary>
/// Gets the amount of unclaimed GAS in the wallet.
/// </summary>
public BigInteger GetUnclaimedGas()
public BigInteger GetWalletUnclaimedGas()
{
return BigInteger.Parse(RpcSend("getunclaimedgas").AsString());
return BigInteger.Parse(RpcSend("getwalletunclaimedgas").AsString());
}

/// <summary>
Expand Down Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions src/RpcServer/RpcServer.Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
54 changes: 35 additions & 19 deletions tests/Neo.Network.RPC.Tests/RpcTestCases.json
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,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",
Expand Down Expand Up @@ -847,23 +864,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",
"Request": {
Expand Down Expand Up @@ -904,10 +904,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
},
Expand Down
30 changes: 19 additions & 11 deletions tests/Neo.Network.RPC.Tests/UT_RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand All @@ -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());
}

Expand Down

0 comments on commit 50e245b

Please sign in to comment.