HomeBlockchainBlockchain DIYBasics of Nethereum

Basics of Nethereum

Nethereum is the .Net integration library for Ethereum, simplifying smart contract management and interaction with Ethereum nodes whether they are public, like Geth Parity or private, like Quorum and Besu.

Nethereum is being developed targeting netstandard 1.1, net451 and also as a portable library, hence it is compatible with all major operating systems (Windows, Linux, MacOS, Android and OSX) and has been tested on cloud, mobile, desktop, Xbox, hololens and windows IoT.

Upcoming releases will be Ethereum 2.0 compliant (when Ethereum 2.0 is released) and include functionalities such as DevP2PPlasma and Micro-Payments.

Features

  • JSON RPC / IPC Ethereum core methods.
  • Geth management API (admin, personal, debugging, miner).
  • Parity management API.
  • Quorum integration.
  • Besu.
  • Simplified smart contract interaction for deployment, function calling, transaction and event filtering and decoding of topics.
  • Unity 3d Unity integration.
  • Blockchain processing.
  • ABI to .Net type encoding and decoding, including attribute-based for complex object deserialisation (nethereum-abi-encoding.md).
  • Hd Wallet creation and management.
  • Rules engine.
  • HD Wallet integration.
  • Transaction, RLP and message signing, verification and recovery of accounts.
  • Libraries for standard contracts Token, ENS and Uport
  • Integrated TestRPC testing to simplify TDD and BDD (Specflow) development.
  • Key storage using Web3 storage standard, compatible with Geth and Parity.
  • Simplified account life cycle for both managed by third party client (personal) or stand alone (signed transactions).
  • Low level Interception of RPC calls.
  • Code generation of smart contracts services.

This is a quick start sample with minimal dependencies. It is meant to help new and seasoned .Net developers.

This will take you through the steps of connecting to Infura and retrieving the balance of an account from the mainnet (the public Ethereum chain).

Infura provides a set of public nodes removing the need to have a local or maintained client fully synchronized with the main Ethereum network.

1. Install .Net

Nethereum works with .Net Core or .Net Framework (from 4.5.1 upwards). You’ll need to have the .Net SDK installed. For new starters we recommend .Net core. Mac or Linux users will also need .Net Core.

Not sure which .Net SDK to download? – choose .Net Core 3.1.

Download .Net SDK

2. Create your app

Create a project using the .Net CLI (below) OR create a project in Visual Studio.

dotnet new console -o NethereumSample
cd NethereumSample

3. Add package reference to Nethereum.Web3 and restore (update / download) the project packages.

 

dotnet add package Nethereum.Web3
dotnet restore

4. Open your IDE (VS Code, Visual Studio etc)

Visual Studio Code or Visual Studio are both good choices for .Net development. Other good IDE’s are also available (Jet Brains Rider for instance). Nethereum’s playground can help you get started immediately with no setup, yet, just be aware that it’s not a full-fledged IDE http://playground.nethereum.com/

Now, open the Program.cs file in the editor.

5. Code to retrieve account balance

Full sample code for Program.cs. See below for a fuller explanation of each step.

using System;
using System.Threading.Tasks;
using Nethereum.Web3;

namespace NethereumSample
{
    class Program
    {
        static void Main(string[] args)
        {
            GetAccountBalance().Wait();
            Console.ReadLine();
        }

        static async Task GetAccountBalance()
        {
var web3 = new Web3("https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c");
            var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae");
            Console.WriteLine($"Balance in Wei: {balance.Value}");

            var etherAmount = Web3.Convert.FromWei(balance.Value);
            Console.WriteLine($"Balance in Ether: {etherAmount}");
        }
    }
}

 

First, we need to add our required namespaces for Nethereum:

 

using Nethereum.Web3;

The next step is to create an instance of Web3, with the infura url for mainnet and your INFURA api key with the format:https://<network>.infura.io/v3/YOUR-PROJECT-ID.

For this sample, we’ll use a special API key 7238211010344719ad14a89db874158c, but for your own project you’ll need to sign up on INFURA and generate your own key.

 

var web3 = new Web3("https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c");

Using the Eth API we can execute the GetBalance request asynchronously, for our selected account. In this scenario I have chosen the Ethereum Foundation account. “0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae

 

var balance = await web3.Eth.GetBalance.SendRequestAsync("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae");

The amount returned is in Wei, the lowest unit of value. We can convert this unit to Ether using the Convertion utility:

var etherAmount = Web3.Convert.FromWei(balance.Value);

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link

Most Popular