2

How can I get the latest block, coinbase and other information using Nethereum?

  • Take a look to this link: nethereum.readthedocs.io/en/latest/introduction/web3 essentially, you need to instantiate a web3 object and thereafter use the standard json rpc calls to get the information you want. Web3 is a wrapper for this json RPC api github.com/ethereum/wiki/wiki/JSON-RPC – Briomkez Jan 25 at 21:40
  • Thanks for this, but the information is still abstract. I need a concrete example. I can use web3 , instantiate, connect to my node... and then im stuck... so far for example, I am doing : _web3.Eth.CoinBase.... – DottoreM Jan 26 at 10:32
  • I can get smart contracts, unlock account etc. but getting simple information about the node such as the block and coinbase is not working – DottoreM Jan 26 at 10:33
  • Are you sure, your node has the "with" json rpc api? I am AFK now. Maybe later I can write a simple example with geth and JavaScript web3, which follows the same principle :) – Briomkez Jan 26 at 11:46
  • thanks i would appreciate that. but why can i not use c#? this -> _web3.Eth.CoinBase <- is c# – DottoreM Jan 26 at 11:49
1

As promised, I post here a simple example. The main idea is the same for all languages for each a web3 implementation exists (e.g., Javascript, Python and C#).

ETHEREUM CLIENT

First we need to start an Ethereum client (e.g., geth, parity, nethereum) with the desired rpcapi (or ipcapi if you want it works only locally) enabled. For instance with geth:

geth --rpccorsdomain="*" --rpcapi db,eth,net,web3,personal,miner --rpcaddr 0.0.0.0 --rpc

A brief comment on the options:

  • rpc enables the rpc server
  • rpcapi to expose the selected RPC APIs. At this link you can get an overall overview. In this case I selected (essentially all APIs)
  • rpccorsdomain restricts the IP address which can perform calls (in this case I selected all IPs, it is not secure)
  • rpcaddr express which address to use, with 0.0.0.0 you use its external IP

SIMPLE NETHEREUM-WEB3 EXAMPLE

To configure a simple web3 Nethereum project you can follow this 5-step tutorial. Now we want to read the information from the running geth instance with C#.

First of all, we need to instantiate a web3 instance connected with the client (in my case the address of the running Ethereum instance, is my raspberry PI on 192.168.1.6)

 var web3 = new Web3("http://192.168.1.6:8545");

In the following I report some of the calls you requested:

  • Block number:

    var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
    Console.WriteLine($"Block Number: {blockNumber.Value}");
    
  • Coinbase:

    var coinbase = await web3.Eth.CoinBase.SendRequestAsync();
    Console.WriteLine($"Coinbase: {coinbase}");
    

If the interface eth does not belong to the rpcapi option of the client, then the C#'s Eth API is simply not available.

For the other information you need to find the C# equivalent methods' name of the JSON RPC APIs from Nethereum. Unfortunately, I did not manage to find a complete list and the naming convention is (to me at least) not 100% clear. Here, you can find some clue but the list is not complete.

ALTERNATIVE

A more complicated alternative is to perform directly the json rpc api calls with Nethereum. After all, web3 is simply a wrapper for the JSON RPC API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.