//查询最小单位 async Task test_decimals() { var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "decimals", null); cgas_common.ResultItem item = result.value; Console.WriteLine(item.subItem[0].AsInteger()); }
//查询标志 async Task test_symbol() { var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "symbol", null); cgas_common.ResultItem item = result.value; Console.WriteLine(item.subItem[0].AsString()); }
//查询总量 async Task test_totalSupply() { var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "totalSupply", null); cgas_common.ResultItem item = result.value; Console.WriteLine(Helper.changeDecimals(item.subItem[0].AsInteger(), 8)); }
async Task test_getRefundTarget() { Console.WriteLine("Input txid:"); string txid = Console.ReadLine(); var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "getRefundTarget", "(hex256)" + txid); cgas_common.ResultItem item = result.value; Console.WriteLine("value:" + ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160())); }
//查询余额 async Task test_BalanceOf() { Console.WriteLine("Input target address:"); string addr = Console.ReadLine(); var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "balanceOf", "(addr)" + addr); cgas_common.ResultItem item = result.value; Console.WriteLine(Helper.changeDecimals(item.subItem[0].AsInteger(), 8)); }
async Task test_getTXInfo() { Console.WriteLine("Input txid:"); string txid = Console.ReadLine(); var result = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "getTxInfo", "(hex256)" + txid); cgas_common.ResultItem item = result.value; cgas_common.ResultItem[] items = item.subItem[0].subItem; //查询交易详细信息 Console.WriteLine("from:" + ThinNeo.Helper.GetAddressFromScriptHash(items[0].AsHash160())); Console.WriteLine("to:" + ThinNeo.Helper.GetAddressFromScriptHash(items[1].AsHash160())); Console.WriteLine("value:" + items[2].AsInteger()); }
async Task test_getRefund() { //if (lasttxid == null) //{ // Console.WriteLine("你还没有正确执行Refund"); // return; //} Console.WriteLine("refund txid:"); var lastTxid = Console.ReadLine(); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(cgas_common.sc_cgas); Console.WriteLine("address=" + nep55_address); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address); if (dir.ContainsKey(Config.id_GAS) == false) { Console.WriteLine("no gas"); return; } List <Utxo> newlist = new List <Utxo>(); foreach (var utxo in dir[Config.id_GAS]) { if (utxo.n == 0 && utxo.txid.ToString().Equals(lastTxid)) { newlist.Add(utxo); } } if (newlist.Count == 0) { Console.WriteLine("找不到要使用的UTXO"); return; } else { lastTxid = newlist[0].txid.ToString(); } {//检查是否是前面交易存储的 var ret = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "getRefundTarget", "(hex256)" + lastTxid); cgas_common.ResultItem item = ret.value; var value = ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160()); if (value.Length == 0)//未标记的UTXO,不能使用 { Console.WriteLine("这个utxo没有标记"); return; } if (value != this.address) { Console.WriteLine("这个utxo不是标记给你用的"); return; } } ThinNeo.Transaction tran = Helper.makeTran(newlist, address, new ThinNeo.Hash256(Config.id_NEO), newlist[0].value); tran.type = ThinNeo.TransactionType.ContractTransaction; tran.version = 0; //sign and broadcast {//做智能合约的签名 byte[] iscript = null; using (var sb = new ThinNeo.ScriptBuilder()) { sb.EmitPushNumber(0); sb.EmitPushNumber(0); iscript = sb.ToArray(); } tran.AddWitnessScript(n55contract, iscript); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); string poststr = System.Text.Encoding.UTF8.GetString(postdata); //Console.WriteLine("-----post info begin----"); //Console.WriteLine(poststr); //Console.WriteLine("-----post info end----"); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + 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(); Console.WriteLine("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; Console.WriteLine("txid=" + txid); } if (bSucc) { Nep55_1.lastNep5Tran = tran.GetHash(); Console.WriteLine("besucc txid=" + tran.GetHash().ToString()); } } }
//退款操作 async Task test_refund() { Console.WriteLine("Input refund tokens:"); string refund = Console.ReadLine(); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(cgas_common.sc_cgas); Console.WriteLine("nep55_address=" + nep55_address); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address); if (dir.ContainsKey(Config.id_GAS) == false) { Console.WriteLine("no gas"); return; } List <Utxo> newlist = new List <Utxo>(dir[Config.id_GAS]); for (var i = newlist.Count - 1; i >= 0; i--) { string txid = newlist[i].txid.ToString(); var ret = await cgas_common.api_InvokeScript(cgas_common.sc_cgas, "getRefundTarget", "(hex256)" + txid); cgas_common.ResultItem item = ret.value; if (newlist[i].n > 0) { continue; } var value = item.subItem[0].AsString(); if (value.Length > 0)//已经标记的UTXO,不能使用 { newlist.RemoveAt(i); } } 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")); //参数倒序入 sb.EmitAppCall(cgas_common.sc_cgas); //nep5脚本 script = sb.ToArray(); } Console.WriteLine("contract address=" + nep55_address);//往合约地址转账 //生成交易 tran = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(Config.id_GAS), Decimal.Parse(refund)); 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[] iscript = null; using (var sb = new ThinNeo.ScriptBuilder()) { sb.EmitPushString("whatever"); sb.EmitPushNumber(250); iscript = sb.ToArray(); } tran.AddWitnessScript(n55contract, iscript); } {//做提款人的签名 var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); string poststr = System.Text.Encoding.UTF8.GetString(postdata); //Console.WriteLine("-----post info begin----"); //Console.WriteLine(poststr); //Console.WriteLine("-----post info end----"); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + 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(); Console.WriteLine("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; Console.WriteLine("txid=" + txid); } if (bSucc) { lasttxid = tran.GetHash(); Nep55_1.lastNep5Tran = tran.GetHash(); Console.WriteLine("你可以从这个UTXO拿走NEO了 txid=" + lasttxid.ToString() + "[0]"); } else { lasttxid = null; } } }