private static async Task <TransactionReceipt> SendEther(Account account, Records recipient, Web3 web3) { var transactionPolling = web3.TransactionManager.TransactionReceiptService; //var currentBalance = await web3.Eth.GetBalance.SendRequestAsync(account.Address); //assumed client is mining already //when sending a transaction using an Account, a raw transaction is signed and send using the private key return(await transactionPolling.SendRequestAndWaitForReceiptAsync(() => { var transactionInput = new TransactionInput { From = account.Address, //Gas = new HexBigInteger(25000), GasPrice = new HexBigInteger(10 ^ 10), To = recipient.Address, Value = new HexBigInteger(new BigInteger(recipient.Value)), Nonce = web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(account.Address).Result }; var txSigned = new Nethereum.Signer.TransactionSigner(); var signedTx = txSigned.SignTransaction(account.PrivateKey, transactionInput.To, transactionInput.Value, transactionInput.Nonce); var transaction = new Nethereum.RPC.Eth.Transactions.EthSendRawTransaction(web3.Client); return transaction.SendRequestAsync(signedTx); })); }
/// <summary> /// 签名交易 /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="amount"></param> /// <returns></returns> public static string SignTransation(string from, string to, decimal amount) { var privateKey = "privateKey";//转出钱包的私钥 var sign = new Nethereum.Signer.TransactionSigner(); var weiAmount = Nethereum.Util.UnitConversion.Convert.ToWei(amount); BigInteger nonce_bigint = Convert.ToInt32(Program.GetTransactionCount(from), 16); var gasLimit = new BigInteger(21000); var gasPrice = new BigInteger(200000000000); var txSign = sign.SignTransaction(privateKey, to, weiAmount, nonce_bigint, gasPrice, gasLimit); return("0x" + txSign); }
/// <summary> /// 签名一个交易 /// </summary> /// <param name="privatekey"></param> /// <param name="to_address"></param> /// <param name="weiNum"></param> /// <param name="nonce">通过GetTransactionCount获取0x000 16进制的值</param> /// <returns>签名后的16进制表达形式</returns> public string SignTransaction(string privatekey, string to_address, decimal to_num, string nonce, string from_address) { var sign = new Nethereum.Signer.TransactionSigner(); var weiNum = Nethereum.Util.UnitConversion.Convert.ToWei(to_num); var hexNum = new HexBigInteger(weiNum).HexValue; BigInteger nonce_bigint = Convert.ToInt32(nonce, 16); var gasLimit = new HexBigInteger(GetGasLimit(from_address, to_address, hexNum, nonce)).Value; var gasPrice = new BigInteger(100000000000); var txSign = sign.SignTransaction(privatekey, to_address, weiNum, nonce_bigint, gasPrice, gasLimit); return("0x" + txSign); }
/// <summary> /// 签名一个交易 /// </summary> /// <param name="privatekey"></param> /// <param name="to_address"></param> /// <param name="weiNum"></param> /// <param name="nonce">通过GetTransactionCount获取0x000 16进制的值</param> /// <returns>签名后的16进制表达形式</returns> public static string SignTransaction(string privatekey, string to_address, decimal to_num, string nonce) { var sign = new Nethereum.Signer.TransactionSigner(); var weiNum = Nethereum.Util.UnitConversion.Convert.ToWei(to_num); //var hexNum = new HexBigInteger(weiNum).HexValue; BigInteger nonce_bigint = Convert.ToInt32(nonce, 16); //var gasLimit = "0x186a0";//100000 //var gasPrice = "0x174876e800";//100000000000------>100000000000x100000/10^18=0.01 var txSign = sign.SignTransaction(privatekey, to_address, weiNum, nonce_bigint, new BigInteger(100000000000), new BigInteger(100000)); return("0x" + txSign); }
private static async Task <TransactionReceipt> SendEther(Account account, string recipient, Web3 web3, BigInteger transferwei) { var transactionPolling = web3.TransactionManager.TransactionReceiptService; HexBigInteger nonce = web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(account.Address).Result; return(await transactionPolling.SendRequestAndWaitForReceiptAsync(() => { var transactionInput = new TransactionInput { From = account.Address, //Gas = new HexBigInteger(25000), GasPrice = new HexBigInteger(10 ^ 10), To = recipient, Value = new HexBigInteger(transferwei), Nonce = nonce }; var txSigned = new Nethereum.Signer.TransactionSigner(); var signedTx = txSigned.SignTransaction(account.PrivateKey, transactionInput.To, transactionInput.Value, transactionInput.Nonce); var transaction = new Nethereum.RPC.Eth.Transactions.EthSendRawTransaction(web3.Client); return transaction.SendRequestAsync(signedTx); })); }