Exemplo n.º 1
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;
        }