How I found a mining calculation by reading through Javascript

in #programming7 years ago

thumb_image.jpg

Image Source

If you've ever wondered about what some of the code on a website you visit might look like, here's one example that I did recently.

As part of my last article one of the calculations I had to perform was expected Bitcoin earnings per day based on difficulty and mining power. Of course I had no idea how to calculate this myself, and the brief amount of googling I did to find a formula didn't yielded some results, but I wasn't really sure how to apply it correctly. So I decided to just look at how one of the mining calculators did it!

The first thing I did was open up BitcoinWisdom's difficulty page. I like it because it's a fairly simple page without a whole lot going on and it's been generally reliable for me.

Next I opened up the browser tools to inspect the site. This will vary from browser to browser but most modern browsers will have this in some form or another. For this article I'm just going to focus on Chrome. Opening up the tools view is pretty easy, you can just right click -> Inspect and it'll take you right to the html for that part of the page.

chrome dev tools.png

Next you need to identify where the code actually is. Generally this means looking for a button or text box where after you perform an action (eg. clicking) the page does something. In this case we care about the text box because it automatically updates whenever you type a number into it. The automatic update means that in the background, there's some code that is watching the text box and waiting for the value to change, and as soon as it does the code runs and updates the remaining values for you. In programming lingo this type of code is called a listener.

If you look in the dev tools view you'll see an event listeners tab:

event_listeners_highlighted.png

This tab shows all of the listeners attached to the element you selected. In this view there are two listeners - DOMContentLoaded and keyup. The one we care about in this case is keyup, since that means the listener code runs every time a key is pressed inside the text box. If you click the arrow next to it you'll see there is a filename and line number:

event_listeners.png

Clicking on the filename will take you to the code itself:

code_itself.png

And from there, you can analyze the code to see what it's doing and figure out how it works. In this case I eventually deduced that the formula looks something like this:

Expected return per day = [Hashes/s] x (1 / (difficulty x 2^32) x block_value) x 86400

The 86400 is to the number of seconds in a day, since the calculation results in coin/second and we want coin/day. And that's pretty much it! We can just plug in our numbers and calculate away:

Expected return = 3000000000000 x (1 / (595921917085 x 2^32) x $2311.015/btc) x 86400 = 0.001266 btc/day.


The following is a more in-depth look at the code and how I arrived at the formula. If you aren't familiar with Javascript or even programming then this might not make a whole lot of sense, but I'm happy to explain things/answer questions if you'd like! In case you want to learn more about Javascript or programming in general though, just look around for online coding courses/tutorials, there's a ton of them! One site I've found that's pretty good is https://www.codecademy.com/.

So looking at the first few lines I see a couple interesting things to take note of - there are two variable assignments that look important, "difficulty" and "hrtocoin". "difficulty" stands out to me because it's probably the variable that has our difficulty value in it, and "hrtocoin" stands out because it references the difficulty variable. I don't know how they're used quite yet though so I'll keep reading further down.

variable_assignments.png

The next line is really just a null check, nothing too fancy.

null_check.png

Now we're getting to the heart of it, here's the code that actually runs the calculations:

for_loop.png

It's ok if you don't understand it at first - I had trouble when I read it myself. It's pretty tightly-written code so it can be a bit confusing to follow.

Here's what I gathered though - that first line, with "val =", is getting the value you typed into one of the boxes and converting it to Hashes/s. The two for loops below it are then meant to loop through every text box (except difficulty and BTC/USD) in order to update their values. Then, each text box gets set to a value by taking the Hashes/s and converting it to various metric representations and formats (MH/s, BTC/day, etc). What we care about is the calculation for BTC/day, which is the middle column. And if we look at the middle column of the bases array from earlier we can see that it's value is hrtocoin. grouped_units ends up resolving to 86400 for BTC/day if you follow the grouped_units array that's further up in the script:

grouped_units.png

So once we have those we then can start to piece together the calculation which looks something like: H/s x 86400 x hrtocoin, and this kind of makes sense when you think about it since we have Hashes/s being multiplied by what is ostensibly the conversion rate from seconds to days - 86400 seconds in one day. Adding in the hrtocoin calculation, our entire formula looks like this: H/s x 86400 x (1 / (difficulty x 2 ^ 32) x block_value).

We can test this with some sample numbers. Let's say our hash rate is 1000GH/s, with a difficulty of 595921917085 and a block_value of $2347.05. Our formula looks like this: 1000000000000 H/s x 86400 x (1 / (595921917085 x 2 ^ 32) x $2347.05), and this gives us a total of 0.000422 BTC/day, which matches what BitcoinWisdom is saying:

btw_table.png

So what do you think? Was this a helpful overview or was it just confusing? I'd love to hear your comments :)