public static async Task <Result> api_InvokeScript(Hash160 scripthash, string methodname, params string[] subparam) { byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } sb.EmitParamJson(array); sb.EmitPushString(methodname); sb.EmitAppCall(scripthash); data = sb.ToArray(); } string script = ThinNeo.Helper.Bytes2HexString(data); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(script)); var text = await Helper.HttpPost(url, postdata); MyJson.JsonNode_Object json = MyJson.Parse(text) as MyJson.JsonNode_Object; Result rest = new Result(); Console.WriteLine(json); rest.textInfo = text; if (json.ContainsKey("result")) { var result = json["result"].AsList()[0].AsDict()["stack"].AsList(); rest.value = ResultItem.FromJson("Array", result); } return(rest);// subPrintLine("得到的结果是:" + result); }
public static string MakeRpcUrlPost(string url, string method, out byte[] data, params MyJson.IJsonNode[] _params) { //if (url.Last() != '/') // url = url + "/"; var json = new MyJson.JsonNode_Object(); json["id"] = new MyJson.JsonNode_ValueNumber(3); json["jsonrpc"] = new MyJson.JsonNode_ValueString("2.0"); json["method"] = new MyJson.JsonNode_ValueString(method); StringBuilder sb = new StringBuilder(); var array = new MyJson.JsonNode_Array(); for (var i = 0; i < _params.Length; i++) { array.Add(_params[i]); } json["params"] = array; data = System.Text.Encoding.UTF8.GetBytes(json.ToString()); return(url); }
public static async Task <string> api_SendTransaction(byte[] prikey, Hash160 schash, string methodname, params string[] subparam) { byte[] data = null; //MakeTran ThinNeo.Transaction tran = null; { using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } sb.EmitParamJson(array); sb.EmitPushString(methodname); sb.EmitAppCall(schash); data = sb.ToArray(); Console.WriteLine(ThinNeo.Helper.Bytes2HexString(data)); } } return(await nns_common.api_SendTransaction(prikey, data)); }
//升级合约 async Task test_setUpgrade() { byte[] prikey_admin = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.testwif_admin); byte[] pubkey_admin = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey_admin); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey_admin); Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes("C:\\Neo\\SmartContracts\\0x801c2e175451d4e732b49cb95db3fa988f915234.avm"); //这里填你的合约所在地址 string str_script = ThinNeo.Helper.Bytes2HexString(script); byte[] aa = ThinNeo.Helper.HexString2Bytes(str_script); var ss = 1 | 2 | 4; using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { //倒叙插入数据 var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + str_script); array.AddArrayValue("(bytes)0710"); array.AddArrayValue("(bytes)05"); array.AddArrayValue("(int)" + 7); array.AddArrayValue("(str)合约升级测试"); //name array.AddArrayValue("(str)1"); //version array.AddArrayValue("(str)ss"); //author array.AddArrayValue("(str)1"); //email array.AddArrayValue("(str)sssss"); //desc sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)upgrade")); //参数倒序入 var shash = newsar_common.sc_sar; sb.EmitAppCall(shash); string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = await Helper.HttpPost(url, postdata); //string result = http.Post(api, "invokescript", new MyJson.JsonNode_Array() { new MyJson.JsonNode_ValueString(scriptPublish) },Encoding.UTF8); //var consume = (((MyJson.Parse(result) as MyJson.JsonNode_Object)["result"] as MyJson.JsonNode_Array)[0] as MyJson.JsonNode_Object)["gas_consumed"].ToString(); //decimal gas_consumed = decimal.Parse(consume); ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.gas = 1010;// Math.Ceiling(gas_consumed - 10); extdata.script = sb.ToArray(); //拼装交易体 ThinNeo.Transaction tran = Helper.makeTran(dir[Config.id_GAS], null, new ThinNeo.Hash256(Config.id_GAS), extdata.gas); tran.version = 1; tran.extdata = extdata; tran.type = ThinNeo.TransactionType.InvocationTransaction; byte[] msg = tran.GetMessage(); byte[] signdata = ThinNeo.Helper.Sign(msg, prikey_admin); tran.AddWitness(signdata, pubkey_admin, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(data); url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); result = await Helper.HttpPost(url, postdata); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result); Console.WriteLine(resJO.ToString()); } }