By the end of this lesson, you will be able to:
This module is targeted at helping Web2 developers transition into Web3 along with their established skill set to build Web3 applications with complete ease. In this lesson, we provide our readers with a detailed guide on how to interact with smart contracts on the BNB Chain by leveraging the Nethereum library for use with .Net programming languages.
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. BSC has an Ethereum-like API available which is completely compatible with Ethereum-style JSON RPC invocations. Therefore, developers can leverage this compatibility and use the Nethereum library to interact with a BSC Network and leverage their .Net programming skills to develop blockchain-based applications.
This is a hands-on guide and readers are encourged to perform these tasks along for a better understanding. Before starting off with the practical demo, it is necessary to make sure you fulfill the following pre-requisites.
In order to use the Nethereum library, the first important step is to have a smart contract successfully deployed on the BNB Smart Chain network. For development purposes, a testnet can be used. Complete Module 2A and Module 2B of this course of writing, compiling, and deploying a HelloWorld smart contract onto the BNB Smart Chain and save the contract address.
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. Download .Net SDK from here.
In a new folder, use the dotnet command to create a new console app.
$ dotnet create new console
Install the Nethereum packages by running the following commands
$ dotnet add package Nethereum.Web3
$ dotnet add package Nethereum.Contracts
Program.cs
file to include the following code snippets in the given order.using System;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Contracts;
using System.Numerics;
using System.Threading.Tasks;
using Nethereum.Hex.HexTypes;
url
. You can get the complete list of RPC endpoints from here.address
. This is obtained when the contract is deployed.ABI
, this is stored in a JSON
file as an output of compiling your smart contract. If you have used Truffle for deploying your smart contract, then ABI can be found in the build
folder of your project.chainId
variable.var account = new Account(privateKey, chainId);
new Web3(account, url);
Where you pass your account and the RPC URL you wish to connect to.web3.Eth.GetContract(ABI, address)
which takes the ABI and contract address as input.namespace console
{
class Program
{
static void Main(string[] args)
{
//The URL endpoint for the blockchain network.
string url = "https://data-seed-prebsc-1-s1.binance.org:8545";
//The contract address.
string address = "0x34823954bA4eb6600572239f39D05412373f9ac4";
//The ABI for the contract.
string ABI = @"[
{
""inputs"": [],
""name"": ""greet"",
""outputs"": [
{
""internalType"": ""string"",
""name"": """",
""type"": ""string""
}
],
""stateMutability"": ""view"",
""type"": ""function"",
""constant"": true
},
{
""inputs"": [],
""name"": ""getMessage"",
""outputs"": [
{
""internalType"": ""string"",
""name"": """",
""type"": ""string""
}
],
""stateMutability"": ""view"",
""type"": ""function"",
""constant"": true
},
{
""inputs"": [
{
""internalType"": ""string"",
""name"": ""_name"",
""type"": ""string""
}
],
""name"": ""setName"",
""outputs"": [
{
""internalType"": ""bool"",
""name"": ""success"",
""type"": ""bool""
}
],
""stateMutability"": ""nonpayable"",
""type"": ""function""
}
]";
var privateKey = "Your_Private_Key";
var senderAddress = "0x27cf2CEAcdedce834f1673005Ed1C60efA63c081";
var chainId = 97; //chain id of BNB Smart Chain Testnet
var account = new Account(privateKey, chainId);
//Creates the connection to the network and gets an instance of the contract.
var web3 = new Web3(account, url);
Contract greeterContract = web3.Eth.GetContract(ABI, address);
}
}
}
The HelloWorld smart contract has a getMessage()
function which returns the greeting message. To call this function add the following code the Main()
function.
//Call the getMessage function to read the value of greet
Task<String> greetMessageFunction = greeterContract.GetFunction("getMessage").CallAsync<String>();
greetMessageFunction.Wait();
string message = (string)greetMessageFunction.Result;
Console.WriteLine(message);
Task
type variable that will return a string variable. This will be used for fetching the getMessage
function and then using it to call the function.await async
operations, we use the Task
type variable's Wait()
function to ensure that the function call is completed and then the next task is completed.string
and displayed on the screen.Send methods are the type of interaction that modify the contract's storage (change variables), meaning a transaction needs to be signed and sent.
In the HelloWorld
smart contract, there is one send method namely, setName
, which takes a string
as input and sets this as the value of the smart contract's variable greet
. Add the following code to the Main()
function to call a send method
//Execute set name function
try{
web3.TransactionManager.UseLegacyAsDefault = true;
HexBigInteger gas = new HexBigInteger(new BigInteger(500000));
HexBigInteger value = new HexBigInteger(new BigInteger(0));
Task setNameFunction = greeterContract.GetFunction("setName").SendTransactionAsync(account.Address, gas, value, "BNBChain");
setNameFunction.Wait();
Console.WriteLine("Greeting Set Successfully");
}catch(Exception e){
Console.WriteLine("Error: {0}", e.Message);
}
web3.TransactionManager.UseLegacyAsDefault
to true
.Task
variable to make sure that we can skip the use of async wait
and use the Wait
function of the Task
type to successfully execute the function call.SendTransactionAsync
takes an account address to sign the transaction, the gas, value of any added amount required and the value to set the greet
variable to.try and catch
block to handle any exception that may occur in the execution of the function call.Compile/Build your application to make sure there are no compile time errors. To do so, run the following command.
$ dotnet build
To run your successfully build application, run the following command.
$ dotnet run
The output of running this program should be similar to as follows
This module is targeted at helping Web2 developers transition into Web3 along with their established skill set to build Web3 applications with complete ease. In this lesson, we provide our readers with a detailed guide on how to interact with smart contracts on the BNB Chain by leveraging the Nethereum library for use with .Net programming languages.