private async Task <JObject> asyncApplyGas(List <string> targetAddress, decimal amount, Dictionary <string, List <Utxo> > dir) { // 转换私钥 byte[] prikey = accountInfo.prikey; byte[] pubkey = accountInfo.pubkey; string address = accountInfo.address; // 获取余额 string id_gas = assetid; //Dictionary<string, List<Utxo>> dir2 = await TransHelper.GetBalanceByAddress(nelJsonRpcUrl, address); if (dir == null || dir[id_gas] == null) { // 余额不足 return(insufficientBalance()); } List <Utxo> balanceUtxo = dir[id_gas]; if (balanceUtxo.Sum(p => p.value) < amount * targetAddress.Count()) { // 余额不足 return(insufficientBalance()); } // 构造并发送交易 ThinNeo.Transaction tran = TransHelper.makeTran(dir[id_gas], targetAddress, new ThinNeo.Hash256(id_gas), amount); tran.type = ThinNeo.TransactionType.ContractTransaction; byte[] msg = tran.GetMessage(); byte[] signdata = ThinNeo.Helper.Sign(msg, prikey); tran.AddWitness(signdata, pubkey, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(data); byte[] postdata; try { string url = TransHelper.MakeRpcUrlPost(nelJsonRpcUrl, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); var result = await TransHelper.HttpPost(url, postdata); Console.WriteLine("result:" + result); if (JObject.Parse(result)["result"] == null) { return(txFail(txid)); } JObject res = (JObject)(((JArray)(JObject.Parse(result)["result"]))[0]); string flag = res["sendrawtransactionresult"].ToString(); if (flag != "True" && res["txid"].ToString() != txid) { return(txFail(txid)); } return(txSucc(txid)); } catch (Exception) { return(txFail(txid)); } }
public async Task <JObject> sendTransaction(byte[] prikey, Hash160 schash, string method, string[] subparam) { // 构造合约交易 byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); // byte[] randombytes = new byte[32]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(randombytes); } BigInteger randomNum = new BigInteger(randombytes); sb.EmitPushNumber(randomNum); sb.Emit(ThinNeo.VM.OpCode.DROP); for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } sb.EmitParamJson(array); sb.EmitPushString(method); sb.EmitAppCall(schash); data = sb.ToArray(); } // 获取余额 //string id_nnc = assetid; Dictionary <string, List <Utxo> > dir = getBalance(accountInfo.address); if (dir == null || dir[id_gas] == null) { // 余额不足 return(insufficientBalance()); } // 构造并签名 var tran = new ThinNeo.Transaction(); tran.type = ThinNeo.TransactionType.InvocationTransaction; tran.extdata = new ThinNeo.InvokeTransData { script = data, gas = 0 }; tran.inputs = new ThinNeo.TransactionInput[0]; tran.outputs = new ThinNeo.TransactionOutput[0]; tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = TransactionAttributeUsage.Script; tran.attributes[0].data = ThinNeo.Helper.GetPublicKeyHashFromAddress(accountInfo.address); byte[] signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, accountInfo.pubkey, accountInfo.address); string txid = tran.GetHash().ToString(); byte[] transdata = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(transdata); // 发送交易 try { byte[] postdata; string url = TransHelper.MakeRpcUrlPost(nelJsonRpcUrl, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); var result = await TransHelper.HttpPost(url, postdata); Console.WriteLine("result:" + result); if (JObject.Parse(result)["result"] == null) { return(txFail(txid)); } JObject res = (JObject)(((JArray)(JObject.Parse(result)["result"]))[0]); string flag = res["sendrawtransactionresult"].ToString(); if (flag.ToLower() != "true" && res["txid"].ToString() != txid) { return(txFail(txid)); } return(txSucc(txid)); } catch (Exception) { return(txFail(txid)); } }