예제 #1
0
        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;
        }