Would you wish to know how one can construct a dapp with C#? Furthermore, would you want to find the quickest approach to construct a Web3 app with C# and .NET? If that’s the case, then this text is for you! When utilizing Moralis’ enterprise-grade APIs and SDK for blockchain improvement, you may construct a dapp with C# and .NET effortlessly. In actual fact, with Moralis, we will accomplish this improvement process in 4 easy steps:
- Create a C# app
- Import and arrange the newest Moralis .NET SDK
- Combine your utility with Moralis providers
- Fetch blockchain knowledge from any supported chain
To deal with the above steps simply, you need to additionally deal with two stipulations. You should create your free Moralis account and set up and arrange Visible Studio. Moreover, your Moralis account gives you entry to your Moralis Web3 API key, which can be your gateway to Web3 improvement. As such, we’ll first present you how one can acquire that key. Furthermore, it’s value mentioning that Moralis is all about cross-chain interoperability, letting you utilize the identical strains of code to deal with the entire main programmable blockchains. Additionally, you may attain a wider viewers and by no means get caught to a specific blockchain. Because of this, you future-proof your dapp improvement.
Nonetheless, even if we’ll construct a dapp with C# and .NET on this tutorial, it’s best to needless to say Moralis can be cross-platform interoperable. So, you need to use fashionable platforms akin to Firebase, Supabase, Unity, and lots of others to dive into the Web3 area.
Construct a Dapp with C# and .NET – Taking Care of Stipulations
We’ll first present you how one can acquire your Moralis Web3 API key to provoke this text. In spite of everything, doing so is an important piece of the “construct a dapp with C#” quest. So, ensure that to create your free Moralis account now. Chances are you’ll use the “create your free Moralis account” hyperlink above or go to the Moralis homepage. There, you’ll have to click on on one of many “Begin for Free” buttons:
Whichever of the 2 choices you select, you’ll land on this web page:
Trying on the above screenshot, you may see that you simply’ll have to enter your e-mail handle and create your password. However you may additionally use your Google account. In case you go along with the previous choice, ensure that to substantiate your account. You do that by clicking on the affirmation hyperlink that can land in your e-mail inbox.
Along with your Moralis account up and working, you’ll be capable to entry your Moralis admin space. From there, you’ll be capable to entry your Web3 API key in two methods.
First, you may simply click on on the “Web3 APIs” choice within the facet menu:
Then select between “EVM API” and “Solana API”. Once you wish to concentrate on Ethereum or different EVM-compatible chains, the previous can be your go-to choice. Lastly, click on on “Copy API key” after which “Web3 Api Key”:
With the above click on, you’ll copy your Web3 API key. As a affirmation of that motion, you will notice a notification within the top-right nook:
Second, you may copy your Moralis Web3 API key out of your “Account Settings” web page. To get there, click on on the “Account” choice within the facet menu. As soon as on the “Account Setting” web page, choose the “Keys” tab after which copy your Web3 API key:
Set up and Set Up Visible Studio
The subsequent prerequisite is to put in and arrange Visible Studio. To make use of this free IDE, use your favourite browser and search engine to question it for “Visible Studio”:
When you land on the official Visible Studio web site, click on on “Free Visible Studio”. It will take you to the “obtain” part, the place you must click on on the “Free obtain” button:
Lastly, set up Visible Studio in your laptop.
Construct a Dapp with C# and .NET – Step 1: Create a C# App
You’ll begin this step of the “construct a dapp with C#” process by opening Visible Studio. Subsequent, create a brand new mission. Furthermore, ensure that to pick “C# Console” because the template:
Then, you must configure your new mission. You do that by getting into your mission title and placement. Be at liberty to comply with our lead and title your mission “ConsoleDemo”:
Trying on the above screenshot, you may see that we used the “MoralisDemo” folder beneath location. Furthermore, identical to with the mission title, be at liberty to comply with our lead. Lastly, choose the “.NET 6.0” framework and click on on the “Create” button:
By creating your C# utility, Visible Studio will generate a primary “C# Console” mission for you:
Furthermore, as indicated by the above picture, Visible Studio ought to create the “Program.cs” script in your behalf.
Construct a Dapp with C# and .NET – Step 2: Import and Set Up the Newest Moralis .NET SDK
By including Web3 performance, you may convert the above-created C# app right into a dapp. Nevertheless, you must import Moralis’ SDK so as to add that performance. So, first, you must handle “NuGet Packages”. You could find this selection beneath “Instruments” > “NuGet Package deal Supervisor” > “Handle NuGet Packages for Resolution…”:
As soon as contained in the “Handle NuGet Packages for Resolution…” choice, ensure that to checkmark the “Embody prerelease” field. Then, enter “Moralis” into the search bar. Among the many given outcomes, choose the newest Moralis bundle and click on on the “Set up” button:
Construct a Dapp with C# and .NET – Step 3: Combine Your App with Moralis Companies
With Moralis’ .NET SDK put in, you might be able to combine your app with Moralis providers. This step will allow you to construct a dapp with C#. So, begin by opening the “Program.cs” file within the “Resolution Explorer” window. Then, choose the present content material of that file and delete it. Subsequent, paste within the following two “utilizing” statements:
utilizing Moralis; utilizing Moralis.Web3Api.Fashions;
With the “utilizing” statements in place, proceed by including “namespace“, “class“, and primary public static “Predominant“. Moreover, you must ensure that to set “MoralisClient.ConnectionData” with the beforehand obtained Moralis Web3 API key. So, simply change the “YOUR MORALIS WEB3API KEY” along with your key. These are the strains of code you must use to cowl that:
namespace ConsoleDemo { inner class Program { static void Predominant(string[] args) { // Setup Moralis MoralisClient.ConnectionData = new Moralis.Fashions.ServerConnectionData() { ApiKey = "YOUR MORALIS WEB3API KEY" }; } } } Subsequent, you need to create the "DisplayCryptoData" static async operate beneath the "Predominant" operate. Furthermore, this operate ought to settle for two parameters - "handle" (string) and "chainId" (ChainList). Nonetheless, the return sort needs to be "Process": inner static async Process DisplayCryptoData(string handle, ChainList chainId) { }
The above operate additionally determines that your utility will settle for the identical two arguments – the handle and the chain ID. Since it is important to make sure that these arguments are appropriately handed and legitimate, we advocate including the strains of code that can validate the arguments (see under). Lastly, you need to add the “Process.Run” assertion to name the “DisplayCryptoData” async operate.
Your “Predominant” Perform
With the entire above in place, the next are the strains of code that your “Predominant” operate ought to comprise:
static void Predominant(string[] args) { if (args.Size < 2) { Console.Write("Utilization: ConsoleDemo.exe ADDRESS CLIENT_ID"); return; } string handle = args[0]; int chainId = 1; if (!int.TryParse(args[1], out chainId)) { Console.Error.WriteLine("CHAIN_ID have to be a quantity."); } // Setup Moralis MoralisClient.ConnectionData = new Moralis.Fashions.ServerConnectionData() { ApiKey = "YOUR MORALIS WEB3API KEY" }; Process.Run(async () => { await DisplayCryptoData(handle, (ChainList)chainId); }).Wait(); }
With the above strains of code, you’ve every thing in place to construct a dapp with C# that can be capable to fetch on-chain knowledge. Within the closing step of this quest, you’ll have an opportunity to learn to fetch and show the native steadiness, ERC-20 token balances, and NFTs’ metadata.
Construct a Dapp with C# and .NET – Step 4: Fetch Blockchain Information from Any Supported Chain
So as to add Web3 performance to your utility, we’ll concentrate on the “DisplayCryptoData” operate. Basically, you’ll want so as to add the suitable strains of code to this beforehand created async operate. For starters, you’ll want to show the handle in query:
Console.WriteLine($"For handle: {handle}...n");
Fetching and Displaying Native Stability
You’ve now put in and built-in the Moralis SDK within the earlier steps. As such, now you can make the most of the Moralis Web3 API. Additionally, you need to use intuitive endpoints to fetch all types of on-chain knowledge. On the subject of fetching the native steadiness, the “GetNativeBalance” endpoint does the trick. So, that is the road of code you must add to the “DisplayCryptoData” operate:
// Load native steadiness for handle NativeBalance bal = await MoralisClient.Web3Api.Account.GetNativeBalance(handle, chainId);
As well as, you additionally want the next strains of code to correctly format and show the native steadiness:
double nativeBal = 0; double.TryParse(bal.Stability, out nativeBal); Console.WriteLine($"Your native steadiness is {nativeBal / Math.Pow(10,18)}");
Fetching and Displaying ERC-20 Token Balances
On the subject of fetching and displaying ERC-20 token balances, you get to comply with the identical ideas as with the native steadiness. After all, you must use a unique endpoint. On this case, it will likely be “GetTokenBalances“:
// Load ERC-20 Token Record for handle Record<Erc20TokenBalance> erc20Balnaces = await MoralisClient.Web3Api.Account.GetTokenBalances(handle, chainId);
Moreover, not like the native steadiness, there may be a number of ERC-20 token varieties in a pockets handle. As such, we have to use an inventory and show it correctly. Nonetheless, you must also needless to say there won’t be any ERC-20 tokens in a specific Web3 pockets. These are the strains of code that can correctly show ERC-20 token balances:
Console.WriteLine("nnYour ERC 20 Tokens:"); if (erc20Balnaces != null && erc20Balnaces.Rely > 0) { // Print out every token with image and steadiness. foreach (Erc20TokenBalance tb in erc20Balnaces) { Console.WriteLine($"t{tb.Image} - {tb.Title}: {tb.NativeTokenBalance}"); } } else { Console.WriteLine("tNone"); }
Fetching and Displaying NFTs
On the subject of fetching NFTs, or ought to we are saying their metadata, issues comply with the identical ideas as within the above two examples. After all, we have to use an applicable endpoint. On this case, “GetNFTs” does the trick:
// Load first 10 NFTs for the handle NftOwnerCollection nfts = await MoralisClient.Web3Api.Account.GetNFTs(handle, (ChainList)chainId, "", null, 10);
These are the strains of code displaying the title, steadiness, and metadata for every of the above-fetched ten NFTs:
// Load first 10 NFTs for the handle NftOwnerCollection nfts = await MoralisClient.Web3Api.Account.GetNFTs(handle, (ChainList)chainId, "", null, 10); Console.WriteLine("nnYour NFTs:"); if (nfts != null && nfts.Outcome.Rely > 0) { // Print out every token with image and steadiness. foreach (NftOwner nft in nfts.Outcome) { Console.WriteLine($"t{nft.Title}: {nft.Quantity}ntMetaData: {nft.Metadata}nn"); } } else { Console.WriteLine("tNone"); }
Notice: It’s the NFTs’ metadata that comprises URLs of the recordsdata (e.g., PNG). So, by creating a correct frontend, you would simply show fetched NFTs.
Final however not least, you may view the whole code to construct a dapp with C# on GitHub. Furthermore, it’s best to use the Moralis documentation for additional help.
Run Your Dapp and Debug
With all of the code in place, you may run your dapp in Visible Studio. In the event you use the code as offered herein, the output can be:
Utilization: ConsoleDemo.exe ADDRESS CLIENT_ID
As such, we encourage you to click on on “Open debug launch profiles UI” (inside “Resolution Explorer” > “Properties” > “Debug” > “Common”):
Then, populate the “Command line arguments” entry subject with a pockets handle and chain ID:
Notice: We advocate you utilize your pockets handle. Additionally, use the chain ID that matches the chain on which you’ve some steadiness. To search out ID numbers for the programmable blockchains, use the “Supported Chains” web page within the Moralis documentation. For instance, the quantity “1” used above corresponds to the Ethereum chain.
In the event you run your dapp once more after getting into your pockets handle and a series ID, it can work correctly. Accordingly, the console will show the native steadiness, ERC-20 token balances, and NFTs’ metadata for a given handle and chain. Moreover, this additionally implies that you’ve efficiently created a backend dapp with C# and .NET!
How you can Construct a Dapp with C# and .NET in 4 Steps – Abstract
This text demonstrated how one can construct a dapp with C# and .NET. Moreover, it confirmed you the way to do this by taking you thru these 4 steps:
- Create a C# app
- Import and arrange the newest Moralis .NET SDK
- Combine your utility with Moralis providers
- Fetch blockchain knowledge from any supported chain
With the talents and information obtained herein, you now know how one can create primary backend dapps. So, you could be able to take your C# and Web3 fundamentals to the subsequent degree. Therefore, we encourage you to make use of one in all our tutorials and construct a full-stack killer dapp. Do not forget that you need to use different programming languages and legacy improvement platforms. Whether or not you wish to dive deeper into C# and .NET dapp improvement or if you happen to’d wish to discover Firebase, for instance, it’s best to use the Moralis documentation. Nevertheless, for much more superior instance tasks, the Moralis weblog and the Moralis YouTube channel needs to be your go-to retailers. These two locations may also help you turn into a Web3 developer without spending a dime.
Moreover, we should always inform you that to go full-time crypto, changing into blockchain licensed may be your entry ticket. That is the place Moralis Academy enters the scene. That is the place you attend top-notch blockchain improvement programs, obtain a personalised examine path, and expertise skilled mentorship. What’s extra, Moralis Academy offers you with a membership in probably the most advancing communities within the crypto realm!