From c0de35fb74df1acdcb91cbf22d5175533d216603 Mon Sep 17 00:00:00 2001 From: Christopher R Schuchardt Date: Wed, 30 Aug 2023 06:44:31 -0400 Subject: [PATCH] RpcServer: added GetContractState by contract id support (#813) * Changed GetContractState rpc method to allow contract ids * Add support to getcontractstate by id in rpcclient --------- Co-authored-by: Christopher R. Schuchardt --- src/RpcClient/RpcClient.cs | 9 +++++++++ src/RpcServer/RpcServer.Blockchain.cs | 14 +++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/RpcClient/RpcClient.cs b/src/RpcClient/RpcClient.cs index 4bc0398f3..957025f1a 100644 --- a/src/RpcClient/RpcClient.cs +++ b/src/RpcClient/RpcClient.cs @@ -245,6 +245,15 @@ public async Task GetContractStateAsync(string hash) return ContractStateFromJson((JObject)result); } + /// + /// Queries contract information, according to the contract id. + /// + public async Task GetContractStateAsync(int id) + { + var result = await RpcSendAsync(GetRpcName(), id).ConfigureAwait(false); + return ContractStateFromJson((JObject)result); + } + public static ContractState ContractStateFromJson(JObject json) { return new ContractState diff --git a/src/RpcServer/RpcServer.Blockchain.cs b/src/RpcServer/RpcServer.Blockchain.cs index 9cc1abaaf..5fc81fb5f 100644 --- a/src/RpcServer/RpcServer.Blockchain.cs +++ b/src/RpcServer/RpcServer.Blockchain.cs @@ -120,9 +120,17 @@ protected virtual JToken GetBlockHeader(JArray _params) [RpcMethod] protected virtual JToken GetContractState(JArray _params) { - UInt160 script_hash = ToScriptHash(_params[0].AsString()); - ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash); - return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract"); + if (int.TryParse(_params[0].AsString(), out int contractId)) + { + var contracts = NativeContract.ContractManagement.GetContractById(system.StoreView, contractId); + return contracts?.ToJson() ?? throw new RpcException(-100, "Unknown contract"); + } + else + { + UInt160 script_hash = ToScriptHash(_params[0].AsString()); + ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash); + return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract"); + } } private static UInt160 ToScriptHash(string keyword)