Sending Ether with Nethereum
This article will show you how to send Ether using a local Ethereum Node.
The below instructions apply to Windows, Mac and Linux OSes.
First, make sure your environment is set to work with .NET Core, you can find all the instructions on https://www.microsoft.com/net/learn/get-started
Create a .NET app
In a new directory, use the dotnet command to create a new console app:
$ dotnet create new console
Your app directory now contains a file named:
Program.cs this is the file that we will use throughout this tutorial.
Install Nethereum packages
You can now add Nethereum to your stack by installing one of Nethereum Nugets
For this Tutorial, you will need Nethereum Web3:
$ dotnet add package Nethereum.Web3
First, let’s download the test chain matching your environment from https://github.com/Nethereum/Testchains
Start a Geth chain (geth-clique-linux\, geth-clique-windows\ or geth-clique-mac\) using startgeth.bat (Windows) or startgeth.sh (Mac/Linux). The chain is setup with Proof of Authority consensus and will start the mining process immediately.
Then, let’s add using statements to Nethereum.Web3.
using System; using Nethereum.Web3; using Nethereum.Web3.Accounts; using Nethereum.Web3.Accounts.Managed; using Nethereum.Hex.HexTypes;
Sending a transaction
To send a transaction, we will manage our account and sign the raw transaction locally.
Here is how to set up a new account by creating an
account object instance:
var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7"; var account = new Account(privateKey);
var web3 = new Web3(account);
var toAddress = "0x13f022d72158410433cbd66f5dd8bf6d2d129924";
Sending Ether using the EtherTransferService with the default gas price and gas amount
The tranfer of Ether is the simplest transaction on chain. This type of transaction uses the default amount of Gas 21000. Each transaction has a price per unit of gas, if we don’t specify the gas price the current average price provided by the client would be used.
Using the EtherTransferService, we can simply do an Ether transfer as follows:
var transaction = await web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 1.11m);
Note, that the amout supplied is in Ether and the service converts it to Wei, the lowest unit value used for transactions.
Sending Ether using the EtherTransferService with the default gas amount but suplying the price
We can also choose the gas price for our transaction, in this case the last argument. The higher price of a transaction cost, the higher the possibility to be picked and prioritised by the miners quickly.
The unit used to provide the price is Gwei, in this scenario 2 GWei.
var transaction = await web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 1.11m, 2);
Sending Ether using the EtherTransferService providing both the price and the amount of gas
If sending Ether to a smart contract, this might require a bigger amount of gas than the default due the smart contract doing further processing.
In this scenario, we can do the following:
var transaction = web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 1.11m, 2, new BigInteger(25000));
Using a HD Wallet
string Words = "ripple scissors kick mammal hire column oak again sun offer wealth tomorrow wagon turn fatal"; string Password = "password"; var wallet = new Wallet(Words, Password); var account = wallet.GetAccount(0); var toAddress = "0x13f022d72158410433cbd66f5dd8bf6d2d129924"; var web3 = new Web3(account); var transaction = await web3.Eth.GetEtherTransferService() .TransferEtherAndWaitForReceiptAsync(toAddress, 1.11m, 2);