public Utxo(string _addr, ThinNeo.Hash256 _txid, string _asset, decimal _value, int _n) { this.addr = _addr; this.txid = _txid; this.asset = _asset; this.value = _value; this.n = _n; }
//拼交易体 static Transaction makeTran(Dictionary <string, List <Utxo> > dir_utxos, string[] targetaddrs, ThinNeo.Hash256 assetid, decimal sendcount) { if (!dir_utxos.ContainsKey(assetid.ToString())) { throw new Exception("no enough money."); } List <Utxo> utxos = dir_utxos[assetid.ToString()]; var tran = new Transaction(); tran.type = TransactionType.ContractTransaction; tran.version = 0;//0 or 1 tran.extdata = null; tran.attributes = new ThinNeo.Attribute[0]; utxos.Sort((a, b) => { if (a.value > b.value) { return(1); } else if (a.value < b.value) { return(-1); } else { return(0); } }); //十进制小数 decimal count = decimal.Zero; string scraddr = ""; List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>(); for (var i = 0; i < utxos.Count; i++) { ThinNeo.TransactionInput input = new ThinNeo.TransactionInput(); input.hash = utxos[i].txid; input.index = (ushort)utxos[i].n; list_inputs.Add(input); count += utxos[i].value; scraddr = utxos[i].addr; if (count >= (sendcount)) { break; } } tran.inputs = list_inputs.ToArray(); if (count >= sendcount)//输入大于等于输出 { List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>(); //输出 if (sendcount > decimal.Zero && targetaddrs.Length > 0) { foreach (string targetaddr in targetaddrs) { ThinNeo.TransactionOutput output = new ThinNeo.TransactionOutput(); output.assetId = assetid; output.value = sendcount; output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetaddr); list_outputs.Add(output); } } //找零 var change = count - sendcount; if (change > decimal.Zero) { ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput(); outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr); outputchange.value = change; outputchange.assetId = assetid; list_outputs.Add(outputchange); } tran.outputs = list_outputs.ToArray(); } else { throw new Exception("no enough money."); } return(tran); }
//拼交易体 Transaction makeTran(Dictionary <string, List <UTXO> > dir_utxos, string fromAddress, string targetAddress, ThinNeo.Hash256 assetid, decimal sendcount) { if (!dir_utxos.ContainsKey(assetid.ToString())) { throw new Exception("no enough money."); } List <UTXO> utxos = dir_utxos[assetid.ToString()]; Transaction tran = new Transaction(); utxos.Sort((a, b) => { if (a.value > b.value) { return(1); } else if (a.value < b.value) { return(-1); } else { return(0); } }); decimal count = decimal.Zero; List <TransactionInput> list_inputs = new List <TransactionInput>(); for (var i = 0; i < utxos.Count; i++) { TransactionInput input = new TransactionInput(); input.hash = utxos[i].txid; input.index = (ushort)utxos[i].n; list_inputs.Add(input); count += utxos[i].value; if (count >= (sendcount)) { break; } } tran.inputs = list_inputs.ToArray(); if (count >= sendcount)//输入大于等于输出 { List <TransactionOutput> list_outputs = new List <TransactionOutput>(); //输出 if (sendcount > decimal.Zero) { TransactionOutput output = new TransactionOutput(); output.assetId = assetid; output.value = sendcount; output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetAddress); list_outputs.Add(output); } //找零 var change = count - sendcount; if (change > decimal.Zero) { TransactionOutput outputchange = new TransactionOutput(); outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(fromAddress); outputchange.value = change; outputchange.assetId = assetid; list_outputs.Add(outputchange); } tran.outputs = list_outputs.ToArray(); } else { throw new Exception("no enough money."); } return(tran); }
//退款操作 async Task test_refund() { Console.WriteLine("Input refund tokens:"); string refund = Console.ReadLine(); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(sneo_common.sc_sneo); Console.WriteLine("nep55_address=" + nep55_address); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address); if (dir.ContainsKey(Config.id_NEO) == false) { Console.WriteLine("no neo"); return; } List <Utxo> newlist = new List <Utxo>(dir[Config.id_NEO]); for (var i = newlist.Count - 1; i >= 0; i--) { var ret = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getRefundTarget", "(hex256)" + newlist[i].txid.ToString()); sneo_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(sneo_common.sc_sneo); //nep5脚本 script = sb.ToArray(); } Console.WriteLine("contract address=" + nep55_address);//往合约地址转账 //生成交易 tran = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(Config.id_NEO), 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; } } }
public static ThinNeo.Transaction makeTran(Dictionary <string, List <Utxo> > dir_utxos, string targetaddr, ThinNeo.Hash256 assetid, decimal sendcount) { var tran = new ThinNeo.Transaction(); tran.type = ThinNeo.TransactionType.ContractTransaction; tran.version = 0; tran.extdata = null; tran.attributes = new ThinNeo.Attribute[0]; List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>(); tran.inputs = list_inputs.ToArray(); List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>(); tran.outputs = list_outputs.ToArray(); return(tran); }
IEnumerator HTTP_nel_post_sgas_gas(decimal num) { var nep55_shash = new ThinNeo.Hash160(global.id_sgas); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash); WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(nep55_address)); WWW www = new WWW(global.api, www_getuxo_form); yield return(www); Dictionary <string, List <Utxo> > _dir = get_utxo(www.text); if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); } List <Utxo> newlist = new List <Utxo>(_dir[global.id_GAS]); for (var i = newlist.Count - 1; i >= 0; i--) { Debug.Log(newlist[i].txid.ToString()); 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 = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } if (newlist[i].n > 0) { continue; } WWWForm www_getTarget_form = Helper.GetWWWFormPost("invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script))); WWW www_form = new WWW(global.api, www_getTarget_form); yield return(www_form); var jsonCU = MyJson.Parse(www_form.text); 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); } } string paparms; 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(roleInfo.getInstance().scripthash)); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入 var shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); //拼接发给后台做验证的json字符串 MyJson.JsonNode_Object account = new MyJson.JsonNode_Object(); account["sbParamJson"] = array; account["sbPushString"] = new MyJson.JsonNode_ValueString("refund"); account["nnc"] = new MyJson.JsonNode_ValueString(global.id_sgas); account["cnts"] = new MyJson.JsonNode_ValueString(num.ToString()); paparms = account.ToString(); } //生成交易 tran = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(global.id_GAS), num); 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 = roleInfo.getInstance().scripthash; } //sign and broadcast {//做智能合约的签名 byte[] n55contract = null; { WWWForm www_getState = Helper.GetWWWFormPost("getcontractstate", new MyJson.JsonNode_ValueString(global.id_sgas)); WWW www_state = new WWW(global.api, www_getState); yield return(www_state); var _json = MyJson.Parse(www_state.text).AsDict(); var _resultv = _json["result"].AsList()[0].AsDict(); n55contract = 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(n55contract, iscript); } {//做提款人的签名 var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); WWWForm www_transaction = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata)); WWW www_tran = new WWW(global.api, www_transaction); yield return(www_tran); Debug.Log(www_tran.text); var json = MyJson.Parse(www_tran.text).AsDict(); ThinNeo.Hash256 lasttxid = null; if (json.ContainsKey("result")) { bool bSucc = false; if (json["result"].type == MyJson.jsontype.Value_Number) { bSucc = json["result"].AsBool(); Debug.Log("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; } if (bSucc) { lasttxid = tran.GetHash(); Debug.Log("你可以从这个UTXO拿走GAS了 txid=" + lasttxid.ToString() + "[0]"); api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, lasttxid.ToString(), "0", num.ToString(), "2", paparms, global.netType, "0", (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); testtool.panel_main.on_refresh_plat_NotifyList(); }); } else { lasttxid = null; } } }
private static ThinNeo.Transaction makeTran(List <Utxo> utxos, string targetaddr, ThinNeo.Hash256 assetid, decimal sendcount, decimal extgas = 0, List <Utxo> utxos_ext = null, string extaddr = null) { var tran = new ThinNeo.Transaction(); tran.type = ThinNeo.TransactionType.ContractTransaction; if (extgas >= 1) { tran.version = 1;//0 or 1 } else { tran.version = 0;//0 or 1 } tran.extdata = null; tran.attributes = new ThinNeo.Attribute[0]; var scraddr = ""; utxos.Sort((a, b) => { if (a.value > b.value) { return(1); } else if (a.value < b.value) { return(-1); } else { return(0); } }); decimal count = decimal.Zero; List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>(); for (var i = 0; i < utxos.Count; i++) { ThinNeo.TransactionInput input = new ThinNeo.TransactionInput(); input.hash = utxos[i].txid; input.index = (ushort)utxos[i].n; list_inputs.Add(input); count += utxos[i].value; scraddr = utxos[i].addr; if (count >= sendcount) { break; } } decimal count_ext = decimal.Zero; if (utxos_ext != null) { //手续费 ThinNeo.TransactionInput input = new ThinNeo.TransactionInput(); input.hash = utxos_ext[0].txid; input.index = (ushort)utxos_ext[0].n; count_ext = utxos_ext[0].value; list_inputs.Add(input); } tran.inputs = list_inputs.ToArray(); if (count >= sendcount)//输入大于等于输出 { List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>(); //输出 if (sendcount > decimal.Zero && targetaddr != null) { ThinNeo.TransactionOutput output = new ThinNeo.TransactionOutput(); output.assetId = assetid; output.value = sendcount; output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetaddr); list_outputs.Add(output); } var change = count - sendcount - extgas; decimal extchange = decimal.Zero; //找零 if (utxos_ext != null) { change = count - sendcount; extchange = count_ext - extgas; } else { change = count - sendcount - extgas; } if (change > decimal.Zero) { ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput(); outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr); outputchange.value = change; outputchange.assetId = assetid; list_outputs.Add(outputchange); } if (extchange > decimal.Zero) { ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput(); outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(extaddr); outputchange.value = extchange; outputchange.assetId = assetid; list_outputs.Add(outputchange); } tran.outputs = list_outputs.ToArray(); } else { throw new Exception("no enough money."); } return(tran); }