//获取地址的utxo来得出地址的资产 public static async Task <Dictionary <string, List <Utxo> > > GetBalanceByAddress(string api, string _addr) { MyJson.JsonNode_Object response = (MyJson.JsonNode_Object)MyJson.Parse(await Helper.HttpGet(api + "?method=getutxo&id=1¶ms=['" + _addr + "']")); MyJson.JsonNode_Array resJA = (MyJson.JsonNode_Array)response["result"]; Dictionary <string, List <Utxo> > _dir = new Dictionary <string, List <Utxo> >(); foreach (MyJson.JsonNode_Object j in resJA) { Utxo utxo = new Utxo(j["addr"].ToString(), new ThinNeo.Hash256(j["txid"].ToString()), j["asset"].ToString(), decimal.Parse(j["value"].ToString()), int.Parse(j["n"].ToString())); if (_dir.ContainsKey(j["asset"].ToString())) { _dir[j["asset"].ToString()].Add(utxo); } else { List <Utxo> l = new List <Utxo>(); l.Add(utxo); _dir[j["asset"].ToString()] = l; } } return(_dir); }
async Task test_refund() { decimal amount = 0; while (true) { subPrintLine("Input amount:"); string str_amount = Console.ReadLine(); try { amount = decimal.Parse(str_amount); break; } catch (Exception e) { subPrintLine("input number"); } } string sgas_address = ThinNeo.Helper.GetAddressFromScriptHash(Config.dapp_sgas); //获取sgas合约地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, sgas_address); if (dir.ContainsKey(Config.id_GAS) == false) { subPrintLine("no gas"); return; } List <Utxo> newlist = new List <Utxo>(dir[Config.id_GAS]); //检查sgas地址拥有的gas的utxo是否有被标记过 for (var i = newlist.Count - 1; i >= 0; i--) { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + newlist[i].txid.ToString()); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getRefundTarget")); //参数倒序入 var shash = Config.dapp_sgas; sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } if (newlist[i].n > 0) { continue; } var urlCheckUTXO = Helper.MakeRpcUrl(Config.api, "invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script))); string resultCheckUTXO = await Helper.HttpGet(urlCheckUTXO); var jsonCU = MyJson.Parse(resultCheckUTXO); var stack = jsonCU.AsDict()["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict(); var value = stack["value"].AsString(); if (value.Length > 0)//已经标记的UTXO,不能使用 { newlist.RemoveAt(i); } } //添加系统费 Dictionary <string, List <Utxo> > dir2 = await Helper.GetBalanceByAddress(Config.api, address); List <Utxo> newlist2 = new List <Utxo>(dir2[Config.id_GAS]); ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + ThinNeo.Helper.Bytes2HexString(scriptHash)); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入 var shash = Config.dapp_sgas; sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } //sgas 自己给自己转账 用来生成一个utxo 合约会把这个utxo标记给发起的地址使用 tran = Helper.makeTran(newlist, sgas_address, new ThinNeo.Hash256(Config.id_GAS), (decimal)0.00000001, (decimal)0.00000001, newlist2, address); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; //附加鉴证 tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script; tran.attributes[0].data = scriptHash; } //sign and broadcast {//做智能合约的签名 byte[] sgasScript = null; { var urlgetscript = Helper.MakeRpcUrl(Config.api, "getcontractstate", new MyJson.JsonNode_ValueString(Config.dapp_sgas.ToString())); var resultgetscript = await Helper.HttpGet(urlgetscript); var _json = MyJson.Parse(resultgetscript).AsDict(); var _resultv = _json["result"].AsList()[0].AsDict(); sgasScript = ThinNeo.Helper.HexString2Bytes(_resultv["script"].AsString()); } byte[] iscript = null; using (var sb = new ThinNeo.ScriptBuilder()) { sb.EmitPushString("whatever"); sb.EmitPushNumber(250); iscript = sb.ToArray(); } tran.AddWitnessScript(sgasScript, iscript); } {//做提款人的签名 var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.HttpPost(url, postdata); subPrintLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { bool bSucc = false; if (json["result"].type == MyJson.jsontype.Value_Number) { bSucc = json["result"].AsBool(); subPrintLine("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; } if (bSucc) { Hash256 txid = tran.GetHash(); url = Helper.MakeRpcUrlPost(Config.api, "getrawtransaction", out postdata, new MyJson.JsonNode_ValueString(txid.ToString())); while (true) { subPrintLine("正在等待交易验证,请稍后。。。。"); result = await Helper.HttpPost(url, postdata); json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { //tx的第一个utxo就是给自己的 Utxo utxo = new Utxo(address, txid, Config.id_GAS, amount, 0); //把这个txid里的utxo[0]的value转给自己 TranGas(new List <Utxo>() { utxo }, amount); break; } await Task.Delay(5000); } } else { } } }