public string RpcGenerateTransactionRawTx(CommandInfo ci) { JObject j = JObject.Parse(ci.Parameter); Transaction tr = _transactionManager.ConvertFromJson(j); string hex = tr.To.Value.ToHex(); Module m = null; if (!_loadedModules.TryGetValue(hex.Replace("0x", ""), out m)) { if (!_loadedModules.TryGetValue("0x" + hex.Replace("0x", ""), out m)) { ci.ErrorMsg.Add("Abi Not Loaded."); return(string.Empty); } } Method method = m.Methods?.FirstOrDefault(mt => mt.Name.Equals(tr.MethodName)); if (method == null) { ci.ErrorMsg.Add("Method not found."); return(string.Empty); } JArray p = j["params"] == null ? null : JArray.Parse(j["params"].ToString()); tr.Params = j["params"] == null ? null : method.SerializeParams(p.ToObject <string[]>()); tr.type = TransactionType.ContractTransaction; tr = tr.AddBlockReference(_rpcAddress); _transactionManager.SignTransaction(tr); var rawtx = _transactionManager.ConvertTransactionRawTx(tr); return(rawtx["rawtx"].ToString()); }
public void RpcGetContractAbi(CommandInfo ci) { if (ci.Parameter == "") { if (_genesisAddress == null) { ci.ErrorMsg.Add("Please connect_chain first."); return; } ci.Parameter = _genesisAddress; } var req = RpcRequestManager.CreateRequest(new JObject { ["address"] = ci.Parameter }, ci.Category, 1); Module m = null; if (!_loadedModules.TryGetValue(ci.Parameter, out m)) { string returnCode = string.Empty; long timeSpan = 0; string resp = _requestManager.PostRequest(req.ToString(), out returnCode, out timeSpan); ci.TimeSpan = timeSpan; if (!CheckResponse(ci, returnCode, resp)) { return; } JObject jObj = JObject.Parse(resp); var res = JObject.FromObject(jObj["result"]); JToken ss = res["abi"]; byte[] aa = ByteArrayHelpers.FromHexString(ss.ToString()); MemoryStream ms = new MemoryStream(aa); m = Serializer.Deserialize <Module>(ms); _loadedModules.Add(ci.Parameter, m); ci.InfoMsg.Add(resp); } var obj = JObject.FromObject(m); ci.InfoMsg.Add(obj.ToString()); ci.Result = true; }
public void RpcBroadcastTx(CommandInfo ci) { if (!ci.Parameter.Contains("{")) { RpcBroadcastWithRawTx(ci); return; } JObject j = JObject.Parse(ci.Parameter); Transaction tr = _transactionManager.ConvertFromJson(j); if (tr == null) { return; } string hex = tr.To.Value.ToHex(); Module m = null; if (!_loadedModules.TryGetValue(hex.Replace("0x", ""), out m)) { if (!_loadedModules.TryGetValue("0x" + hex.Replace("0x", ""), out m)) { ci.ErrorMsg.Add("Abi Not Loaded."); return; } } Method method = m.Methods?.FirstOrDefault(mt => mt.Name.Equals(tr.MethodName)); if (method == null) { ci.ErrorMsg.Add("Method not found."); return; } JArray p = j["params"] == null ? null : JArray.Parse(j["params"].ToString()); tr.Params = j["params"] == null ? null : method.SerializeParams(p.ToObject <string[]>()); tr.type = TransactionType.ContractTransaction; tr = tr.AddBlockReference(_rpcAddress); _transactionManager.SignTransaction(tr); var rawtx = _transactionManager.ConvertTransactionRawTx(tr); var req = RpcRequestManager.CreateRequest(rawtx, ci.Category, 1); string returnCode = string.Empty; long timeSpan = 0; string resp = _requestManager.PostRequest(req.ToString(), out returnCode, out timeSpan); ci.TimeSpan = timeSpan; if (!CheckResponse(ci, returnCode, resp)) { return; } JObject rObj = JObject.Parse(resp); var rj = rObj["result"]; string hash = rj["hash"] == null ? rj["error"].ToString() :rj["hash"].ToString(); string res = rj["hash"] == null ? "error" : "txId"; var jobj = new JObject { [res] = hash }; ci.InfoMsg.Add(jobj.ToString()); ci.Result = true; }
public void RpcDeployContract(CommandInfo ci) { if (!ci.CheckParameterValid(3)) { return; } string filename = ci.Parameter.Split(" ")[0]; // Read sc bytes SmartContractReader screader = new SmartContractReader(); byte[] sc = screader.Read(filename); string hex = sc.ToHex(); var name = Globals.GenesisBasicContract; Module m = _loadedModules.Values.FirstOrDefault(ld => ld.Name.Equals(name)); if (m == null) { ci.ErrorMsg.Add("ABI not loaded."); return; } Method meth = m.Methods.FirstOrDefault(mt => mt.Name.Equals("DeploySmartContract")); if (meth == null) { ci.ErrorMsg.Add("Method not Found."); return; } byte[] serializedParams = meth.SerializeParams(new List <string> { "1", hex }); _transactionManager.SetCmdInfo(ci); Transaction tx = _transactionManager.CreateTransaction(ci.Parameter.Split(" ")[2], _genesisAddress, ci.Parameter.Split(" ")[1], "DeploySmartContract", serializedParams, TransactionType.ContractTransaction); if (tx == null) { return; } tx = _transactionManager.SignTransaction(tx); if (tx == null) { return; } var rawtx = _transactionManager.ConvertTransactionRawTx(tx); var req = RpcRequestManager.CreateRequest(rawtx, "broadcast_tx", 1); string returnCode = string.Empty; long timeSpan = 0; string resp = _requestManager.PostRequest(req.ToString(), out returnCode, out timeSpan); ci.TimeSpan = timeSpan; if (!CheckResponse(ci, returnCode, resp)) { return; } JObject jObj = JObject.Parse(resp); var j = jObj["result"]; string hash = j["hash"] == null ? j["error"].ToString() :j["hash"].ToString(); string res = j["hash"] == null ? "error" : "txId"; var jobj = new JObject { [res] = hash }; ci.InfoMsg.Add(jobj.ToString()); ci.Result = true; }