예제 #1
0
        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());
        }
예제 #2
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;
        }
예제 #3
0
        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();

            if (!_loadedModules.TryGetValue(_genesisAddress, out var m))
            {
                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);

            tx = tx.AddBlockReference(_rpcAddress);
            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"];

            if (j["error"] != null)
            {
                ci.ErrorMsg.Add(j["error"].ToString());
                ci.Result = false;
                return;
            }
            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;
        }