Skip to content

Commit

Permalink
Allow querying contracts by name (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Sep 15, 2020
1 parent 19086b2 commit c4de92b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/RpcClient/Models/RpcApplicationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static RpcNotifyEventArgs FromJson(JObject json)
{
return new RpcNotifyEventArgs
{
Contract = UInt160.Parse(json["contract"].AsString()),
Contract = json["contract"].ToScriptHash(),
EventName = json["eventname"].AsString(),
State = Utility.StackItemFromJson(json["state"])
};
Expand Down
4 changes: 2 additions & 2 deletions src/RpcClient/Models/RpcNep5Balances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static RpcNep5Balances FromJson(JObject json)
RpcNep5Balances nep5Balance = new RpcNep5Balances
{
Balances = ((JArray)json["balance"]).Select(p => RpcNep5Balance.FromJson(p)).ToList(),
UserScriptHash = json["address"].AsString().ToScriptHash()
UserScriptHash = json["address"].ToScriptHash()
};
return nep5Balance;
}
Expand All @@ -52,7 +52,7 @@ public static RpcNep5Balance FromJson(JObject json)
{
RpcNep5Balance balance = new RpcNep5Balance
{
AssetHash = UInt160.Parse(json["assethash"].AsString()),
AssetHash = json["assethash"].ToScriptHash(),
Amount = BigInteger.Parse(json["amount"].AsString()),
LastUpdatedBlock = (uint)json["lastupdatedblock"].AsNumber()
};
Expand Down
6 changes: 3 additions & 3 deletions src/RpcClient/Models/RpcNep5Transfers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static RpcNep5Transfers FromJson(JObject json)
{
Sent = ((JArray)json["sent"]).Select(p => RpcNep5Transfer.FromJson(p)).ToList(),
Received = ((JArray)json["received"]).Select(p => RpcNep5Transfer.FromJson(p)).ToList(),
UserScriptHash = json["address"].AsString().ToScriptHash()
UserScriptHash = json["address"].ToScriptHash()
};
return transfers;
}
Expand Down Expand Up @@ -69,8 +69,8 @@ public static RpcNep5Transfer FromJson(JObject json)
{
RpcNep5Transfer transfer = new RpcNep5Transfer();
transfer.TimestampMS = (ulong)json["timestamp"].AsNumber();
transfer.AssetHash = UInt160.Parse(json["assethash"].AsString());
transfer.UserScriptHash = json["transferaddress"].AsString().ToScriptHash();
transfer.AssetHash = json["assethash"].ToScriptHash();
transfer.UserScriptHash = json["transferaddress"].ToScriptHash();
transfer.Amount = BigInteger.Parse(json["amount"].AsString());
transfer.BlockIndex = (uint)json["blockindex"].AsNumber();
transfer.TransferNotifyIndex = (ushort)json["transfernotifyindex"].AsNumber();
Expand Down
4 changes: 2 additions & 2 deletions src/RpcClient/Models/RpcTransferOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static RpcTransferOut FromJson(JObject json)
{
return new RpcTransferOut
{
Asset = UInt160.Parse(json["asset"].AsString()),
Asset = json["asset"].ToScriptHash(),
Value = json["value"].AsString(),
ScriptHash = json["address"].AsString().ToScriptHash(),
ScriptHash = json["address"].ToScriptHash(),
};
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public UInt256 SubmitBlock(byte[] block)
/// </summary>
public RpcInvokeResult InvokeFunction(string scriptHash, string operation, RpcStack[] stacks, params Signer[] signer)
{
List<JObject> parameters = new List<JObject> { scriptHash, operation, stacks.Select(p => p.ToJson()).ToArray() };
List<JObject> parameters = new List<JObject> { scriptHash.AsScriptHash(), operation, stacks.Select(p => p.ToJson()).ToArray() };
if (signer.Length > 0)
{
parameters.Add(signer.Select(p => (JObject)p.ToJson()).ToArray());
Expand All @@ -350,7 +350,7 @@ public RpcInvokeResult InvokeScript(byte[] script, params Signer[] signers)

public RpcUnclaimedGas GetUnclaimedGas(string address)
{
return RpcUnclaimedGas.FromJson(RpcSend("getunclaimedgas", address));
return RpcUnclaimedGas.FromJson(RpcSend("getunclaimedgas", address.AsScriptHash()));
}

#endregion SmartContract
Expand Down Expand Up @@ -408,7 +408,7 @@ public string GetNewAddress()
/// <returns>new address as string</returns>
public BigDecimal GetWalletBalance(string assetId)
{
byte decimals = new Nep5API(this).Decimals(UInt160.Parse(assetId));
byte decimals = new Nep5API(this).Decimals(UInt160.Parse(assetId.AsScriptHash()));
BigInteger balance = BigInteger.Parse(RpcSend("getwalletbalance", assetId)["balance"].AsString());
return new BigDecimal(balance, decimals);
}
Expand Down Expand Up @@ -452,7 +452,7 @@ public bool OpenWallet(string path, string password)
/// <returns>This function returns Signed Transaction JSON if successful, ContractParametersContext JSON if signing failed.</returns>
public JObject SendFrom(string assetId, string fromAddress, string toAddress, string amount)
{
return RpcSend("sendfrom", assetId, fromAddress, toAddress, amount);
return RpcSend("sendfrom", assetId.AsScriptHash(), fromAddress.AsScriptHash(), toAddress.AsScriptHash(), amount);
}

/// <summary>
Expand All @@ -464,7 +464,7 @@ public JObject SendMany(string fromAddress, IEnumerable<RpcTransferOut> outputs)
var parameters = new List<JObject>();
if (!string.IsNullOrEmpty(fromAddress))
{
parameters.Add(fromAddress);
parameters.Add(fromAddress.AsScriptHash());
}
parameters.Add(outputs.Select(p => p.ToJson()).ToArray());

Expand All @@ -477,7 +477,7 @@ public JObject SendMany(string fromAddress, IEnumerable<RpcTransferOut> outputs)
/// <returns>This function returns Signed Transaction JSON if successful, ContractParametersContext JSON if signing failed.</returns>
public JObject SendToAddress(string assetId, string address, string amount)
{
return RpcSend("sendtoaddress", assetId, address, amount);
return RpcSend("sendtoaddress", assetId.AsScriptHash(), address.AsScriptHash(), amount);
}

#endregion Wallet
Expand All @@ -504,7 +504,7 @@ public RpcNep5Transfers GetNep5Transfers(string address, ulong? startTimestamp =
{
startTimestamp ??= 0;
endTimestamp ??= DateTime.UtcNow.ToTimestampMS();
return RpcNep5Transfers.FromJson(RpcSend("getnep5transfers", address, startTimestamp, endTimestamp));
return RpcNep5Transfers.FromJson(RpcSend("getnep5transfers", address.AsScriptHash(), startTimestamp, endTimestamp));
}

/// <summary>
Expand All @@ -513,7 +513,7 @@ public RpcNep5Transfers GetNep5Transfers(string address, ulong? startTimestamp =
/// </summary>
public RpcNep5Balances GetNep5Balances(string address)
{
return RpcNep5Balances.FromJson(RpcSend("getnep5balances", address));
return RpcNep5Balances.FromJson(RpcSend("getnep5balances", address.AsScriptHash()));
}

#endregion Plugins
Expand Down
28 changes: 25 additions & 3 deletions src/RpcClient/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM.Types;
using Neo.Wallets;
using System;
Expand All @@ -27,6 +28,27 @@ private static (BigInteger numerator, BigInteger denominator) Fraction(decimal d
return (numerator, denominator);
}

public static UInt160 ToScriptHash(this JObject value)
{
var addressOrScriptHash = value.AsString();

return addressOrScriptHash.Length < 40 ?
addressOrScriptHash.ToScriptHash() : UInt160.Parse(addressOrScriptHash);
}

public static string AsScriptHash(this string addressOrScriptHash)
{
foreach (var native in NativeContract.Contracts)
{
if (addressOrScriptHash.Equals(native.Name, StringComparison.InvariantCultureIgnoreCase) ||
addressOrScriptHash == native.Id.ToString())
return native.Hash.ToString();
}

return addressOrScriptHash.Length < 40 ?
addressOrScriptHash : UInt160.Parse(addressOrScriptHash).ToString();
}

/// <summary>
/// Parse WIF or private key hex string to KeyPair
/// </summary>
Expand Down Expand Up @@ -114,7 +136,7 @@ public static void FromJson(this BlockBase block, JObject json)
block.MerkleRoot = UInt256.Parse(json["merkleroot"].AsString());
block.Timestamp = (ulong)json["time"].AsNumber();
block.Index = (uint)json["index"].AsNumber();
block.NextConsensus = json["nextconsensus"].AsString().ToScriptHash();
block.NextConsensus = json["nextconsensus"].ToScriptHash();
block.Witness = ((JArray)json["witnesses"]).Select(p => WitnessFromJson(p)).FirstOrDefault();
}

Expand Down Expand Up @@ -145,9 +167,9 @@ public static Signer SignerFromJson(JObject json)
{
return new Signer
{
Account = UInt160.Parse(json["account"].AsString()),
Account = json["account"].ToScriptHash(),
Scopes = (WitnessScope)Enum.Parse(typeof(WitnessScope), json["scopes"].AsString()),
AllowedContracts = ((JArray)json["allowedContracts"])?.Select(p => UInt160.Parse(p.AsString())).ToArray(),
AllowedContracts = ((JArray)json["allowedContracts"])?.Select(p => p.ToScriptHash()).ToArray(),
AllowedGroups = ((JArray)json["allowedGroups"])?.Select(p => ECPoint.Parse(p.AsString(), ECCurve.Secp256r1)).ToArray()
};
}
Expand Down

0 comments on commit c4de92b

Please sign in to comment.