Broadcasting Votes on the HIVE blockchain using C# & HIVE.NET Library

in Programming & Dev3 years ago (edited)

In this post I am going to show you how to vote for posts on the hive blockchain using C# and the HIVE.NET library created by @arcange

This will be written at the beginner level including environment setup
I will be assuming you know a little about programming, but will try to keep things simple.

Development Environment

We will be using visual studio community 2022 to develop our application. It is available for free from Microsoft. Visual studio community 2022 download

You will also need the HIVE.NET library so go ahead and download that as well

When you install Visual Studio make sure that .NET development is checked.

Once installed open Visual Studio and select 'Create a new project'
Then select C# Console App (.NET Framework)

You can name the project anything you like. We will be naming ours Hive_Vote
Once your project is named select '.NET Framework 4.8' from the dropdown.

.NET Library Setup

Open the HIVE.NET library you downloaded earlier and copy the following files into the folder for your project. They should be in the same folder as the program.cs file.

CBase58.cs
CHiveAPI.cs
CHived.cs
CHivedWallet.cs
CHiveWallet.cs
COperations.cs
CSerializer.cs

Now that the files are in the project folder we need to add them to the project.
Right click on the solution explorer and select add > existing item
select all of the files listed above and click add.

Move the Libraries folder over to the project make sure it includes the following files

Cryptography.ECDSA.dll
Newtonsoft.Json.dll

Now we need to link these files to the project.
in the top bar click project > Add Reference
Click browse, navigate to the libraries folder you just created
select both files click add then ok.

Your project is now setup.

Creating the program

First lets add our references to our project.
These go at the top of our program.

using HiveAPI.CS;
using System.Net.Http;

Then we need to setup some variables
I like to contain these using a region for clarity
This goes above the static void main

    #region Variables
    private const string hiveUser = "your hive username without the @";
    private const string postingKey = "your posting key here";
    #endregion

You'll replace the values with your hive username and posting key.

Now lets get on to the actual function.

We could just put all the code into the Main, but because its likely you'll want to call it
multiple times I wrapped it into a neat little function this goes below the Variables but above the Main

private static string hiveVote(string voter, string author, string permlink, short weight)
        {
            HttpClient oHTTP = new HttpClient();
            CHived oHived = new CHived(oHTTP, "https://anyx.io");

            COperations.vote oVote = new COperations.vote
            {
                voter = voter,
                author = author,
                permlink = permlink,
                weight = weight
            };

            try
            {
                string txid = oHived.broadcast_transaction(
                    new object[] { oVote },
                    new string[] { postingKey }
                    );

                return txid;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

So what does this function do?
The Function requires 4 input parameters.
The User Voting, The author of the post they are voting for, the permalink of the post, and finally the weight of their vote.

The first two lines declare us a Http Client to use and call the CHived function of the Hive.net library.

The next section contains a COperations.vote object which defines all the parameters for our vote.

And the last section is a try catch block that broadcasts the transaction to the chain.

Try catch blocks allow you to execute code that might not work. The Try block is executed first one line at a time. If at any point it fails the program will jump to the catch block with the error code (Exception e) you can then print this message to the console to figure out what went wrong.

As you can see in the Try block we have a return and in the Catch block we have a return. Either path will allow us to return to the main part of the program. This will either return the transaction ID or it will return an error message.

Now that we have our function written lets focus on the main part of our program.

First we declare a string to hold the response and make it equal to the function.
Since the the function accepts 4 parameters. We'll pass those through.
and we'll use Console.Writeline to display the response.

Finally we'll add Console.Readline() in order to force the console to wait for an input before closing.
If we forget to add this the program will close as soon as its tried to broadcast a vote.

 string response = hiveVote(hiveUser, "chaoscommander", "broadcasting-votes-on-the-hive-blockchain-using-c-and-hivenet-library", 100);
 Console.WriteLine(response);
 Console.ReadLine(); //to prevent the console from closing right away.

Go ahead and try it. Save the program then Click Start or press F5

console.PNG

You can copy and paste the transaction id into the hive block explorer and see the vote.

For our next post we'll go over how to transfer tokens using the library.

You can download the full code on my Github.

If you have any questions feel free to comment below.

Sort:  

Thank you for sharing!