public Transaction MakeInvokeTxFromTransferTx(Transaction transferTx, string contractHash, JArray inputJA, decimal contractGas) { var TX = transferTx; TX.type = TransactionType.InvocationTransaction; var itd = new InvokeTransData(); TX.extdata = itd; itd.script = ThinNeo.Helper.HexString2Bytes(neoHelper.Jarray2script(contractHash, inputJA)); itd.gas = contractGas; return(TX); }
/// <summary> /// 重载交易构造方法,对于复杂交易传入脚本 /// </summary> /// <param name="prikey">私钥</param> /// <param name="script">交易脚本</param> /// <returns></returns> public static async Task <string> api_SendTransaction(List <byte[]> prikeys, byte[] script) { List <TransactionInput> list_inputs = new List <TransactionInput>(); List <TransactionOutput> list_outputs = new List <TransactionOutput>(); foreach (var prikey in prikeys) { byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); if (dir.ContainsKey(Nep55_1.id_GAS) == false) { Console.WriteLine("no gas"); return(null); } TransactionInput input = new TransactionInput(); input.hash = dir[Nep55_1.id_GAS][0].txid; input.index = (ushort)dir[Nep55_1.id_GAS][0].n; list_inputs.Add(input); TransactionOutput outputchange = new TransactionOutput(); outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); outputchange.value = dir[Nep55_1.id_GAS][0].value; outputchange.assetId = new Hash256(Nep55_1.id_GAS); list_outputs.Add(outputchange); } //MakeTran Transaction tran = new Transaction(); tran.type = TransactionType.ContractTransaction; tran.version = 0;//0 or 1 tran.extdata = null; tran.attributes = new ThinNeo.Attribute[0]; tran.inputs = list_inputs.ToArray(); tran.outputs = list_outputs.ToArray(); byte[] data = script; tran.type = TransactionType.InvocationTransaction; var idata = new InvokeTransData(); tran.extdata = idata; idata.script = data; idata.gas = 0; foreach (var prikey in prikeys) { //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); tran.AddWitness(signdata, pubkey, address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api_local, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.HttpPost(url, postdata); return(result); }
async Task test_mintTokens() { 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"); } } //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); if (dir.ContainsKey(Config.id_GAS) == false) { subPrintLine("no gas"); return; } Transaction tran = null; { byte[] script = null; using (var sb = new ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入 Hash160 shash = Config.dapp_sgas; sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var sgasScripthash = Config.dapp_sgas; var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(sgasScripthash); subPrintLine("contract address=" + targetaddr);//往合约地址转账 //生成交易 tran = Helper.makeTran(dir[Config.id_GAS], targetaddr, new Hash256(Config.id_GAS), amount, (decimal)0.00000000); tran.type = TransactionType.InvocationTransaction; var idata = new InvokeTransData(); tran.extdata = idata; idata.script = script; idata.gas = (decimal)0.00000000; //sign and broadcast 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")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); subPrintLine("txid=" + txid); } } }
public void Run() { string wif = "KwwJMvfFPcRx2HSgQRPviLv4wPrxRaLk7kfQntkH8kCXzTgAts8t"; //自己 string asset = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; //币种(GAS) byte[] prikey = Helper.GetPrivateKeyFromWIF(wif); byte[] pubkey = Helper.GetPublicKeyFromPrivateKey(prikey); string address = Helper.GetAddressFromPublicKey(pubkey); SQLServer sQLServer = new SQLServer(); sQLServer.Open(); //接收一个读取对象 SqlDataReader reader = sQLServer.GetUTXO(address); //整理utxo Dictionary <string, List <UTXO> > dic_UTXO = GetUTXO(reader); sQLServer.Close(); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes("C:\\Neo\\SmartContracts\\0x35eac9327df0a34f2302a1c7832d888b6a366c0e.avm"); //这里填你的合约所在地址 byte[] parameter__list = Helper.HexString2Bytes("0710"); //这里填合约入参 例:0610代表(string,[]) byte[] return_type = Helper.HexString2Bytes("05"); //这里填合约的出参 int need_storage = 1; int need_nep4 = 0; int need_canCharge = 4; string name = "NEO Name Credit(CLI)"; string version = "2.0"; string auther = "Youngsun"; string email = "*****@*****.**"; string description = "0"; using (ScriptBuilder sb = new ScriptBuilder()) { //倒叙插入数据 sb.EmitPushString(description); sb.EmitPushString(email); sb.EmitPushString(auther); sb.EmitPushString(version); sb.EmitPushString(name); sb.EmitPushNumber(need_storage | need_nep4 | need_canCharge); sb.EmitPushBytes(return_type); sb.EmitPushBytes(parameter__list); sb.EmitPushBytes(script); sb.EmitSysCall("Neo.Contract.Create"); string scriptPublish = Helper.Bytes2HexString(sb.ToArray()); //用invokescript试运行并得到消耗 HttpRequest httpRequest = new HttpRequest(); JObject result = httpRequest.Get("invokescript", scriptPublish); string consume = result["result"]["gas_consumed"].ToString(); decimal gas_consumed = decimal.Parse(consume); InvokeTransData extdata = new InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = Math.Ceiling(gas_consumed - 10); //拼装交易体 Transaction tran = MakeTransaction(dic_UTXO, null, new Hash256(asset), extdata.gas); tran.version = 1; tran.extdata = extdata; tran.type = TransactionType.InvocationTransaction; byte[] msg = tran.GetMessage(); byte[] signdata = Helper.Sign(msg, prikey); tran.AddWitness(signdata, pubkey, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = Helper.Bytes2HexString(data); //广播 JObject jObject = httpRequest.Post("sendrawtransaction", rawdata); string info = jObject.ToString(); Console.WriteLine(info); } }
async Task test_mintTokens() { 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"); } } //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); if (dir.ContainsKey(Config.id_GAS) == false) { subPrintLine("no gas"); return; } Transaction tran = null; { byte[] script = null; using (var sb = new ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入 Hash160 shash = Config.dapp_sgas; sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var sgasScripthash = Config.dapp_sgas; var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(sgasScripthash); subPrintLine("contract address=" + targetaddr);//往合约地址转账 //生成交易 tran = Helper.makeTran(dir[Config.id_GAS], targetaddr, new Hash256(Config.id_GAS), amount, (decimal)0.00000001); tran.type = TransactionType.InvocationTransaction; var idata = new InvokeTransData(); tran.extdata = idata; idata.script = script; idata.gas = (decimal)0.00000001; //sign and broadcast 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 a = "d1012200c10a6d696e74546f6b656e7367be0da5478954f03c44c06d63bf613dc98c621e96010000000000000000011a47bddefb7d8fff356c939a1fed120d2848464f4ed719cf9134767a822dc936010002e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6000e1f50500000000be0da5478954f03c44c06d63bf613dc98c621e96e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c603e5dd190000000005ffb879d70015f9f8407eaad6adc3e196d8fbeac014140e6f9f97d589a6353f21ba25da7c1f430af10206d7a10597af22dc7d9c8aecef52338da3d1880a7edcc2178823fc7251ff3cb693e39da8da1c352eb3cc4f27045232103aa9038ee904c71cecf450c88a7ff0d23891d6cdc24b1eeb50bfd0e305fbec26bac"; var aa = ThinNeo.Helper.HexString2Bytes(a); var tran2 = new Transaction(); tran2.Deserialize(new System.IO.MemoryStream(aa)); 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")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); subPrintLine("txid=" + txid); } } }
public void Run() { string wif = "KwwJMvfFPcRx2HSgQRPviLv4wPrxRaLk7kfQntkH8kCXzTgAts8t"; //自己 string targetAddress = "AQye22dcXV1jCrzzC4iGbyM68LADwPSs11"; //别人 string asset = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; //币种(GAS转换NNC) decimal sendCount = new decimal(1); byte[] prikey = Helper.GetPrivateKeyFromWIF(wif); byte[] pubkey = Helper.GetPublicKeyFromPrivateKey(prikey); string address = Helper.GetAddressFromPublicKey(pubkey); SQLServer sQLServer = new SQLServer(); sQLServer.Open(); //接收一个读取对象 SqlDataReader reader = sQLServer.GetUTXO(address, asset); //整理utxo Dictionary <string, List <UTXO> > dic_UTXO = GetUTXO(reader); sQLServer.Close(); //拼交易 Transaction transaction = MakeTransaction(dic_UTXO, address, targetAddress, new Hash256(asset), 0); transaction.version = 0; transaction.attributes = new ThinNeo.Attribute[0]; //与普通转账的区别 transaction.type = TransactionType.InvocationTransaction;//调用合约的转账 InvokeTransData invokeTransData = new InvokeTransData(); byte[] script = null; using (ScriptBuilder sb = new ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + address); //from array.AddArrayValue("(addr)" + targetAddress); //to array.AddArrayValue("(int)" + "1" + "00"); //value sb.EmitParamJson(array); //参数倒序入 sb.EmitPushString("transfer"); //参数倒序入 sb.EmitAppCall(new Hash160("0xbab964febd82c9629cc583596975f51811f25f47")); //合约 script = sb.ToArray(); } invokeTransData.script = script; invokeTransData.gas = 0; transaction.extdata = invokeTransData; /// byte[] msg = transaction.GetMessage(); string msgStr = Helper.Bytes2HexString(msg); byte[] signdata = Helper.Sign(msg, prikey);//签名 transaction.AddWitness(signdata, pubkey, address); string txid = transaction.GetHash().ToString(); byte[] data = transaction.GetRawData(); string rawdata = Helper.Bytes2HexString(data); //广播 HttpRequest httpRequest = new HttpRequest(); JObject jObject = httpRequest.Post("sendrawtransaction", rawdata); string info = jObject.ToString(); Console.WriteLine("NNC" + info); }