Nethereum.Web3 傳送帶簽名交易 SendRawTransaction
阿新 • • 發佈:2020-12-11
var web3 = new Web3("http://localhost:8545");
//轉賬數量
var amount = Web3.Convert.ToWei(1);
Console.WriteLine($"amount: {amount}");
var callInput = (CallInput)EtherTransferTransactionInputBuilder.CreateTransactionInput(senderAddress, receiveAddress, 1);
//預估 gas 和 price
var price = await web3.Eth.GasPrice.SendRequestAsync();
Console.WriteLine($"price: {price}");
var gas = web3.Eth.TransactionManager.EstimateGasAsync(callInput).Result;
Console.WriteLine($"egas: {gas}" );
var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(senderAddress);
Console.WriteLine($"txCount: {txCount}");
var encoded = Web3.OfflineTransactionSigner.SignTransaction(privateKey, receiveAddress, amount, txCount.Value, price, gas);
//驗證交易編碼
bool verify = Web3.OfflineTransactionSigner.VerifyTransaction(encoded);
Console.WriteLine($"verify: {verify}");
//傳送交易
var hash = await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + encoded);
Console.WriteLine($"hash: {hash}");
//請求交易回執
var receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(hash);
while (receipt == null)
{
Thread.Sleep(1000);
receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(hash);
}
Console.WriteLine($"receipt: {receipt.BlockNumber}");