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); }
async Task test_getAllSAR4C() { //查询所有状态为1的SAR DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "getsar4CListByType", out postdata, new JsonNode_ValueNumber(1), new JsonNode_ValueNumber(1000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string addr = ob["addr"].AsString(); Console.WriteLine("addr" + addr); //查询旧合约SAR var result2 = await newsar_common.api_InvokeScript(newsar_common.sc_sar, "getSAR4C", "(addr)" + addr); newsar_common.ResultItem item = result2.value; newsar_common.ResultItem[] items = item.subItem[0].subItem; if (items != null) { string owner = ThinNeo.Helper.GetAddressFromScriptHash(items[0].AsHash160()); string txid = items[1].AsHashString(); BigInteger locked = items[2].AsInteger(); BigInteger hasDrawed = items[3].AsInteger(); string assetType = items[4].AsString(); BigInteger status = items[5].AsInteger(); Console.WriteLine("###############"); Console.WriteLine("from:" + owner); Console.WriteLine("txid:" + txid); Console.WriteLine("locked:" + locked); Console.WriteLine("hasDrawed:" + hasDrawed); Console.WriteLine("assetType:" + assetType); Console.WriteLine("status:" + status); //Console.WriteLine("bondLocked:" + items[6].AsInteger()); //Console.WriteLine("bondDrawed:" + items[7].AsInteger()); } } DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); }
async Task test_batchTransferNEO() { DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; string api = "http://api.alchemint.io/api/privatenet"; //查询交易,总数可能很多 var url = Helper.MakeRpcUrlPost(api, "getaddrs", out postdata, new JsonNode_ValueNumber(1000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string to = ob["addr"].AsString(); Console.WriteLine("to:" + to); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, this.address); if (dir.ContainsKey(Config.id_NEO) == false) { Console.WriteLine("no neo"); return; } List <Utxo> newlist = dir[Config.id_NEO]; ThinNeo.Transaction tran = Helper.makeTran(newlist, to, new ThinNeo.Hash256(Config.id_NEO), 100); var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); string poststr = System.Text.Encoding.UTF8.GetString(postdata); result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); Thread.Sleep(40000); } DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); //等待时间 }
//查询所有地址 async Task test_queryAllTransfer() { DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; //查询交易,总数可能很多 var url = Helper.MakeRpcUrlPost(Config.api, "getnep5transfersbyasset", out postdata, new JsonNode_ValueString(Config.sc_sds), new JsonNode_ValueNumber(10000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); //System.IO.File.WriteAllText(@"D:\address\addssssss0907.json", result, Encoding.UTF8); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string from = ob["from"].AsString(); string to = ob["to"].AsString(); string value = ob["value"].AsString(); string txid = ob["txid"].AsString(); int index = ob["blockindex"].AsInt(); url = Helper.MakeRpcUrlPost(Config.api, "getblocktime", out postdata, new JsonNode_ValueNumber(index)); result = await Helper.HttpPost(url, postdata); json = MyJson.Parse(result) as MyJson.JsonNode_Object; arrs = json["result"].AsList(); string time = arrs[0].AsDict()["time"].AsString(); //long unixTimeStamp = 1478162177; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt2 = startTime.AddSeconds(double.Parse(time)); string dt2str = dt2.ToString("yyyy/MM/dd HH:mm:ss:ffff"); System.Console.WriteLine(); //json["result"].AsDict()["time"]; //JsonNode_Array arrs = json["result"].AsList(); string str = txid + "," + from + "," + to + "," + value + "," + dt2str + "\r\n"; string newPath = @"D:\address\transfers0907.csv"; File.AppendAllText(newPath, str); } DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); }
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); }
//批量转账 async Task test_batchTransfer2() { DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; string api = "http://api.alchemint.io/api/privatenet"; //查询交易,总数可能很多 var url = Helper.MakeRpcUrlPost(api, "getaddrs", out postdata, new JsonNode_ValueNumber(1000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string to = ob["addr"].AsString(); Console.WriteLine("to:" + to); result = await sds_common.api_SendbatchTransfer(prikey, Config.sds, "transfer", "(addr)" + this.address, "(addr)" + to, "(int)" + 10000000000 ); subPrintLine(result); } DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); //等待时间 }
//查询所有地址 async Task test_queryAllAddress() { DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; //查询交易,总数可能很多 var url = Helper.MakeRpcUrlPost(Config.api, "getnep5transfersbyasset", out postdata, new JsonNode_ValueString(sds_common.sc), new JsonNode_ValueNumber(10000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); //System.IO.File.WriteAllText(@"D:\address\addssssss.json", result, Encoding.UTF8); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string from = ob["from"].AsString(); string to = ob["to"].AsString(); if (!string.IsNullOrEmpty(from)) { list.Add(from); if (from == to) { Console.WriteLine("from:" + from + "/to:" + to); } } if (!string.IsNullOrEmpty(to)) { list.Add(to); } } BigInteger sum = 0; List <string> adds = list.Distinct().ToList(); Console.WriteLine("total address:" + adds.Count); string[] balances = new string[] { }; foreach (string s in adds) { //int index = adds.IndexOf(s); //Console.WriteLine("address:" + s); //调用RPC //var re = await sds_common.api_InvokeScriptByRPC(sds_common.sc_sds, "balanceOf", //"(addr)" + s); //调用API var re = await sds_common.api_InvokeScript(sds_common.sc_sds, "balanceOf", "(addr)" + s); sds_common.ResultItem item = re.value; BigInteger mount = item.subItem[0].AsInteger(); sum = sum + mount; if (mount > 0) { //排除掉所有switcheo地址 //if (s != "AKJQMHma9MA8KK5M8iQg8ASeg3KZLsjwvB") //{ string str = s + "," + mount + "\r\n"; string newPath = @"D:\address\balances0809.txt"; File.AppendAllText(newPath, str); //} } } Console.WriteLine("sum:" + sum); DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); }
//升级合约 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()); } }